1 /* CheckboxMenuItem.java -- A menu option with a checkbox on it.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, 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.util.EventListener;
46 import javax.accessibility.Accessible;
47 import javax.accessibility.AccessibleAction;
48 import javax.accessibility.AccessibleContext;
49 import javax.accessibility.AccessibleValue;
52 * This class implements a menu item that has a checkbox on it indicating
53 * the selected state of some option.
55 * @author Aaron M. Renn (arenn@urbanophile.com)
56 * @author Tom Tromey (tromey@redhat.com)
58 public class CheckboxMenuItem extends MenuItem
59 implements ItemSelectable, Accessible
67 * The number used to generate the name returned by getName.
69 private static transient long next_chkmenuitem_number;
71 // Serialization constant
72 private static final long serialVersionUID = 6190621106981774043L;
79 * @serial The state of the checkbox, with <code>true</code> being on and
80 * <code>false</code> being off.
82 private boolean state;
84 // List of registered ItemListeners
85 private transient ItemListener item_listeners;
87 /*************************************************************************/
94 * Initializes a new instance of <code>CheckboxMenuItem</code> with no
95 * label and an initial state of off.
97 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
106 /*************************************************************************/
109 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
110 * specified label and an initial state of off.
112 * @param label The label of the menu item.
114 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
118 CheckboxMenuItem(String label)
123 /*************************************************************************/
126 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
127 * specified label and initial state.
129 * @param label The label of the menu item.
130 * @param state The initial state of the menu item, where <code>true</code>
131 * is on, and <code>false</code> is off.
133 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
137 CheckboxMenuItem(String label, boolean state)
142 if (GraphicsEnvironment.isHeadless())
143 throw new HeadlessException ();
146 /*************************************************************************/
153 * Returns the state of this menu item.
155 * @return The state of this menu item.
163 /*************************************************************************/
166 * Sets the state of this menu item.
168 * @param state The initial state of the menu item, where <code>true</code>
169 * is on, and <code>false</code> is off.
171 public synchronized void
172 setState(boolean state)
177 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
182 /*************************************************************************/
185 * Returns an array of length 1 with the menu item label for this object
186 * if the state is on. Otherwise <code>null</code> is returned.
188 * @return An array with this menu item's label if it has a state of on,
189 * or <code>null</code> otherwise.
197 Object[] obj = new Object[1];
203 /*************************************************************************/
206 * Create's this object's native peer
208 public synchronized void
212 peer = getToolkit().createCheckboxMenuItem(this);
217 /*************************************************************************/
220 * Adds the specified listener to the list of registered item listeners
223 * @param listener The listener to add.
225 public synchronized void
226 addItemListener(ItemListener listener)
228 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
230 enableEvents(AWTEvent.ITEM_EVENT_MASK);
233 /*************************************************************************/
236 * Removes the specified listener from the list of registered item
237 * listeners for this object.
239 * @param listener The listener to remove.
241 public synchronized void
242 removeItemListener(ItemListener listener)
244 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
247 /*************************************************************************/
250 * Processes the specified event by calling <code>processItemEvent()</code>
251 * if it is an instance of <code>ItemEvent</code> or calling the superclass
254 * @param event The event to process.
257 processEvent(AWTEvent event)
259 if (event instanceof ItemEvent)
260 processItemEvent((ItemEvent)event);
262 super.processEvent(event);
265 /*************************************************************************/
268 * Processes the specified event by dispatching it to any registered listeners.
270 * @param event The event to process.
273 processItemEvent(ItemEvent event)
275 if (item_listeners != null)
276 item_listeners.itemStateChanged(event);
280 dispatchEventImpl(AWTEvent e)
282 if (e instanceof ItemEvent)
286 state = (((ItemEvent) e).getStateChange() == ItemEvent.SELECTED);
290 if (e.id <= ItemEvent.ITEM_LAST
291 && e.id >= ItemEvent.ITEM_FIRST
292 && (item_listeners != null
293 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
296 super.dispatchEventImpl(e);
299 /*************************************************************************/
302 * Returns a debugging string for this object.
304 * @return A debugging string for this object.
309 return ("label=" + getLabel() + ",state=" + state
310 + "," + super.paramString());
314 * Returns an array of all the objects currently registered as FooListeners
315 * upon this <code>CheckboxMenuItem</code>. FooListeners are registered using
316 * the addFooListener method.
318 * @exception ClassCastException If listenerType doesn't specify a class or
319 * interface that implements java.util.EventListener.
321 public <T extends EventListener> T[] getListeners (Class<T> listenerType)
323 if (listenerType == ItemListener.class)
324 return AWTEventMulticaster.getListeners (item_listeners, listenerType);
326 return super.getListeners (listenerType);
330 * Returns an aray of all item listeners currently registered to this
331 * <code>CheckBoxMenuItem</code>.
333 public ItemListener[] getItemListeners ()
335 return (ItemListener[]) getListeners (ItemListener.class);
339 protected class AccessibleAWTCheckboxMenuItem extends AccessibleAWTMenuItem
340 implements AccessibleAction, AccessibleValue
342 // I think the base class provides the necessary implementation
344 private static final long serialVersionUID = -1122642964303476L;
348 * Gets the AccessibleContext associated with this <code>CheckboxMenuItem</code>.
349 * The context is created, if necessary.
351 * @return the associated context
353 public AccessibleContext getAccessibleContext()
355 /* Create the context if this is the first request */
356 if (accessibleContext == null)
357 accessibleContext = new AccessibleAWTCheckboxMenuItem();
358 return accessibleContext;
362 * Generate a unique name for this <code>CheckboxMenuItem</code>.
364 * @return A unique name for this <code>CheckboxMenuItem</code>.
366 String generateName()
368 return "chkmenuitem" + getUniqueLong();
371 private static synchronized long getUniqueLong()
373 return next_chkmenuitem_number++;
376 } // class CheckboxMenuItem