Arranging the checkbox in checkboxtableviewer horizontally in eclipse e4 - java

I am using eclipse e4 application. I am displaying a checkboxtableviewer in a Part. Now the checkbox are arranged vertically like the following pattern below.
checkbox MIN
checkbox MAX
checkbox AVG
checkbox COUNT
the following is the code snippet I have used.
public class StatisticsPart {
private CheckboxTableViewer tableViewer;
public Object[] statisticsSelected;
#Inject
public StatisticsPart() {
//TODO Your code here
}
#PostConstruct
public void postConstruct(Composite parent) {
tableViewer = new CheckboxTableViewer(parent, SWT.BORDER|SWT.HORIZONTAL);
tableViewer.add("MIN");
tableViewer.add("MAX");
tableViewer.add("AVG");
tableViewer.add("COUNT");
tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
tableViewer.addCheckStateListener(new ICheckStateListener() {
#Override
public void checkStateChanged(CheckStateChangedEvent event) {
statisticsSelected = tableViewer.getCheckedElements();
}
});
}
}
I want to display the checkbox name horizontally and all the corresponding checkboxes below it like the following pattern.
MIN MAX AVG COUNT
checkbox checkbox checkbox checkbox
Can anyone please help how to do this.
Thanks in advance

CheckboxTableViewer only supports one check box per row.
You could use a normal TableViewer and define four columns.
Use a ColumnLabelProvider for each column and override the getImage method to provide a checked or unchecked image.
You can change the value by using a CheckboxCellEditor in the editing support for the column.

Related

Remove a selection listener on a table item

I have a table where multiple table items are available.
Out of them, for some table items background and foreground color is set.
On selection of a colored item,since the text color was white, the text is difficult to read
So, I need to change the forground color to default ie. black.
I had done it using the selection listener
private SelectionListener selectionListener;
private void mouseTrackListener() {
selectionListener = new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
((TableItem) e.item).setForeground(null);
}
}
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
};
this.table.addSelectionListener(selectionListener);
this.table.removeSelectionListener(selectionListener);
}
And the color got changed successfully.
But now I am selecting any other item which is not colored, so I want to remove the above selection listener and set the text color to colored ie. white.
I am not getting how to use this.table.removeSelectionListener.
Can some one please help.
You need to remember the selection listener somewhere, probably a field in the class managing the table.
private SelectionListener listener;
...
listener = new SelectionListener() ....
...
table.addSelectionListener(listener);
...
table.removeSelectionListener(listener);
Be sure to only create the listener once (possibly in the class constructor).
An alternative is to just add the listener (once) and then test a flag in the listener to decide if you setForeground.

How to update ProgressBar in TableViewer?

