KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBJComboBox


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import java.util.Vector JavaDoc;
5
6
7 /**
8  * A JComboBox that sets the KeySelectionManager in the constructors.
9  *
10  * @author Trudi
11  */

12
13 public class CBJComboBox extends JComboBox
14 {
15
16     /**
17      * Creates a JComboBox with a default data model.
18      * Sets a key selection manager.
19      */

20
21     public CBJComboBox()
22     {
23         super();
24         setKeySelectionManager(new CBKeySelectionManager(this));
25     }
26
27
28     /**
29      * Creates a JComboBox that takes it's items from an existing ComboBoxModel.
30      * Sets a key selection manager.
31      */

32
33     public CBJComboBox(ComboBoxModel aModel)
34     {
35         super(aModel);
36         setKeySelectionManager(new CBKeySelectionManager(this));
37     }
38
39
40     /**
41      * Creates a JComboBox that contains the elements in the specified array.
42      * Sets a key selection manager.
43      */

44
45     public CBJComboBox(Object JavaDoc[] items)
46     {
47         super(items);
48         setKeySelectionManager(new CBKeySelectionManager(this));
49     }
50
51
52     /**
53      * Creates a JComboBox that contains the elements in the specified Vector.
54      * Sets a key selection manager.
55      */

56
57     public CBJComboBox(Vector JavaDoc items)
58     {
59         super(items);
60         setKeySelectionManager(new CBKeySelectionManager(this));
61     }
62
63
64     /**
65      * Changes the default key selection manager to allow two character identification
66      * of items within the combo box. For example if the user presses two keys within two
67      * seconds this class checks for a word beginning with the two characters that the keys
68      * represent (e.g. c+r = creatorsName). Normally the key selection manager would jump
69      * to the first word in the list that begins with a 'c' then the first word in the list
70      * that begins with an 'r'. This behaviour will still occur if the user presses keys that
71      * have a duration more than two seconds apart, for example, if a 'c' is pressed then three
72      * seconds later an 'r' the list would not jump to 'creatorsName' it would have jumped to
73      * the first word in the list beginning with an 'r'.
74      */

75
76     public class CBKeySelectionManager implements KeySelectionManager
77     {
78         /**
79          * The last keystroke the user entered before the current one.
80          */

81
82         String JavaDoc oldKey = null;
83
84         /**
85          * The position in combo box that the selection is to be moved to.
86          */

87
88         int position = -1;
89
90         /**
91          * The time in milliseconds of the last keystroke.
92          */

93
94         long oldTime = -1;
95
96         /**
97          * The combo box that this key-selection-manager is added to.
98          */

99
100         CBJComboBox combo;
101
102
103         /**
104          * Constructor that does nothing more than registers the combo box that this
105          * key selection manager belongs to.
106          *
107          * @param combo the CBJComboBox that this key selection manager is added to.
108          */

109
110         public CBKeySelectionManager(CBJComboBox combo)
111         {
112             super();
113             this.combo = combo;
114         }
115
116
117         /**
118          * A wrapper to selectionForKey(char aKey, ComboBoxModel aModel, long time)...just
119          * adds the time so that the manager can determine if two seconds has passed since
120          * the last key stroke.
121          *
122          * @param aKey a char value, usually indicating a keyboard key that was pressed.
123          * @param aModel the component's data model, containing the list of selectable items.
124          * @return an int equal to the selected row, where 0 is the first item and -1 is none.
125          */

126
127         public int selectionForKey(char aKey, ComboBoxModel aModel)
128         {
129             return selectionForKey(aKey, aModel, System.currentTimeMillis());
130         }
131
132
133         /**
134          * A wrapper to selectionForKey(char aKey, ComboBoxModel aModel, long time)...just
135          * adds the time so that the manager can determine if two seconds has passed since
136          * the last key stroke.
137          *
138          * @param aKey a char value, usually indicating a keyboard key that was pressed.
139          * @param aModel the component's data model, containing the list of selectable items.
140          * @param time the time the key stroke was pressed (actually this is not the exact time
141          * the key was pressed but the time the selectionForKey(char aKey, ComboBoxModel aModel)
142          * was called...'time' is just used to determine if two seconds has lapsed and if so
143          * indicating that the 'oldKey' should be cleared.
144          * @return an int equal to the selected row, where 0 is the first item and -1 is none.
145          */

146
147         public int selectionForKey(char aKey, ComboBoxModel aModel, long time)
148         {
149             String JavaDoc key = null; //TE: the beginning of the list item to be searched for within the list.
150

151             if (oldTime > -1)
152             {
153                 //TE: if two seconds have passed since the last keystroke...don't
154
//TE: keep the old key.
155
if ((time - oldTime) > 2000)
156                     oldKey = null;
157             }
158
159
160             if (oldKey != null) //TE: if the old key has not previously been
161
key = oldKey + String.valueOf(aKey); //TE: assigned make the key only equal the keystroke key.
162
else //TE: if the old key has previously been assigned make
163
key = String.valueOf(aKey); //TE: the key equal the old key + the keystroke key.
164

165             key = key.toLowerCase();
166
167             int count = combo.getItemCount();
168
169             for (int i = 0; i < count; i++)
170             {
171                 //TE: iterate thru the list checking for the first list item that begins with the key.
172

173                 Object JavaDoc temp = combo.getItemAt(i);
174
175                 if ((((String JavaDoc) temp).toLowerCase()).startsWith(key)) //TE: if there is a match...
176
{
177
178                     position = i;
179                     oldKey = String.valueOf(aKey); //TE: remember the current key.
180
oldTime = time; //TE: remember the current time.
181

182                     return position;
183                 }
184             }
185
186             oldKey = String.valueOf(aKey); //TE: remember the current key.
187
oldTime = time; //TE: remember the current time.
188

189             return position;
190         }
191     }
192 }
Popular Tags