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.peer.CheckboxMenuItemPeer;
42 import java.awt.peer.MenuItemPeer;
43 import java.awt.peer.MenuComponentPeer;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
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,
62 // Serialization constant
63 private static final long serialVersionUID = 6190621106981774043L;
70 * @serial The state of the checkbox, with <code>true</code> being on and
71 * <code>false</code> being off.
73 private boolean state;
75 // List of registered ItemListeners
76 private transient ItemListener item_listeners;
78 /*************************************************************************/
85 * Initializes a new instance of <code>CheckboxMenuItem</code> with no
86 * label and an initial state of off.
94 /*************************************************************************/
97 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
98 * specified label and an initial state of off.
100 * @param label The label of the menu item.
103 CheckboxMenuItem(String label)
108 /*************************************************************************/
111 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
112 * specified label and initial state.
114 * @param label The label of the menu item.
115 * @param state The initial state of the menu item, where <code>true</code>
116 * is on, and <code>false</code> is off.
119 CheckboxMenuItem(String label, boolean state)
125 /*************************************************************************/
132 * Returns the state of this menu item.
134 * @return The state of this menu item.
142 /*************************************************************************/
145 * Sets the state of this menu item.
147 * @param state The initial state of the menu item, where <code>true</code>
148 * is on, and <code>false</code> is off.
150 public synchronized void
151 setState(boolean state)
156 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
161 /*************************************************************************/
164 * Returns an array of length 1 with the menu item label for this object
165 * if the state is on. Otherwise <code>null</code> is returned.
167 * @param An array with this menu item's label if it has a state of on,
168 * or <code>null</code> otherwise.
176 Object[] obj = new Object[1];
182 /*************************************************************************/
185 * Create's this object's native peer
187 public synchronized void
192 // This choice of toolkit seems unsatisfying, but I'm not sure
194 peer = getToolkit().createCheckboxMenuItem(this);
199 /*************************************************************************/
202 * Adds the specified listener to the list of registered item listeners
205 * @param listener The listener to add.
207 public synchronized void
208 addItemListener(ItemListener listener)
210 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
212 enableEvents(AWTEvent.ITEM_EVENT_MASK);
215 /*************************************************************************/
218 * Removes the specified listener from the list of registered item
219 * listeners for this object.
221 * @param listener The listener to remove.
223 public synchronized void
224 removeItemListener(ItemListener listener)
226 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
229 /*************************************************************************/
232 * Processes the specified event by calling <code>processItemEvent()</code>
233 * if it is an instance of <code>ItemEvent</code> or calling the superclass
236 * @param event The event to process.
239 processEvent(AWTEvent event)
241 if (event instanceof ItemEvent)
242 processItemEvent((ItemEvent)event);
244 super.processEvent(event);
247 /*************************************************************************/
250 * Processes the specified event by dispatching it to any registered listeners.
252 * @param event The event to process.
255 processItemEvent(ItemEvent event)
257 if (item_listeners != null)
258 item_listeners.itemStateChanged(event);
261 /*************************************************************************/
264 * Returns a debugging string for this object.
266 * @return A debugging string for this object.
271 return ("label=" + getLabel() + ",state=" + state
272 + "," + super.paramString());
275 } // class CheckboxMenuItem