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

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

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

package jidefx.scene.control.searchable;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;

/**
 * {@code ListViewSearchable} is an concrete implementation of {@link Searchable} that enables the search function
 * on ListView. 

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

{@code
 * ListView list = ....;
 * ListViewSearchable searchable = new ListViewSearchable(list);
 * }
* Now the ListView will have the search function. *

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

{@code
 * ListView list = ....;
 * ListViewSearchable searchable = new ListViewSearchable(list) {
 *      protected String convertElementToString(Object object) {
 *          ...
 *      }
 * };
 * }
*

* * @param the element type in the ListView. */ @SuppressWarnings({"Convert2Lambda", "unchecked"}) public class ListViewSearchable extends Searchable { private ListChangeListener _listChangeListener; private ChangeListener> _itemsChangeListener; public ListViewSearchable(ListView listView) { super(listView); } @Override public void installListeners() { super.installListeners(); if (_listChangeListener == null) { _listChangeListener = new ListChangeListener() { @Override public void onChanged(Change c) { hidePopup(); } }; } ((ListView) _node).getItems().addListener(_listChangeListener); if (_itemsChangeListener == null) { _itemsChangeListener = new ChangeListener>() { @Override public void changed(ObservableValue> observable, ObservableList oldValue, ObservableList newValue) { hidePopup(); } }; } ((ListView) _node).itemsProperty().addListener(_itemsChangeListener); } @Override public void uninstallListeners() { if (_listChangeListener != null) { ((ListView) _node).getItems().removeListener(_listChangeListener); _listChangeListener = null; } if (_itemsChangeListener != null) { ((ListView) _node).itemsProperty().removeListener(_itemsChangeListener); _itemsChangeListener = null; } super.uninstallListeners(); } @Override protected int getSelectedIndex() { return ((ListView) _node).getSelectionModel().getSelectedIndex(); } @Override protected void setSelectedIndex(int index, boolean incremental) { ListView listView = (ListView) _node; if (incremental) { listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); listView.getSelectionModel().select(index); listView.scrollTo(index); } else { listView.getSelectionModel().clearSelection(index); listView.getSelectionModel().select(index); listView.scrollTo(index); } } @Override protected int getElementCount() { return ((ListView) _node).getItems().size(); } @Override protected T getElementAt(int index) { return ((ListView) _node).getItems().get(index); } @Override protected String convertElementToString(T element) { return element != null ? element.toString() : ""; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy