SWT Table checkbox for all columns - swt

I am trying to create a two-column-table with checkboxes in both of them
I have created a table with the following snippet:
Table table = new Table(parent, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(300);
tableColumn.setText("Check Column1");
TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
tableColumn1.setWidth(300);
tableColumn1.setText("Check Column2");
TableItem tableItem=new TableItem(table,SWT.NONE);
Image image=getImage(imagePath);
tableItem.setImage(0, image);
what I am getting is the first column the checkbox was created but not in the second one
Do you know why?

This is not supported by the standard SWT Table Widget. Try the Nattable from Nebula for a more feature rich table widget.

If you want to show CheckBox in Table Cell, you could do it in two ways.
show image ( check on/off) depending on state of the data that you have and you need listen on mouse click on table cell.
you could add Button ( SWT.CHECK) as I mentioned below
SWT - Tableviewer adding a remove button to a column in the table

Related

NatTable - Strange behavior when sorting

I have a nattable with sort/filter capabilities based off of
http://www.eclipse.org/nattable/documentation.php?page=sorting
and example 6031_GlazedListsFilterExample.java
Initially my table has zero rows.
Scenario 1:
I view a CTabItem that contains a NatTable with no rows.
If I then populate the rows and click on the column headers, nothing happens (sorting seems disabled).
Scenario 2:
I do NOT view a CTabItem that contains the NatTable with no rows.
I then populate the rows
I then view the CTabItem that contains the NatTable which now has rows.
I click on the column headers and everything sorts as expected (sorting seems enabled)
Scenario 3:
I do NOT view a CTabItem that contains the NatTable with no rows.
I then populate the rows
I then view the CTabItem that contains the NatTable which now has rows.
I then remove all row data
I click on the column headers and everything sorts as expected (sorting seems enabled). * even though there are no rows I still see the up/down icons appear in the column header cell
Is there a reason that the column header actions are not 'updated' after the initial 'view' of the NatTable? In other words, it seems to take the presence/absence of rows into account for the rest of the tables life after the first time the NatTable is viewed, regardless of if the rows change.
Relevant Code sections shown below:
private CompositeLayer createExampleLayer(Collection<T> values,
IColumnPropertyAccessor<T> columnPropertyAccessor,
IDataProvider columnHeaderDataProvider, IConfigRegistry
configRegistry, Matcher<T> matcher) {
BodyLayerStack<T> bodyLayerStack = new BodyLayerStack<>(
values, columnPropertyAccessor);
// build the column header layer
DataLayer columnHeaderDataLayer = new
DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new
ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack,
bodyLayerStack.getSelectionLayer());
SortHeaderLayer<T> sortHeaderLayer = new SortHeaderLayer<>
(columnHeaderLayer, new GlazedListsSortModel<T>
(bodyLayerStack.getSortedList(), columnPropertyAccessor,
configRegistry,
bodyLayerStack.getBodyDataLayer()), false);
FilterRowHeaderComposite<T> filterRowHeaderLayer = new
FilterRowHeaderComposite<>(
new DefaultGlazedListsFilterStrategy<T>
(bodyLayerStack.getFilterList(), columnPropertyAccessor,
configRegistry),
sortHeaderLayer, columnHeaderDataLayer.getDataProvider(),
configRegistry);
// Omitted code for rowHeaderLayer and cornerLayer
return new GridLayer(bodyLayerStack, filterRowHeaderLayer,
rowHeaderLayer, cornerLayer);
}
public BodyLayerStack(Collection<T> values,
IColumnPropertyAccessor<T> columnPropertyAccessor) {
eventList = GlazedLists.eventList(values);
TransformedList<T, T> rowObjectsGlazedList =
GlazedLists.threadSafeList(eventList);
this.sortedList = new SortedList<>(rowObjectsGlazedList, null);
// wrap the SortedList with the FilterList
this.filterList = new FilterList<>(sortedList);
this.bodyDataProvider = new ListDataProvider<>(this.filterList,
columnPropertyAccessor);
this.bodyDataLayer = new DataLayer(getBodyDataProvider());
// layer for event handling of GlazedLists and PropertyChanges
GlazedListsEventLayer<T> glazedListsEventLayer = new
GlazedListsEventLayer<>(bodyDataLayer, this.filterList);
this.selectionLayer = new SelectionLayer(glazedListsEventLayer);
ViewportLayer viewportLayer = new ViewportLayer(getSelectionLayer());
setUnderlyingLayer(viewportLayer);
}
private void enableSorting() {
this.nattable.addConfiguration(new SingleClickSortConfiguration());
}
Looks like the creation of your SortHeaderLayer is not correct. The last parameter of the GlazedListsSortModel needs to be the IDataLayer of the column header, not the body layer.
Changing your code to the following should make things work. It did at least on my side.
SortHeaderLayer<T> sortHeaderLayer = new SortHeaderLayer<>
(columnHeaderLayer, new GlazedListsSortModel<T>
(bodyLayerStack.getSortedList(), columnPropertyAccessor,
configRegistry,
columnHeaderDataLayer), false);

