2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / CheckboxMenuItem.java
blobc7df075b3100a1b43e77b67d9dbb476f90f55fa5
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)
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.awt.peer.MenuItemPeer;
45 import java.util.EventListener;
47 /**
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
58 * Static Variables
61 // Serialization constant
62 private static final long serialVersionUID = 6190621106981774043L;
65 * Instance Variables
68 /**
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 /*************************************************************************/
80 * Constructors
83 /**
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()
88 * returns true.
90 public
91 CheckboxMenuItem()
93 this("", false);
96 /*************************************************************************/
98 /**
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()
105 * returns true.
107 public
108 CheckboxMenuItem(String label)
110 this(label, false);
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()
124 * returns true.
126 public
127 CheckboxMenuItem(String label, boolean state)
129 super(label);
130 this.state = state;
132 if (GraphicsEnvironment.isHeadless())
133 throw new HeadlessException ();
136 /*************************************************************************/
139 * Instance Methods
143 * Returns the state of this menu item.
145 * @return The state of this menu item.
147 public boolean
148 getState()
150 return(state);
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)
164 this.state = state;
165 if (peer != null)
167 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
168 cp.setState (state);
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.
181 public Object[]
182 getSelectedObjects()
184 if (state == false)
185 return(null);
187 Object[] obj = new Object[1];
188 obj[0] = getLabel();
190 return(obj);
193 /*************************************************************************/
196 * Create's this object's native peer
198 public synchronized void
199 addNotify()
201 if (peer != null)
203 // This choice of toolkit seems unsatisfying, but I'm not sure
204 // what else to do.
205 peer = getToolkit().createCheckboxMenuItem(this);
207 super.addNotify ();
210 /*************************************************************************/
213 * Adds the specified listener to the list of registered item listeners
214 * for this object.
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
245 * method otherwise.
247 * @param event The event to process.
249 protected void
250 processEvent(AWTEvent event)
252 if (event instanceof ItemEvent)
253 processItemEvent((ItemEvent)event);
254 else
255 super.processEvent(event);
258 /*************************************************************************/
261 * Processes the specified event by dispatching it to any registered listeners.
263 * @param event The event to process.
265 protected void
266 processItemEvent(ItemEvent event)
268 if (item_listeners != null)
269 item_listeners.itemStateChanged(event);
272 void
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))
279 processEvent(e);
280 else
281 super.dispatchEventImpl(e);
284 /*************************************************************************/
287 * Returns a debugging string for this object.
289 * @return A debugging string for this object.
291 public String
292 paramString()
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