KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SAbstractIconTextCompound


1 /*
2  * $Id: SAbstractIconTextCompound.java,v 1.11 2005/05/27 09:17:36 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16
17 import org.wings.style.CSSSelector;
18 import org.wings.style.CSSStyleSheet;
19 import org.wings.style.CSSAttributeSet;
20 import org.wings.style.CSSProperty;
21
22 import javax.swing.event.ChangeEvent JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import javax.swing.event.EventListenerList JavaDoc;
25 import java.awt.*;
26 import java.awt.event.ItemEvent JavaDoc;
27 import java.awt.event.ItemListener JavaDoc;
28
29 /**
30  * An abstract class, which compounds icon and text. It is the base class for
31  * {@link SAbstractButton} and {@link SClickable}. It supports 7 different icon
32  * states.
33  *
34  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
35  * @version $Revision: 1.11 $
36  */

37 public abstract class SAbstractIconTextCompound
38         extends SComponent
39         implements ItemSelectable {
40     public static final int ICON_COUNT = 7;
41     public static final int DISABLED_ICON = 0;
42     public static final int DISABLED_SELECTED_ICON = 1;
43     public static final int ENABLED_ICON = 2;
44     public static final int SELECTED_ICON = 3;
45     public static final int ROLLOVER_ICON = 4;
46     public static final int ROLLOVER_SELECTED_ICON = 5;
47     public static final int PRESSED_ICON = 6;
48
49     public static final CSSSelector SELECTOR_SELECTION = new CSSSelector.Pseudo("SELECTION");
50
51     private SButtonModel model;
52
53     /**
54      * The button model's <code>changeListener</code>.
55      */

56     protected ChangeListener JavaDoc changeListener = null;
57
58     protected transient ChangeEvent JavaDoc changeEvent = null;
59
60     /**
61      * the text the button is showing
62      */

63     private String JavaDoc text;
64
65     /**
66      * The icon to be displayed
67      */

68     private SIcon icon;
69
70     private SIcon disabledIcon;
71
72     private SIcon selectedIcon;
73
74     private SIcon pressedIcon;
75
76     private SIcon disabledSelectedIcon;
77
78     private SIcon rolloverIcon;
79
80     private SIcon rolloverSelectedIcon;
81
82     private int verticalTextPosition = SConstants.CENTER;
83
84     private int horizontalTextPosition = SConstants.RIGHT;
85
86     private int iconTextGap = 0;
87
88     private boolean delayEvents = false;
89
90     /**
91      * Create a button with given text.
92      *
93      * @param text the button text
94      */

95     public SAbstractIconTextCompound(String JavaDoc text) {
96         setText(text);
97         model = new SDefaultButtonModel();
98     }
99
100     /**
101      * Creates a new submit button
102      */

103     public SAbstractIconTextCompound() {
104         this(null);
105     }
106
107     public SButtonModel getModel() {
108         return model;
109     }
110
111     public void setModel(SButtonModel model) {
112         if (model == null)
113             throw new IllegalArgumentException JavaDoc("null not allowed");
114         this.model = model;
115         reloadIfChange(this.model, model);
116     }
117
118     /**
119      * Returns the selected items or null if no items are selected.
120      */

121     public Object JavaDoc[] getSelectedObjects() {
122         return model.isSelected() ? new Object JavaDoc[]{this} : null;
123     }
124
125     /**
126      * Adds a ItemListener to the button.
127      *
128      * @see #removeItemListener(ItemListener)
129      */

130     public void addItemListener(ItemListener JavaDoc il) {
131         addEventListener(ItemListener JavaDoc.class, il);
132     }
133
134     /**
135      * Remove the given itemListener from list of
136      * item listeners.
137      *
138      * @see #addItemListener(ItemListener)
139      */

140     public void removeItemListener(ItemListener JavaDoc il) {
141         removeEventListener(ItemListener JavaDoc.class, il);
142     }
143
144     /**
145      * Adds a <code>ChangeListener</code> to the button.
146      *
147      * @param l the listener to be added
148      */

149     public void addChangeListener(ChangeListener JavaDoc l) {
150         addEventListener(ChangeListener JavaDoc.class, l);
151     }
152
153     /**
154      * Removes a ChangeListener from the button.
155      *
156      * @param l the listener to be removed
157      */

158     public void removeChangeListener(ChangeListener JavaDoc l) {
159         removeEventListener(ChangeListener JavaDoc.class, l);
160     }
161
162     /**
163      * Notifies all listeners that have registered interest for
164      * notification on this event type. The event instance
165      * is lazily created using the parameters passed into
166      * the fire method.
167      *
168      * @see EventListenerList
169      */

170     protected void fireStateChanged() {
171         // Guaranteed to return a non-null array
172
Object JavaDoc[] listeners = getListenerList();
173         // Process the listeners last to first, notifying
174
// those that are interested in this event
175
for (int i = listeners.length - 2; i >= 0; i -= 2) {
176             if (listeners[i] == ChangeListener JavaDoc.class) {
177                 // Lazily create the event:
178
if (changeEvent == null)
179                     changeEvent = new ChangeEvent JavaDoc(this);
180                 ((ChangeListener JavaDoc) listeners[i + 1]).stateChanged(changeEvent);
181             }
182         }
183     }
184
185     public void setHorizontalTextPosition(int textPosition) {
186         horizontalTextPosition = textPosition;
187         reloadIfChange(this.horizontalTextPosition, textPosition);
188
189     }
190
191     public int getHorizontalTextPosition() {
192         return horizontalTextPosition;
193     }
194
195     public void setVerticalTextPosition(int textPosition) {
196         verticalTextPosition = textPosition;
197         reloadIfChange(this.verticalTextPosition, textPosition);
198     }
199
200     public int getVerticalTextPosition() {
201         return verticalTextPosition;
202     }
203
204     public void setIconTextGap(int gap) {
205         iconTextGap = gap;
206         reloadIfChange(this.iconTextGap, gap);
207     }
208
209     public int getIconTextGap() {
210         return iconTextGap;
211     }
212
213     public void setIcon(SIcon i) {
214         reloadIfChange(icon, i);
215         icon = i;
216     }
217
218     public SIcon getIcon() {
219         return icon;
220     }
221
222     public void setPressedIcon(SIcon i) {
223         reloadIfChange(pressedIcon, i);
224         pressedIcon = i;
225     }
226
227     public SIcon getPressedIcon() {
228         return pressedIcon;
229     }
230
231     public void setRolloverIcon(SIcon i) {
232         reloadIfChange(rolloverIcon, i);
233         rolloverIcon = i;
234     }
235
236     public SIcon getRolloverIcon() {
237         return rolloverIcon;
238     }
239
240     public void setRolloverSelectedIcon(SIcon i) {
241         reloadIfChange(rolloverSelectedIcon, i);
242         rolloverSelectedIcon = i;
243     }
244
245     public SIcon getRolloverSelectedIcon() {
246         return rolloverSelectedIcon;
247     }
248
249     public void setSelectedIcon(SIcon i) {
250         reloadIfChange(selectedIcon, i);
251         selectedIcon = i;
252     }
253
254     public SIcon getSelectedIcon() {
255         return selectedIcon;
256     }
257
258     public void setDisabledSelectedIcon(SIcon i) {
259         reloadIfChange(disabledSelectedIcon, i);
260         disabledSelectedIcon = i;
261     }
262
263     public SIcon getDisabledSelectedIcon() {
264         return disabledSelectedIcon;
265     }
266
267     public void setDisabledIcon(SIcon i) {
268         reloadIfChange(disabledIcon, i);
269         disabledIcon = i;
270     }
271
272     public SIcon getDisabledIcon() {
273         /**** TODO
274          if(disabledIcon == null) {
275          if(icon != null && icon instanceof ImageIcon)
276          disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon)icon).getImage()));
277          }
278          ***/

279         return disabledIcon;
280     }
281
282     /**
283      * Return the background color.
284      *
285      * @return the background color
286      */

287     public Color getSelectionBackground() {
288         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
289     }
290
291     /**
292      * Set the foreground color.
293      *
294      * @param color the new foreground color
295      */

296     public void setSelectionBackground(Color color) {
297         setAttribute(SELECTOR_SELECTION, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color));
298     }
299
300     /**
301      * Return the foreground color.
302      *
303      * @return the foreground color
304      */

