1 14 package org.wings; 15 16 import org.wings.plaf.ComboBoxCG; 17 18 import javax.swing.*; 19 import javax.swing.event.EventListenerList ; 20 import javax.swing.event.ListDataEvent ; 21 import javax.swing.event.ListDataListener ; 22 import java.awt.*; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.ItemEvent ; 26 import java.awt.event.ItemListener ; 27 import java.util.Vector ; 28 29 35 public class SComboBox 36 extends SComponent 37 implements LowLevelEventListener, ListDataListener , ItemSelectable { 38 43 protected ComboBoxModel dataModel; 44 45 50 protected SListCellRenderer renderer; 51 52 55 protected int maximumRowCount = 8; 56 57 60 protected String actionCommand = "comboBoxChanged"; 61 62 63 private final SCellRendererPane cellRendererPane = new SCellRendererPane(); 65 66 70 protected Object selectedItemReminder; 71 72 private boolean firingActionEvent = false; 74 75 private boolean selectingItem = false; 77 78 private boolean delayEvent = false; 79 private boolean delayedEvent = false; 80 81 86 public SComboBox(ComboBoxModel model) { 87 setModel(model); 88 } 89 90 93 public SComboBox(final Object items[]) { 94 setModel(new DefaultComboBoxModel(items)); 95 } 96 97 100 public SComboBox(Vector items) { 101 setModel(new DefaultComboBoxModel(items)); 102 } 103 104 109 public SComboBox() { 110 setModel(new DefaultComboBoxModel()); 111 } 112 113 118 public void setModel(ComboBoxModel model) { 119 if (isDifferent(dataModel, model)) { 120 if (dataModel != null) 121 dataModel.removeListDataListener(this); 122 123 dataModel = model; 124 125 dataModel.addListDataListener(this); 126 127 selectedItemReminder = dataModel.getSelectedItem(); 129 130 reload(); 131 } 132 } 133 134 139 public final ComboBoxModel getModel() { 140 return dataModel; 141 } 142 143 149 public void setMaximumRowCount(int count) { 150 if (maximumRowCount != count) { 151 maximumRowCount = count; 152 153 reload(); 154 } 155 } 156 157 162 public int getMaximumRowCount() { 163 return maximumRowCount; 164 } 165 166 169 public void setRenderer(SListCellRenderer newRenderer) { 170 if (isDifferent(renderer, newRenderer)) { 171 renderer = newRenderer; 172 reload(); 173 } 174 } 175 176 179 public SListCellRenderer getRenderer() { 180 return renderer; 181 } 182 183 188 public void setSelectedItem(Object object) { 189 if (isDifferent(object, dataModel.getSelectedItem())) { 190 191 194 if (!delayEvent) { 195 selectingItem = true; 196 dataModel.setSelectedItem(object); 197 selectingItem = false; 198 199 selectedItemChanged(); 204 206 fireActionEvent(); 207 208 delayedEvent = false; 209 } else { 210 delayedEvent = true; 211 selectedItemReminder = object; 212 } 213 214 reload(); 215 } 216 } 217 218 223 public Object getSelectedItem() { 224 return dataModel.getSelectedItem(); 225 } 226 227 232 public void setSelectedIndex(int index) { 233 int size = dataModel.getSize(); 234 235 if (index == -1) 236 setSelectedItem(null); 237 else if (index < -1 || index >= size) 238 throw new IllegalArgumentException ("setSelectedIndex: " + index + 239 " out of bounds"); 240 else 241 setSelectedItem(dataModel.getElementAt(index)); 242 } 243 244 250 public int getSelectedIndex() { 251 Object selected = dataModel.getSelectedItem(); 252 253 if (selected == null) return -1; 254 255 for (int i = 0; i < getItemCount(); i++) { 256 if (selected.equals(getItemAt(i))) { 257 return i; 258 } 259 } 260 return -1; 261 } 262 263 268 public void addItem(Object object) { 269 checkMutableComboBoxModel(); 270 ((MutableComboBoxModel) dataModel).addElement(object); 271 } 272 273 279 public void insertItemAt(Object object, int index) { 280 checkMutableComboBoxModel(); 281 ((MutableComboBoxModel) dataModel).insertElementAt(object, index); 282 } 283 284 292 public void removeItem(Object object) { 293 checkMutableComboBoxModel(); 294 ((MutableComboBoxModel) dataModel).removeElement(object); 295 } 296 297 303 public void removeItemAt(int index) { 304 checkMutableComboBoxModel(); 305 ((MutableComboBoxModel) dataModel).removeElementAt(index); 306 } 307 308 311 public void removeAllItems() { 312 checkMutableComboBoxModel(); 313 MutableComboBoxModel model = (MutableComboBoxModel) dataModel; 314 int size = model.getSize(); 315 316 if (model instanceof DefaultComboBoxModel) { 317 ((DefaultComboBoxModel) model).removeAllElements(); 318 } else { 319 for (int i = 0; i < size; ++i) { 320 Object element = model.getElementAt(0); 321 model.removeElement(element); 322 } 323 } 324 } 325 326 void checkMutableComboBoxModel() { 327 if (!(dataModel instanceof MutableComboBoxModel)) 328 throw new RuntimeException ("Cannot use this method with a non-Mutable data model."); 329 } 330 331 332 333 334 340 public void addItemListener(ItemListener listener) { 341 addEventListener(ItemListener .class, listener); 342 } 343 344 349 public void removeItemListener(ItemListener listener) { 350 removeEventListener(ItemListener .class, listener); 351 } 352 353 358 public ItemListener [] getItemListener() { 359 return (ItemListener []) getListeners(ItemListener .class); 360 } 361 362 368 public void addActionListener(ActionListener listener) { 369 addEventListener(ActionListener .class, listener); 370 reload(); 371 } 372 373 378 public void removeActionListener(ActionListener listener) { 379 removeEventListener(ActionListener .class, listener); 380 reload(); 381 } 382 383 390 public ActionListener [] getActionListeners() { 391 return (ActionListener []) getListeners(ActionListener .class); 392 } 393 394 403 public void setActionCommand(String command) { 404 actionCommand = command; 405 } 406 407 414 public String getActionCommand() { 415 return actionCommand; 416 } 417 418 423 protected void fireItemStateChanged(ItemEvent e) { 424 Object [] listeners = getListenerList(); 426 for (int i = listeners.length - 2; i >= 0; i -= 2) { 429 if (listeners[i] == ItemListener .class) { 430 ((ItemListener ) listeners[i + 1]).itemStateChanged(e); 431 } 432 } 433 } 434 435 441 protected void fireActionEvent() { 442 if (!firingActionEvent) { 443 firingActionEvent = true; 445 446 ActionEvent e = null; 447 448 Object [] listeners = getListenerList(); 450 for (int i = listeners.length - 2; i >= 0; i -= 2) { 453 if (listeners[i] == ActionListener .class) { 454 if (e == null) 455 e = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 456 getActionCommand()); 457 ((ActionListener ) listeners[i + 1]).actionPerformed(e); 458 } 459 } 460 firingActionEvent = false; 461 } 462 } 463 464 469 public Object [] getSelectedObjects() { 470 Object selectedObject = getSelectedItem(); 471 if (selectedObject == null) 472 return new Object [0]; 473 else 474 return new Object []{selectedObject}; 475 } 476 477 483 public void contentsChanged(ListDataEvent e) { 484 if (isDifferent(selectedItemReminder, dataModel.getSelectedItem())) { 485 selectedItemChanged(); 486 if (!selectingItem) { 487 fireActionEvent(); 488 } 489 reload(); 490 } 491 } 492 493 499 public void intervalAdded(ListDataEvent e) { 500 reload(); 501 } 502 503 509 public void intervalRemoved(ListDataEvent e) { 510 reload(); 511 } 512 513 514 515 520 public int getItemCount() { 521 return dataModel.getSize(); 522 } 523 524 530 public Object getItemAt(int index) { 531 return dataModel.getElementAt(index); 532 } 533 534 538 protected void selectedItemChanged() { 539 if (selectedItemReminder != null) { 540 fireItemStateChanged(new ItemEvent (this, ItemEvent.ITEM_STATE_CHANGED, 541 selectedItemReminder, 542 ItemEvent.DESELECTED)); 543 } 544 545 selectedItemReminder = dataModel.getSelectedItem(); 547 548 if (selectedItemReminder != null) { 549 fireItemStateChanged(new ItemEvent (this, ItemEvent.ITEM_STATE_CHANGED, 550 selectedItemReminder, 551 ItemEvent.SELECTED)); 552 } 553 } 554 555 public void processLowLevelEvent(String action, String [] values) { 556 processKeyEvents(values); 557 558 delayEvent = true; 559 560 int selectedIndex = -1; 561 for (int i = 0; i < values.length; i++) { 563 try { 564 if (values[i].length() > 0) selectedIndex = Integer.parseInt(values[i]); 565 } catch (Exception ex) { 566 } 568 } 569 570 if (selectedIndex >= 0) { 571 setSelectedIndex(selectedIndex); 572 573 SForm.addArmedComponent(this); 574 } 575 576 delayEvent = false; 577 } 578 579 580 public void setParent(SContainer p) { 581 super.setParent(p); 582 if (getCellRendererPane() != null) 583 getCellRendererPane().setParent(p); 584 } 585 586 protected void setParentFrame(SFrame f) { 587 super.setParentFrame(f); 588 if (getCellRendererPane() != null) 589 getCellRendererPane().setParentFrame(f); 590 } 591 592 593 public void fireIntermediateEvents() { 594 } 595 596 public void fireFinalEvents() { 597 super.fireFinalEvents(); 598 if (delayedEvent) { 599 if (isDifferent(selectedItemReminder, dataModel.getSelectedItem())) { 600 selectingItem = true; 604 dataModel.setSelectedItem(selectedItemReminder); 605 selectingItem = false; 606 selectedItemChanged(); 607 fireActionEvent(); 608 } 609 610 611 delayedEvent = false; 612 } 613 614 } 615 616 617 private boolean epochCheckEnabled = true; 618 619 620 public boolean isEpochCheckEnabled() { 621 return epochCheckEnabled; 622 } 623 624 625 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 626 this.epochCheckEnabled = epochCheckEnabled; 627 } 628 629 public final SCellRendererPane getCellRendererPane() { 630 return cellRendererPane; 631 } 632 633 public void setCG(ComboBoxCG cg) { 634 super.setCG(cg); 635 } 636 637 public String getSelectionParameter(int index) { 638 return Integer.toString(index); 639 } 640 } 641 642 643 | Popular Tags |