KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > list > AbstractListComponent


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.list;
31
32 import java.util.EventListener JavaDoc;
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 /**
48  * An abstract base class for list components.
49  */

50 public abstract class AbstractListComponent extends Component {
51
52     public static final String JavaDoc INPUT_ACTION = "action";
53
54     public static final String JavaDoc PROPERTY_ACTION_COMMAND = "actionCommand";
55     
56     public static final String JavaDoc ACTION_LISTENERS_CHANGED_PROPERTY = "actionListeners";
57     public static final String JavaDoc LIST_DATA_CHANGED_PROPERTY = "listData";
58     public static final String JavaDoc LIST_MODEL_CHANGED_PROPERTY = "listModel";
59     public static final String JavaDoc LIST_CELL_RENDERER_CHANGED_PROPERTY = "listCellRenderer";
60     public static final String JavaDoc SELECTION_MODEL_CHANGED_PROPERTY = "listSelectionModel";
61     public static final String JavaDoc SELECTION_CHANGED_PROPERTY = "listSelectionChanged";
62
63     public static final String JavaDoc PROPERTY_BORDER = "border";
64     public static final String JavaDoc PROPERTY_DISABLED_BACKGROUND = "disabledBackground";
65     public static final String JavaDoc PROPERTY_DISABLED_BORDER = "disabledBorder";
66     public static final String JavaDoc PROPERTY_DISABLED_FONT = "disabledFont";
67     public static final String JavaDoc PROPERTY_DISABLED_FOREGROUND = "disabledForeground";
68     public static final String JavaDoc PROPERTY_HEIGHT = "height";
69     public static final String JavaDoc PROPERTY_INSETS = "insets";
70     public static final String JavaDoc PROPERTY_ROLLOVER_BACKGROUND = "rolloverBackground";
71     public static final String JavaDoc PROPERTY_ROLLOVER_ENABLED = "rolloverEnabled";
72     public static final String JavaDoc PROPERTY_ROLLOVER_FONT = "rolloverFont";
73     public static final String JavaDoc PROPERTY_ROLLOVER_FOREGROUND = "rolloverForeground";
74     public static final String JavaDoc PROPERTY_TOOL_TIP_TEXT = "toolTipText";
75     public static final String JavaDoc PROPERTY_WIDTH = "width";
76
77     public static final DefaultListCellRenderer DEFAULT_LIST_CELL_RENDERER = new DefaultListCellRenderer();
78
79     /**
80      * Local handler for list selection events.
81      */

82     private ChangeListener changeHandler = new ChangeListener() {
83
84         /**
85          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
86          */

87         public void stateChanged(ChangeEvent e) {
88             firePropertyChange(SELECTION_CHANGED_PROPERTY, null, null);
89         }
90     };
91
92     /**
93      * Local handler of <code>ListDataEvent</code>s.
94      */

95     private ListDataListener listDataHandler = new ListDataListener() {
96     
97         /**
98          * @see nextapp.echo2.app.event.ListDataListener#contentsChanged(nextapp.echo2.app.event.ListDataEvent)
99          */

100         public void contentsChanged(ListDataEvent e) {
101             firePropertyChange(LIST_DATA_CHANGED_PROPERTY, null, null);
102         }
103
104         /**
105          * @see nextapp.echo2.app.event.ListDataListener#intervalAdded(nextapp.echo2.app.event.ListDataEvent)
106          */

107         public void intervalAdded(ListDataEvent e) {
108             firePropertyChange(LIST_DATA_CHANGED_PROPERTY, null, null);
109         }
110
111         /**
112          * @see nextapp.echo2.app.event.ListDataListener#intervalRemoved(nextapp.echo2.app.event.ListDataEvent)
113          */

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     /**
124      * Creates a new <code>AbstractListComponent</code> with default models.
125      */

126     public AbstractListComponent() {
127         this(null, null);
128     }
129     
130     /**
131      * Creates a new <code>AbstractListComponent</code> with the specified
132      * models.
133      *
134      * @param model the list data model
135      * @param selectionModel the selection model
136      */

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     /**
150      * Adds an <code>ActionListener</code> to the list component.
151      * The <code>ActionListener</code> will be invoked when the user
152      * selects an item.
153      *
154      * @param l the <code>ActionListener</code> to add
155      */

156     public void addActionListener(ActionListener l) {
157         getEventListenerList().addListener(ActionListener.class, l);
158         // Notification of action listener changes is provided due to
159
// existence of hasActionListeners() method.
160
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, null, l);
161     }
162
163     /**
164      * Fires an action event to all listeners.
165      */

166     private void fireActionEvent() {
167         if (!hasEventListenerList()) {
168             return;
169         }
170         EventListener JavaDoc[] 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 JavaDoc) getRenderProperty(PROPERTY_ACTION_COMMAND));
175             }
176             ((ActionListener) listeners[i]).actionPerformed(e);
177         }
178     }
179     
180     /**
181      * Returns the action command which will be provided in
182      * <code>ActionEvent</code>s fired by this
183      * <code>AbstractListComponent</code>.
184      *
185      * @return the action command
186      */

