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.awt.*;
014: import java.beans.PropertyChangeEvent;
015: import java.beans.PropertyChangeListener;
016:
017: /**
018: * <code>ComboBoxSearchable</code> is an concrete implementation of {@link Searchable}
019: * that enables the search function in non-editable JComboBox.
020: * <p>It's very simple to use it. Assuming you have a JComboBox, all you need to do is to
021: * call
022: * <code><pre>
023: * JComboBox comboBox = ....;
024: * ComboBoxSearchable searchable = new ComboBoxSearchable(comboBox);
025: * </pre></code>
026: * Now the JComboBox will have the search function.
027: * <p/>
028: * There is very little customization you need to do to ComboBoxSearchable. The only thing you might
029: * need is when the element in the JComboBox needs a special conversion to convert to string. If so, you can overide
030: * convertElementToString() to provide you own algorithm to do the conversion.
031: * <code><pre>
032: * JComboBox comboBox = ....;
033: * ComboBoxSearchable searchable = new ComboBoxSearchable(comboBox) {
034: * protected String convertElementToString(Object object) {
035: * ...
036: * }
037: * };
038: * </pre></code>
039: * <p/>
040: * Additional customization can be done on the base Searchable class such as background and foreground color, keystrokes,
041: * case sensitivity,
042: */
043: public class ComboBoxSearchable extends Searchable implements
044: ListDataListener, PropertyChangeListener {
045:
046: private boolean _showPopupDuringSearching = true;
047:
048: public ComboBoxSearchable(JComboBox comboBox) {
049: super (comboBox);
050:
051: // to avoid conflict with default type-match feature of JComboBox.
052: comboBox
053: .setKeySelectionManager(new JComboBox.KeySelectionManager() {
054: public int selectionForKey(char aKey,
055: ComboBoxModel aModel) {
056: return -1;
057: }
058: });
059: comboBox.getModel().addListDataListener(this );
060: comboBox.addPropertyChangeListener("model", this );
061: }
062:
063: @Override
064: public void uninstallListeners() {
065: super .uninstallListeners();
066: if (_component instanceof JComboBox) {
067: ((JComboBox) _component).getModel().removeListDataListener(
068: this );
069: }
070: _component.removePropertyChangeListener("model", this );
071: }
072:
073: /**
074: * Checks if the popup is showing during searching.
075: *
076: * @return true if popup is visible during searching.
077: * @deprecated misspelled. Use {@link #isShowPopupDuringSearching()}.
078: */
079: public boolean isShowPopupDuringSeaching() {
080: return _showPopupDuringSearching;
081: }
082:
083: /**
084: * Sets the property which determines if the popup should be shown during searching.
085: *
086: * @param showPopupDuringSeaching
087: * @deprecated misspelled. Use {@link #setShowPopupDuringSearching(boolean)}
088: */
089: public void setShowPopupDuringSeaching(
090: boolean showPopupDuringSeaching) {
091: _showPopupDuringSearching = showPopupDuringSeaching;
092: }
093:
094: /**
095: * Checks if the popup is showing during searching.
096: *
097: * @return true if popup is visible during searching.
098: */
099: public boolean isShowPopupDuringSearching() {
100: return _showPopupDuringSearching;
101: }
102:
103: /**
104: * Sets the property which determines if the popup should be shown during searching.
105: *
106: * @param showPopupDuringSearching
107: */
108: public void setShowPopupDuringSearching(
109: boolean showPopupDuringSearching) {
110: _showPopupDuringSearching = showPopupDuringSearching;
111: }
112:
113: @Override
114: protected void setSelectedIndex(int index, boolean incremental) {
115: if (isShowPopupDuringSearching()) {
116: try {
117: ((JComboBox) _component).showPopup();
118: } catch (IllegalComponentStateException e) {
119: }
120: }
121: if (((JComboBox) _component).getSelectedIndex() != index) {
122: ((JComboBox) _component).setSelectedIndex(index);
123: }
124: }
125:
126: @Override
127: protected int getSelectedIndex() {
128: return ((JComboBox) _component).getSelectedIndex();
129: }
130:
131: @Override
132: protected Object getElementAt(int index) {
133: ComboBoxModel comboBoxModel = ((JComboBox) _component)
134: .getModel();
135: return comboBoxModel.getElementAt(index);
136: }
137:
138: @Override
139: protected int getElementCount() {
140: ComboBoxModel comboBoxModel = ((JComboBox) _component)
141: .getModel();
142: return comboBoxModel.getSize();
143: }
144:
145: /**
146: * Converts the element in Jcombobox to string. The returned value will be the
147: * <code>toString()</code> of whatever element that returned from <code>list.getModel().getElementAt(i)</code>.
148: *
149: * @param object
150: * @return the string representing the element in the JComboBox.
151: */
152: @Override
153: protected String convertElementToString(Object object) {
154: if (object != null) {
155: return object.toString();
156: } else {
157: return "";
158: }
159: }
160:
161: public void contentsChanged(ListDataEvent e) {
162: if (e.getIndex0() == -1 && e.getIndex1() == -1) {
163: } else {
164: hidePopup();
165: fireSearchableEvent(new SearchableEvent(this ,
166: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
167: }
168: }
169:
170: public void intervalAdded(ListDataEvent e) {
171: hidePopup();
172: fireSearchableEvent(new SearchableEvent(this ,
173: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
174: }
175:
176: public void intervalRemoved(ListDataEvent e) {
177: hidePopup();
178: fireSearchableEvent(new SearchableEvent(this ,
179: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
180: }
181:
182: public void propertyChange(PropertyChangeEvent evt) {
183: if ("model".equals(evt.getPropertyName())) {
184: hidePopup();
185:
186: if (evt.getOldValue() instanceof ComboBoxModel) {
187: ((ComboBoxModel) evt.getOldValue())
188: .removeListDataListener(this );
189: }
190:
191: if (evt.getNewValue() instanceof ComboBoxModel) {
192: ((ComboBoxModel) evt.getNewValue())
193: .addListDataListener(this );
194: }
195: }
196: fireSearchableEvent(new SearchableEvent(this,
197: SearchableEvent.SEARCHABLE_MODEL_CHANGE));
198: }
199:
200: }
|