1 /* Checkbox.java -- An AWT checkbox widget
2 Copyright (C) 1999, 2000, 2001, 2002, 2005 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
;
46 import javax
.accessibility
.Accessible
;
47 import javax
.accessibility
.AccessibleAction
;
48 import javax
.accessibility
.AccessibleContext
;
49 import javax
.accessibility
.AccessibleRole
;
50 import javax
.accessibility
.AccessibleState
;
51 import javax
.accessibility
.AccessibleStateSet
;
52 import javax
.accessibility
.AccessibleValue
;
55 * This class implements a component which has an on/off state. Two
56 * or more Checkboxes can be grouped by a CheckboxGroup.
58 * @author Aaron M. Renn (arenn@urbanophile.com)
59 * @author Tom Tromey (tromey@redhat.com)
61 public class Checkbox
extends Component
62 implements ItemSelectable
, Accessible
, Serializable
65 // FIXME: Need readObject/writeObject for this.
71 // Serialization Constant
72 private static final long serialVersionUID
= 7270714317450821763L;
74 /*************************************************************************/
81 * @serial The checkbox group for this checkbox.
83 private CheckboxGroup group
;
86 * @serial The label on this checkbox.
91 * @serial The state of this checkbox.
93 private boolean state
;
95 // The list of listeners for this object.
96 private transient ItemListener item_listeners
;
99 * The number used to generate the name returned by getName.
101 private static transient long next_checkbox_number
;
104 * This class provides accessibility support for the
107 * @author Jerry Quinn (jlquinn@optonline.net)
108 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
110 protected class AccessibleAWTCheckbox
111 extends AccessibleAWTComponent
112 implements ItemListener
, AccessibleAction
, AccessibleValue
115 * Serialization constant to match JDK 1.5
117 private static final long serialVersionUID
= 7881579233144754107L;
120 * Default constructor which simply calls the
121 * super class for generic component accessibility
124 public AccessibleAWTCheckbox()
130 * Captures changes to the state of the checkbox and
131 * fires appropriate accessible property change events.
133 * @param event the event fired.
134 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
136 public void itemStateChanged(ItemEvent event
)
138 firePropertyChange(ACCESSIBLE_STATE_PROPERTY
,
139 state ?
null : AccessibleState
.CHECKED
,
140 state ? AccessibleState
.CHECKED
: null);
144 * Returns an implementation of the <code>AccessibleAction</code>
145 * interface for this accessible object. In this case, the
146 * current instance is simply returned (with a more appropriate
147 * type), as it also implements the accessible action as well as
150 * @return the accessible action associated with this context.
151 * @see javax.accessibility.AccessibleAction
153 public AccessibleAction
getAccessibleAction()
159 * Returns an implementation of the <code>AccessibleValue</code>
160 * interface for this accessible object. In this case, the
161 * current instance is simply returned (with a more appropriate
162 * type), as it also implements the accessible value as well as
165 * @return the accessible value associated with this context.
166 * @see javax.accessibility.AccessibleValue
168 public AccessibleValue
getAccessibleValue()
174 * The following methods are implemented in the JDK (up to
175 * 1.5) as stubs. We do likewise here.
179 * Returns the number of actions associated with this accessible
180 * object. This default implementation returns 0.
182 * @return the number of accessible actions available.
183 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
185 public int getAccessibleActionCount()
187 // 1.4.1 and 1.5 do this
192 * Returns a description of the action with the supplied id.
193 * This default implementation always returns null.
195 * @param i the id of the action whose description should be
197 * @return a <code>String</code> describing the action.
198 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
200 public String
getAccessibleActionDescription(int i
)
207 * Executes the action with the specified id. This
208 * default implementation simply returns false.
210 * @param i the id of the action to perform.
211 * @return true if the action was performed.
212 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
214 public boolean doAccessibleAction(int i
)
221 * Returns the current value of this accessible object.
222 * If no value has been set, null is returned. This
223 * default implementation always returns null, regardless.
225 * @return the numeric value of this object, or null if
226 * no value has been set.
227 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
229 public Number
getCurrentAccessibleValue()
236 * Sets the current value of this accessible object
237 * to that supplied. In this default implementation,
238 * the value is never set and the method always returns
241 * @param number the new accessible value.
242 * @return true if the value was set.
243 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
245 public boolean setCurrentAccessibleValue(Number number
)
252 * Returns the minimum acceptable accessible value used
253 * by this object, or null if no minimum value exists.
254 * This default implementation always returns null.
256 * @return the minimum acceptable accessible value, or null
257 * if there is no minimum.
258 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
260 public Number
getMinimumAccessibleValue()
266 * Returns the maximum acceptable accessible value used
267 * by this object, or null if no maximum value exists.
268 * This default implementation always returns null.
270 * @return the maximum acceptable accessible value, or null
271 * if there is no maximum.
272 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
274 public Number
getMaximumAccessibleValue()
280 * Returns the role of this accessible object.
282 * @return the instance of <code>AccessibleRole</code>,
283 * which describes this object.
284 * @see javax.accessibility.AccessibleRole
286 public AccessibleRole
getAccessibleRole()
288 return AccessibleRole
.CHECK_BOX
;
292 * Returns the state set of this accessible object.
294 * @return a set of <code>AccessibleState</code>s
295 * which represent the current state of the
297 * @see javax.accessibility.AccessibleState
298 * @see javax.accessibility.AccessibleStateSet
300 public AccessibleStateSet
getAccessibleStateSet()
302 AccessibleStateSet set
= super.getAccessibleStateSet();
304 set
.add(AccessibleState
.CHECKED
);
310 /*************************************************************************/
317 * Initializes a new instance of <code>Checkbox</code> with no label,
318 * an initial state of off, and that is not part of any checkbox group.
323 this("", false, null);
326 /*************************************************************************/
329 * Initializes a new instance of <code>Checkbox</code> with the specified
330 * label, an initial state of off, and that is not part of any checkbox
333 * @param label The label for this checkbox.
336 Checkbox(String label
)
338 this(label
, false, null);
341 /*************************************************************************/
344 * Initializes a new instance of <code>Checkbox</code> with the specified
345 * label and initial state, and that is not part of any checkbox
348 * @param label The label for this checkbox.
349 * @param state The initial state of the checkbox, <code>true</code> for
350 * on, <code>false</code> for off.
353 Checkbox(String label
, boolean state
)
355 this(label
, state
, null);
358 /*************************************************************************/
361 * Initializes a new instance of <code>Checkbox</code> with the specified
362 * label, initial state, and checkbox group.
364 * @param label The label for this checkbox.
365 * @param group The checkbox group for this box, or <code>null</code>
366 * if there is no checkbox group.
367 * @param state The initial state of the checkbox, <code>true</code> for
368 * on, <code>false</code> for off.
371 Checkbox(String label
, CheckboxGroup group
, boolean state
)
373 this(label
, state
, group
);
376 /*************************************************************************/
379 * Initializes a new instance of <code>Checkbox</code> with the specified
380 * label, initial state, and checkbox group.
382 * @param label The label for this checkbox.
383 * @param state The initial state of the checkbox, <code>true</code> for
384 * on, <code>false</code> for off.
385 * @param group The checkbox group for this box, or <code>null</code>
386 * if there is no checkbox group.
389 Checkbox(String label
, boolean state
, CheckboxGroup group
)
396 /*************************************************************************/
403 * Returns the label for this checkbox.
405 * @return The label for this checkbox.
413 /*************************************************************************/
416 * Sets the label for this checkbox to the specified value.
418 * @param label The new checkbox label.
420 public synchronized void
421 setLabel(String label
)
426 CheckboxPeer cp
= (CheckboxPeer
) peer
;
431 /*************************************************************************/
434 * Returns the state of this checkbox.
436 * @return The state of this checkbox, which will be <code>true</code> for
437 * on and <code>false</code> for off.
445 /*************************************************************************/
448 * Sets the state of this checkbox to the specified value.
450 * @param state The new state of the checkbox, which will be <code>true</code>
451 * for on or <code>false</code> for off.
453 public synchronized void
454 setState(boolean state
)
459 CheckboxPeer cp
= (CheckboxPeer
) peer
;
464 /*************************************************************************/
467 * Returns an array of length one containing the checkbox label if this
468 * checkbox is selected. Otherwise <code>null</code> is returned.
470 * @return The selection state of this checkbox.
478 Object
[] objs
= new Object
[1];
484 /*************************************************************************/
487 * Returns the checkbox group this object is a member of, if any.
489 * @return This object's checkbox group, of <code>null</code> if it is
490 * not a member of any group.
498 /*************************************************************************/
501 * Sets this object's checkbox group to the specified group.
503 * @param group The new checkbox group, or <code>null</code> to make this
504 * object part of no checkbox group.
506 public synchronized void
507 setCheckboxGroup(CheckboxGroup group
)
512 CheckboxPeer cp
= (CheckboxPeer
) peer
;
513 cp
.setCheckboxGroup (group
);
517 /*************************************************************************/
520 * Creates this object's native peer.
526 peer
= getToolkit ().createCheckbox (this);
530 public ItemListener
[] getItemListeners ()
532 return (ItemListener
[])
533 AWTEventMulticaster
.getListeners (item_listeners
, ItemListener
.class);
537 * Adds a new listeners to the list of registered listeners for this object.
539 * @param listener The new listener to add.
541 public synchronized void
542 addItemListener(ItemListener listener
)
544 item_listeners
= AWTEventMulticaster
.add(item_listeners
, listener
);
547 /*************************************************************************/
550 * Removes a listener from the list of registered listeners for this object.
552 * @param listener The listener to remove.
554 public synchronized void
555 removeItemListener(ItemListener listener
)
557 item_listeners
= AWTEventMulticaster
.remove(item_listeners
, listener
);
560 /*************************************************************************/
563 * Processes this event by calling <code>processItemEvent()</code> if it
564 * is any instance of <code>ItemEvent</code>. Otherwise it is passed to
565 * the superclass for processing.
567 * @param event The event to process.
570 processEvent(AWTEvent event
)
572 if (event
instanceof ItemEvent
)
573 processItemEvent((ItemEvent
)event
);
575 super.processEvent(event
);
578 /*************************************************************************/
581 * Processes this event by dispatching it to any registered listeners.
583 * @param event The <code>ItemEvent</code> to process.
586 processItemEvent(ItemEvent event
)
588 if (item_listeners
!= null)
589 item_listeners
.itemStateChanged(event
);
593 dispatchEventImpl(AWTEvent e
)
595 if (e
.id
<= ItemEvent
.ITEM_LAST
596 && e
.id
>= ItemEvent
.ITEM_FIRST
597 && (item_listeners
!= null
598 || (eventMask
& AWTEvent
.ITEM_EVENT_MASK
) != 0))
601 super.dispatchEventImpl(e
);
604 /*************************************************************************/
607 * Returns a debugging string for this object.
612 return ("label=" + label
+ ",state=" + state
+ ",group=" + group
613 + "," + super.paramString());
617 * Gets the AccessibleContext associated with this <code>Checkbox</code>.
618 * The context is created, if necessary.
620 * @return the associated context
622 public AccessibleContext
getAccessibleContext()
624 /* Create the context if this is the first request */
625 if (accessibleContext
== null)
627 AccessibleAWTCheckbox ac
= new AccessibleAWTCheckbox();
628 accessibleContext
= ac
;
631 return accessibleContext
;
635 * Generate a unique name for this checkbox.
637 * @return A unique name for this checkbox.
639 String
generateName()
641 return "checkbox" + getUniqueLong();
644 private static synchronized long getUniqueLong()
646 return next_checkbox_number
++;