001: /*
002: * @(#)${NAME}
003: *
004: * Copyright 2002 - 2004 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import com.jidesoft.swing.event.SearchableEvent;
009:
010: import javax.swing.*;
011: import javax.swing.event.ListDataEvent;
012: import javax.swing.event.ListDataListener;
013: import java.beans.PropertyChangeEvent;
014: import java.beans.PropertyChangeListener;
015:
016: /**
017: * <code>ListSearchable</code> is an concrete implementation of {@link Searchable}
018: * that enables the search function in JList.
019: * <p>It's very simple to use it. Assuming you have a JList, all you need to do is to
020: * call
021: * <code><pre>
022: * JList list = ....;
023: * ListSearchable searchable = new ListSearchable(list);
024: * </pre></code>
025: * Now the JList will have the search function.
026: * <p/>
027: * There is very little customization you need to do to ListSearchable. The only thing you might
028: * need is when the element in the JList needs a special conversion to convert to string. If so, you can overide
029: * convertElementToString() to provide you own algorithm to do the conversion.
030: * <code><pre>
031: * JList list = ....;
032: * ListSearchable searchable = new ListSearchable(list) {
033: * protected String convertElementToString(Object object) {
034: * ...
035: * }
036: * };
037: * </pre></code>
038: * <p/>
039: * Additional customization can be done on the base Searchable class such as background and foreground color, keystrokes,
040: * case sensitivity.
041: */
042: public class ListSearchable extends Searchable implements
043: ListDataListener, PropertyChangeListener {
044:
045: public ListSearchable(JList list) {
046: super (list);
047: list.getModel().addListDataListener(this );
048: list.addPropertyChangeListener("model", this );
049: }
050:
051: @Override
052: public void uninstallListeners() {
053: super .uninstallListeners();
054: if (_component instanceof JList) {
055: ((JList) _component).getModel()
056: .removeListDataListener(this );
057: }
058: _component.removePropertyChangeListener("model", this );
059: }
060:
061: @Override
062: protected void setSelectedIndex(int index, boolean incremental) {
063: if (incremental) {
064: ((JList) _component).addSelectionInterval(index, index);
065: } else {
066: if (((JList) _component).getSelectedIndex() != index) {
067: ((JList) _component).setSelectedIndex(index);
068: }
069: }
070: ((JList) _component).ensureIndexIsVisible(index);
071: }
072:
073: @Override
074: protected int getSelectedIndex() {
075: return ((JList) _component).getSelectedIndex();
076: }
077:
078: @Override
079: protected Object getElementAt(int index) {
080: ListModel listModel = ((JList) _component).getModel();
081: return listModel.getElementAt(index);
082: }
083:
084: @Override
085: protected int getElementCount() {
086: ListModel listModel = ((JList) _component).getModel();
087: return listModel.getSize();
088: }
089:
090: /**
091: * Converts the element in Jlist to string. The returned value will be the
092: * <code>toString()</code> of whatever element that returned from <code>list.getModel().getElementAt(i)</code>.
093: *
094: * @param object
095: * @return the string representing the element in the JList.
096: */
097: @Override
098: protected String convertElementToString(Object object) {
099: if (object != null) {
100: return object.toString();
101: } else {
102: return "";
103: }
104: }
105:
106: public void contentsChanged(ListDataEvent e) {
107: if (e.getIndex0() == -1 && e.getIndex1() == -1) {
108: return;
109: }
110: hidePopup();
111: fireSearchableEvent(new SearchableEvent(this ,
112: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
113: }
114:
115: public void intervalAdded(ListDataEvent e) {
116: hidePopup();
117: fireSearchableEvent(new SearchableEvent(this ,
118: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
119: }
120:
121: public void intervalRemoved(ListDataEvent e) {
122: hidePopup();
123: fireSearchableEvent(new SearchableEvent(this ,
124: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
125: }
126:
127: public void propertyChange(PropertyChangeEvent evt) {
128: if ("model".equals(evt.getPropertyName())) {
129: hidePopup();
130:
131: ListModel oldModel = (ListModel) evt.getOldValue();
132: if (oldModel != null) {
133: oldModel.removeListDataListener(this );
134: }
135:
136: ListModel newModel = (ListModel) evt.getNewValue();
137: if (newModel != null) {
138: newModel.addListDataListener(this );
139: }
140: fireSearchableEvent(new SearchableEvent(this,
141: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
142: }
143: }
144: }
|