source: trunk/gcc/libjava/java/awt/CheckboxMenuItem.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.7 KB
Line 
1/* CheckboxMenuItem.java -- A menu option with a checkbox on it.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.CheckboxMenuItemPeer;
42import java.awt.peer.MenuItemPeer;
43import java.awt.peer.MenuComponentPeer;
44import java.awt.event.ItemEvent;
45import java.awt.event.ItemListener;
46
47/**
48 * This class implements a menu item that has a checkbox on it indicating
49 * the selected state of some option.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
53 */
54public class CheckboxMenuItem extends MenuItem implements ItemSelectable,
55 java.io.Serializable
56{
57
58/*
59 * Static Variables
60 */
61
62// Serialization constant
63private static final long serialVersionUID = 6190621106981774043L;
64
65/*
66 * Instance Variables
67 */
68
69/**
70 * @serial The state of the checkbox, with <code>true</code> being on and
71 * <code>false</code> being off.
72 */
73private boolean state;
74
75// List of registered ItemListeners
76private transient ItemListener item_listeners;
77
78/*************************************************************************/
79
80/*
81 * Constructors
82 */
83
84/**
85 * Initializes a new instance of <code>CheckboxMenuItem</code> with no
86 * label and an initial state of off.
87 *
88 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
89 * returns true.
90 */
91public
92CheckboxMenuItem()
93{
94 this("", false);
95}
96
97/*************************************************************************/
98
99/**
100 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
101 * specified label and an initial state of off.
102 *
103 * @param label The label of the menu item.
104 *
105 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
106 * returns true.
107 */
108public
109CheckboxMenuItem(String label)
110{
111 this(label, false);
112}
113
114/*************************************************************************/
115
116/**
117 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
118 * specified label and initial state.
119 *
120 * @param label The label of the menu item.
121 * @param state The initial state of the menu item, where <code>true</code>
122 * is on, and <code>false</code> is off.
123 *
124 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
125 * returns true.
126 */
127public
128CheckboxMenuItem(String label, boolean state)
129{
130 super(label);
131 this.state = state;
132
133 if (GraphicsEnvironment.isHeadless())
134 throw new HeadlessException ();
135}
136
137/*************************************************************************/
138
139/*
140 * Instance Methods
141 */
142
143/**
144 * Returns the state of this menu item.
145 *
146 * @return The state of this menu item.
147 */
148public boolean
149getState()
150{
151 return(state);
152}
153
154/*************************************************************************/
155
156/**
157 * Sets the state of this menu item.
158 *
159 * @param state The initial state of the menu item, where <code>true</code>
160 * is on, and <code>false</code> is off.
161 */
162public synchronized void
163setState(boolean state)
164{
165 this.state = state;
166 if (peer != null)
167 {
168 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
169 cp.setState (state);
170 }
171}
172
173/*************************************************************************/
174
175/**
176 * Returns an array of length 1 with the menu item label for this object
177 * if the state is on. Otherwise <code>null</code> is returned.
178 *
179 * @param An array with this menu item's label if it has a state of on,
180 * or <code>null</code> otherwise.
181 */
182public Object[]
183getSelectedObjects()
184{
185 if (state == false)
186 return(null);
187
188 Object[] obj = new Object[1];
189 obj[0] = getLabel();
190
191 return(obj);
192}
193
194/*************************************************************************/
195
196/**
197 * Create's this object's native peer
198 */
199public synchronized void
200addNotify()
201{
202 if (peer != null)
203 {
204 // This choice of toolkit seems unsatisfying, but I'm not sure
205 // what else to do.
206 peer = getToolkit().createCheckboxMenuItem(this);
207 }
208 super.addNotify ();
209}
210
211/*************************************************************************/
212
213/**
214 * Adds the specified listener to the list of registered item listeners
215 * for this object.
216 *
217 * @param listener The listener to add.
218 */
219public synchronized void
220addItemListener(ItemListener listener)
221{
222 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
223
224 enableEvents(AWTEvent.ITEM_EVENT_MASK);
225}
226
227/*************************************************************************/
228
229/**
230 * Removes the specified listener from the list of registered item
231 * listeners for this object.
232 *
233 * @param listener The listener to remove.
234 */
235public synchronized void
236removeItemListener(ItemListener listener)
237{
238 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
239}
240
241/*************************************************************************/
242
243/**
244 * Processes the specified event by calling <code>processItemEvent()</code>
245 * if it is an instance of <code>ItemEvent</code> or calling the superclass
246 * method otherwise.
247 *
248 * @param event The event to process.
249 */
250protected void
251processEvent(AWTEvent event)
252{
253 if (event instanceof ItemEvent)
254 processItemEvent((ItemEvent)event);
255 else
256 super.processEvent(event);
257}
258
259/*************************************************************************/
260
261/**
262 * Processes the specified event by dispatching it to any registered listeners.
263 *
264 * @param event The event to process.
265 */
266protected void
267processItemEvent(ItemEvent event)
268{
269 if (item_listeners != null)
270 item_listeners.itemStateChanged(event);
271}
272
273void
274dispatchEventImpl(AWTEvent e)
275{
276 if (e.id <= ItemEvent.ITEM_LAST
277 && e.id >= ItemEvent.ITEM_FIRST
278 && (item_listeners != null
279 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
280 processEvent(e);
281 else
282 super.dispatchEventImpl(e);
283}
284
285/*************************************************************************/
286
287/**
288 * Returns a debugging string for this object.
289 *
290 * @return A debugging string for this object.
291 */
292public String
293paramString()
294{
295 return ("label=" + getLabel() + ",state=" + state
296 + "," + super.paramString());
297}
298
299} // class CheckboxMenuItem
300
Note: See TracBrowser for help on using the repository browser.