PDA

View Full Version : How to make items in a Tree unselectable



jack83
8 May 2014, 12:07 AM
I have a Tree object which contains a few objects of my own class (MenuItem) which has a few simple attributes (mostly Strings). When specific conditions are met, I would like to make some of the items in the Tree are unselectable, so that the user cannot select them in the tree.

Here is how my tree is defined:


Tree<MenuItem, ?> menuTree = getView().getMenu();

TreeSelectionModel<MenuItem> selectionModel = menuTree.getSelectionModel();
selectionModel.addSelectionHandler(new SelectionHandler<MenuItem>() {

@Override
public void onSelection(SelectionEvent<MenuItem> event) {
MenuItem menu = event.getSelectedItem();

// some business logic code

}
});


Is there any way how to do that? Do I need to have a custom selection model for my Tree?

Colin Alworth
8 May 2014, 8:28 AM
Add a BeforeSelectionHandler, check the item and see if you want it to be selected, and if no, cancel the event. Doing so will stop the actual SelectionEvent and selection behavior from occurring.

jack83
12 May 2014, 4:19 AM
Thank you Colin for a good hint.

Is there any way to disable the whole tree (with its all items)? Calling setEnabled(false) has no effects. When the tree is in a container which is then disabled, it does not also bring any changes.

feelinforyou
12 May 2014, 5:08 AM
tree.getSelectionModel().setLocked(true);