Debian Codegraph @

source/org/jfree/report/modules/gui/swing/common/KeyedComboBoxModel.java

raw
1 /**
2 * =========================================================
3 * Pentaho-Reporting-Classic : a free Java reporting library
4 * =========================================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * KeyedComboBoxModel.java
27 * ------------
28 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
29 */
30
31 package org.jfree.report.modules.gui.swing.common;
32
33 import java.util.ArrayList;
34 import javax.swing.ComboBoxModel;
35 import javax.swing.event.ListDataEvent;
36 import javax.swing.event.ListDataListener;
37
38 /**
39 * The KeyedComboBox model allows to define an internal key (the data element)
40 * for every entry in the model.
41 * <p/>
42 * This class is usefull in all cases, where the public text differs from the
43 * internal view on the data. A separation between presentation data and
44 * processing data is a prequesite for localizing combobox entries. This model
45 * does not allow selected elements, which are not in the list of valid
46 * elements.
47 *
48 * @author Thomas Morgner
49 */
50 public class KeyedComboBoxModel implements ComboBoxModel
51 {
52
53 /**
54 * The internal data carrier to map keys to values and vice versa.
55 */
56 private static class ComboBoxItemPair
57 {
58 /**
59 * The key.
60 */
61 private Object key;
62 /**
63 * The value for the key.
64 */
65 private Object value;
66
67 /**
68 * Creates a new item pair for the given key and value. The value can be
69 * changed later, if needed.
70 *
71 * @param key the key
72 * @param value the value
73 */
74 public ComboBoxItemPair(final Object key, final Object value)
75 {
76 this.key = key;
77 this.value = value;
78 }
79
80 /**
81 * Returns the key.
82 *
83 * @return the key.
84 */
85 public Object getKey()
86 {
87 return key;
88 }
89
90 /**
91 * Returns the value.
92 *
93 * @return the value for this key.
94 */
95 public Object getValue()
96 {
97 return value;
98 }
99
100 /**
101 * Redefines the value stored for that key.
102 *
103 * @param value the new value.
104 */
105 public void setValue(final Object value)
106 {
107 this.value = value;
108 }
109 }
110
111 /**
112 * The index of the selected item.
113 */
114 private int selectedItemIndex;
115 private Object selectedItemValue;
116 /**
117 * The data (contains ComboBoxItemPairs).
118 */
119 private ArrayList data;
120 /**
121 * The listeners.
122 */
123 private ArrayList listdatalistener;
124 /**
125 * The cached listeners as array.
126 */
127 private transient ListDataListener[] tempListeners;
128 private boolean allowOtherValue;
129
130 /**
131 * Creates a new keyed combobox model.
132 */
133 public KeyedComboBoxModel()
134 {
135 data = new ArrayList();
136 listdatalistener = new ArrayList();
137 }
138
139 /**
140 * Creates a new keyed combobox model for the given keys and values. Keys
141 * and values must have the same number of items.
142 *
143 * @param keys the keys
144 * @param values the values
145 */
146 public KeyedComboBoxModel(final Object[] keys, final Object[] values)
147 {
148 this();
149 setData(keys, values);
150 }
151
152 /**
153 * Replaces the data in this combobox model. The number of keys must be
154 * equals to the number of values.
155 *
156 * @param keys the keys
157 * @param values the values
158 */
159 public void setData(final Object[] keys, final Object[] values)
160 {
161 if (values.length != keys.length)
162 {
163 throw new IllegalArgumentException("Values and text must have the same length.");
164 }
165
166 data.clear();
167 data.ensureCapacity(keys.length);
168
169 for (int i = 0; i < values.length; i++)
170 {
171 add(keys[i], values[i]);
172 }
173
174 selectedItemIndex = -1;
175 final ListDataEvent evt = new ListDataEvent
176 (this, ListDataEvent.CONTENTS_CHANGED, 0, data.size() - 1);
177 fireListDataEvent(evt);
178 }
179
180 /**
181 * Notifies all registered list data listener of the given event.
182 *
183 * @param evt the event.
184 */
185 protected synchronized void fireListDataEvent(final ListDataEvent evt)
186 {
187 if (tempListeners == null)
188 {
189 tempListeners = (ListDataListener[]) listdatalistener.toArray
190 (new ListDataListener[listdatalistener.size()]);
191 }
192
193 final ListDataListener[] listeners = tempListeners;
194 for (int i = 0; i < listeners.length; i++)
195 {
196 final ListDataListener l = listeners[i];
197 l.contentsChanged(evt);
198 }
199 }
200
201 /**
202 * Returns the selected item.
203 *
204 * @return The selected item or <code>null</code> if there is no selection
205 */
206 public Object getSelectedItem()
207 {
208 return selectedItemValue;
209 }
210
211 /**
212 * Defines the selected key. If the object is not in the list of values, no
213 * item gets selected.
214 *
215 * @param anItem the new selected item.
216 */
217 public void setSelectedKey(final Object anItem)
218 {
219 if (anItem == null)
220 {
221 selectedItemIndex = -1;
222 selectedItemValue = null;
223 }
224 else
225 {
226 final int newSelectedItem = findDataElementIndex(anItem);
227 if (newSelectedItem == -1)
228 {
229 selectedItemIndex = -1;
230 selectedItemValue = null;
231 }
232 else
233 {
234 selectedItemIndex = newSelectedItem;
235 selectedItemValue = getElementAt(selectedItemIndex);
236 }
237 }
238 fireListDataEvent(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1));
239 }
240
241 /**
242 * Set the selected item. The implementation of this method should notify
243 * all registered <code>ListDataListener</code>s that the contents have
244 * changed.
245 *
246 * @param anItem the list object to select or <code>null</code> to clear the
247 * selection
248 */
249 public void setSelectedItem(final Object anItem)
250 {
251 if (anItem == null)
252 {
253 selectedItemIndex = -1;
254 selectedItemValue = null;
255 }
256 else
257 {
258 final int newSelectedItem = findElementIndex(anItem);
259 if (newSelectedItem == -1)
260 {
261 if (isAllowOtherValue())
262 {
263 selectedItemIndex = -1;
264 selectedItemValue = anItem;
265 }
266 else
267 {
268 selectedItemIndex = -1;
269 selectedItemValue = null;
270 }
271 }
272 else
273 {
274 selectedItemIndex = newSelectedItem;
275 selectedItemValue = getElementAt(selectedItemIndex);
276 }
277 }
278 fireListDataEvent(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1));
279 }
280
281 private boolean isAllowOtherValue()
282 {
283 return allowOtherValue;
284 }
285
286 public void setAllowOtherValue(final boolean allowOtherValue)
287 {
288 this.allowOtherValue = allowOtherValue;
289 }
290
291 /**
292 * Adds a listener to the list that's notified each time a change to the data
293 * model occurs.
294 *
295 * @param l the <code>ListDataListener</code> to be added
296 */
297 public synchronized void addListDataListener(final ListDataListener l)
298 {
299 if (l == null)
300 {
301 throw new NullPointerException();
302 }
303 listdatalistener.add(l);
304 tempListeners = null;
305 }
306
307 /**
308 * Returns the value at the specified index.
309 *
310 * @param index the requested index
311 * @return the value at <code>index</code>
312 */
313 public Object getElementAt(final int index)
314 {
315 if (index >= data.size())
316 {
317 return null;
318 }
319
320 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(index);
321 if (datacon == null)
322 {
323 return null;
324 }
325 return datacon.getValue();
326 }
327
328 /**
329 * Returns the key from the given index.
330 *
331 * @param index the index of the key.
332 * @return the the key at the specified index.
333 */
334 public Object getKeyAt(final int index)
335 {
336 if (index >= data.size())
337 {
338 return null;
339 }
340
341 if (index < 0)
342 {
343 return null;
344 }
345
346 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(index);
347 if (datacon == null)
348 {
349 return null;
350 }
351 return datacon.getKey();
352 }
353
354 /**
355 * Returns the selected data element or null if none is set.
356 *
357 * @return the selected data element.
358 */
359 public Object getSelectedKey()
360 {
361 return getKeyAt(selectedItemIndex);
362 }
363
364 /**
365 * Returns the length of the list.
366 *
367 * @return the length of the list
368 */
369 public int getSize()
370 {
371 return data.size();
372 }
373
374 /**
375 * Removes a listener from the list that's notified each time a change to
376 * the data model occurs.
377 *
378 * @param l the <code>ListDataListener</code> to be removed
379 */
380 public void removeListDataListener(final ListDataListener l)
381 {
382 listdatalistener.remove(l);
383 tempListeners = null;
384 }
385
386 /**
387 * Searches an element by its data value. This method is called by the
388 * setSelectedItem method and returns the first occurence of the element.
389 *
390 * @param anItem the item
391 * @return the index of the item or -1 if not found.
392 */
393 private int findDataElementIndex(final Object anItem)
394 {
395 if (anItem == null)
396 {
397 throw new NullPointerException("Item to find must not be null");
398 }
399
400 for (int i = 0; i < data.size(); i++)
401 {
402 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(i);
403 if (anItem.equals(datacon.getKey()))
404 {
405 return i;
406 }
407 }
408 return -1;
409 }
410
411 /**
412 * Tries to find the index of element with the given key. The key must not
413 * be null.
414 *
415 * @param key the key for the element to be searched.
416 * @return the index of the key, or -1 if not found.
417 */
418 public int findElementIndex(final Object key)
419 {
420 if (key == null)
421 {
422 throw new NullPointerException("Item to find must not be null");
423 }
424
425 for (int i = 0; i < data.size(); i++)
426 {
427 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(i);
428 if (key.equals(datacon.getValue()))
429 {
430 return i;
431 }
432 }
433 return -1;
434 }
435
436 /**
437 * Removes an entry from the model.
438 *
439 * @param key the key
440 */
441 public void removeDataElement(final Object key)
442 {
443 final int idx = findDataElementIndex(key);
444 if (idx == -1)
445 {
446 return;
447 }
448
449 data.remove(idx);
450 final ListDataEvent evt = new ListDataEvent
451 (this, ListDataEvent.INTERVAL_REMOVED, idx, idx);
452 fireListDataEvent(evt);
453 }
454
455 /**
456 * Adds a new entry to the model.
457 *
458 * @param key the key
459 * @param cbitem the display value.
460 */
461 public void add(final Object key, final Object cbitem)
462 {
463 final ComboBoxItemPair con = new ComboBoxItemPair(key, cbitem);
464 data.add(con);
465 final ListDataEvent evt = new ListDataEvent
466 (this, ListDataEvent.INTERVAL_ADDED, data.size() - 2, data.size() - 2);
467 fireListDataEvent(evt);
468 }
469
470 /**
471 * Removes all entries from the model.
472 */
473 public void clear()
474 {
475 final int size = getSize();
476 data.clear();
477 final ListDataEvent evt = new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, 0, size - 1);
478 fireListDataEvent(evt);
479 }
480
481 }