1:
42: package ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
61: public class KeyedComboBoxModel implements ComboBoxModel
62: {
63:
64:
67: private static class ComboBoxItemPair
68: {
69:
72: private Object key;
73:
76: private Object value;
77:
78:
85: public ComboBoxItemPair(final Object key, final Object value)
86: {
87: this.key = key;
88: this.value = value;
89: }
90:
91:
96: public Object getKey()
97: {
98: return this.key;
99: }
100:
101:
106: public Object getValue()
107: {
108: return this.value;
109: }
110:
111:
116: public void setValue(final Object value)
117: {
118: this.value = value;
119: }
120: }
121:
122:
125: private int selectedItemIndex;
126: private Object selectedItemValue;
127:
130: private ArrayList data;
131:
134: private ArrayList listdatalistener;
135:
138: private transient ListDataListener[] tempListeners;
139: private boolean allowOtherValue;
140:
141:
144: public KeyedComboBoxModel()
145: {
146: this.data = new ArrayList();
147: this.listdatalistener = new ArrayList();
148: }
149:
150:
157: public KeyedComboBoxModel(final Object[] keys, final Object[] values)
158: {
159: this();
160: setData(keys, values);
161: }
162:
163:
170: public void setData(final Object[] keys, final Object[] values)
171: {
172: if (values.length != keys.length)
173: {
174: throw new IllegalArgumentException("Values and text must have the same length.");
175: }
176:
177: this.data.clear();
178: this.data.ensureCapacity(keys.length);
179:
180: for (int i = 0; i < values.length; i++)
181: {
182: add(keys[i], values[i]);
183: }
184:
185: this.selectedItemIndex = -1;
186: final ListDataEvent evt = new ListDataEvent
187: (this, ListDataEvent.CONTENTS_CHANGED, 0, this.data.size() - 1);
188: fireListDataEvent(evt);
189: }
190:
191:
196: protected synchronized void fireListDataEvent(final ListDataEvent evt)
197: {
198: if (this.tempListeners == null)
199: {
200: this.tempListeners = (ListDataListener[]) this.listdatalistener.toArray
201: (new ListDataListener[this.listdatalistener.size()]);
202: }
203:
204: final ListDataListener[] listeners = this.tempListeners;
205: for (int i = 0; i < listeners.length; i++)
206: {
207: final ListDataListener l = listeners[i];
208: l.contentsChanged(evt);
209: }
210: }
211:
212:
217: public Object getSelectedItem()
218: {
219: return this.selectedItemValue;
220: }
221:
222:
228: public void setSelectedKey(final Object anItem)
229: {
230: if (anItem == null)
231: {
232: this.selectedItemIndex = -1;
233: this.selectedItemValue = null;
234: }
235: else
236: {
237: final int newSelectedItem = findDataElementIndex(anItem);
238: if (newSelectedItem == -1)
239: {
240: this.selectedItemIndex = -1;
241: this.selectedItemValue = null;
242: }
243: else
244: {
245: this.selectedItemIndex = newSelectedItem;
246: this.selectedItemValue = getElementAt(this.selectedItemIndex);
247: }
248: }
249: fireListDataEvent(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1));
250: }
251:
252:
260: public void setSelectedItem(final Object anItem)
261: {
262: if (anItem == null)
263: {
264: this.selectedItemIndex = -1;
265: this.selectedItemValue = null;
266: }
267: else
268: {
269: final int newSelectedItem = findElementIndex(anItem);
270: if (newSelectedItem == -1)
271: {
272: if (isAllowOtherValue())
273: {
274: this.selectedItemIndex = -1;
275: this.selectedItemValue = anItem;
276: }
277: else
278: {
279: this.selectedItemIndex = -1;
280: this.selectedItemValue = null;
281: }
282: }
283: else
284: {
285: this.selectedItemIndex = newSelectedItem;
286: this.selectedItemValue = getElementAt(this.selectedItemIndex);
287: }
288: }
289: fireListDataEvent(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1));
290: }
291:
292: private boolean isAllowOtherValue()
293: {
294: return this.allowOtherValue;
295: }
296:
297:
300: public void setAllowOtherValue(final boolean allowOtherValue)
301: {
302: this.allowOtherValue = allowOtherValue;
303: }
304:
305:
311: public synchronized void addListDataListener(final ListDataListener l)
312: {
313: if (l == null)
314: {
315: throw new NullPointerException();
316: }
317: this.listdatalistener.add(l);
318: this.tempListeners = null;
319: }
320:
321:
327: public Object getElementAt(final int index)
328: {
329: if (index >= this.data.size())
330: {
331: return null;
332: }
333:
334: final ComboBoxItemPair datacon = (ComboBoxItemPair) this.data.get(index);
335: if (datacon == null)
336: {
337: return null;
338: }
339: return datacon.getValue();
340: }
341:
342:
348: public Object getKeyAt(final int index)
349: {
350: if (index >= this.data.size())
351: {
352: return null;
353: }
354:
355: if (index < 0)
356: {
357: return null;
358: }
359:
360: final ComboBoxItemPair datacon = (ComboBoxItemPair) this.data.get(index);
361: if (datacon == null)
362: {
363: return null;
364: }
365: return datacon.getKey();
366: }
367:
368:
373: public Object getSelectedKey()
374: {
375: return getKeyAt(this.selectedItemIndex);
376: }
377:
378:
383: public int getSize()
384: {
385: return this.data.size();
386: }
387:
388:
394: public void removeListDataListener(final ListDataListener l)
395: {
396: this.listdatalistener.remove(l);
397: this.tempListeners = null;
398: }
399:
400:
407: private int findDataElementIndex(final Object anItem)
408: {
409: if (anItem == null)
410: {
411: throw new NullPointerException("Item to find must not be null");
412: }
413:
414: for (int i = 0; i < this.data.size(); i++)
415: {
416: final ComboBoxItemPair datacon = (ComboBoxItemPair) this.data.get(i);
417: if (anItem.equals(datacon.getKey()))
418: {
419: return i;
420: }
421: }
422: return -1;
423: }
424:
425:
432: public int findElementIndex(final Object key)
433: {
434: if (key == null)
435: {
436: throw new NullPointerException("Item to find must not be null");
437: }
438:
439: for (int i = 0; i < this.data.size(); i++)
440: {
441: final ComboBoxItemPair datacon = (ComboBoxItemPair) this.data.get(i);
442: if (key.equals(datacon.getValue()))
443: {
444: return i;
445: }
446: }
447: return -1;
448: }
449:
450:
455: public void removeDataElement(final Object key)
456: {
457: final int idx = findDataElementIndex(key);
458: if (idx == -1)
459: {
460: return;
461: }
462:
463: this.data.remove(idx);
464: final ListDataEvent evt = new ListDataEvent
465: (this, ListDataEvent.INTERVAL_REMOVED, idx, idx);
466: fireListDataEvent(evt);
467: }
468:
469:
475: public void add(final Object key, final Object cbitem)
476: {
477: final ComboBoxItemPair con = new ComboBoxItemPair(key, cbitem);
478: this.data.add(con);
479: final ListDataEvent evt = new ListDataEvent
480: (this, ListDataEvent.INTERVAL_ADDED, this.data.size() - 2, this.data.size() - 2);
481: fireListDataEvent(evt);
482: }
483:
484:
487: public void clear()
488: {
489: final int size = getSize();
490: this.data.clear();
491: final ListDataEvent evt = new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, 0, size - 1);
492: fireListDataEvent(evt);
493: }
494:
495: }