187     public String JavaDoc getActionCommand() {
188         return (String JavaDoc) getProperty(PROPERTY_ACTION_COMMAND);
189     }
190     
191     /**
192      * Returns the <code>Border</code> surrounding the list component.
193      *
194      * @return the border
195      */

196     public Border getBorder() {
197         return (Border) getProperty(PROPERTY_BORDER);
198     }
199     
200     /**
201      * Returns the <code>ListCellRenderer</code> used to render items.
202      *
203      * @return the renderer
204      */

205     public ListCellRenderer getCellRenderer() {
206         return listCellRenderer;
207     }
208     
209     /**
210      * Returns the background color displayed when the component is
211      * disabled.
212      *
213      * @return the color
214      */

215     public Color getDisabledBackground() {
216         return (Color) getProperty(PROPERTY_DISABLED_BACKGROUND);
217     }
218
219     /**
220      * Returns the border displayed when the component is
221      * disabled.
222      *
223      * @return the border
224      */

225     public Border getDisabledBorder() {
226         return (Border) getProperty(PROPERTY_DISABLED_BORDER);
227     }
228
229     /**
230      * Returns the font displayed when the component is
231      * disabled.
232      *
233      * @return the font
234      */

235     public Font getDisabledFont() {
236         return (Font) getProperty(PROPERTY_DISABLED_FONT);
237     }
238
239     /**
240      * Returns the foreground color displayed when the component is
241      * disabled.
242      *
243      * @return the color
244      */

245     public Color getDisabledForeground() {
246         return (Color) getProperty(PROPERTY_DISABLED_FOREGROUND);
247     }
248
249     /**
250      * Returns the height.
251      * This property only supports <code>Extent</code>s with
252      * fixed (i.e., not percent) units.
253      *
254      * @return the height
255      */

256     public Extent getHeight() {
257         return (Extent) getProperty(PROPERTY_HEIGHT);
258     }
259     
260     /**
261      * Returns the inset margin around between the list components border and content.
262      *
263      * @return the inset margin
264      */

265     public Insets getInsets() {
266         return (Insets) getProperty(PROPERTY_INSETS);
267     }
268     
269     /**
270      * Returns the model.
271      *
272      * @return the model
273      */

274     public ListModel getModel() {
275         return model;
276     }
277     
278     /**
279      * Returns the rollover background.
280      *
281      * @return the rollover background
282      */

283     public Color getRolloverBackground() {
284         return (Color) getProperty(PROPERTY_ROLLOVER_BACKGROUND);
285     }
286     
287     /**
288      * Returns the rollover font.
289      *
290      * @return the rollover font
291      */

292     public Font getRolloverFont() {
293         return (Font) getProperty(PROPERTY_ROLLOVER_FONT);
294     }
295     
296     /**
297      * Returns the rollover foreground.
298      *
299      * @return the rollover foreground
300      */

301     public Color getRolloverForeground() {
302         return (Color) getProperty(PROPERTY_ROLLOVER_FOREGROUND);
303     }
304     
305     /**
306      * Returns the selection model.
307      *
308      * @return the selection model
309      */

310     public ListSelectionModel getSelectionModel() {
311         return selectionModel;
312     }
313     
314     /**
315      * Returns the tool tip text (displayed when the mouse cursor is hovered
316      * over the component).
317      *
318      * @return the tool tip text
319      */

320     public String JavaDoc getToolTipText() {
321         return (String JavaDoc) getProperty(PROPERTY_TOOL_TIP_TEXT);
322     }
323     
324     /**
325      * Returns the width.
326      * This property supports <code>Extent</code>s with
327      * fixed or percentile units.
328      *
329      * @return the width
330      */

331     public Extent getWidth() {
332         return (Extent) getProperty(PROPERTY_WIDTH);
333     }
334     
335     /**
336      * Determines the any <code>ActionListener</code>s are registered.
337      *
338      * @return true if any action listeners are registered
339      */

340     public boolean hasActionListeners() {
341         return hasEventListenerList() && getEventListenerList().getListenerCount(ActionListener.class) != 0;
342     }
343     
344     /**
345      * Determines if rollover effects are enabled.
346      *
347      * @return true if rollover effects are enabled
348      */

349     public boolean isRolloverEnabled() {
350         Boolean JavaDoc value = (Boolean JavaDoc) getProperty(PROPERTY_ROLLOVER_ENABLED);
351         return value == null ? false : value.booleanValue();
352     }
353
354     /**
355      * This component does not support children.
356      *
357      * @see nextapp.echo2.app.Component#isValidChild(nextapp.echo2.app.Component)
358      */

359     public boolean isValidChild(Component child) {
360         return false;
361     }
362
363     /**
364      * @see nextapp.echo2.app.Component#processInput(java.lang.String, java.lang.Object)
365      */

