All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jidefx.scene.control.searchable.ChoiceBoxSearchable Maven / Gradle / Ivy

/*
 * @(#)ChoiceBoxSearchable.java 5/19/2013
 *
 * Copyright 2002 - 2013 JIDE Software Inc. All rights reserved.
 */

package jidefx.scene.control.searchable;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.control.ChoiceBox;
import javafx.util.StringConverter;

/**
 * {@code ChoiceBoxSearchable} is an concrete implementation of {@link Searchable} that enables the search function on
 * non-editable ChoiceBox. 

It's very simple to use it. Assuming you have a ChoiceBox, all you need to do is to call *

{@code
 * ChoiceBox choiceBox = ....;
 * ChoiceBoxSearchable searchable = new ChoiceBoxSearchable(choiceBox);
 * }
* Now the ChoiceBox will have the search function. *

* There is very little customization you need to do to ChoiceBoxSearchable. The only thing you might need is when the * element in the ChoiceBox needs a special conversion to convert to string. If so, you can override * convertElementToString() to provide you own algorithm to do the conversion. *

{@code
 * ChoiceBox choiceBox = ....;
 * ChoiceBoxSearchable searchable = new ChoiceBoxSearchable(choiceBox) {
 *      protected String convertElementToString(Object object) {
 *          ...
 *      }
 * };
 * }
*

* * @param the element type in the ChoiceBox. */ @SuppressWarnings({"Convert2Lambda", "unchecked"}) public class ChoiceBoxSearchable extends Searchable { private ListChangeListener _listChangeListener; private ChangeListener> _itemsChangeListener; public ChoiceBoxSearchable(ChoiceBox choiceBox) { super(choiceBox); } @Override public void installListeners() { super.installListeners(); if (_listChangeListener == null) { _listChangeListener = new ListChangeListener() { @Override public void onChanged(Change c) { hidePopup(); } }; } ((ChoiceBox) _node).getItems().addListener(_listChangeListener); if (_itemsChangeListener == null) { _itemsChangeListener = new ChangeListener>() { @Override public void changed(ObservableValue> observable, ObservableList oldValue, ObservableList newValue) { hidePopup(); } }; } ((ChoiceBox) _node).itemsProperty().addListener(_itemsChangeListener); } @Override public void uninstallListeners() { if (_listChangeListener != null) { ((ChoiceBox) _node).getItems().removeListener(_listChangeListener); _listChangeListener = null; } if (_itemsChangeListener != null) { ((ChoiceBox) _node).itemsProperty().removeListener(_itemsChangeListener); _itemsChangeListener = null; } super.uninstallListeners(); } private BooleanProperty _showPopupProperty; public BooleanProperty showPopupProperty() { if (_showPopupProperty == null) { _showPopupProperty = new SimpleBooleanProperty(false); } return _showPopupProperty; } public boolean isShowPopup() { return showPopupProperty().get(); } public void setShowPopup(boolean showPopup) { showPopupProperty().set(showPopup); } @Override protected int getSelectedIndex() { return ((ChoiceBox) _node).getSelectionModel().getSelectedIndex(); } @Override protected void setSelectedIndex(int index, boolean incremental) { ChoiceBox choiceBox = (ChoiceBox) _node; choiceBox.getSelectionModel().select(index); if (isShowPopup() && !choiceBox.isShowing()) { choiceBox.show(); } } @Override protected int getElementCount() { return ((ChoiceBox) _node).getItems().size(); } @Override protected T getElementAt(int index) { return ((ChoiceBox) _node).getItems().get(index); } @Override protected String convertElementToString(T element) { ChoiceBox choiceBox = (ChoiceBox) _node; StringConverter converter = choiceBox.getConverter(); return converter != null ? converter.toString(element) : (element != null ? element.toString() : ""); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy