1 | /* CheckboxMenuItem.java -- A menu option with a checkbox on it.
|
---|
2 | Copyright (C) 1999, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 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.peer.CheckboxMenuItemPeer;
|
---|
42 | import java.awt.peer.MenuItemPeer;
|
---|
43 | import java.awt.peer.MenuComponentPeer;
|
---|
44 | import java.awt.event.ItemEvent;
|
---|
45 | import 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 | */
|
---|
54 | public class CheckboxMenuItem extends MenuItem implements ItemSelectable,
|
---|
55 | java.io.Serializable
|
---|
56 | {
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Static Variables
|
---|
60 | */
|
---|
61 |
|
---|
62 | // Serialization constant
|
---|
63 | private 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 | */
|
---|
73 | private boolean state;
|
---|
74 |
|
---|
75 | // List of registered ItemListeners
|
---|
76 | private 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 | */
|
---|
91 | public
|
---|
92 | CheckboxMenuItem()
|
---|
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 | */
|
---|
108 | public
|
---|
109 | CheckboxMenuItem(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 | */
|
---|
127 | public
|
---|
128 | CheckboxMenuItem(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 | */
|
---|
148 | public boolean
|
---|
149 | getState()
|
---|
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 | */
|
---|
162 | public synchronized void
|
---|
163 | setState(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 | */
|
---|
182 | public Object[]
|
---|
183 | getSelectedObjects()
|
---|
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 | */
|
---|
199 | public synchronized void
|
---|
200 | addNotify()
|
---|
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 | */
|
---|
219 | public synchronized void
|
---|
220 | addItemListener(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 | */
|
---|
235 | public synchronized void
|
---|
236 | removeItemListener(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 | */
|
---|
250 | protected void
|
---|
251 | processEvent(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 | */
|
---|
266 | protected void
|
---|
267 | processItemEvent(ItemEvent event)
|
---|
268 | {
|
---|
269 | if (item_listeners != null)
|
---|
270 | item_listeners.itemStateChanged(event);
|
---|
271 | }
|
---|
272 |
|
---|
273 | void
|
---|
274 | dispatchEventImpl(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 | */
|
---|
292 | public String
|
---|
293 | paramString()
|
---|
294 | {
|
---|
295 | return ("label=" + getLabel() + ",state=" + state
|
---|
296 | + "," + super.paramString());
|
---|
297 | }
|
---|
298 |
|
---|
299 | } // class CheckboxMenuItem
|
---|
300 |
|
---|