how to add filter to nattable in an eclipse plugin? - nattable

configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
new ComboBoxCellEditor(Arrays.asList("200536", "200538")),
DisplayMode.NORMAL,
FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + 2);
//
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
new ComboBoxCellEditor(Arrays.asList("07")),
DisplayMode.NORMAL,
FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + 3);
final Style rowStyle = new Style();
rowStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper
.getColor(197, 212, 231));
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, rowStyle,
DisplayMode.NORMAL, GridRegion.FILTER_ROW);
i tried to add filters in nattable but the ComboBox didn't appear yet ..

You need to create a layer composition that contains a filter row. Have a look at our examples application to see how this is done in various scenarios.

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);

How to coloriate differently even rows and odd rows of a List in LWUIT?

There is a List in a LWUIT application. I want to make odd rows and even rows to be of different colors. How to achieve that ?
You can set two differents UIIDs to the rows. Setting this UIID you can modify selectively the colors of your rows.
EDIT
Ok this will be more difficult.
You need to make a Render and set it in your List with List.setRender(Render r).
The ´Render´ class will extend from ListCellRender. In this class you can set UIID to the Render, setting its Selected or Unselected styles.
See this example. #Shai Almog could have more info for your problem.
http://www.lwuit.com/2008/07/lwuit-list-renderer-by-chen-fishbein.html
What you need is the Generic List Cell Renderer, you will probably have to create the styles in code, or set the UIID from the resource editor.
List list = new List(createGenericListCellRendererModelData());
list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
private Container createGenericRendererContainer() {
Container c = new Container(new BorderLayout());
c.setUIID("ListRenderer");
Label name = new Label();
name.setFocusable(true);
name.setName("Name");
c.addComponent(BorderLayout.CENTER, name);
Label surname = new Label();
surname.setFocusable(true);
surname.setName("Surname");
c.addComponent(BorderLayout.SOUTH, surname);
CheckBox selected = new CheckBox();
selected.setName("Selected");
selected.setFocusable(true);
c.addComponent(BorderLayout.WEST, selected);
return c;
}
private Hashtable[] createGenericListCellRendererModelData() {
Hashtable[] data = new Hashtable[5];
data[0] = new Hashtable();
data[0].put("Name", "Shai");
data[0].put("Surname", "Almog");
data[0].put("Selected", Boolean.TRUE);
data[1] = new Hashtable();
data[1].put("Name", "Chen");
data[1].put("Surname", "Fishbein");
data[1].put("Selected", Boolean.TRUE);
data[2] = new Hashtable();
data[2].put("Name", "Ofir");
data[2].put("Surname", "Leitner");
data[3] = new Hashtable();
data[3].put("Name", "Yaniv");
data[3].put("Surname", "Vakarat");
data[4] = new Hashtable();
data[4].put("Name", "Meirav");
data[4].put("Surname", "Nachmanovitch");
return data;
}
Full details here : http://lwuit.blogspot.com/2011/03/list-rendering-easy-way-generic-list.html (code gotten from this link).

Can user add new Sections in a view?

I am using eclipse 3.6 and developing RCP application with java 6.
I am using the Section and trying to let the use able to add new n-sections. I need the text in the field after that.
Now the User can see a section. I need that he is able to add a n-sections and then to write text in stopRouteStreet-field. I would like to read all the n Text written in this field.
Any idea how to do this?.
Here is my code
Section sectionStop = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TWISTIE|Section.TITLE_BAR);
td = new TableWrapData(TableWrapData.FILL);
td.colspan = 2;
sectionStop.setLayoutData(td);
sectionStop.addExpansionListener(new ExpansionAdapter() {
public void expansionStateChanged(ExpansionEvent e) {
form.reflow(true);
}
});
sectionStop.setText(Messages.SearchMapView_endPoint); //$NON-NLS-1$
Composite sectionClientStop = toolkit.createComposite(sectionStop);
sectionClientStop.setLayout(new GridLayout());
final Composite stopComposite = toolkit.createComposite(sectionClientStop, SWT.NONE);
final GridLayout gridLayoutStop = new GridLayout();
gridLayoutStop.numColumns = 2;
stopComposite.setLayout(gridLayoutStop);
toolkit.createLabel(stopComposite, Messages.SearchMapView_Street);
stopRouteStreet = toolkit.createText(stopComposite, "", SWT.BORDER); //$NON-NLS-1$
sectionStop.setClient(sectionClientStop);
You need a global variable (a HashMap would do), that saves a mapping between each newly created Section and the Text control.
// define global field
HashMap <Section, Text> dynamicControls = new HashMap <Section, Text> ();
// after you create the text field, save the newly created Text field
....
...
dynamicControls.put(section, text);
// Later when you need to read the values in all the text fields
for(Section s: dynamicControls.keySet()){
Text textField = dynamicControls.get(s);
System.out.println(textField.getText());
}

Hiding cursor in Text component in Eclipse RCP application

In my eclipse RCP application there are a few buttons & few input boxes & this below Text component. My problem is as soon as I press one of the buttons a cursor starts blinking in the below test component. Can you please let me know how to solve this.
I tried:
setting focus to false for Text.
SWT.READ_ONLY for Text .
Code:
Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_NO);
protocolFilterDescription.setCursor(cursor);
Nothing seems to get rid of this unnecessary cursor.
protocolFilterDescription = new Text(parent, SWT.NONE | SWT.READ_ONLY );
FormData protocolFilterDescriptionLData = new FormData();
protocolFilterDescriptionLData.left = new FormAttachment(0, 1000, 650);
protocolFilterDescriptionLData.top = new FormAttachment(0, 1000, 290);
protocolFilterDescriptionLData.width = 450;
protocolFilterDescriptionLData.height = 12;
protocolFilterDescription.setLayoutData(protocolFilterDescriptionLData);
protocolFilterDescription.setForeground(new Color(parent.getDisplay(),
204, 153, 0));
protocolFilterDescription.setBackground(Display.getCurrent()
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
protocolFilterDescription.setFont(new Font(parent.getDisplay(),"Verdana",
6, 1));
protocolFilterDescription
.setText("captured");
You have to set the focus of some other SWT component to true to remove the focus from the Text component.
You'll probably have to do this in an ActionListener.
If you want to completely remove the cursor from the Text control (which implies inability to perform a selection there, etc), try calling setEnabled(false) on it.
Also, such requirement suggests that you maybe don't need Text component at all, and could use Label instead.

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.