366     public void processInput(String JavaDoc inputName, Object JavaDoc 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     /**
382      * Removes an <code>ActionListener</code> from the list component.
383      *
384      * @param l the <code>ActionListener</code> to remove
385      */

386     public void removeActionListener(ActionListener l) {
387         if (!hasEventListenerList()) {
388             return;
389         }
390         getEventListenerList().removeListener(ActionListener.class, l);
391         // Notification of action listener changes is provided due to
392
// existence of hasActionListeners() method.
393
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, l, null);
394     }
395     
396     /**
397      * Sets the action command which will be provided in
398      * <code>ActionEvent</code>s fired by this
399      * <code>AbstractListComponent</code>.
400      *
401      * @param newValue the new action command
402      */

403     public void setActionCommand(String JavaDoc newValue) {
404         setProperty(PROPERTY_ACTION_COMMAND, newValue);
405     }
406     
407     /**
408      * Sets the <code>Border</code> surrounding the list component.
409      *
410      * @param newValue the new <code>Border</code>
411      */

412     public void setBorder(Border newValue) {
413         setProperty(PROPERTY_BORDER, newValue);
414     }
415     
416     /**
417      * Sets the renderer for items.
418      * The renderer may not be null (use <code>DEFAULT_LIST_CELL_RENDERER</code>
419      * for default behavior).
420      *
421      * @param newValue the new renderer
422      */

423     public void setCellRenderer(ListCellRenderer newValue) {
424         if (newValue == null) {
425             throw new IllegalArgumentException JavaDoc("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     /**
433      * Sets the background color displayed when the component is disabled.
434      *
435      * @param newValue the new <code>Color</code>
436      */

437     public void setDisabledBackground(Color newValue) {
438         setProperty(PROPERTY_DISABLED_BACKGROUND, newValue);
439     }
440
441     /**
442      * Sets the border displayed when the component is disabled.
443      *
444      * @param newValue the new border
445      */

446     public void setDisabledBorder(Border newValue) {
447         setProperty(PROPERTY_DISABLED_BORDER, newValue);
448     }
449
450     /**
451      * Sets the font displayed when the component is disabled.
452      *
453      * @param newValue the new <code>Font</code>
454      */

455     public void setDisabledFont(Font newValue) {
456         setProperty(PROPERTY_DISABLED_FONT, newValue);
457     }
458
459     /**
460      * Sets the foreground color displayed when the component is disabled.
461      *
462      * @param newValue the new <code>Color</code>
463      */

464     public void setDisabledForeground(Color newValue) {
465         setProperty(PROPERTY_DISABLED_FOREGROUND, newValue);
466     }
467
468     /**
469      * Sets the height.
470      * This property only supports <code>Extent</code>s with
471      * fixed (i.e., not percent) units.
472      *
473      * @param newValue the new height
474      */

475     public void setHeight(Extent newValue) {
476         setProperty(PROPERTY_HEIGHT, newValue);
477     }
478     
479     /**
480      * Sets the inset margin around between the list components border and content.
481      *
482      * @param newValue the new inset margin
483      */

484     public void setInsets(Insets newValue) {
485         setProperty(PROPERTY_INSETS, newValue);
486     }
487     
488     /**
489      * Sets the model.
490      * The model may not be null.
491      *
492      * @param newValue the new model
493      */

494     public void setModel(ListModel newValue) {
495         if (newValue == null) {
496             throw new IllegalArgumentException JavaDoc("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     /**
508      * Sets the rollover background.
509      *
510      * @param newValue the new rollover background
511      */

512     public void setRolloverBackground(Color newValue) {
513         setProperty(PROPERTY_ROLLOVER_BACKGROUND, newValue);
514     }
515     
516     /**
517      * Sets whether rollover effects are enabled.
518      *
519      * @param newValue the new rollover enabled state
520      */

521     public void setRolloverEnabled(boolean newValue) {
522         setProperty(PROPERTY_ROLLOVER_ENABLED, new Boolean JavaDoc(newValue));
523     }
524     
525     /**
526      * Sets the rollover font.
527      *
528      * @param newValue the new rollover font
529      */

530     public void setRolloverFont(Font newValue) {
531         setProperty(PROPERTY_ROLLOVER_FONT, newValue);
532     }
533     
534     /**
535      * Sets the rollover foreground.
536      *
537      * @param newValue the new rollover foreground
538      */

539     public void setRolloverForeground(Color newValue) {
540         setProperty(PROPERTY_ROLLOVER_FOREGROUND, newValue);
541     }
542     
543     /**
544      * Sets the selection model.
545      * The selection model may not be null.
546      *
547      * @param newValue the new selection model
548      */

549     public void setSelectionModel(ListSelectionModel newValue) {
550         if (newValue == null) {
551             throw new IllegalArgumentException JavaDoc("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     /**
563      * Sets the tool tip text (displayed when the mouse cursor is hovered
564      * over the component).
565      *
566      * @param newValue the new tool tip text
567      */

568     public void setToolTipText(String JavaDoc newValue) {
569         setProperty(PROPERTY_TOOL_TIP_TEXT, newValue);
570     }
571
572     /**
573      * Sets the width.
574      * This property supports <code>Extent</code>s with
575      * fixed or percentile units.
576      *
577      * @param newValue the new width
578      */

579     public void setWidth(Extent newValue) {
580         setProperty(PROPERTY_WIDTH, newValue);
581     }
582 }
583
Popular Tags