1 /* Checkbox.java -- An AWT checkbox widget
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import java
.awt
.event
.ItemEvent
;
42 import java
.awt
.event
.ItemListener
;
43 import java
.awt
.peer
.CheckboxPeer
;
44 import java
.io
.Serializable
;
47 * This class implements a component which has an on/off state. Two
48 * or more Checkboxes can be grouped by a CheckboxGroup.
50 * @author Aaron M. Renn (arenn@urbanophile.com)
51 * @author Tom Tromey <tromey@redhat.com>
53 public class Checkbox
extends Component
implements ItemSelectable
, Serializable
56 // FIXME: Need readObject/writeObject for this.
62 // Serialization Constant
63 private static final long serialVersionUID
= 7270714317450821763L;
65 /*************************************************************************/
72 * @serial The checkbox group for this checkbox.
74 private CheckboxGroup group
;
77 * @serial The label on this checkbox.
82 * @serial The state of this checkbox.
84 private boolean state
;
86 // The list of listeners for this object.
87 private transient ItemListener item_listeners
;
89 /*************************************************************************/
96 * Initializes a new instance of <code>Checkbox</code> with no label,
97 * an initial state of off, and that is not part of any checkbox group.
102 this("", false, null);
105 /*************************************************************************/
108 * Initializes a new instance of <code>Checkbox</code> with the specified
109 * label, an initial state of off, and that is not part of any checkbox
112 * @param label The label for this checkbox.
115 Checkbox(String label
)
117 this(label
, false, null);
120 /*************************************************************************/
123 * Initializes a new instance of <code>Checkbox</code> with the specified
124 * label and initial state, and that is not part of any checkbox
127 * @param label The label for this checkbox.
128 * @param state The initial state of the checkbox, <code>true</code> for
129 * on, <code>false</code> for off.
132 Checkbox(String label
, boolean state
)
134 this(label
, state
, null);
137 /*************************************************************************/
140 * Initializes a new instance of <code>Checkbox</code> with the specified
141 * label, initial state, and checkbox group.
143 * @param label The label for this checkbox.
144 * @param group The checkbox group for this box, or <code>null</code>
145 * if there is no checkbox group.
146 * @param state The initial state of the checkbox, <code>true</code> for
147 * on, <code>false</code> for off.
150 Checkbox(String label
, CheckboxGroup group
, boolean state
)
152 this(label
, state
, group
);
155 /*************************************************************************/
158 * Initializes a new instance of <code>Checkbox</code> with the specified
159 * label, initial state, and checkbox group.
161 * @param label The label for this checkbox.
162 * @param state The initial state of the checkbox, <code>true</code> for
163 * on, <code>false</code> for off.
164 * @param group The checkbox group for this box, or <code>null</code>
165 * if there is no checkbox group.
168 Checkbox(String label
, boolean state
, CheckboxGroup group
)
175 /*************************************************************************/
182 * Returns the label for this checkbox.
184 * @return The label for this checkbox.
192 /*************************************************************************/
195 * Sets the label for this checkbox to the specified value.
197 * @param label The new checkbox label.
199 public synchronized void
200 setLabel(String label
)
205 CheckboxPeer cp
= (CheckboxPeer
) peer
;
210 /*************************************************************************/
213 * Returns the state of this checkbox.
215 * @return The state of this checkbox, which will be <code>true</code> for
216 * on and <code>false</code> for off.
224 /*************************************************************************/
227 * Sets the state of this checkbox to the specified value.
229 * @param state The new state of the checkbox, which will be <code>true</code>
230 * for on or <code>false</code> for off.
232 public synchronized void
233 setState(boolean state
)
238 CheckboxPeer cp
= (CheckboxPeer
) peer
;
243 /*************************************************************************/
246 * Returns an array of length one containing the checkbox label if this
247 * checkbox is selected. Otherwise <code>null</code> is returned.
249 * @return The selection state of this checkbox.
257 Object
[] objs
= new Object
[1];
263 /*************************************************************************/
266 * Returns the checkbox group this object is a member of, if any.
268 * @return This object's checkbox group, of <code>null</code> if it is
269 * not a member of any group.
277 /*************************************************************************/
280 * Sets this object's checkbox group to the specified group.
282 * @param group The new checkbox group, or <code>null</code> to make this
283 * object part of no checkbox group.
285 public synchronized void
286 setCheckboxGroup(CheckboxGroup group
)
291 CheckboxPeer cp
= (CheckboxPeer
) peer
;
292 cp
.setCheckboxGroup (group
);
296 /*************************************************************************/
299 * Creates this object's native peer.
305 peer
= getToolkit ().createCheckbox (this);
309 public ItemListener
[] getItemListeners ()
311 return (ItemListener
[])
312 AWTEventMulticaster
.getListeners (item_listeners
, ItemListener
.class);
316 * Adds a new listeners to the list of registered listeners for this object.
318 * @param listener The new listener to add.
320 public synchronized void
321 addItemListener(ItemListener listener
)
323 item_listeners
= AWTEventMulticaster
.add(item_listeners
, listener
);
326 /*************************************************************************/
329 * Removes a listener from the list of registered listeners for this object.
331 * @param listener The listener to remove.
333 public synchronized void
334 removeItemListener(ItemListener listener
)
336 item_listeners
= AWTEventMulticaster
.remove(item_listeners
, listener
);
339 /*************************************************************************/
342 * Processes this event by calling <code>processItemEvent()</code> if it
343 * is any instance of <code>ItemEvent</code>. Otherwise it is passed to
344 * the superclass for processing.
346 * @param event The event to process.
349 processEvent(AWTEvent event
)
351 if (event
instanceof ItemEvent
)
352 processItemEvent((ItemEvent
)event
);
354 super.processEvent(event
);
357 /*************************************************************************/
360 * Processes this event by dispatching it to any registered listeners.
362 * @param event The <code>ItemEvent</code> to process.
365 processItemEvent(ItemEvent event
)
367 if (item_listeners
!= null)
368 item_listeners
.itemStateChanged(event
);
372 dispatchEventImpl(AWTEvent e
)
374 if (e
.id
<= ItemEvent
.ITEM_LAST
375 && e
.id
>= ItemEvent
.ITEM_FIRST
376 && (item_listeners
!= null
377 || (eventMask
& AWTEvent
.ITEM_EVENT_MASK
) != 0))
380 super.dispatchEventImpl(e
);
383 /*************************************************************************/
386 * Returns a debugging string for this object.
391 return ("label=" + label
+ ",state=" + state
+ ",group=" + group
392 + "," + super.paramString());