KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > button > ToggleButton


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.button;
31
32 import java.util.EventListener JavaDoc;
33
34 import nextapp.echo2.app.Alignment;
35 import nextapp.echo2.app.Extent;
36 import nextapp.echo2.app.ImageReference;
37 import nextapp.echo2.app.event.ChangeEvent;
38 import nextapp.echo2.app.event.ChangeListener;
39
40 /**
41  * An abstract base class for 'two state' buttons.
42  */

43 public abstract class ToggleButton extends AbstractButton {
44     
45     public static final String JavaDoc PROPERTY_PRESSED_STATE_ICON = "pressedStateIcon";
46     public static final String JavaDoc PROPERTY_PRESSED_SELECTED_STATE_ICON = "pressedSelectedStateIcon";
47     public static final String JavaDoc PROPERTY_ROLLOVER_STATE_ICON = "rolloverStateIcon";
48     public static final String JavaDoc PROPERTY_ROLLOVER_SELECTED_STATE_ICON = "rolloverSelectedStateIcon";
49     public static final String JavaDoc PROPERTY_STATE_ALIGNMENT = "stateAlignment";
50     public static final String JavaDoc PROPERTY_STATE_ICON = "stateIcon";
51     public static final String JavaDoc PROPERTY_STATE_MARGIN = "stateMargin";
52     public static final String JavaDoc PROPERTY_STATE_POSITION = "statePosition";
53     public static final String JavaDoc PROPERTY_SELECTED_STATE_ICON = "selectedStateIcon";
54     
55     public static final String JavaDoc SELECTED_CHANGED_PROPERTY = "selected";
56     
57     /**
58      * Forwards events generated by the model to listeners registered with the
59      * component instance.
60      */

61     private ChangeListener changeForwarder = new ChangeListener() {
62
63         /**
64          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
65          */

66         public void stateChanged(ChangeEvent e) {
67             fireStateChanged();
68             if (isSelected()) {
69                 firePropertyChange(SELECTED_CHANGED_PROPERTY, Boolean.FALSE, Boolean.TRUE);
70             } else {
71                 firePropertyChange(SELECTED_CHANGED_PROPERTY, Boolean.TRUE, Boolean.FALSE);
72             }
73         }
74     };
75
76     /**
77      * Adds a <code>ChangeListener</code> to receive notification of state
78      * changes, i.e., to the selected state of a <code>ToggleButton</code>.
79      *
80      * @param l the listener to add
81      */

82     public void addChangeListener(ChangeListener l) {
83         getEventListenerList().addListener(ChangeListener.class, l);
84     }
85     
86     /**
87      * Notifies all listeners that have registered for this event type.
88      */

89     public void fireStateChanged() {
90         EventListener JavaDoc[] listeners = getEventListenerList().getListeners(ChangeListener.class);
91         ChangeEvent e = new ChangeEvent(this);
92         for (int index = 0; index < listeners.length; ++index) {
93             ((ChangeListener) listeners[index]).stateChanged(e);
94         }
95     }
96     
97     /**
98      * Returns the selected state icon displayed when the
99      * button is being pressed.
100      *
101      * @return the icon
102      */

103     public ImageReference getPressedSelectedStateIcon() {
104         return (ImageReference) getProperty(PROPERTY_PRESSED_SELECTED_STATE_ICON);
105     }
106     
107     /**
108      * Returns the default (non-selected) state icon displayed when the
109      * button is being pressed.
110      *
111      * @return the icon
112      */

113     public ImageReference getPressedStateIcon() {
114         return (ImageReference) getProperty(PROPERTY_PRESSED_STATE_ICON);
115     }
116     
117     /**
118      * Returns the selected state icon displayed when the mouse
119      * cursor is inside the bounds of the button.
120      *
121      * @return the icon
122      */

123     public ImageReference getRolloverSelectedStateIcon() {
124         return (ImageReference) getProperty(PROPERTY_ROLLOVER_SELECTED_STATE_ICON);
125     }
126     
127     /**
128      * Returns the default (non-selected) state icon displayed when the mouse
129      * cursor is inside the bounds of the button.
130      *
131      * @return the icon
132      */

133     public ImageReference getRolloverStateIcon() {
134         return (ImageReference) getProperty(PROPERTY_ROLLOVER_STATE_ICON);
135     }
136     
137     /**
138      * Returns the selected state icon.
139      *
140      * @return the icon
141      */

142     public ImageReference getSelectedStateIcon() {
143         return (ImageReference) getProperty(PROPERTY_SELECTED_STATE_ICON);
144     }
145     
146     /**
147      * Returns the alignment of the state icon relative to the button's
148      * text/icon.
149      *
150      * @return the state alignment
151      */

152     public Alignment getStateAlignment() {
153         return (Alignment) getProperty(PROPERTY_STATE_ALIGNMENT);
154     }
155
156     /**
157      * Returns the default (non-selected) state icon.
158      *
159      * @return the icon
160      */

161     public ImageReference getStateIcon() {
162         return (ImageReference) getProperty(PROPERTY_STATE_ICON);
163     }
164     
165     /**
166      * Returns the margin size between the state icon and the
167      * toggle button's icon and/or text. The margin will only
168      * be displayed in the case where icon and/or text are
169      * present.
170      * This property only supports <code>Extent</code>s with
171      * fixed (i.e., not percent) units.
172      *
173      * @return the margin size
174      */

175     public Extent getStateMargin() {
176         return (Extent) getProperty(PROPERTY_STATE_MARGIN);
177     }
178
179     /**
180      * Returns the position of the state icon relative to the button's
181      * text/icon.
182      *
183      * @return the state position
184      */

185     public Alignment getStatePosition() {
186         return (Alignment) getProperty(PROPERTY_STATE_POSITION);
187     }
188     
189     /**
190      * Determines the selection state.
191      *
192      * @return the selection state
193      */

194     public boolean isSelected() {
195         return ((ToggleButtonModel) getModel()).isSelected();
196     }
197
198     /**
199      * @see nextapp.echo2.app.Component#processInput(java.lang.String, java.lang.Object)
200      */

201     public void processInput(String JavaDoc name, Object JavaDoc value) {
202         super.processInput(name, value);
203         if (SELECTED_CHANGED_PROPERTY.equals(name)) {
204             setSelected(((Boolean JavaDoc) value).booleanValue());
205         }
206     }
207
208     /**
209      * Removes a <code>ChangeListener</code> from being notified of state
210      * changes, i.e., to the selected state of a <code>ToggleButton</code>.
211      *
212      * @param l the listener to remove
213      */

214     public void removeChangeListener(ChangeListener l) {
215         getEventListenerList().removeListener(ChangeListener.class, l);
216     }
217     
218     /**
219      * @see nextapp.echo2.app.button.AbstractButton#setModel(nextapp.echo2.app.button.ButtonModel)
220      */

221     public void setModel(ButtonModel newValue) {
222         if (!(newValue instanceof ToggleButtonModel)) {
223             throw new IllegalArgumentException JavaDoc("Model is not a ToggleButtonModel.");
224         }
225
226         ToggleButtonModel oldValue = (ToggleButtonModel) getModel();
227
228         if (oldValue != null) {
229             oldValue.removeChangeListener(changeForwarder);
230         }
231         
232         super.setModel(newValue);
233
234         ((ToggleButtonModel) newValue).addChangeListener(changeForwarder);
235     }
236
237     /**
238      * Sets the selected state icon displayed when the
239      * button is being pressed.
240      *
241      * @param newValue the new icon
242      */

243     public void setPressedSelectedStateIcon(ImageReference newValue) {
244         setProperty(PROPERTY_PRESSED_SELECTED_STATE_ICON, newValue);
245     }
246     
247     /**
248      * Sets the default (non-selected) state icon displayed when the
249      * button is being pressed.
250      *
251      * @param newValue the new icon
252      */

253     public void setPressedStateIcon(ImageReference newValue) {
254         setProperty(PROPERTY_PRESSED_STATE_ICON, newValue);
255     }
256     
257     /**
258      * Sets the selected state icon displayed when the mouse
259      * cursor is inside the bounds of the button.
260      *
261      * @param newValue the new icon
262      */

263     public void setRolloverSelectedStateIcon(ImageReference newValue) {
264         setProperty(PROPERTY_ROLLOVER_SELECTED_STATE_ICON, newValue);
265     }
266     
267     /**
268      * Sets the default (non-selected) state icon displayed when the mouse
269      * cursor is inside the bounds of the button.
270      *
271      * @param newValue the new icon
272      */

273     public void setRolloverStateIcon(ImageReference newValue) {
274         setProperty(PROPERTY_ROLLOVER_STATE_ICON, newValue);
275     }
276     
277     /**
278      * Sets the selected state icon.
279      *
280      * @param newValue the new icon
281      */

282     public void setSelectedStateIcon(ImageReference newValue) {
283         setProperty(PROPERTY_SELECTED_STATE_ICON, newValue);
284     }
285     
286     /**
287      * Sets the selection state.
288      *
289      * @param newValue the new selection state
290      */

291     public void setSelected(boolean newValue) {
292         ((ToggleButtonModel) getModel()).setSelected(newValue);
293     }
294     
295     /**
296      * Sets the alignment of the state icon relative to the text/icon
297      * of the button.
298      * Note that only one of the provided <code>Alignment</code>'s
299      * settings should be non-default.
300      *
301      * @param newValue the new state alignment
302      */

303     public void setStateAlignment(Alignment newValue) {
304         setProperty(PROPERTY_STATE_ALIGNMENT, newValue);
305     }
306     
307     /**
308      * Sets the default (non-selected) state icon.
309      *
310      * @param newValue the new icon
311      */

312     public void setStateIcon(ImageReference newValue) {
313         setProperty(PROPERTY_STATE_ICON, newValue);
314     }
315     
316     /**
317      * Sets the margin size between the state icon and the
318      * toggle button's icon and/or text. The margin will only
319      * be displayed in the case where icon and/or text are
320      * present.
321      * This property only supports <code>Extent</code>s with
322      * fixed (i.e., not percent) units.
323      *
324      * @param newValue the new margin size
325      */

326     public void setStateMargin(Extent newValue) {
327         setProperty(PROPERTY_STATE_MARGIN, newValue);
328     }
329
330     /**
331      * Sets the position of the state icon relative to the text/icon
332      * of the button.
333      * Note that only one of the provided <code>Alignment</code>'s
334      * settings should be non-default.
335      *
336      * @param newValue the new state position
337      */

338     public void setStatePosition(Alignment newValue) {
339         setProperty(PROPERTY_STATE_POSITION, newValue);
340     }
341 }
342
Popular Tags