Module MaterialFX

Class SingleSelectionManager<T>

java.lang.Object
io.github.palexdev.materialfx.selection.SingleSelectionManager<T>

public class SingleSelectionManager<T> extends Object
Helper class for AbstractSingleSelectionModel models to properly handle the selection and the bindings with properties or other models.

Both the selectedIndex and selectedItem properties are SynchronizedProperties, see SynchronizedProperty.

So when you select an index the item will be automatically updated and only then a change event will be fired, the same thing happens if you select an item.

Invalid values, like -1 for the index or null for the item, will throw an exception. To clear the selection use clearSelection(), a boolean flag will be set to true thus allowing setting the aforementioned values.
  • Property Details

  • Constructor Details

  • Method Details

    • clearSelection

      public void clearSelection()
      Sets the index to -1 and item to null by using SynchronizedProperty.setAndWait(Object, ObservableValue).
      Throws:
      IllegalStateException - if the selection model is bound, isBound()
    • updateSelection

      public void updateSelection(int index)
      Updates the selection with the given index (and the retrieved item) by using SynchronizedProperty.setAndWait(Object, ObservableValue).
      Throws:
      IllegalStateException - if the selection model is bound, isBound()
    • updateSelection

      public void updateSelection(T item)
      Updates the selection with the given item (and the retrieved index) by using SynchronizedProperty.setAndWait(Object, ObservableValue).
      Throws:
      IllegalStateException - if the selection model is bound, isBound()
    • getSelectedIndex

      public int getSelectedIndex()
      Returns:
      the current selected index
    • selectedIndexProperty

      public SynchronizedIntegerProperty selectedIndexProperty()
      The selected index property.
      See Also:
    • getSelectedItem

      public T getSelectedItem()
      Returns:
      the current selected item
    • selectedItemProperty

      public SynchronizedObjectProperty<T> selectedItemProperty()
      The selected item property.
      See Also:
    • setClearing

      public void setClearing(boolean clearing)
      Flag to specify that updateSelection should be ignored as clearSelection() was invoked.
    • bindIndex

      public void bindIndex(ObservableValue<? extends Number> source, Function<Integer,T> indexConverter)
      Binds the index property to given source ObservableValue. The indexConverter function is used to convert the index values to an item of the selection model.

      By default creates this binding:
       
            BindingManager.instance().bind(selectedIndex)
                .with((oldValue, newValue) -> {
                    T item = indexConverter.apply(newValue.intValue());
                    selectedIndex.setAndWait(newValue.intValue(), selectedItem);
                    selectedItem.set(item);
                })
                .to(source)
                .create();
       
       
      To change it you should override the SingleSelectionModel method.
    • bindIndexBidirectional

      public void bindIndexBidirectional(Property<Number> other, Function<Integer,T> indexConverter, TriConsumer<Boolean,Integer,Property<Number>> updateOther)
      Binds the index property bidirectionally to given other Property. The indexConverter function is used to convert the index from the other property to an item of the selection model.

      The updateOther TriConsumer is used to customize the way the other property is updated, the first parameter is the clearing flag of the selection manager, the second parameter is the new index, the third parameter is the other property reference.

      By default creates this binding:
       
            BiBindingManager.instance().bindBidirectional(selectedIndex)
                .with((oldValue, newValue) -> {
                    if (newValue.intValue() == -1) {
                        clearSelection();
                        return;
                    }
      
                    if (newValue.intValue() == selectedIndex.getValue()) {
                        return;
                    }
                T item = indexConverter.apply(newValue.intValue());
                selectedIndex.setAndWait(newValue.intValue(), selectedItem);
                selectedItem.set(item);
            })
            .to(other, (oldValue, newValue) -> updateOther.accept(clearing, newValue.intValue(), other))
            .create();
       
       
      To change it you should override the SingleSelectionModel method.
    • bindItem

      public void bindItem(ObservableValue<? extends T> source, Function<T,Integer> itemConverter)
      Binds the item property to given source ObservableValue. The itemConverter function is used to convert the item values to an index of the selection model.

      By default creates this binding:
       
            BindingManager.instance().bind(selectedItem)
                .with((oldValue, newValue) -> {
                    if (!selectionModel.getUnmodifiableItems().contains(newValue)) {
                        throw new IllegalArgumentException("The given item is not present is this selection model's list");
                    }
                    int index = itemConverter.apply(newValue);
                    selectedItem.setAndWait(newValue, selectedIndex);
                    selectedIndex.set(index);
                })
                .to(source)
                .create();
       
       
      To change it you should override the SingleSelectionModel method.
    • bindItemBidirectional

      public void bindItemBidirectional(Property<T> other, Function<T,Integer> itemConverter, TriConsumer<Boolean,T,Property<T>> updateOther)
      Binds the item property bidirectionally to given other Property. The itemConverter function is used to convert the item from the other property to an index of the selection model.

      The updateOther TriConsumer is used to customize the way the other property is updated, the first parameter is the clearing flag of the selection manager, the second parameter is the new item, the third parameter is the other property reference.

      By default creates this binding:
       
            BiBindingManager.instance().bindBidirectional(selectedItem)
                .with((oldValue, newValue) -> {
                    if (newValue == null) {
                        clearSelection();
                        return;
                    }
      
                    if (!selectionModel.getUnmodifiableItems().contains(newValue)) {
                        throw new IllegalArgumentException("The given item is not present is this selection model's list");
                    }
      
                    int index = itemConverter.apply(newValue);
                    selectedItem.setAndWait(newValue, selectedIndex);
                    selectedIndex.set(index);
                })
                .to(other, (oldValue, newValue) -> updateOther.accept(clearing, newValue, other))
                .create();
       
       
      To change it you should override the SingleSelectionModel method.
    • unbind

      public void unbind()
      Unbinds the selection.
    • unbindIndexBidirectional

      public void unbindIndexBidirectional(Property<Number> other)
      Removes the bidirectional binding between the index property and the given other property.
    • unbindItemBidirectional

      public void unbindItemBidirectional(Property<T> other)
      Removes the bidirectional binding between the item property and the given other property.
    • unbindBidirectional

      public void unbindBidirectional()
      Removes all bidirectional bindings.
    • isBound

      public boolean isBound()
      Returns true if the selected index or item properties are bound unidirectionally.