source: trunk/gcc/libjava/java/awt/Checkbox.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: 9.9 KB
Line 
1/* Checkbox.java -- An AWT checkbox widget
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.CheckboxPeer;
42import java.awt.peer.ComponentPeer;
43import java.awt.event.ItemEvent;
44import java.awt.event.ItemListener;
45import java.io.Serializable;
46
47/**
48 * This class implements a component which has an on/off state. Two
49 * or more Checkboxes can be grouped by a CheckboxGroup.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
53 */
54public class Checkbox extends Component implements ItemSelectable, Serializable
55{
56
57// FIXME: Need readObject/writeObject for this.
58
59/*
60 * Static Variables
61 */
62
63// Serialization Constant
64private static final long serialVersionUID = 7270714317450821763L;
65
66/*************************************************************************/
67
68/*
69 * Instance Variables
70 */
71
72/**
73 * @serial The checkbox group for this checkbox.
74 */
75private CheckboxGroup group;
76
77/**
78 * @serial The label on this checkbox.
79 */
80private String label;
81
82/**
83 * @serial The state of this checkbox.
84 */
85private boolean state;
86
87// The list of listeners for this object.
88private transient ItemListener item_listeners;
89
90/*************************************************************************/
91
92/*
93 * Constructors
94 */
95
96/**
97 * Initializes a new instance of <code>Checkbox</code> with no label,
98 * an initial state of off, and that is not part of any checkbox group.
99 */
100public
101Checkbox()
102{
103 this("", false, null);
104}
105
106/*************************************************************************/
107
108/**
109 * Initializes a new instance of <code>Checkbox</code> with the specified
110 * label, an initial state of off, and that is not part of any checkbox
111 * group.
112 *
113 * @param label The label for this checkbox.
114 */
115public
116Checkbox(String label)
117{
118 this(label, false, null);
119}
120
121/*************************************************************************/
122
123/**
124 * Initializes a new instance of <code>Checkbox</code> with the specified
125 * label and initial state, and that is not part of any checkbox
126 * group.
127 *
128 * @param label The label for this checkbox.
129 * @param state The initial state of the checkbox, <code>true</code> for
130 * on, <code>false</code> for off.
131 */
132public
133Checkbox(String label, boolean state)
134{
135 this(label, state, null);
136}
137
138/*************************************************************************/
139
140/**
141 * Initializes a new instance of <code>Checkbox</code> with the specified
142 * label, initial state, and checkbox group.
143 *
144 * @param label The label for this checkbox.
145 * @param group The checkbox group for this box, or <code>null</code>
146 * if there is no checkbox group.
147 * @param state The initial state of the checkbox, <code>true</code> for
148 * on, <code>false</code> for off.
149 */
150public
151Checkbox(String label, CheckboxGroup group, boolean state)
152{
153 this(label, state, group);
154}
155
156/*************************************************************************/
157
158/**
159 * Initializes a new instance of <code>Checkbox</code> with the specified
160 * label, initial state, and checkbox group.
161 *
162 * @param label The label for this checkbox.
163 * @param state The initial state of the checkbox, <code>true</code> for
164 * on, <code>false</code> for off.
165 * @param group The checkbox group for this box, or <code>null</code>
166 * if there is no checkbox group.
167 */
168public
169Checkbox(String label, boolean state, CheckboxGroup group)
170{
171 this.label = label;
172 this.state = state;
173 this.group = group;
174}
175
176/*************************************************************************/
177
178/*
179 * Instance Variables
180 */
181
182/**
183 * Returns the label for this checkbox.
184 *
185 * @return The label for this checkbox.
186 */
187public String
188getLabel()
189{
190 return(label);
191}
192
193/*************************************************************************/
194
195/**
196 * Sets the label for this checkbox to the specified value.
197 *
198 * @param label The new checkbox label.
199 */
200public synchronized void
201setLabel(String label)
202{
203 this.label = label;
204 if (peer != null)
205 {
206 CheckboxPeer cp = (CheckboxPeer) peer;
207 cp.setLabel(label);
208 }
209}
210
211/*************************************************************************/
212
213/**
214 * Returns the state of this checkbox.
215 *
216 * @return The state of this checkbox, which will be <code>true</code> for
217 * on and <code>false</code> for off.
218 */
219public boolean
220getState()
221{
222 return(state);
223}
224
225/*************************************************************************/
226
227/**
228 * Sets the state of this checkbox to the specified value.
229 *
230 * @param state The new state of the checkbox, which will be <code>true</code>
231 * for on or <code>false</code> for off.
232 */
233public synchronized void
234setState(boolean state)
235{
236 this.state = state;
237 if (peer != null)
238 {
239 CheckboxPeer cp = (CheckboxPeer) peer;
240 cp.setState (state);
241 }
242}
243
244/*************************************************************************/
245
246/**
247 * Returns an array of length one containing the checkbox label if this
248 * checkbox is selected. Otherwise <code>null</code> is returned.
249 *
250 * @return The selection state of this checkbox.
251 */
252public Object[]
253getSelectedObjects()
254{
255 if (state == false)
256 return(null);
257
258 Object[] objs = new Object[1];
259 objs[0] = label;
260
261 return(objs);
262}
263
264/*************************************************************************/
265
266/**
267 * Returns the checkbox group this object is a member of, if any.
268 *
269 * @return This object's checkbox group, of <code>null</code> if it is
270 * not a member of any group.
271 */
272public CheckboxGroup
273getCheckboxGroup()
274{
275 return(group);
276}
277
278/*************************************************************************/
279
280/**
281 * Sets this object's checkbox group to the specified group.
282 *
283 * @param group The new checkbox group, or <code>null</code> to make this
284 * object part of no checkbox group.
285 */
286public synchronized void
287setCheckboxGroup(CheckboxGroup group)
288{
289 this.group = group;
290 if (peer != null)
291 {
292 CheckboxPeer cp = (CheckboxPeer) peer;
293 cp.setCheckboxGroup (group);
294 }
295}
296
297/*************************************************************************/
298
299/**
300 * Creates this object's native peer.
301 */
302public void
303addNotify()
304{
305 if (peer == null)
306 peer = getToolkit ().createCheckbox (this);
307 super.addNotify ();
308}
309
310/*************************************************************************/
311
312/**
313 * Adds a new listeners to the list of registered listeners for this object.
314 *
315 * @param listener The new listener to add.
316 */
317public synchronized void
318addItemListener(ItemListener listener)
319{
320 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
321}
322
323/*************************************************************************/
324
325/**
326 * Removes a listener from the list of registered listeners for this object.
327 *
328 * @param listener The listener to remove.
329 */
330public synchronized void
331removeItemListener(ItemListener listener)
332{
333 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
334}
335
336/*************************************************************************/
337
338/**
339 * Processes this event by calling <code>processItemEvent()</code> if it
340 * is any instance of <code>ItemEvent</code>. Otherwise it is passed to
341 * the superclass for processing.
342 *
343 * @param event The event to process.
344 */
345protected void
346processEvent(AWTEvent event)
347{
348 if (event instanceof ItemEvent)
349 processItemEvent((ItemEvent)event);
350 else
351 super.processEvent(event);
352}
353
354/*************************************************************************/
355
356/**
357 * Processes this event by dispatching it to any registered listeners.
358 *
359 * @param event The <code>ItemEvent</code> to process.
360 */
361protected void
362processItemEvent(ItemEvent event)
363{
364 if (item_listeners != null)
365 item_listeners.itemStateChanged(event);
366}
367
368void
369dispatchEventImpl(AWTEvent e)
370{
371 if (e.id <= ItemEvent.ITEM_LAST
372 && e.id >= ItemEvent.ITEM_FIRST
373 && (item_listeners != null
374 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
375 processEvent(e);
376 else
377 super.dispatchEventImpl(e);
378}
379
380/*************************************************************************/
381
382/**
383 * Returns a debugging string for this object.
384 */
385protected String
386paramString()
387{
388 return ("label=" + label + ",state=" + state + ",group=" + group
389 + "," + super.paramString());
390}
391
392} // class Checkbox
Note: See TracBrowser for help on using the repository browser.