Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / CheckboxMenuItem.java
blobd5e4ef511fef5018bce899124a0c5a4bdb73de26
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)
9 any later version.
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
19 02111-1307 USA.
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.
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. */
39 package java.awt;
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;
51 /**
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
63 * Static Variables
66 // Serialization constant
67 private static final long serialVersionUID = 6190621106981774043L;
70 * Instance Variables
73 /**
74 * @serial The state of the checkbox, with <code>true</code> being on and
75 * <code>false</code> being off.
77 private boolean state;
79 // List of registered ItemListeners
80 private transient ItemListener item_listeners;
82 /*************************************************************************/
85 * Constructors
88 /**
89 * Initializes a new instance of <code>CheckboxMenuItem</code> with no
90 * label and an initial state of off.
92 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
93 * returns true.
95 public
96 CheckboxMenuItem()
98 this("", false);
101 /*************************************************************************/
104 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
105 * specified label and an initial state of off.
107 * @param label The label of the menu item.
109 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
110 * returns true.
112 public
113 CheckboxMenuItem(String label)
115 this(label, false);
118 /*************************************************************************/
121 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
122 * specified label and initial state.
124 * @param label The label of the menu item.
125 * @param state The initial state of the menu item, where <code>true</code>
126 * is on, and <code>false</code> is off.
128 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
129 * returns true.
131 public
132 CheckboxMenuItem(String label, boolean state)
134 super(label);
135 this.state = state;
137 if (GraphicsEnvironment.isHeadless())
138 throw new HeadlessException ();
141 /*************************************************************************/
144 * Instance Methods
148 * Returns the state of this menu item.
150 * @return The state of this menu item.
152 public boolean
153 getState()
155 return(state);
158 /*************************************************************************/
161 * Sets the state of this menu item.
163 * @param state The initial state of the menu item, where <code>true</code>
164 * is on, and <code>false</code> is off.
166 public synchronized void
167 setState(boolean state)
169 this.state = state;
170 if (peer != null)
172 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
173 cp.setState (state);
177 /*************************************************************************/
180 * Returns an array of length 1 with the menu item label for this object
181 * if the state is on. Otherwise <code>null</code> is returned.
183 * @return An array with this menu item's label if it has a state of on,
184 * or <code>null</code> otherwise.
186 public Object[]
187 getSelectedObjects()
189 if (state == false)
190 return(null);
192 Object[] obj = new Object[1];
193 obj[0] = getLabel();
195 return(obj);
198 /*************************************************************************/
201 * Create's this object's native peer
203 public synchronized void
204 addNotify()
206 if (peer == null)
207 peer = getToolkit().createCheckboxMenuItem(this);
209 super.addNotify ();
212 /*************************************************************************/
215 * Adds the specified listener to the list of registered item listeners
216 * for this object.
218 * @param listener The listener to add.
220 public synchronized void
221 addItemListener(ItemListener listener)
223 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
225 enableEvents(AWTEvent.ITEM_EVENT_MASK);
228 /*************************************************************************/
231 * Removes the specified listener from the list of registered item
232 * listeners for this object.
234 * @param listener The listener to remove.
236 public synchronized void
237 removeItemListener(ItemListener listener)
239 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
242 /*************************************************************************/
245 * Processes the specified event by calling <code>processItemEvent()</code>
246 * if it is an instance of <code>ItemEvent</code> or calling the superclass
247 * method otherwise.
249 * @param event The event to process.
251 protected void
252 processEvent(AWTEvent event)
254 if (event instanceof ItemEvent)
255 processItemEvent((ItemEvent)event);
256 else
257 super.processEvent(event);
260 /*************************************************************************/
263 * Processes the specified event by dispatching it to any registered listeners.
265 * @param event The event to process.
267 protected void
268 processItemEvent(ItemEvent event)
270 if (item_listeners != null)
271 item_listeners.itemStateChanged(event);
274 void
275 dispatchEventImpl(AWTEvent e)
277 if (e instanceof ItemEvent)
279 synchronized (this)
281 state = (((ItemEvent) e).getStateChange() == ItemEvent.SELECTED);
285 if (e.id <= ItemEvent.ITEM_LAST
286 && e.id >= ItemEvent.ITEM_FIRST
287 && (item_listeners != null
288 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
289 processEvent(e);
290 else
291 super.dispatchEventImpl(e);
294 /*************************************************************************/
297 * Returns a debugging string for this object.
299 * @return A debugging string for this object.
301 public String
302 paramString()
304 return ("label=" + getLabel() + ",state=" + state
305 + "," + super.paramString());
309 * Returns an array of all the objects currently registered as FooListeners
310 * upon this <code>CheckboxMenuItem</code>. FooListeners are registered using
311 * the addFooListener method.
313 * @exception ClassCastException If listenerType doesn't specify a class or
314 * interface that implements java.util.EventListener.
316 public EventListener[] getListeners (Class listenerType)
318 if (listenerType == ItemListener.class)
319 return AWTEventMulticaster.getListeners (item_listeners, listenerType);
321 return super.getListeners (listenerType);
325 * Returns an aray of all item listeners currently registered to this
326 * <code>CheckBoxMenuItem</code>.
328 public ItemListener[] getItemListeners ()
330 return (ItemListener[]) getListeners (ItemListener.class);
334 protected class AccessibleAWTCheckboxMenuItem extends AccessibleAWTMenuItem
335 implements AccessibleAction, AccessibleValue
337 // I think the base class provides the necessary implementation
341 * Gets the AccessibleContext associated with this <code>CheckboxMenuItem</code>.
342 * The context is created, if necessary.
344 * @return the associated context
346 public AccessibleContext getAccessibleContext()
348 /* Create the context if this is the first request */
349 if (accessibleContext == null)
350 accessibleContext = new AccessibleAWTCheckboxMenuItem();
351 return accessibleContext;
354 } // class CheckboxMenuItem