- All Implemented Interfaces:
ITreeSelectionModel<T>
- Direct Known Subclasses:
TreeCheckModel
public class TreeSelectionModel<T> extends Object implements ITreeSelectionModel<T>
ITreeSelectionModel interface.
This provides common methods for items selection.
To select an item it should call the TreeSelectionModel associated with the tree which contains the item
with AbstractMFXTreeItem.getSelectionModel() and call the select(AbstractMFXTreeItem, MouseEvent) method.
In the constructor a listener is added to the ListProperty of this class, which contains all the selected items, and
its role is to change the selected property of the item.
-
Constructor Summary
Constructors Constructor Description TreeSelectionModel() -
Method Summary
Modifier and Type Method Description booleanallowsMultipleSelection()voidclearSelection()Resets every item in the list to selected false and then clears the list.AbstractMFXTreeItem<T>getSelectedItem()Gets the selected item.ListProperty<AbstractMFXTreeItem<T>>getSelectedItems()voidscanTree(AbstractMFXTreeItem<T> item)If you set some item to be selected before the tree is laid out then it's needed to scan the tree and add all the selected items to the list.protected voidselect(AbstractMFXTreeItem<T> item)This method is called when the mouseEvent argument passed toselect(AbstractMFXTreeItem, MouseEvent)is null.voidselect(AbstractMFXTreeItem<T> item, MouseEvent mouseEvent)This method is called byMFXTreeItemSkinwhen the mouse is pressed on the item.voidsetAllowsMultipleSelection(boolean multipleSelection)Sets the selection mode of the model, single or multiple.
-
Constructor Details
-
TreeSelectionModel
public TreeSelectionModel()
-
-
Method Details
-
select
This method is called when the mouseEvent argument passed toselect(AbstractMFXTreeItem, MouseEvent)is null. It is used for example when you want the tree to start with one or more selected items like this:MFXTreeItem<String> root = new MFXTreeItem<>("ROOT"); MFXTreeItem<String> i1 = new MFXTreeItem<>("I1"); MFXTreeItem<String> i1a = new MFXTreeItem<>("I1A"); MFXTreeItem<String> i2 = new MFXTreeItem<>("I1B"); root.setSelected(true); i1.setSelected(true); i1a.setSelected(true); i2.setSelected(true);If the model is set to not allow multiple selection then we clear the list and then add the item to it.
- Parameters:
item- the item to select- See Also:
MFXTreeItem
-
scanTree
If you set some item to be selected before the tree is laid out then it's needed to scan the tree and add all the selected items to the list.- Specified by:
scanTreein interfaceITreeSelectionModel<T>
-
select
This method is called byMFXTreeItemSkinwhen the mouse is pressed on the item. We need the mouse event as a parameter in case multiple selection is allowed because we need to check if the Shift key or Ctrl key were pressed.If the mouseEvent is null we call the other
select(AbstractMFXTreeItem)method.If the selection is single
clearSelection()we clear the selection and add the new selected item to the list.If the selection is multiple we check if the item was already selected, if that is the case by default the item is deselected.
In case neither Shift nor Ctrl are pressed we clear the selection.
- Specified by:
selectin interfaceITreeSelectionModel<T>
-
clearSelection
public void clearSelection()Resets every item in the list to selected false and then clears the list.- Specified by:
clearSelectionin interfaceITreeSelectionModel<T>
-
getSelectedItem
Gets the selected item. If the selection is multiplegetSelectedItems()should be called instead, as this method will only return the first item of the list.- Specified by:
getSelectedItemin interfaceITreeSelectionModel<T>- Returns:
- the first selected item of the list
-
getSelectedItems
- Specified by:
getSelectedItemsin interfaceITreeSelectionModel<T>- Returns:
- the ListProperty which contains all the selected items.
-
allowsMultipleSelection
public boolean allowsMultipleSelection()- Specified by:
allowsMultipleSelectionin interfaceITreeSelectionModel<T>- Returns:
- true if allows multiple selection, false if not.
-
setAllowsMultipleSelection
public void setAllowsMultipleSelection(boolean multipleSelection)Sets the selection mode of the model, single or multiple.- Specified by:
setAllowsMultipleSelectionin interfaceITreeSelectionModel<T>
-