305     public Color getSelectionForeground() {
306         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
307     }
308
309     /**
310      * Set the foreground color.
311      *
312      * @param color the new foreground color
313      */

314     public void setSelectionForeground(Color color) {
315         setAttribute(SELECTOR_SELECTION, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color));
316     }
317
318     /**
319      * Set the font.
320      *
321      * @param font the new font
322      */

323     public void setSelectionFont(SFont font) {
324         setAttributes(SELECTOR_SELECTION, CSSStyleSheet.getAttributes(font));
325     }
326
327     /**
328      * Return the font.
329      *
330      * @return the font
331      */

332     public SFont getSelectionFont() {
333         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
334     }
335
336     /**
337      * Sets the label of the button.
338      */

339     public void setText(String JavaDoc t) {
340         reloadIfChange(text, t);
341         text = t;
342     }
343
344     public String JavaDoc getText() {
345         return text;
346     }
347
348     public boolean isSelected() {
349         return model.isSelected();
350     }
351
352     /**
353      * Toggle the selection. If the new selection
354      * is different to the old selection
355      * an {@link ItemEvent} is raised.
356      */

357     public void setSelected(boolean selected) {
358         if (model.isSelected() != selected) {
359             model.setSelected(selected);
360             fireStateChanged();
361             reload();
362         }
363     }
364
365     /**
366      * Sets the proper icons for buttonstatus enabled resp. disabled.
367      */

