java.lang.Object
io.github.palexdev.materialfx.selection.SingleSelectionManager<T>
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 useclearSelection(), a boolean flag will be set to true thus allowing setting the aforementioned values.-
Property Summary
PropertiesTypePropertyDescriptionThe selected index property.The selected item property. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidBinds the index property to given sourceObservableValue.voidbindIndexBidirectional(Property<Number> other, Function<Integer, T> indexConverter, TriConsumer<Boolean, Integer, Property<Number>> updateOther) Binds the index property bidirectionally to given otherProperty.voidBinds the item property to given sourceObservableValue.voidbindItemBidirectional(Property<T> other, Function<T, Integer> itemConverter, TriConsumer<Boolean, T, Property<T>> updateOther) Binds the item property bidirectionally to given otherProperty.voidSets the index to -1 and item to null by usingSynchronizedProperty.setAndWait(Object, ObservableValue).intbooleanisBound()Returns true if the selected index or item properties are bound unidirectionally.The selected index property.The selected item property.voidsetClearing(boolean clearing) Flag to specify that updateSelection should be ignored asclearSelection()was invoked.voidunbind()Unbinds the selection.voidRemoves all bidirectional bindings.voidunbindIndexBidirectional(Property<Number> other) Removes the bidirectional binding between the index property and the given other property.voidunbindItemBidirectional(Property<T> other) Removes the bidirectional binding between the item property and the given other property.voidupdateSelection(int index) Updates the selection with the given index (and the retrieved item) by usingSynchronizedProperty.setAndWait(Object, ObservableValue).voidupdateSelection(T item) Updates the selection with the given item (and the retrieved index) by usingSynchronizedProperty.setAndWait(Object, ObservableValue).
-
Property Details
-
selectedIndex
The selected index property.- See Also:
-
selectedItem
The selected item property.- See Also:
-
-
Constructor Details
-
SingleSelectionManager
-
-
Method Details
-
clearSelection
public void clearSelection()Sets the index to -1 and item to null by usingSynchronizedProperty.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 usingSynchronizedProperty.setAndWait(Object, ObservableValue).- Throws:
IllegalStateException- if the selection model is bound,isBound()
-
updateSelection
Updates the selection with the given item (and the retrieved index) by usingSynchronizedProperty.setAndWait(Object, ObservableValue).- Throws:
IllegalStateException- if the selection model is bound,isBound()
-
getSelectedIndex
public int getSelectedIndex()- Returns:
- the current selected index
-
selectedIndexProperty
The selected index property.- See Also:
-
getSelectedItem
- Returns:
- the current selected item
-
selectedItemProperty
The selected item property.- See Also:
-
setClearing
public void setClearing(boolean clearing) Flag to specify that updateSelection should be ignored asclearSelection()was invoked. -
bindIndex
Binds the index property to given sourceObservableValue. The indexConverter function is used to convert the index values to an item of the selection model. By default creates this binding:
To change it you should override theBindingManager.instance().bind(selectedIndex) .with((oldValue, newValue) -> { T item = indexConverter.apply(newValue.intValue()); selectedIndex.setAndWait(newValue.intValue(), selectedItem); selectedItem.set(item); }) .to(source) .create();SingleSelectionModelmethod. -
bindIndexBidirectional
public void bindIndexBidirectional(Property<Number> other, Function<Integer, T> indexConverter, TriConsumer<Boolean, Integer, Property<Number>> updateOther) Binds the index property bidirectionally to given otherProperty. The indexConverter function is used to convert the index from the other property to an item of the selection model.The updateOther
By default creates this binding:TriConsumeris 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.
To change it you should override theBiBindingManager.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();SingleSelectionModelmethod. -
bindItem
Binds the item property to given sourceObservableValue. The itemConverter function is used to convert the item values to an index of the selection model. By default creates this binding:
To change it you should override theBindingManager.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();SingleSelectionModelmethod. -
bindItemBidirectional
public void bindItemBidirectional(Property<T> other, Function<T, Integer> itemConverter, TriConsumer<Boolean, T, Property<T>> updateOther) Binds the item property bidirectionally to given otherProperty. The itemConverter function is used to convert the item from the other property to an index of the selection model.The updateOther
By default creates this binding:TriConsumeris 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.
To change it you should override theBiBindingManager.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();SingleSelectionModelmethod. -
unbind
public void unbind()Unbinds the selection. -
unbindIndexBidirectional
Removes the bidirectional binding between the index property and the given other property. -
unbindItemBidirectional
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.
-