FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / awt / Checkbox.java
blob1268fe86eca4429eb8cb7542d0ac46ceee1d6228
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)
9 any later version.
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
19 02111-1307 USA.
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.
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. */
39 package java.awt;
41 import java.awt.peer.CheckboxPeer;
42 import java.awt.peer.ComponentPeer;
43 import java.awt.event.ItemEvent;
44 import java.awt.event.ItemListener;
45 import java.io.Serializable;
47 /**
48 * This class implements a component which has an on/off state. Two
49 * or more Checkboxes can be grouped by a CheckboxGroup.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
54 public class Checkbox extends Component implements ItemSelectable, Serializable
57 // FIXME: Need readObject/writeObject for this.
60 * Static Variables
63 // Serialization Constant
64 private static final long serialVersionUID = 7270714317450821763L;
66 /*************************************************************************/
69 * Instance Variables
72 /**
73 * @serial The checkbox group for this checkbox.
75 private CheckboxGroup group;
77 /**
78 * @serial The label on this checkbox.
80 private String label;
82 /**
83 * @serial The state of this checkbox.
85 private boolean state;
87 // The list of listeners for this object.
88 private transient ItemListener item_listeners;
90 /*************************************************************************/
93 * Constructors
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.
100 public
101 Checkbox()
103 this("", false, null);
106 /*************************************************************************/
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.
113 * @param label The label for this checkbox.
115 public
116 Checkbox(String label)
118 this(label, false, null);
121 /*************************************************************************/
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.
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.
132 public
133 Checkbox(String label, boolean state)
135 this(label, state, null);
138 /*************************************************************************/
141 * Initializes a new instance of <code>Checkbox</code> with the specified
142 * label, initial state, and checkbox group.
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.
150 public
151 Checkbox(String label, CheckboxGroup group, boolean state)
153 this(label, state, group);
156 /*************************************************************************/
159 * Initializes a new instance of <code>Checkbox</code> with the specified
160 * label, initial state, and checkbox group.
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.
168 public
169 Checkbox(String label, boolean state, CheckboxGroup group)
171 this.label = label;
172 this.state = state;
173 this.group = group;
176 /*************************************************************************/
179 * Instance Variables
183 * Returns the label for this checkbox.
185 * @return The label for this checkbox.
187 public String
188 getLabel()
190 return(label);
193 /*************************************************************************/
196 * Sets the label for this checkbox to the specified value.
198 * @param label The new checkbox label.
200 public synchronized void
201 setLabel(String label)
203 this.label = label;
204 if (peer != null)
206 CheckboxPeer cp = (CheckboxPeer) peer;
207 cp.setLabel(label);
211 /*************************************************************************/
214 * Returns the state of this checkbox.
216 * @return The state of this checkbox, which will be <code>true</code> for
217 * on and <code>false</code> for off.
219 public boolean
220 getState()
222 return(state);
225 /*************************************************************************/
228 * Sets the state of this checkbox to the specified value.
230 * @param state The new state of the checkbox, which will be <code>true</code>
231 * for on or <code>false</code> for off.
233 public synchronized void
234 setState(boolean state)
236 this.state = state;
237 if (peer != null)
239 CheckboxPeer cp = (CheckboxPeer) peer;
240 cp.setState (state);
244 /*************************************************************************/
247 * Returns an array of length one containing the checkbox label if this
248 * checkbox is selected. Otherwise <code>null</code> is returned.
250 * @return The selection state of this checkbox.
252 public Object[]
253 getSelectedObjects()
255 if (state == false)
256 return(null);
258 Object[] objs = new Object[1];
259 objs[0] = label;
261 return(objs);
264 /*************************************************************************/
267 * Returns the checkbox group this object is a member of, if any.
269 * @return This object's checkbox group, of <code>null</code> if it is
270 * not a member of any group.
272 public CheckboxGroup
273 getCheckboxGroup()
275 return(group);
278 /*************************************************************************/
281 * Sets this object's checkbox group to the specified group.
283 * @param group The new checkbox group, or <code>null</code> to make this
284 * object part of no checkbox group.
286 public synchronized void
287 setCheckboxGroup(CheckboxGroup group)
289 this.group = group;
290 if (peer != null)
292 CheckboxPeer cp = (CheckboxPeer) peer;
293 cp.setCheckboxGroup (group);
297 /*************************************************************************/
300 * Creates this object's native peer.
302 public void
303 addNotify()
305 if (peer == null)
306 peer = getToolkit ().createCheckbox (this);
307 super.addNotify ();
310 /*************************************************************************/
313 * Adds a new listeners to the list of registered listeners for this object.
315 * @param listener The new listener to add.
317 public synchronized void
318 addItemListener(ItemListener listener)
320 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
323 /*************************************************************************/
326 * Removes a listener from the list of registered listeners for this object.
328 * @param listener The listener to remove.
330 public synchronized void
331 removeItemListener(ItemListener listener)
333 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
336 /*************************************************************************/
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.
343 * @param event The event to process.
345 protected void
346 processEvent(AWTEvent event)
348 if (event instanceof ItemEvent)
349 processItemEvent((ItemEvent)event);
350 else
351 super.processEvent(event);
354 /*************************************************************************/
357 * Processes this event by dispatching it to any registered listeners.
359 * @param event The <code>ItemEvent</code> to process.
361 protected void
362 processItemEvent(ItemEvent event)
364 if (item_listeners != null)
365 item_listeners.itemStateChanged(event);
368 void
369 dispatchEventImpl(AWTEvent e)
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);
380 /*************************************************************************/
383 * Returns a debugging string for this object.
385 protected String
386 paramString()
388 return ("label=" + label + ",state=" + state + ",group=" + group
389 + "," + super.paramString());
392 } // class Checkbox