|
|
Re: How to get the selected object from the tree table [message #1832251 is a reply to message #1832043] |
Mon, 14 September 2020 10:08 |
Dirk Fauth Messages: 47 Registered: March 2020 |
Member |
|
|
Your description is kind of confusing. There is no glazedeventlist. But I suppose you mean the GlazedLists EventList.
It seems you are missing the index-position transformation in NatTable and the GlazedLists transformations. Let me try to explain this:
1. GlazedLists transformations
The EventList contains the data from the base underlying collection in a non-transformed way. Placing another GlazedLists collection on top of the base EventList (e.g. SortedList, FilterList, TreeList) means that inside that collection there are transformations. If sorting, filtering or a tree structure is applied, the index of the element in the top GlazedLists collection is not the same as in the base EventList.
2. NatTable index-position transformations
NatTable shares a similar design. Inside some layers the structure of the data can be changed, e.g. row reordering, row hide/show, tree expand/collapse. In this process the index stays always the same, while the position can change from layer to layer. This is especially important for the ViewportLayer, as the positions always start from 0, while the index can be something very different dependent on the scrolling position. The index always relates to the index in the collection that is used in the DataLayer.
When you try to get the selected object from the SelectionLayer in a programmatical way, you first need to convert the row position to the row index if you have transformation layers in your layer stack:
int rowPosition = selectionLayer.getSelectionAnchor().getRowPosition();
int rowIndex = selectionLayer.getRowIndexByPosition(rowPosition);
If you use an IRowDataProvider (you typically do as the ListDataProvider is an IRowDataProvider) you can then get the selected row object via
bodyDataProvider.getRowObject(rowIndex)
If you want to get the selected object directly from the underlying collection, you need to ensure that you use the top most GlazedLists collection. As your title mentions that you want to get the selected row object from a tree, I suppose you need to ask the TreeList for the selected object, not the base EventList, as the indexes are not the same.
You can also use the listener approach to react on a selection like it is done typically in a RCP application. For this you can use the RowSelectionProvider:
RowSelectionProvider<Person> selectionProvider =
new RowSelectionProvider<>(
firstSelectionLayer,
firstBodyDataProvider);
selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
log("Selection changed:");
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
@SuppressWarnings("rawtypes")
Iterator it = selection.iterator();
while (it.hasNext()) {
Person selected = (Person) it.next();
log(" "
+ selected.getFirstName()
+ " "
+ selected.getLastName());
}
}
});
In an Eclipse 4 application you can also make use of the Eclipse ESelectionService and the NatTable E4SelectionListener:
@Inject
ESelectionService service;
@PostConstruct
public void postConstruct(Composite parent) {
...
E4SelectionListener<Person> esl = new E4SelectionListener<>(service, selectionLayer, bodyDataProvider);
...
}
@Inject
@Optional
void handleSelection(@Named(IServiceConstants.ACTIVE_SELECTION) List<Person> selected) {
if (selected != null) {
String msg = "";
for (Person p : selected) {
msg += p.getFirstName() + " " + p.getLastName() + " selected\n";
}
outputArea.append(msg);
}
}
Using one of the selection listener approaches keeps you away from the need to perform the index-position-transformation yourself. But it is dependent on your use case how you need to retrieve the selection and the selected object from a NatTable. From an Eclipse design perspective the usage of the RowSelectionProvider or the E4SelectionListener should be preferred. If you need to attach some custom menu items inside a NatTable you probably need to access the selected objects programmatically using keeping the index-position-transformation and the collection in use in mind.
You can find several examples about selections in NatTable in our examples application inside Tutorial Examples - Layers - Selection and in E4 Examples - SelectionListenerExample
or in GitHub
https://github.com/eclipse/nebula.widgets.nattable/tree/master/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_505_Selection
https://github.com/eclipse/nebula.widgets.nattable/blob/master/org.eclipse.nebula.widgets.nattable.examples.e4/src/org/eclipse/nebula/widgets/nattable/examples/e4/part/SelectionListenerExample.java
|
|
|
Powered by
FUDForum. Page generated in 0.04013 seconds