OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / java / awt / CheckboxMenuItem.java
1 /* CheckboxMenuItem.java -- A menu option with a checkbox on it.
2    Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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
19 02110-1301 USA.
20
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.
25
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. */
37
38
39 package java.awt;
40
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43 import java.awt.peer.CheckboxMenuItemPeer;
44 import java.util.EventListener;
45
46 import javax.accessibility.Accessible;
47 import javax.accessibility.AccessibleAction;
48 import javax.accessibility.AccessibleContext;
49 import javax.accessibility.AccessibleValue;
50
51 /**
52   * This class implements a menu item that has a checkbox on it indicating
53   * the selected state of some option.
54   *
55   * @author Aaron M. Renn (arenn@urbanophile.com)
56   * @author Tom Tromey (tromey@redhat.com)
57   */
58 public class CheckboxMenuItem extends MenuItem
59   implements ItemSelectable, Accessible
60 {
61
62 /*
63  * Static Variables
64  */
65
66 // Serialization constant
67 private static final long serialVersionUID = 6190621106981774043L;
68
69 /*
70  * Instance Variables
71  */
72
73 /**
74   * @serial The state of the checkbox, with <code>true</code> being on and
75   * <code>false</code> being off.
76   */
77 private boolean state;
78
79 // List of registered ItemListeners
80 private transient ItemListener item_listeners;
81
82 /*************************************************************************/
83
84 /*
85  * Constructors
86  */
87
88 /**
89   * Initializes a new instance of <code>CheckboxMenuItem</code> with no
90   * label and an initial state of off.
91   *
92   * @exception HeadlessException If GraphicsEnvironment.isHeadless()
93   * returns true.
94   */
95 public
96 CheckboxMenuItem()
97 {
98   this("", false);
99 }
100
101 /*************************************************************************/
102
103 /**
104   * Initializes a new instance of <code>CheckboxMenuItem</code> with the
105   * specified label and an initial state of off.
106   *
107   * @param label The label of the menu item.
108   *
109   * @exception HeadlessException If GraphicsEnvironment.isHeadless()
110   * returns true.
111   */
112 public
113 CheckboxMenuItem(String label)
114 {
115   this(label, false);
116 }
117
118 /*************************************************************************/
119
120 /**
121   * Initializes a new instance of <code>CheckboxMenuItem</code> with the
122   * specified label and initial state.
123   *
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.
127   *
128   * @exception HeadlessException If GraphicsEnvironment.isHeadless()
129   * returns true.
130   */
131 public
132 CheckboxMenuItem(String label, boolean state)
133 {
134   super(label);
135   this.state = state;
136
137   if (GraphicsEnvironment.isHeadless())
138     throw new HeadlessException ();
139 }
140
141 /*************************************************************************/
142
143 /*
144  * Instance Methods
145  */
146
147 /**
148   * Returns the state of this menu item.
149   *
150   * @return The state of this menu item.
151   */
152 public boolean
153 getState()
154 {
155   return(state);
156 }
157
158 /*************************************************************************/
159
160 /**
161   * Sets the state of this menu item.
162   *
163   * @param state The initial state of the menu item, where <code>true</code>
164   * is on, and <code>false</code> is off.
165   */
166 public synchronized void
167 setState(boolean state)
168 {
169   this.state = state;
170   if (peer != null)
171     {
172       CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
173       cp.setState (state);
174     }
175 }
176
177 /*************************************************************************/
178
179 /**
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.
182   *
183   * @return An array with this menu item's label if it has a state of on,
184   * or <code>null</code> otherwise.
185   */
186 public Object[]
187 getSelectedObjects()
188 {
189   if (state == false)
190     return(null);
191
192   Object[] obj = new Object[1];
193   obj[0] = getLabel();
194
195   return(obj);
196 }
197
198 /*************************************************************************/
199
200 /**
201   * Create's this object's native peer
202   */
203 public synchronized void
204 addNotify()
205 {
206   if (peer == null)
207     peer = getToolkit().createCheckboxMenuItem(this);
208
209   super.addNotify ();
210 }
211
212 /*************************************************************************/
213
214 /**
215   * Adds the specified listener to the list of registered item listeners
216   * for this object.
217   *
218   * @param listener The listener to add.
219   */
220 public synchronized void
221 addItemListener(ItemListener listener)
222 {
223   item_listeners = AWTEventMulticaster.add(item_listeners, listener);
224
225   enableEvents(AWTEvent.ITEM_EVENT_MASK);
226 }
227
228 /*************************************************************************/
229
230 /**
231   * Removes the specified listener from the list of registered item
232   * listeners for this object.
233   *
234   * @param listener The listener to remove.
235   */
236 public synchronized void
237 removeItemListener(ItemListener listener)
238 {
239   item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
240 }
241
242 /*************************************************************************/
243
244 /**
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.
248   *
249   * @param event The event to process.
250   */
251 protected void
252 processEvent(AWTEvent event)
253 {
254   if (event instanceof ItemEvent)
255     processItemEvent((ItemEvent)event);
256   else
257     super.processEvent(event);
258 }
259
260 /*************************************************************************/
261
262 /**
263   * Processes the specified event by dispatching it to any registered listeners.
264   *
265   * @param event The event to process.
266   */
267 protected void
268 processItemEvent(ItemEvent event)
269 {
270   if (item_listeners != null)
271     item_listeners.itemStateChanged(event);
272 }
273
274 void
275 dispatchEventImpl(AWTEvent e)
276 {
277   if (e instanceof ItemEvent)
278     {
279       synchronized (this)
280         {
281           state = (((ItemEvent) e).getStateChange() == ItemEvent.SELECTED);
282         }
283     }
284
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);
292 }
293
294 /*************************************************************************/
295
296 /**
297   * Returns a debugging string for this object.
298   *
299   * @return A debugging string for this object.
300   */
301 public String
302 paramString()
303 {
304   return ("label=" + getLabel() + ",state=" + state
305           + "," + super.paramString());
306 }
307
308   /**
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.
312    *
313    * @exception ClassCastException If listenerType doesn't specify a class or
314    * interface that implements java.util.EventListener.
315    */
316   public EventListener[] getListeners (Class listenerType)
317   {
318     if (listenerType == ItemListener.class)
319       return AWTEventMulticaster.getListeners (item_listeners, listenerType); 
320               
321     return super.getListeners (listenerType);
322   }
323
324   /**
325    * Returns an aray of all item listeners currently registered to this
326    * <code>CheckBoxMenuItem</code>.
327    */
328   public ItemListener[] getItemListeners ()
329   {
330     return (ItemListener[]) getListeners (ItemListener.class);
331   }
332
333
334   protected class AccessibleAWTCheckboxMenuItem extends AccessibleAWTMenuItem
335     implements AccessibleAction, AccessibleValue
336   {
337     // I think the base class provides the necessary implementation
338   }
339   
340   /**
341    * Gets the AccessibleContext associated with this <code>CheckboxMenuItem</code>.
342    * The context is created, if necessary.
343    *
344    * @return the associated context
345    */
346   public AccessibleContext getAccessibleContext()
347   {
348     /* Create the context if this is the first request */
349     if (accessibleContext == null)
350       accessibleContext = new AccessibleAWTCheckboxMenuItem();
351     return accessibleContext;
352   }
353
354 } // class CheckboxMenuItem
355