1 42 package org.jfree.ui; 43 44 import java.util.ArrayList ; 45 import javax.swing.ComboBoxModel ; 46 import javax.swing.event.ListDataEvent ; 47 import javax.swing.event.ListDataListener ; 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 key; 99 } 100 101 106 public Object getValue() 107 { 108 return 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 data = new ArrayList (); 147 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 data.clear(); 178 data.ensureCapacity(keys.length); 179 180 for (int i = 0; i < values.length; i++) 181 { 182 add(keys[i], values[i]); 183 } 184 185 selectedItemIndex = -1; 186 final ListDataEvent evt = new ListDataEvent 187 (this, ListDataEvent.CONTENTS_CHANGED, 0, data.size() - 1); 188 fireListDataEvent(evt); 189 } 190 191 196 protected synchronized void fireListDataEvent(final ListDataEvent evt) 197 { 198 if (tempListeners == null) 199 { 200 tempListeners = (ListDataListener []) listdatalistener.toArray 201 (new ListDataListener [listdatalistener.size()]); 202 } 203 for (int i = 0; i < tempListeners.length; i++) 204 { 205 final ListDataListener l = tempListeners[i]; 206 l.contentsChanged(evt); 207 } 208 } 209 210 215 public Object getSelectedItem() 216 { 217 return selectedItemValue; 218 } 219 220 226 public void setSelectedKey(final Object anItem) 227 { 228 if (anItem == null) 229 { 230 selectedItemIndex = -1; 231 selectedItemValue = null; 232 } 233 else 234 { 235 final int newSelectedItem = findDataElementIndex(anItem); 236 if (newSelectedItem == -1) 237 { 238 selectedItemIndex = -1; 239 selectedItemValue = null; 240 } 241 else 242 { 243 selectedItemIndex = newSelectedItem; 244 selectedItemValue = getElementAt(selectedItemIndex); 245 } 246 } 247 fireListDataEvent(new ListDataEvent (this, ListDataEvent.CONTENTS_CHANGED, -1, -1)); 248 } 249 250 258 public void setSelectedItem(final Object anItem) 259 { 260 if (anItem == null) 261 { 262 selectedItemIndex = -1; 263 selectedItemValue = null; 264 } 265 else 266 { 267 final int newSelectedItem = findElementIndex(anItem); 268 if (newSelectedItem == -1) 269 { 270 if (isAllowOtherValue()) 271 { 272 selectedItemIndex = -1; 273 selectedItemValue = anItem; 274 } 275 else 276 { 277 selectedItemIndex = -1; 278 selectedItemValue = null; 279 } 280 } 281 else 282 { 283 selectedItemIndex = newSelectedItem; 284 selectedItemValue = getElementAt(selectedItemIndex); 285 } 286 } 287 fireListDataEvent(new ListDataEvent (this, ListDataEvent.CONTENTS_CHANGED, -1, -1)); 288 } 289 290 private boolean isAllowOtherValue() 291 { 292 return allowOtherValue; 293 } 294 295 public void setAllowOtherValue(final boolean allowOtherValue) 296 { 297 this.allowOtherValue = allowOtherValue; 298 } 299 300 306 public synchronized void addListDataListener(final ListDataListener l) 307 { 308 listdatalistener.add(l); 309 tempListeners = null; 310 } 311 312 318 public Object getElementAt(final int index) 319 { 320 if (index >= data.size()) 321 { 322 return null; 323 } 324 325 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(index); 326 if (datacon == null) 327 { 328 return null; 329 } 330 return datacon.getValue(); 331 } 332 333 339 public Object getKeyAt(final int index) 340 { 341 if (index >= data.size()) 342 { 343 return null; 344 } 345 346 if (index < 0) 347 { 348 return null; 349 } 350 351 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(index); 352 if (datacon == null) 353 { 354 return null; 355 } 356 return datacon.getKey(); 357 } 358 359 364 public Object getSelectedKey() 365 { 366 return getKeyAt(selectedItemIndex); 367 } 368 369 374 public int getSize() 375 { 376 return data.size(); 377 } 378 379 385 public void removeListDataListener(final ListDataListener l) 386 { 387 listdatalistener.remove(l); 388 tempListeners = null; 389 } 390 391 398 private int findDataElementIndex(final Object anItem) 399 { 400 if (anItem == null) 401 { 402 throw new NullPointerException ("Item to find must not be null"); 403 } 404 405 for (int i = 0; i < data.size(); i++) 406 { 407 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(i); 408 if (anItem.equals(datacon.getKey())) 409 { 410 return i; 411 } 412 } 413 return -1; 414 } 415 416 423 public int findElementIndex(final Object key) 424 { 425 if (key == null) 426 { 427 throw new NullPointerException ("Item to find must not be null"); 428 } 429 430 for (int i = 0; i < data.size(); i++) 431 { 432 final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(i); 433 if (key.equals(datacon.getValue())) 434 { 435 return i; 436 } 437 } 438 return -1; 439 } 440 441 446 public void removeDataElement(final Object key) 447 { 448 final int idx = findDataElementIndex(key); 449 if (idx == -1) 450 { 451 return; 452 } 453 454 data.remove(idx); 455 final ListDataEvent evt = new ListDataEvent 456 (this, ListDataEvent.INTERVAL_REMOVED, idx, idx); 457 fireListDataEvent(evt); 458 } 459 460 466 public void add(final Object key, final Object cbitem) 467 { 468 final ComboBoxItemPair con = new ComboBoxItemPair(key, cbitem); 469 data.add(con); 470 final ListDataEvent evt = new ListDataEvent 471 (this, ListDataEvent.INTERVAL_ADDED, data.size() - 2, data.size() - 2); 472 fireListDataEvent(evt); 473 } 474 475 478 public void clear() 479 { 480 final int size = getSize(); 481 data.clear(); 482 final ListDataEvent evt = new ListDataEvent (this, ListDataEvent.INTERVAL_REMOVED, 0, size - 1); 483 fireListDataEvent(evt); 484 } 485 486 } 487 | Popular Tags |