368     public void setIcons(SIcon[] icons) {
369         setIcon(icons[ENABLED_ICON]);
370         setDisabledIcon(icons[DISABLED_ICON]);
371         setDisabledSelectedIcon(icons[DISABLED_SELECTED_ICON]);
372         setRolloverIcon(icons[ROLLOVER_ICON]);
373         setRolloverSelectedIcon(icons[ROLLOVER_SELECTED_ICON]);
374         setPressedIcon(icons[PRESSED_ICON]);
375         setSelectedIcon(icons[SELECTED_ICON]);
376     }
377
378
379     private ItemEvent JavaDoc delayedItemEvent;
380
381     protected final void delayEvents(boolean b) {
382         delayEvents = b;
383     }
384
385     protected final boolean shouldDelayEvents() {
386         return delayEvents;
387     }
388
389     /**
390      * Reports a selection change.
391      *
392      * @param ie report this event to all listeners
393      * @see java.awt.event.ItemListener
394      * @see java.awt.ItemSelectable
395      */

396     protected void fireItemStateChanged(ItemEvent JavaDoc ie) {
397         if (ie == null)
398             return;
399
400         if (delayEvents) {
401             delayedItemEvent = ie;
402             return;
403         } // end of if ()
404

405         // Guaranteed to return a non-null array
406
Object JavaDoc[] listeners = getListenerList();
407         // Process the listeners last to first, notifying
408
// those that are interested in this event
409
for (int i = listeners.length - 2; i >= 0; i -= 2) {
410             if (listeners[i] == ItemListener JavaDoc.class) {
411                 ((ItemListener JavaDoc) listeners[i + 1]).itemStateChanged(ie);
412             }
413         }
414     }
415
416     public void fireIntermediateEvents() {
417         if (delayEvents && delayedItemEvent != null) {
418             delayEvents = false;
419             fireItemStateChanged(delayedItemEvent);
420             delayEvents = true;
421         } // end of if ()
422

423     }
424
425     public void fireFinalEvents() {
426         super.fireFinalEvents();
427         delayEvents = false;
428     }
429 }
430
Popular Tags