In my TableViewer I have an OwnerDrawLabelProvider where for a specific column in my table I add a ProgressBar to a TableEditor instance.
My problem is the following:
I'm able to set the selection of the ProgressBar but when trying to update it remains at the same value.
CODE:
#Override
public void update(ViewerCell cell) {
if(columnIndex == 4){
Table table = tableViewer.getTable();
TableItem item;
TableItem[] items;
TableEditor editor;
items = table.getItems();
ProgressBar bar = new ProgressBar(table, SWT.NONE);
bar.setMinimum(0);
bar.setMaximum(100);
bar.setState(SWT.NORMAL);
bar.setSelection(0);
bar.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
if(mediator.isSent()){
List<Item> itemsSendToSubmit = mediator.getItemsSendToSubmit();
if(!itemsSendToSubmit.isEmpty()){
for(Iterator<Item> itemIterator = itemsSendToSubmit.iterator(); itemIterator.hasNext(); )
{
Item itemSubmited = itemIterator.next();
for(TableItem tableItem: items)
{ if(tableItem.getText(0).contains(itemSubmited.getId()))
{
bar.setSelection(PluginUtility.getBuildProgress(itemSubmited.getPlan()));
editor = new TableEditor(table);
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(bar, tableItem, 4);
}
}
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
I read about an issue that setSelection method for ProgressBar has some problems. I created my own ProgressBar by extending the base class and I overrided setSelection method with the fix code but still doesn't work.
In a normal main function, this works.
Can I get some suggestions of what can be the problem or how adding this ProgressBar in a TableViewer influences its behavior ?
EDIT: If I create a single instance of progressbar when the label provider is created and then pass it to the tableeditor it will update the progressbar for the last element on which I say editor.setEditor(bar, tableItem, 4);
but I need to display a progressbar for each item and update it for each item !
Perhaps the update happens, but the visuals are not updated, depending on how you call this code.
You have to make sure that you're not mobilizing the ui thread when you're doing this update.
In eclipse 3.x this means using
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// ... do any work that updates the screen ...
}
});
Or using injection to get the UISynchronize in 4.x
Basically put the setSelection code in there.
See this article on background processing in eclipse plugins :
http://www.vogella.com/tutorials/EclipseJobs/article.html
When you update the value of ProgressBar, after that you need to redraw/repaint the TableViewer with all of its properties, then it will show the update progressbar. Hope this will solve your problem.

Disable up and down arrow buttons on JSpinner

I have a JSpinner on which I would like to take control of when editing is enabled. It's easy enough with the keyboard, but how about those little arrow widgets at the side? I can't even find references to them in the JSpinner source or any of its enclosed classes.
You can use setUI() method to hide Jspinner arrow.
public void hideSpinnerArrow(JSpinner spinner) {
Dimension d = spinner.getPreferredSize();
d.width = 30;
spinner.setUI(new BasicSpinnerUI() {
protected Component createNextButton() {
return null;
}
protected Component createPreviousButton() {
return null;
}
});
spinner.setPreferredSize(d);
}
As you see, just make createNextButton() and createPreviousButton() return null.
Because we used a BasicUI so we have to setup Spinner size again. I used PreferredSize.
you can't take control of these two arrow buttons but you can do like this
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
p.setEnabled(false);
}
suppose you want a button pressed and the user will not able to use jspinner at all this is a hint for more actions you can modified it as well
you can also allow use to use the jspinner untill for a specific value using
if(spinner.getValue()==10){
//show error message and
spinner.setEnabled(false);
}
If the UI class used derives from BasicSpinnerUI, the arrow buttons can be removed with:
for (Component component : spinner.getComponents()) {
if (component.getName() != null && component.getName().endsWith("Button")) {
spinner.remove(component);
}
}

JFace DialogCellEditor: how to make buttons always appear?

I use JFace DialogCellEditor to show a button in a cell of a row of my JFace TableViewer which triggers a dialog when activated. This behaviour works well with the following code but the button only appears when the cell of the table hosting the button is explicitly selected.
public class CompareDialogCellEditor extends DialogCellEditor {
public CompareDialogCellEditor(Composite parent) {
super(parent);
}
#Override
protected Button createButton(Composite parent) {
Button button = super.createButton(parent);
button.setText("");
button.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKeys.COMPARE_ICON).createImage());
return button;
}
#Override
protected Object openDialogBox(Control cellEditorWindow) {
MessageDialog.openInformation(cellEditorWindow.getShell(), "Test", "It works");
return null;
}
}
Is there a way to force the button to always appear in the table and not only when the cell is selected? (the same behaviour goes for a label set by the overridden method setContents(...) )
Thanks
You can only edit one Viewer cell at a time. Viewer won't support editing multiple cells at a time unless you do some customization.
I can think of following solutions.
Paint widget ( button, text, combo..etc) like image on table cell and invoke
CellEditor when user activates it.
You can find some examples here about how to paint on Table Cell.
http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
I posted an answer about how to show button in table cell here. you can following the same concept with CellEditor
SWT - Tableviewer adding a remove button to a column in the table

Show TableViewer not in line, but in matrix order

I use the TableViewer to show informations in a table. The user can select one of the shown options by selecting one line of the table.
I want to create a table in matrix form, in which the user can not only select the line. It should be possible to select every item of the table, like row 2 column 3. For every item selection an action is called to handle this item as it is in the TableViewer.
As far as i now, i can add CellModifier and CellEditors to the line of Columns of the table, but the reference for the action is always the line object and not the selected TableItem.
Does somebody have an example how to create such a matrix inside a Composite?
I can create it by setting a GridLayout and adding the components in a for-loop, but than i get issues, when i want to redraw the Composite with new childrens. The TableViewer does already have this handling, so i dont want to implement it again.
I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.
MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);
public class ColumnSelectionMouseListener implements MouseListener {
private TableColumn selectedColumn;
#Override
public void mouseDoubleClick(MouseEvent e) {
// Nothing to do here
}
#Override
public void mouseDown(MouseEvent e) {
table = (Table) e.widget;
TableItem item = table.getItem(new Point(e.x, e.y));
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumn(i);
Rectangle bounds = item.getBounds(i);
if (bounds.contains(e.x, e.y)) {
selectedColumn = column;
}
}
}
#Override
public void mouseUp(MouseEvent e) {
// Nothing to do here
}
public TableColumn getSelectedField() {
return selectedColumn;
}
}
Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.
Hope this can help.
Manu
Maybe the following JFace snippet will help:
Snippet058CellNavigationIn34
Ingo

Categories

Resources