source: trunk/src/gcc/libjava/java/awt/CheckboxMenuItem.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.1 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 ([email protected])
52 * @author Tom Tromey <[email protected]>
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 */
88public
89CheckboxMenuItem()
90{
91 this("", false);
92}
93
94/*************************************************************************/
95
96/**
97 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
98 * specified label and an initial state of off.
99 *
100 * @param label The label of the menu item.
101 */
102public
103CheckboxMenuItem(String label)
104{
105 this(label, false);
106}
107
108/*************************************************************************/
109
110/**
111 * Initializes a new instance of <code>CheckboxMenuItem</code> with the
112 * specified label and initial state.
113 *
114 * @param label The label of the menu item.
115 * @param state The initial state of the menu item, where <code>true</code>
116 * is on, and <code>false</code> is off.
117 */
118public
119CheckboxMenuItem(String label, boolean state)
120{
121 super(label);
122 this.state = state;
123}
124
125/*************************************************************************/
126
127/*
128 * Instance Methods
129 */
130
131/**
132 * Returns the state of this menu item.
133 *
134 * @return The state of this menu item.
135 */
136public boolean
137getState()
138{
139 return(state);
140}
141
142/*************************************************************************/
143
144/**
145 * Sets the state of this menu item.
146 *
147 * @param state The initial state of the menu item, where <code>true</code>
148 * is on, and <code>false</code> is off.
149 */
150public synchronized void
151setState(boolean state)
152{
153 this.state = state;
154 if (peer != null)
155 {
156 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
157 cp.setState (state);
158 }
159}
160
161/*************************************************************************/
162
163/**
164 * Returns an array of length 1 with the menu item label for this object
165 * if the state is on. Otherwise <code>null</code> is returned.
166 *
167 * @param An array with this menu item's label if it has a state of on,
168 * or <code>null</code> otherwise.
169 */
170public Object[]
171getSelectedObjects()
172{
173 if (state == false)
174 return(null);
175
176 Object[] obj = new Object[1];
177 obj[0] = getLabel();
178
179 return(obj);
180}
181
182/*************************************************************************/
183
184/**
185 * Create's this object's native peer
186 */
187public synchronized void
188addNotify()
189{
190 if (peer != null)
191 {
192 // This choice of toolkit seems unsatisfying, but I'm not sure
193 // what else to do.
194 peer = getToolkit().createCheckboxMenuItem(this);
195 }
196 super.addNotify ();
197}
198
199/*************************************************************************/
200
201/**
202 * Adds the specified listener to the list of registered item listeners
203 * for this object.
204 *
205 * @param listener The listener to add.
206 */
207public synchronized void
208addItemListener(ItemListener listener)
209{
210 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
211
212 enableEvents(AWTEvent.ITEM_EVENT_MASK);
213}
214
215/*************************************************************************/
216
217/**
218 * Removes the specified listener from the list of registered item
219 * listeners for this object.
220 *
221 * @param listener The listener to remove.
222 */
223public synchronized void
224removeItemListener(ItemListener listener)
225{
226 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
227}
228
229/*************************************************************************/
230
231/**
232 * Processes the specified event by calling <code>processItemEvent()</code>
233 * if it is an instance of <code>ItemEvent</code> or calling the superclass
234 * method otherwise.
235 *
236 * @param event The event to process.
237 */
238protected void
239processEvent(AWTEvent event)
240{
241 if (event instanceof ItemEvent)
242 processItemEvent((ItemEvent)event);
243 else
244 super.processEvent(event);
245}
246
247/*************************************************************************/
248
249/**
250 * Processes the specified event by dispatching it to any registered listeners.
251 *
252 * @param event The event to process.
253 */
254protected void
255processItemEvent(ItemEvent event)
256{
257 if (item_listeners != null)
258 item_listeners.itemStateChanged(event);
259}
260
261/*************************************************************************/
262
263/**
264 * Returns a debugging string for this object.
265 *
266 * @return A debugging string for this object.
267 */
268public String
269paramString()
270{
271 return ("label=" + getLabel() + ",state=" + state
272 + "," + super.paramString());
273}
274
275} // class CheckboxMenuItem
276
Note: See TracBrowser for help on using the repository browser.