1 /* CheckboxMenuItem.java -- A menu option with a checkbox on it.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package java.awt;
40
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43 import java.awt.peer.CheckboxMenuItemPeer;
44 import java.awt.peer.MenuItemPeer;
45 import java.util.EventListener;
46
47 /**
48 * This class implements a menu item that has a checkbox on it indicating
49 * the selected state of some option.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
53 */
54 public class CheckboxMenuItem extends MenuItem implements ItemSelectable
55 {
56
57 /*
58 * Static Variables
59 */
60
61 // Serialization constant
62 private static final long serialVersionUID = 6190621106981774043L;
63
64 /*
65 * Instance Variables
66 */
67
68 /**
69 * @serial The state of the checkbox, with <code>true</code> being on and
70 * <code>false</code> being off.
71 */
72 private boolean state;
73
74 // List of registered ItemListeners
75 private transient ItemListener item_listeners;
76
77 /*************************************************************************/
78
79 /*
80 * Constructors
81 */
82
83 /**
84 * Initializes a new instance of <code>CheckboxMenuItem</code> with no
85 * label and an initial state of off.
86 *
87 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
88 * returns true.
89 */
90 public
CheckboxMenuItem()91 CheckboxMenuItem()
92 {
93 this("", false);
94 }
95
96 /*************************************************************************/
97
98 /**
99 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
100 * specified label and an initial state of off.
101 *
102 * @param label The label of the menu item.
103 *
104 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
105 * returns true.
106 */
107 public
CheckboxMenuItem(String label)108 CheckboxMenuItem(String label)
109 {
110 this(label, false);
111 }
112
113 /*************************************************************************/
114
115 /**
116 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
117 * specified label and initial state.
118 *
119 * @param label The label of the menu item.
120 * @param state The initial state of the menu item, where <code>true</code>
121 * is on, and <code>false</code> is off.
122 *
123 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
124 * returns true.
125 */
126 public
CheckboxMenuItem(String label, boolean state)127 CheckboxMenuItem(String label, boolean state)
128 {
129 super(label);
130 this.state = state;
131
132 if (GraphicsEnvironment.isHeadless())
133 throw new HeadlessException ();
134 }
135
136 /*************************************************************************/
137
138 /*
139 * Instance Methods
140 */
141
142 /**
143 * Returns the state of this menu item.
144 *
145 * @return The state of this menu item.
146 */
147 public boolean
getState()148 getState()
149 {
150 return(state);
151 }
152
153 /*************************************************************************/
154
155 /**
156 * Sets the state of this menu item.
157 *
158 * @param state The initial state of the menu item, where <code>true</code>
159 * is on, and <code>false</code> is off.
160 */
161 public synchronized void
setState(boolean state)162 setState(boolean state)
163 {
164 this.state = state;
165 if (peer != null)
166 {
167 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
168 cp.setState (state);
169 }
170 }
171
172 /*************************************************************************/
173
174 /**
175 * Returns an array of length 1 with the menu item label for this object
176 * if the state is on. Otherwise <code>null</code> is returned.
177 *
178 * @param An array with this menu item's label if it has a state of on,
179 * or <code>null</code> otherwise.
180 */
181 public Object[]
getSelectedObjects()182 getSelectedObjects()
183 {
184 if (state == false)
185 return(null);
186
187 Object[] obj = new Object[1];
188 obj[0] = getLabel();
189
190 return(obj);
191 }
192
193 /*************************************************************************/
194
195 /**
196 * Create's this object's native peer
197 */
198 public synchronized void
addNotify()199 addNotify()
200 {
201 if (peer != null)
202 {
203 // This choice of toolkit seems unsatisfying, but I'm not sure
204 // what else to do.
205 peer = getToolkit().createCheckboxMenuItem(this);
206 }
207 super.addNotify ();
208 }
209
210 /*************************************************************************/
211
212 /**
213 * Adds the specified listener to the list of registered item listeners
214 * for this object.
215 *
216 * @param listener The listener to add.
217 */
218 public synchronized void
addItemListener(ItemListener listener)219 addItemListener(ItemListener listener)
220 {
221 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
222
223 enableEvents(AWTEvent.ITEM_EVENT_MASK);
224 }
225
226 /*************************************************************************/
227
228 /**
229 * Removes the specified listener from the list of registered item
230 * listeners for this object.
231 *
232 * @param listener The listener to remove.
233 */
234 public synchronized void
removeItemListener(ItemListener listener)235 removeItemListener(ItemListener listener)
236 {
237 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
238 }
239
240 /*************************************************************************/
241
242 /**
243 * Processes the specified event by calling <code>processItemEvent()</code>
244 * if it is an instance of <code>ItemEvent</code> or calling the superclass
245 * method otherwise.
246 *
247 * @param event The event to process.
248 */
249 protected void
processEvent(AWTEvent event)250 processEvent(AWTEvent event)
251 {
252 if (event instanceof ItemEvent)
253 processItemEvent((ItemEvent)event);
254 else
255 super.processEvent(event);
256 }
257
258 /*************************************************************************/
259
260 /**
261 * Processes the specified event by dispatching it to any registered listeners.
262 *
263 * @param event The event to process.
264 */
265 protected void
processItemEvent(ItemEvent event)266 processItemEvent(ItemEvent event)
267 {
268 if (item_listeners != null)
269 item_listeners.itemStateChanged(event);
270 }
271
272 void
dispatchEventImpl(AWTEvent e)273 dispatchEventImpl(AWTEvent e)
274 {
275 if (e.id <= ItemEvent.ITEM_LAST
276 && e.id >= ItemEvent.ITEM_FIRST
277 && (item_listeners != null
278 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
279 processEvent(e);
280 else
281 super.dispatchEventImpl(e);
282 }
283
284 /*************************************************************************/
285
286 /**
287 * Returns a debugging string for this object.
288 *
289 * @return A debugging string for this object.
290 */
291 public String
paramString()292 paramString()
293 {
294 return ("label=" + getLabel() + ",state=" + state
295 + "," + super.paramString());
296 }
297
298 /**
299 * Returns an array of all the objects currently registered as FooListeners
300 * upon this <code>CheckboxMenuItem</code>. FooListeners are registered using
301 * the addFooListener method.
302 *
303 * @exception ClassCastException If listenerType doesn't specify a class or
304 * interface that implements java.util.EventListener.
305 */
getListeners(Class listenerType)306 public EventListener[] getListeners (Class listenerType)
307 {
308 if (listenerType == ItemListener.class)
309 return AWTEventMulticaster.getListeners (item_listeners, listenerType);
310
311 return super.getListeners (listenerType);
312 }
313
314 /**
315 * Returns an aray of all item listeners currently registered to this
316 * <code>CheckBoxMenuItem</code>.
317 */
getItemListeners()318 public ItemListener[] getItemListeners ()
319 {
320 return (ItemListener[]) getListeners (ItemListener.class);
321 }
322 } // class CheckboxMenuItem
323
324