Eclipse plugin "copy treeItem or tree data "

I am working on Eclipse plugin. In this I am creating a view like console which is based on tree structure.
I am creating tree like below --
ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL );
Composite composite1 = new Composite(sc, SWT.NONE);
Composite composite_1 = creatingcomposite(composite1);
final Tree tree = new Tree(composite_1, SWT.FULL_SELECTION );
TreeItem item = new TreeItem(tree, SWT.NONE);
TreeItem subItem = new TreeItem(item, SWT.NONE);
TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
Now I want to copy some data of node(Like item or sub item),
In any node of tree or tree item suppose there is some data like " Hi this is ram and my contact number is 123456789658" now from this row i want to select/copy the contact number.
so how can I do it with the help of mouse (as we use in any other interface like wordpad).
Look at swt snippets for basic swt tree and menu examples.
Create menu(pop up menu) on tree item(s).
On click of menu items (say copy phone number) get the selected node text and extract the phone number out of this.

Changing dynamically view content in Eclipse plugin view

I'm developing Eclipse plugin that has a treeview with toolbar and buttons. I'd want to make the plugin work so that it would show the treeview as default but in case of some error there would be some text and button to initialize or update the plugin. The plugin view should change dynamically depending on the state of the plugin between the treeview and the "error view".
For now I'm creating the treeview instance and doing the other needed actions in createPartControl method to show the treeview right. How should I implement the dynamic view showing different kind of content in the plugin view? Is that possible at all?
Code of the createPartControlMethod:
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
drillDownAdapter = new DrillDownAdapter(viewer);
viewContentProvider = new ViewContentProvider();
viewer.setContentProvider(viewContentProvider);
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());
viewer.expandToLevel(2);
// Create the help context id for the viewer's control
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "my.plugin.viewer");
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
setToolBarButtonsEnabled();
Simplest way would be to create a Composite with a StackLayout and two children instead:
private Composite container;
private TreeViewer viewer;
private Composite errorComposite;
private StackLayout layout;
public void createPartControl(Composite parent) {
container = new Composite(parent);
layout = new StackLayout();
viewer = new TreeViewer(container, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
... // setup viewer
errorComposite = new Composite(container, SWT.NONE);
... // setup error view
}

How do I prevent multiple columns on SWT Vertical ToolBar?

I'm creating a vertical SWT ToolBar:
ToolBar toolBar = new ToolBar( parent, SWT.RIGHT | SWT.VERTICAL );
And then adding items to it:
for ( MyObject myObject: myObjects ) {
ToolItem toolItem = new ToolItem( toolBar, SWT.NONE );
toolItem.setText( myObject.getLabel() );
toolItem.setImage( myObject.getImage() );
}
The toolbar is created correctly, but as some labels are too long, there are some cases in which new items are created on a second column, in front of the previous item.
So, instead of getting a toolbar that looks like
Item with a very long label
Tiny item
Little item
I get a toolbar that looks like
Item with a very long label
Tiny item Little item
If the long label is long enough, a third column will appear.
Does anyone know if there's a way to prevent the creation of a new column on the toolbar?
Thank you.

Editable SWT table

How to edit SWT table Values without Using Mouse Listeners?
Do the TableEditor snippets in the below link help?
SWT Snippets
The first example in the TableEditor section uses a SelectionListener on the table (unlike the second example which uses a MouseDown event you mentioned you don't want)
You could perhaps make use of the TraverseListener or KeyListener too to help you achieve what you want.
final int EDITABLECOLUMN = 1;
tblProvisionInfo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// Clean up any previous editor control
final TableEditor editor = new TableEditor(tblProvisionInfo);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
Control oldEditor = editor.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// Identify the selected row
TableItem item = (TableItem) e.item;
if (item == null)
return;
// The control that will be the editor must be a child of the
// Table
Text newEditor = new Text(tblProvisionInfo, SWT.NONE);
newEditor.setText(item.getText(EDITABLECOLUMN));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent me) {
Text text = (Text) editor.getEditor();
editor.getItem()
.setText(EDITABLECOLUMN, text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, EDITABLECOLUMN);
}
});
Here tblProvision is the name of your table. you can just now edit Your table by clicking on it. I have Declare EDITABLECOLUMN. this is the column that u want to edit.
If you can use JFace as well and not just pain SWT, have a look at the JFace Snippets, especially
Snippet036FocusBorderCellHighlighter - Demonstrates keyboard navigation by highlighting the currently selected cell with a focus border showing once more the flexibility of the new cell navigation support
Snippet034CellEditorPerRowNewAPI - Demonstrates different CellEditor-Types in one COLUMN with 3.3-API of JFace-Viewers
You can get or set the value of a item, for example:
Table table = new Table(parent, SWT.NONE);
TableItem item = new TableItem(table, SWT.NONE);
item.setText("My new Text");
I suggest you to us TableViewer, it is very powerful table which it you can use databinding very easy too.