dpwr
dpwr

Reputation: 2812

GWT CellTree selection

I have a CellTree which utilizes a MultiSelectionModel with CheckboxCells as part of a composite cell. That all seems to be working.

What I'm actually trying to do is to generate tabs in my interface for each item that can be selected in the tree. Sounds trivial, but I'm stumped as to how to get the MyData on a selectionChange. The items need to remain selected so that if I unselect them later, the tabs are then removed again.

I need to be get the MyData for the just selected item in order to know what the contents of the tab should be.

        final MultiSelectionModel<TableLight> selectionModel = new MultiSelectionModel<TableLight>();
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

        public void onSelectionChange(SelectionChangeEvent event) {

        }
    });

I feel like I'm totally missing the obvious. All I can do in the selectionChange event is to interrogate my selection handler to see what the selected set is. Is there a way I can attach a listener to a specific tree selection instead of a global, "something has changed" listener.

Any hints would be much appreciated.

Upvotes: 7

Views: 4159

Answers (3)

marius_neo
marius_neo

Reputation: 1595

Adding an extended selection model will get you the last selected element :

gridSelectionModel = new MultiSelectionModel<MyData>(KEY_PROVIDER){

        public void setSelected(MyData myData, boolean selected) {
            super.setSelected(myData, selected);
            if (selected){
                System.out.println("setSelected selected " + myData);
                // call now some ui handler to use the last selected myData element
            }
        }
    };

I hope this helps.

Upvotes: 6

dpwr
dpwr

Reputation: 2812

Ok, for anyone who finds this later, there is a way!

When you add the CheckboxCell to the CompositeCell you can specify a FieldUpdater to be returned which gets called when the field is changed.

            hasCells.add(new HasCell<TableLight, Boolean>() {

            // Cell containing checkbox
            private Cell cell = new CheckboxCell(true, false);

            public Cell<Boolean> getCell() {
                return cell;
            }

            public FieldUpdater<TableLight, Boolean> getFieldUpdater() {
                return new FieldUpdater<TableLight, Boolean>() {

                    public void update(int index, TableLight object, Boolean value) {
                        if (value) {
                            tablesTabPanel.addTable(object);
                        } else {
                            tablesTabPanel.removeTable(object);
                        }
                    }
                };
            }

            public Boolean getValue(TableLight object) {
                return selectionModel.isSelected(object);
            }
        });

Also, just to avoid confusion. When I was talking about MyData above, that is TableLight in the example.

Upvotes: 3

&#220;mit
&#220;mit

Reputation: 17489

You have to access getSelectedSet() on your selectionModel to get the selected items in your CellTree. Unfortunately there seems to be now way how to get the last selected item (like in the SingleSelectionModel).

 final MultiSelectionModel<TableLight> selectionModel = new MultiSelectionModel<TableLight>();
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

        public void onSelectionChange(SelectionChangeEvent event) {
              Set<TableLight> selectedItems = selectionModel.getSelectedSet();
        }
    });

Upvotes: 2

Related Questions