Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / awt / Checkbox.java
blobeea443edf746353a806020a46580ca14e38a25b2
1 /* Checkbox.java -- An AWT checkbox widget
2 Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.awt;
42 import java.awt.event.ItemEvent;
43 import java.awt.event.ItemListener;
44 import java.awt.peer.CheckboxPeer;
45 import java.io.Serializable;
47 import javax.accessibility.Accessible;
48 import javax.accessibility.AccessibleAction;
49 import javax.accessibility.AccessibleContext;
50 import javax.accessibility.AccessibleRole;
51 import javax.accessibility.AccessibleState;
52 import javax.accessibility.AccessibleStateSet;
53 import javax.accessibility.AccessibleValue;
55 /**
56 * This class implements a component which has an on/off state. Two
57 * or more Checkboxes can be grouped by a CheckboxGroup.
59 * @author Aaron M. Renn (arenn@urbanophile.com)
60 * @author Tom Tromey (tromey@redhat.com)
62 public class Checkbox extends Component
63 implements ItemSelectable, Accessible, Serializable
66 // FIXME: Need readObject/writeObject for this.
69 * Static Variables
72 // Serialization Constant
73 private static final long serialVersionUID = 7270714317450821763L;
75 /*************************************************************************/
78 * Instance Variables
81 /**
82 * @serial The checkbox group for this checkbox.
84 private CheckboxGroup group;
86 /**
87 * @serial The label on this checkbox.
89 private String label;
91 /**
92 * @serial The state of this checkbox.
93 * This is package-private to avoid an accessor method.
95 boolean state;
97 // The list of listeners for this object.
98 private transient ItemListener item_listeners;
101 * The number used to generate the name returned by getName.
103 private static transient long next_checkbox_number;
106 * This class provides accessibility support for the
107 * checkbox.
109 * @author Jerry Quinn (jlquinn@optonline.net)
110 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
112 protected class AccessibleAWTCheckbox
113 extends AccessibleAWTComponent
114 implements ItemListener, AccessibleAction, AccessibleValue
117 * Serialization constant to match JDK 1.5
119 private static final long serialVersionUID = 7881579233144754107L;
122 * Default constructor which simply calls the
123 * super class for generic component accessibility
124 * handling.
126 public AccessibleAWTCheckbox()
128 super();
132 * Captures changes to the state of the checkbox and
133 * fires appropriate accessible property change events.
135 * @param event the event fired.
136 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
138 public void itemStateChanged(ItemEvent event)
140 firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
141 state ? null : AccessibleState.CHECKED,
142 state ? AccessibleState.CHECKED : null);
146 * Returns an implementation of the <code>AccessibleAction</code>
147 * interface for this accessible object. In this case, the
148 * current instance is simply returned (with a more appropriate
149 * type), as it also implements the accessible action as well as
150 * the context.
152 * @return the accessible action associated with this context.
153 * @see javax.accessibility.AccessibleAction
155 public AccessibleAction getAccessibleAction()
157 return this;
161 * Returns an implementation of the <code>AccessibleValue</code>
162 * interface for this accessible object. In this case, the
163 * current instance is simply returned (with a more appropriate
164 * type), as it also implements the accessible value as well as
165 * the context.
167 * @return the accessible value associated with this context.
168 * @see javax.accessibility.AccessibleValue
170 public AccessibleValue getAccessibleValue()
172 return this;
176 * The following methods are implemented in the JDK (up to
177 * 1.5) as stubs. We do likewise here.
181 * Returns the number of actions associated with this accessible
182 * object. This default implementation returns 0.
184 * @return the number of accessible actions available.
185 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
187 public int getAccessibleActionCount()
189 // 1.4.1 and 1.5 do this
190 return 0;
194 * Returns a description of the action with the supplied id.
195 * This default implementation always returns null.
197 * @param i the id of the action whose description should be
198 * retrieved.
199 * @return a <code>String</code> describing the action.
200 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
202 public String getAccessibleActionDescription(int i)
204 // 1.5 does this
205 return null;
209 * Executes the action with the specified id. This
210 * default implementation simply returns false.
212 * @param i the id of the action to perform.
213 * @return true if the action was performed.
214 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
216 public boolean doAccessibleAction(int i)
218 // 1.5 does this
219 return false;
223 * Returns the current value of this accessible object.
224 * If no value has been set, null is returned. This
225 * default implementation always returns null, regardless.
227 * @return the numeric value of this object, or null if
228 * no value has been set.
229 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
231 public Number getCurrentAccessibleValue()
233 // 1.5 does this
234 return null;
238 * Sets the current value of this accessible object
239 * to that supplied. In this default implementation,
240 * the value is never set and the method always returns
241 * false.
243 * @param number the new accessible value.
244 * @return true if the value was set.
245 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
247 public boolean setCurrentAccessibleValue(Number number)
249 // 1.5 does this
250 return false;
254 * Returns the minimum acceptable accessible value used
255 * by this object, or null if no minimum value exists.
256 * This default implementation always returns null.
258 * @return the minimum acceptable accessible value, or null
259 * if there is no minimum.
260 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
262 public Number getMinimumAccessibleValue()
264 return null;
268 * Returns the maximum acceptable accessible value used
269 * by this object, or null if no maximum value exists.
270 * This default implementation always returns null.
272 * @return the maximum acceptable accessible value, or null
273 * if there is no maximum.
274 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
276 public Number getMaximumAccessibleValue()
278 return null;
282 * Returns the role of this accessible object.
284 * @return the instance of <code>AccessibleRole</code>,
285 * which describes this object.
286 * @see javax.accessibility.AccessibleRole
288 public AccessibleRole getAccessibleRole()
290 return AccessibleRole.CHECK_BOX;
294 * Returns the state set of this accessible object.
296 * @return a set of <code>AccessibleState</code>s
297 * which represent the current state of the
298 * accessible object.
299 * @see javax.accessibility.AccessibleState
300 * @see javax.accessibility.AccessibleStateSet
302 public AccessibleStateSet getAccessibleStateSet()
304 AccessibleStateSet set = super.getAccessibleStateSet();
305 if (state)
306 set.add(AccessibleState.CHECKED);
307 return set;
312 /*************************************************************************/
315 * Constructors
319 * Initializes a new instance of <code>Checkbox</code> with no label,
320 * an initial state of off, and that is not part of any checkbox group.
322 public
323 Checkbox()
325 this("", false, null);
328 /*************************************************************************/
331 * Initializes a new instance of <code>Checkbox</code> with the specified
332 * label, an initial state of off, and that is not part of any checkbox
333 * group.
335 * @param label The label for this checkbox.
337 public
338 Checkbox(String label)
340 this(label, false, null);
343 /*************************************************************************/
346 * Initializes a new instance of <code>Checkbox</code> with the specified
347 * label and initial state, and that is not part of any checkbox
348 * group.
350 * @param label The label for this checkbox.
351 * @param state The initial state of the checkbox, <code>true</code> for
352 * on, <code>false</code> for off.
354 public
355 Checkbox(String label, boolean state)
357 this(label, state, null);
360 /*************************************************************************/
363 * Initializes a new instance of <code>Checkbox</code> with the specified
364 * label, initial state, and checkbox group.
366 * @param label The label for this checkbox.
367 * @param group The checkbox group for this box, or <code>null</code>
368 * if there is no checkbox group.
369 * @param state The initial state of the checkbox, <code>true</code> for
370 * on, <code>false</code> for off.
372 public
373 Checkbox(String label, CheckboxGroup group, boolean state)
375 this(label, state, group);
378 /*************************************************************************/
381 * Initializes a new instance of <code>Checkbox</code> with the specified
382 * label, initial state, and checkbox group.
384 * @param label The label for this checkbox.
385 * @param state The initial state of the checkbox, <code>true</code> for
386 * on, <code>false</code> for off.
387 * @param group The checkbox group for this box, or <code>null</code>
388 * if there is no checkbox group.
390 public
391 Checkbox(String label, boolean state, CheckboxGroup group)
393 this.label = label;
394 this.state = state;
395 this.group = group;
397 if ( state && group != null )
399 group.setSelectedCheckbox(this);
403 /*************************************************************************/
406 * Instance Variables
410 * Returns the label for this checkbox.
412 * @return The label for this checkbox.
414 public String
415 getLabel()
417 return(label);
420 /*************************************************************************/
423 * Sets the label for this checkbox to the specified value.
425 * @param label The new checkbox label.
427 public synchronized void
428 setLabel(String label)
430 this.label = label;
431 if (peer != null)
433 CheckboxPeer cp = (CheckboxPeer) peer;
434 cp.setLabel(label);
438 /*************************************************************************/
441 * Returns the state of this checkbox.
443 * @return The state of this checkbox, which will be <code>true</code> for
444 * on and <code>false</code> for off.
446 public boolean
447 getState()
449 return(state);
452 /*************************************************************************/
455 * Sets the state of this checkbox to the specified value.
457 * @param state The new state of the checkbox, which will be <code>true</code>
458 * for on or <code>false</code> for off.
460 public synchronized void
461 setState(boolean state)
463 if (this.state != state)
465 this.state = state;
466 if (peer != null)
468 CheckboxPeer cp = (CheckboxPeer) peer;
469 cp.setState (state);
474 /*************************************************************************/
477 * Returns an array of length one containing the checkbox label if this
478 * checkbox is selected. Otherwise <code>null</code> is returned.
480 * @return The selection state of this checkbox.
482 public Object[]
483 getSelectedObjects()
485 if (state == false)
486 return(null);
488 Object[] objs = new Object[1];
489 objs[0] = label;
491 return(objs);
494 /*************************************************************************/
497 * Returns the checkbox group this object is a member of, if any.
499 * @return This object's checkbox group, of <code>null</code> if it is
500 * not a member of any group.
502 public CheckboxGroup
503 getCheckboxGroup()
505 return(group);
508 /*************************************************************************/
511 * Sets this object's checkbox group to the specified group.
513 * @param group The new checkbox group, or <code>null</code> to make this
514 * object part of no checkbox group.
516 public synchronized void
517 setCheckboxGroup(CheckboxGroup group)
519 this.group = group;
520 if (peer != null)
522 CheckboxPeer cp = (CheckboxPeer) peer;
523 cp.setCheckboxGroup (group);
527 /*************************************************************************/
530 * Creates this object's native peer.
532 public void
533 addNotify()
535 if (peer == null)
536 peer = getToolkit ().createCheckbox (this);
537 super.addNotify ();
540 public ItemListener[] getItemListeners ()
542 return (ItemListener[])
543 AWTEventMulticaster.getListeners (item_listeners, ItemListener.class);
547 * Adds a new listeners to the list of registered listeners for this object.
549 * @param listener The new listener to add.
551 public synchronized void
552 addItemListener(ItemListener listener)
554 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
557 /*************************************************************************/
560 * Removes a listener from the list of registered listeners for this object.
562 * @param listener The listener to remove.
564 public synchronized void
565 removeItemListener(ItemListener listener)
567 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
570 /*************************************************************************/
573 * Processes this event by calling <code>processItemEvent()</code> if it
574 * is any instance of <code>ItemEvent</code>. Otherwise it is passed to
575 * the superclass for processing.
577 * @param event The event to process.
579 protected void
580 processEvent(AWTEvent event)
582 if (event instanceof ItemEvent)
583 processItemEvent((ItemEvent)event);
584 else
585 super.processEvent(event);
588 /*************************************************************************/
591 * Processes this event by dispatching it to any registered listeners.
593 * @param event The <code>ItemEvent</code> to process.
595 protected void
596 processItemEvent(ItemEvent event)
598 if (item_listeners != null)
599 item_listeners.itemStateChanged(event);
602 void
603 dispatchEventImpl(AWTEvent e)
605 if (e.id <= ItemEvent.ITEM_LAST
606 && e.id >= ItemEvent.ITEM_FIRST)
608 ItemEvent ie = (ItemEvent) e;
609 int itemState = ie.getStateChange();
610 setState(itemState == ItemEvent.SELECTED ? true : false);
611 if (item_listeners != null
612 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0)
613 processEvent(e);
615 else
616 super.dispatchEventImpl(e);
619 /*************************************************************************/
622 * Returns a debugging string for this object.
624 protected String
625 paramString()
627 // Note: We cannot add the checkbox group information here because this
628 // would trigger infinite recursion when CheckboxGroup.toString() is
629 // called and the box is in its selected state.
630 return ("label=" + label + ",state=" + state + "," + super.paramString());
634 * Gets the AccessibleContext associated with this <code>Checkbox</code>.
635 * The context is created, if necessary.
637 * @return the associated context
639 public AccessibleContext getAccessibleContext()
641 /* Create the context if this is the first request */
642 if (accessibleContext == null)
644 AccessibleAWTCheckbox ac = new AccessibleAWTCheckbox();
645 accessibleContext = ac;
646 addItemListener(ac);
648 return accessibleContext;
652 * Generate a unique name for this checkbox.
654 * @return A unique name for this checkbox.
656 String generateName()
658 return "checkbox" + getUniqueLong();
661 private static synchronized long getUniqueLong()
663 return next_checkbox_number++;