Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » How to get the selected object from the tree table(not able to get the exact selected object from the tree table)
How to get the selected object from the tree table [message #1832043] Mon, 07 September 2020 10:59 Go to next message
Chinniah Govindasamy is currently offline Chinniah GovindasamyFriend
Messages: 12
Registered: December 2017
Junior Member
Hi All,

i am trying to get the selected row object in the Nataable. i

First tried to get position of the selected row by using

Selectionlayer.getselectedAchor.getRowposition(). using this value i tried to get the value from the glazedeventlist.

but the value i selected and the value form the reason are not same.

could not ale to get the selected row object.

How to get the selected object from the table?



Thanks,
Chinna
Re: How to get the selected object from the tree table [message #1832249 is a reply to message #1832043] Mon, 14 September 2020 09:37 Go to previous messageGo to next message
Chinniah Govindasamy is currently offline Chinniah GovindasamyFriend
Messages: 12
Registered: December 2017
Junior Member
Issue solved.Retrieved the selected object using listdataprovider.getRowObject(int).

Thanks,
Chinna
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 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 30
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
Previous Topic:Scrolling changing style
Next Topic:Same package in multiple versions
Goto Forum:
  


Current Time: Sun Jun 20 21:58:41 GMT 2021

Powered by FUDForum. Page generated in 0.01901 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top