OSDN Git Service

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