1 29 30 package nextapp.echo2.app.list; 31 32 import java.util.EventListener ; 33 34 import nextapp.echo2.app.Border; 35 import nextapp.echo2.app.Color; 36 import nextapp.echo2.app.Component; 37 import nextapp.echo2.app.Extent; 38 import nextapp.echo2.app.Font; 39 import nextapp.echo2.app.Insets; 40 import nextapp.echo2.app.event.ActionEvent; 41 import nextapp.echo2.app.event.ActionListener; 42 import nextapp.echo2.app.event.ChangeEvent; 43 import nextapp.echo2.app.event.ChangeListener; 44 import nextapp.echo2.app.event.ListDataEvent; 45 import nextapp.echo2.app.event.ListDataListener; 46 47 50 public abstract class AbstractListComponent extends Component { 51 52 public static final String INPUT_ACTION = "action"; 53 54 public static final String PROPERTY_ACTION_COMMAND = "actionCommand"; 55 56 public static final String ACTION_LISTENERS_CHANGED_PROPERTY = "actionListeners"; 57 public static final String LIST_DATA_CHANGED_PROPERTY = "listData"; 58 public static final String LIST_MODEL_CHANGED_PROPERTY = "listModel"; 59 public static final String LIST_CELL_RENDERER_CHANGED_PROPERTY = "listCellRenderer"; 60 public static final String SELECTION_MODEL_CHANGED_PROPERTY = "listSelectionModel"; 61 public static final String SELECTION_CHANGED_PROPERTY = "listSelectionChanged"; 62 63 public static final String PROPERTY_BORDER = "border"; 64 public static final String PROPERTY_DISABLED_BACKGROUND = "disabledBackground"; 65 public static final String PROPERTY_DISABLED_BORDER = "disabledBorder"; 66 public static final String PROPERTY_DISABLED_FONT = "disabledFont"; 67 public static final String PROPERTY_DISABLED_FOREGROUND = "disabledForeground"; 68 public static final String PROPERTY_HEIGHT = "height"; 69 public static final String PROPERTY_INSETS = "insets"; 70 public static final String PROPERTY_ROLLOVER_BACKGROUND = "rolloverBackground"; 71 public static final String PROPERTY_ROLLOVER_ENABLED = "rolloverEnabled"; 72 public static final String PROPERTY_ROLLOVER_FONT = "rolloverFont"; 73 public static final String PROPERTY_ROLLOVER_FOREGROUND = "rolloverForeground"; 74 public static final String PROPERTY_TOOL_TIP_TEXT = "toolTipText"; 75 public static final String PROPERTY_WIDTH = "width"; 76 77 public static final DefaultListCellRenderer DEFAULT_LIST_CELL_RENDERER = new DefaultListCellRenderer(); 78 79 82 private ChangeListener changeHandler = new ChangeListener() { 83 84 87 public void stateChanged(ChangeEvent e) { 88 firePropertyChange(SELECTION_CHANGED_PROPERTY, null, null); 89 } 90 }; 91 92 95 private ListDataListener listDataHandler = new ListDataListener() { 96 97 100 public void contentsChanged(ListDataEvent e) { 101 firePropertyChange(LIST_DATA_CHANGED_PROPERTY, null, null); 102 } 103 104 107 public void intervalAdded(ListDataEvent e) { 108 firePropertyChange(LIST_DATA_CHANGED_PROPERTY, null, null); 109 } 110 111 114 public void intervalRemoved(ListDataEvent e) { 115 firePropertyChange(LIST_DATA_CHANGED_PROPERTY, null, null); 116 } 117 }; 118 119 private ListCellRenderer listCellRenderer = DEFAULT_LIST_CELL_RENDERER; 120 private ListModel model; 121 private ListSelectionModel selectionModel; 122 123 126 public AbstractListComponent() { 127 this(null, null); 128 } 129 130 137 public AbstractListComponent(ListModel model, ListSelectionModel selectionModel) { 138 super(); 139 if (model == null) { 140 model = new DefaultListModel(); 141 } 142 if (selectionModel == null) { 143 selectionModel = new DefaultListSelectionModel(); 144 } 145 setModel(model); 146 setSelectionModel(selectionModel); 147 } 148 149 156 public void addActionListener(ActionListener l) { 157 getEventListenerList().addListener(ActionListener.class, l); 158 firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, null, l); 161 } 162 163 166 private void fireActionEvent() { 167 if (!hasEventListenerList()) { 168 return; 169 } 170 EventListener [] listeners = getEventListenerList().getListeners(ActionListener.class); 171 ActionEvent e = null; 172 for (int i = 0; i < listeners.length; ++i) { 173 if (e == null) { 174 e = new ActionEvent(this, (String ) getRenderProperty(PROPERTY_ACTION_COMMAND)); 175 } 176 ((ActionListener) listeners[i]).actionPerformed(e); 177 } 178 } 179 180 187 public String getActionCommand() { 188 return (String ) getProperty(PROPERTY_ACTION_COMMAND); 189 } 190 191 196 public Border getBorder() { 197 return (Border) getProperty(PROPERTY_BORDER); 198 } 199 200 205 public ListCellRenderer getCellRenderer() { 206 return listCellRenderer; 207 } 208 209 215 public Color getDisabledBackground() { 216 return (Color) getProperty(PROPERTY_DISABLED_BACKGROUND); 217 } 218 219 225 public Border getDisabledBorder() { 226 return (Border) getProperty(PROPERTY_DISABLED_BORDER); 227 } 228 229 235 public Font getDisabledFont() { 236 return (Font) getProperty(PROPERTY_DISABLED_FONT); 237 } 238 239 245 public Color getDisabledForeground() { 246 return (Color) getProperty(PROPERTY_DISABLED_FOREGROUND); 247 } 248 249 256 public Extent getHeight() { 257 return (Extent) getProperty(PROPERTY_HEIGHT); 258 } 259 260 265 public Insets getInsets() { 266 return (Insets) getProperty(PROPERTY_INSETS); 267 } 268 269 274 public ListModel getModel() { 275 return model; 276 } 277 278 283 public Color getRolloverBackground() { 284 return (Color) getProperty(PROPERTY_ROLLOVER_BACKGROUND); 285 } 286 287 292 public Font getRolloverFont() { 293 return (Font) getProperty(PROPERTY_ROLLOVER_FONT); 294 } 295 296 301 public Color getRolloverForeground() { 302 return (Color) getProperty(PROPERTY_ROLLOVER_FOREGROUND); 303 } 304 305 310 public ListSelectionModel getSelectionModel() { 311 return selectionModel; 312 } 313 314 320 public String getToolTipText() { 321 return (String ) getProperty(PROPERTY_TOOL_TIP_TEXT); 322 } 323 324 331 public Extent getWidth() { 332 return (Extent) getProperty(PROPERTY_WIDTH); 333 } 334 335 340 public boolean hasActionListeners() { 341 return hasEventListenerList() && getEventListenerList().getListenerCount(ActionListener.class) != 0; 342 } 343 344 349 public boolean isRolloverEnabled() { 350 Boolean value = (Boolean ) getProperty(PROPERTY_ROLLOVER_ENABLED); 351 return value == null ? false : value.booleanValue(); 352 } 353 354 359 public boolean isValidChild(Component child) { 360 return false; 361 } 362 363 366 public void processInput(String inputName, Object inputValue) { 367 super.processInput(inputName, inputValue); 368 369 if (SELECTION_CHANGED_PROPERTY.equals(inputName)) { 370 int[] selectedIndices = (int[]) inputValue; 371 ListSelectionModel selectionModel = getSelectionModel(); 372 selectionModel.clearSelection(); 373 for (int i = 0; i < selectedIndices.length; ++i) { 374 selectionModel.setSelectedIndex(selectedIndices[i], true); 375 } 376 } else if (INPUT_ACTION.equals(inputName)) { 377 fireActionEvent(); 378 } 379 } 380 381 386 public void removeActionListener(ActionListener l) { 387 if (!hasEventListenerList()) { 388 return; 389 } 390 getEventListenerList().removeListener(ActionListener.class, l); 391 firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, l, null); 394 } 395 396 403 public void setActionCommand(String newValue) { 404 setProperty(PROPERTY_ACTION_COMMAND, newValue); 405 } 406 407 412 public void setBorder(Border newValue) { 413 setProperty(PROPERTY_BORDER, newValue); 414 } 415 416 423 public void setCellRenderer(ListCellRenderer newValue) { 424 if (newValue == null) { 425 throw new IllegalArgumentException ("Cell Renderer may not be null."); 426 } 427 ListCellRenderer oldValue = listCellRenderer; 428 listCellRenderer = newValue; 429 firePropertyChange(LIST_CELL_RENDERER_CHANGED_PROPERTY, oldValue, newValue); 430 } 431 432 437 public void setDisabledBackground(Color newValue) { 438 setProperty(PROPERTY_DISABLED_BACKGROUND, newValue); 439 } 440 441 446 public void setDisabledBorder(Border newValue) { 447 setProperty(PROPERTY_DISABLED_BORDER, newValue); 448 } 449 450 455 public void setDisabledFont(Font newValue) { 456 setProperty(PROPERTY_DISABLED_FONT, newValue); 457 } 458 459 464 public void setDisabledForeground(Color newValue) { 465 setProperty(PROPERTY_DISABLED_FOREGROUND, newValue); 466 } 467 468 475 public void setHeight(Extent newValue) { 476 setProperty(PROPERTY_HEIGHT, newValue); 477 } 478 479 484 public void setInsets(Insets newValue) { 485 setProperty(PROPERTY_INSETS, newValue); 486 } 487 488 494 public void setModel(ListModel newValue) { 495 if (newValue == null) { 496 throw new IllegalArgumentException ("Model may not be null."); 497 } 498 ListModel oldValue = model; 499 if (oldValue != null) { 500 oldValue.removeListDataListener(listDataHandler); 501 } 502 newValue.addListDataListener(listDataHandler); 503 model = newValue; 504 firePropertyChange(LIST_MODEL_CHANGED_PROPERTY, oldValue, newValue); 505 } 506 507 512 public void setRolloverBackground(Color newValue) { 513 setProperty(PROPERTY_ROLLOVER_BACKGROUND, newValue); 514 } 515 516 521 public void setRolloverEnabled(boolean newValue) { 522 setProperty(PROPERTY_ROLLOVER_ENABLED, new Boolean (newValue)); 523 } 524 525 530 public void setRolloverFont(Font newValue) { 531 setProperty(PROPERTY_ROLLOVER_FONT, newValue); 532 } 533 534 539 public void setRolloverForeground(Color newValue) { 540 setProperty(PROPERTY_ROLLOVER_FOREGROUND, newValue); 541 } 542 543 549 public void setSelectionModel(ListSelectionModel newValue) { 550 if (newValue == null) { 551 throw new IllegalArgumentException ("Selection model may not be null."); 552 } 553 ListSelectionModel oldValue = selectionModel; 554 if (oldValue != null) { 555 oldValue.removeChangeListener(changeHandler); 556 } 557 newValue.addChangeListener(changeHandler); 558 selectionModel = newValue; 559 firePropertyChange(SELECTION_MODEL_CHANGED_PROPERTY, oldValue, newValue); 560 } 561 562 568 public void setToolTipText(String newValue) { 569 setProperty(PROPERTY_TOOL_TIP_TEXT, newValue); 570 } 571 572 579 public void setWidth(Extent newValue) { 580 setProperty(PROPERTY_WIDTH, newValue); 581 } 582 } 583 | Popular Tags |