001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/swing/KeySelectableJList.java,v $
056: * $Revision: 1.1 $
057: * $Author: dglasser $
058: * $Date: 2003/01/26 01:02:15 $
059: *
060: * --------------------------------------------------------------------
061: */
062: package org.glasser.swing;
063:
064: import javax.swing.*;
065: import javax.swing.ListModel;
066: import java.awt.event.*;
067: import java.util.*;
068: import java.awt.*;
069: import javax.swing.text.*;
070:
071: /**
072: * This is a JList that allows items to be selected with character keys
073: * in the same way as the JComboBox does.
074: */
075: public class KeySelectableJList extends JList {
076:
077: public KeySelectableJList() {
078: super ();
079: }
080:
081: public KeySelectableJList(ListModel model) {
082: super (model);
083: }
084:
085: public KeySelectableJList(final Object[] listData) {
086: super (listData);
087: }
088:
089: public KeySelectableJList(final Vector listData) {
090: super (listData);
091: }
092:
093: final static boolean javaVersionOnePointFourOrGreater;
094:
095: static {
096: String version = System.getProperty("java.version");
097: System.out.println("Java Version: " + version);
098: if (version != null && version.compareTo("1.3.9") > 0) {
099: javaVersionOnePointFourOrGreater = true;
100: System.out
101: .println("Java version is 1.4 or greater, built-in JList key selection functionality will be used.");
102: } else {
103: javaVersionOnePointFourOrGreater = false;
104: }
105: }
106:
107: public void processKeyEvent(KeyEvent ev) {
108:
109: //System.out.println("\nPROCESS KEY EVENT.");
110:
111: super .processKeyEvent(ev);
112:
113: if (javaVersionOnePointFourOrGreater) {
114:
115: // if this is version 1.4 or greater, the only thing we want
116: // to do is scroll the list to the selected item (if it is in a
117: // JScrollPane.)
118: int index = this .getSelectedIndex();
119: if (index > -1) {
120: this .ensureIndexIsVisible(index);
121: }
122: return;
123: }
124:
125: // if this a Java version prior to 1.4, we will invoke the key selection
126: // logic.
127: if (ev.getID() == KeyEvent.KEY_PRESSED) {
128:
129: // see if this is a letter key.
130: char keyChar = ev.getKeyChar();
131: if ((Character.isLetter(keyChar) && ev.isAltDown() == false)) {
132: int index = selectionForKey(keyChar);
133: if (index > -1) {
134: // System.out.println("SETTING SELECTED INDEX TO: " + index);
135: this .setSelectedIndex(index);
136: this .ensureIndexIsVisible(index);
137: }
138: }
139: }
140: }
141:
142: public int selectionForKey(char key) {
143:
144: int selectedIndex = -1;
145: ListModel model = this .getModel();
146: Object selectedItem = this .getSelectedValue();
147:
148: // if there's already an item selected...
149: if (selectedItem != null) {
150:
151: // get the string value of the selected item
152: selectedItem = selectedItem.toString();
153:
154: for (int j = 0; j < model.getSize(); j++) {
155: if (selectedItem.equals(model.getElementAt(j)
156: .toString())) {
157: selectedIndex = j;
158: break;
159: }
160: }
161: }
162:
163: // System.out.println("Selected index is " + selectedIndex);
164:
165: key = Character.toLowerCase(key);
166:
167: for (int j = selectedIndex + 1; j < model.getSize(); j++) {
168: String displayString = model.getElementAt(j).toString();
169: if (displayString.length() > 0
170: && Character.toLowerCase(displayString.charAt(0)) == key) {
171: // System.out.println(">>MATCH FOUND: " + displayString);
172: return j;
173: }
174: }
175:
176: for (int j = 0; j < selectedIndex; j++) {
177: String displayString = model.getElementAt(j).toString();
178: if (displayString.length() > 0
179: && Character.toLowerCase(displayString.charAt(0)) == key) {
180: // System.out.println("<<MATCH FOUND: " + displayString);
181: return j;
182: }
183: }
184:
185: return -1;
186: }
187:
188: public static void main(String[] args) throws Exception {
189: JFrame frame = new JFrame();
190: frame.setDefaultCloseOperation(3);
191: JPanel cp = new JPanel();
192: cp.setLayout(new BorderLayout());
193:
194: JList list = new KeySelectableJList(
195: new String[] { "Apples", "Grapes", "Oranges",
196: "Peaches", "Pears", "Watermelons" });
197:
198: cp.add(new JScrollPane(list), BorderLayout.CENTER);
199: frame.setContentPane(cp);
200: frame.pack();
201: frame.setVisible(true);
202:
203: }
204:
205: }
|