Module MaterialFX

Class TreeSelectionModel<T>

java.lang.Object
io.github.palexdev.materialfx.selection.TreeSelectionModel<T>
All Implemented Interfaces:
ITreeSelectionModel<T>
Direct Known Subclasses:
TreeCheckModel

public class TreeSelectionModel<T> extends Object implements ITreeSelectionModel<T>
Concrete implementation of the 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 Details

    • TreeSelectionModel

      public TreeSelectionModel()
  • Method Details

    • select

      protected void select(AbstractMFXTreeItem<T> item)
      This method is called when the mouseEvent argument passed to select(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:
    • scanTree

      public void scanTree(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.
      Specified by:
      scanTree in interface ITreeSelectionModel<T>
    • select

      public void select(AbstractMFXTreeItem<T> item, MouseEvent mouseEvent)
      This method is called by MFXTreeItemSkin when 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:
      select in interface ITreeSelectionModel<T>
    • clearSelection

      public void clearSelection()
      Resets every item in the list to selected false and then clears the list.
      Specified by:
      clearSelection in interface ITreeSelectionModel<T>
    • getSelectedItem

      public AbstractMFXTreeItem<T> getSelectedItem()
      Gets the selected item. If the selection is multiple getSelectedItems() should be called instead, as this method will only return the first item of the list.
      Specified by:
      getSelectedItem in interface ITreeSelectionModel<T>
      Returns:
      the first selected item of the list
    • getSelectedItems

      public ListProperty<AbstractMFXTreeItem<T>> getSelectedItems()
      Specified by:
      getSelectedItems in interface ITreeSelectionModel<T>
      Returns:
      the ListProperty which contains all the selected items.
    • allowsMultipleSelection

      public boolean allowsMultipleSelection()
      Specified by:
      allowsMultipleSelection in interface ITreeSelectionModel<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:
      setAllowsMultipleSelection in interface ITreeSelectionModel<T>