1 /* CheckboxMenuItem.java -- A menu option with a checkbox on it.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
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
;
48 * This class implements a menu item that has a checkbox on it indicating
49 * the selected state of some option.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
54 public class CheckboxMenuItem
extends MenuItem
implements ItemSelectable
61 // Serialization constant
62 private static final long serialVersionUID
= 6190621106981774043L;
69 * @serial The state of the checkbox, with <code>true</code> being on and
70 * <code>false</code> being off.
72 private boolean state
;
74 // List of registered ItemListeners
75 private transient ItemListener item_listeners
;
77 /*************************************************************************/
84 * Initializes a new instance of <code>CheckboxMenuItem</code> with no
85 * label and an initial state of off.
87 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
96 /*************************************************************************/
99 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
100 * specified label and an initial state of off.
102 * @param label The label of the menu item.
104 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
108 CheckboxMenuItem(String label
)
113 /*************************************************************************/
116 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
117 * specified label and initial state.
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.
123 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
127 CheckboxMenuItem(String label
, boolean state
)
132 if (GraphicsEnvironment
.isHeadless())
133 throw new HeadlessException ();
136 /*************************************************************************/
143 * Returns the state of this menu item.
145 * @return The state of this menu item.
153 /*************************************************************************/
156 * Sets the state of this menu item.
158 * @param state The initial state of the menu item, where <code>true</code>
159 * is on, and <code>false</code> is off.
161 public synchronized void
162 setState(boolean state
)
167 CheckboxMenuItemPeer cp
= (CheckboxMenuItemPeer
) peer
;
172 /*************************************************************************/
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.
178 * @param An array with this menu item's label if it has a state of on,
179 * or <code>null</code> otherwise.
187 Object
[] obj
= new Object
[1];
193 /*************************************************************************/
196 * Create's this object's native peer
198 public synchronized void
203 // This choice of toolkit seems unsatisfying, but I'm not sure
205 peer
= getToolkit().createCheckboxMenuItem(this);
210 /*************************************************************************/
213 * Adds the specified listener to the list of registered item listeners
216 * @param listener The listener to add.
218 public synchronized void
219 addItemListener(ItemListener listener
)
221 item_listeners
= AWTEventMulticaster
.add(item_listeners
, listener
);
223 enableEvents(AWTEvent
.ITEM_EVENT_MASK
);
226 /*************************************************************************/
229 * Removes the specified listener from the list of registered item
230 * listeners for this object.
232 * @param listener The listener to remove.
234 public synchronized void
235 removeItemListener(ItemListener listener
)
237 item_listeners
= AWTEventMulticaster
.remove(item_listeners
, listener
);
240 /*************************************************************************/
243 * Processes the specified event by calling <code>processItemEvent()</code>
244 * if it is an instance of <code>ItemEvent</code> or calling the superclass
247 * @param event The event to process.
250 processEvent(AWTEvent event
)
252 if (event
instanceof ItemEvent
)
253 processItemEvent((ItemEvent
)event
);
255 super.processEvent(event
);
258 /*************************************************************************/
261 * Processes the specified event by dispatching it to any registered listeners.
263 * @param event The event to process.
266 processItemEvent(ItemEvent event
)
268 if (item_listeners
!= null)
269 item_listeners
.itemStateChanged(event
);
273 dispatchEventImpl(AWTEvent e
)
275 if (e
.id
<= ItemEvent
.ITEM_LAST
276 && e
.id
>= ItemEvent
.ITEM_FIRST
277 && (item_listeners
!= null
278 || (eventMask
& AWTEvent
.ITEM_EVENT_MASK
) != 0))
281 super.dispatchEventImpl(e
);
284 /*************************************************************************/
287 * Returns a debugging string for this object.
289 * @return A debugging string for this object.
294 return ("label=" + getLabel() + ",state=" + state
295 + "," + super.paramString());
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.
303 * @exception ClassCastException If listenerType doesn't specify a class or
304 * interface that implements java.util.EventListener.
306 public EventListener
[] getListeners (Class listenerType
)
308 if (listenerType
== ItemListener
.class)
309 return AWTEventMulticaster
.getListeners (item_listeners
, listenerType
);
311 return super.getListeners (listenerType
);
315 * Returns an aray of all item listeners currently registered to this
316 * <code>CheckBoxMenuItem</code>.
318 public ItemListener
[] getItemListeners ()
320 return (ItemListener
[]) getListeners (ItemListener
.class);
322 } // class CheckboxMenuItem