Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / java / awt / Checkbox.java
blob93f6092472391814c05ffe80994fc10cb09f49f5
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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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.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;
54 /**
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.
68 * Static Variables
71 // Serialization Constant
72 private static final long serialVersionUID = 7270714317450821763L;
74 /*************************************************************************/
77 * Instance Variables
80 /**
81 * @serial The checkbox group for this checkbox.
83 private CheckboxGroup group;
85 /**
86 * @serial The label on this checkbox.
88 private String label;
90 /**
91 * @serial The state of this checkbox.
92 * This is package-private to avoid an accessor method.
94 boolean state;
96 // The list of listeners for this object.
97 private transient ItemListener item_listeners;
100 * The number used to generate the name returned by getName.
102 private static transient long next_checkbox_number;
105 * This class provides accessibility support for the
106 * checkbox.
108 * @author Jerry Quinn (jlquinn@optonline.net)
109 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
111 protected class AccessibleAWTCheckbox
112 extends AccessibleAWTComponent
113 implements ItemListener, AccessibleAction, AccessibleValue
116 * Serialization constant to match JDK 1.5
118 private static final long serialVersionUID = 7881579233144754107L;
121 * Default constructor which simply calls the
122 * super class for generic component accessibility
123 * handling.
125 public AccessibleAWTCheckbox()
127 super();
131 * Captures changes to the state of the checkbox and
132 * fires appropriate accessible property change events.
134 * @param event the event fired.
135 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
137 public void itemStateChanged(ItemEvent event)
139 firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
140 state ? null : AccessibleState.CHECKED,
141 state ? AccessibleState.CHECKED : null);
145 * Returns an implementation of the <code>AccessibleAction</code>
146 * interface for this accessible object. In this case, the
147 * current instance is simply returned (with a more appropriate
148 * type), as it also implements the accessible action as well as
149 * the context.
151 * @return the accessible action associated with this context.
152 * @see javax.accessibility.AccessibleAction
154 public AccessibleAction getAccessibleAction()
156 return this;
160 * Returns an implementation of the <code>AccessibleValue</code>
161 * interface for this accessible object. In this case, the
162 * current instance is simply returned (with a more appropriate
163 * type), as it also implements the accessible value as well as
164 * the context.
166 * @return the accessible value associated with this context.
167 * @see javax.accessibility.AccessibleValue
169 public AccessibleValue getAccessibleValue()
171 return this;
175 * The following methods are implemented in the JDK (up to
176 * 1.5) as stubs. We do likewise here.
180 * Returns the number of actions associated with this accessible
181 * object. This default implementation returns 0.
183 * @return the number of accessible actions available.
184 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
186 public int getAccessibleActionCount()
188 // 1.4.1 and 1.5 do this
189 return 0;
193 * Returns a description of the action with the supplied id.
194 * This default implementation always returns null.
196 * @param i the id of the action whose description should be
197 * retrieved.
198 * @return a <code>String</code> describing the action.
199 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
201 public String getAccessibleActionDescription(int i)
203 // 1.5 does this
204 return null;
208 * Executes the action with the specified id. This
209 * default implementation simply returns false.
211 * @param i the id of the action to perform.
212 * @return true if the action was performed.
213 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
215 public boolean doAccessibleAction(int i)
217 // 1.5 does this
218 return false;
222 * Returns the current value of this accessible object.
223 * If no value has been set, null is returned. This
224 * default implementation always returns null, regardless.
226 * @return the numeric value of this object, or null if
227 * no value has been set.
228 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
230 public Number getCurrentAccessibleValue()
232 // 1.5 does this
233 return null;
237 * Sets the current value of this accessible object
238 * to that supplied. In this default implementation,
239 * the value is never set and the method always returns
240 * false.
242 * @param number the new accessible value.
243 * @return true if the value was set.
244 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
246 public boolean setCurrentAccessibleValue(Number number)
248 // 1.5 does this
249 return false;
253 * Returns the minimum acceptable accessible value used
254 * by this object, or null if no minimum value exists.
255 * This default implementation always returns null.
257 * @return the minimum acceptable accessible value, or null
258 * if there is no minimum.
259 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
261 public Number getMinimumAccessibleValue()
263 return null;
267 * Returns the maximum acceptable accessible value used
268 * by this object, or null if no maximum value exists.
269 * This default implementation always returns null.
271 * @return the maximum acceptable accessible value, or null
272 * if there is no maximum.
273 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
275 public Number getMaximumAccessibleValue()
277 return null;
281 * Returns the role of this accessible object.
283 * @return the instance of <code>AccessibleRole</code>,
284 * which describes this object.
285 * @see javax.accessibility.AccessibleRole
287 public AccessibleRole getAccessibleRole()
289 return AccessibleRole.CHECK_BOX;
293 * Returns the state set of this accessible object.
295 * @return a set of <code>AccessibleState</code>s
296 * which represent the current state of the
297 * accessible object.
298 * @see javax.accessibility.AccessibleState
299 * @see javax.accessibility.AccessibleStateSet
301 public AccessibleStateSet getAccessibleStateSet()
303 AccessibleStateSet set = super.getAccessibleStateSet();
304 if (state)
305 set.add(AccessibleState.CHECKED);
306 return set;
311 /*************************************************************************/
314 * Constructors
318 * Initializes a new instance of <code>Checkbox</code> with no label,
319 * an initial state of off, and that is not part of any checkbox group.
321 public
322 Checkbox()
324 this("", false, null);
327 /*************************************************************************/
330 * Initializes a new instance of <code>Checkbox</code> with the specified
331 * label, an initial state of off, and that is not part of any checkbox
332 * group.
334 * @param label The label for this checkbox.
336 public
337 Checkbox(String label)
339 this(label, false, null);
342 /*************************************************************************/
345 * Initializes a new instance of <code>Checkbox</code> with the specified
346 * label and initial state, and that is not part of any checkbox
347 * group.
349 * @param label The label for this checkbox.
350 * @param state The initial state of the checkbox, <code>true</code> for
351 * on, <code>false</code> for off.
353 public
354 Checkbox(String label, boolean state)
356 this(label, state, null);
359 /*************************************************************************/
362 * Initializes a new instance of <code>Checkbox</code> with the specified
363 * label, initial state, and checkbox group.
365 * @param label The label for this checkbox.
366 * @param group The checkbox group for this box, or <code>null</code>
367 * if there is no checkbox group.
368 * @param state The initial state of the checkbox, <code>true</code> for
369 * on, <code>false</code> for off.
371 public
372 Checkbox(String label, CheckboxGroup group, boolean state)
374 this(label, state, group);
377 /*************************************************************************/
380 * Initializes a new instance of <code>Checkbox</code> with the specified
381 * label, initial state, and checkbox group.
383 * @param label The label for this checkbox.
384 * @param state The initial state of the checkbox, <code>true</code> for
385 * on, <code>false</code> for off.
386 * @param group The checkbox group for this box, or <code>null</code>
387 * if there is no checkbox group.
389 public
390 Checkbox(String label, boolean state, CheckboxGroup group)
392 this.label = label;
393 this.state = state;
394 this.group = group;
396 if ( state && group != null )
398 group.setSelectedCheckbox(this);
402 /*************************************************************************/
405 * Instance Variables
409 * Returns the label for this checkbox.
411 * @return The label for this checkbox.
413 public String
414 getLabel()
416 return(label);
419 /*************************************************************************/
422 * Sets the label for this checkbox to the specified value.
424 * @param label The new checkbox label.
426 public synchronized void
427 setLabel(String label)
429 this.label = label;
430 if (peer != null)
432 CheckboxPeer cp = (CheckboxPeer) peer;
433 cp.setLabel(label);
437 /*************************************************************************/
440 * Returns the state of this checkbox.
442 * @return The state of this checkbox, which will be <code>true</code> for
443 * on and <code>false</code> for off.
445 public boolean
446 getState()
448 return(state);
451 /*************************************************************************/
454 * Sets the state of this checkbox to the specified value.
456 * @param state The new state of the checkbox, which will be <code>true</code>
457 * for on or <code>false</code> for off.
459 public synchronized void
460 setState(boolean state)
462 this.state = state;
463 if (peer != null)
465 CheckboxPeer cp = (CheckboxPeer) peer;
466 cp.setState (state);
470 /*************************************************************************/
473 * Returns an array of length one containing the checkbox label if this
474 * checkbox is selected. Otherwise <code>null</code> is returned.
476 * @return The selection state of this checkbox.
478 public Object[]
479 getSelectedObjects()
481 if (state == false)
482 return(null);
484 Object[] objs = new Object[1];
485 objs[0] = label;
487 return(objs);
490 /*************************************************************************/
493 * Returns the checkbox group this object is a member of, if any.
495 * @return This object's checkbox group, of <code>null</code> if it is
496 * not a member of any group.
498 public CheckboxGroup
499 getCheckboxGroup()
501 return(group);
504 /*************************************************************************/
507 * Sets this object's checkbox group to the specified group.
509 * @param group The new checkbox group, or <code>null</code> to make this
510 * object part of no checkbox group.
512 public synchronized void
513 setCheckboxGroup(CheckboxGroup group)
515 this.group = group;
516 if (peer != null)
518 CheckboxPeer cp = (CheckboxPeer) peer;
519 cp.setCheckboxGroup (group);
523 /*************************************************************************/
526 * Creates this object's native peer.
528 public void
529 addNotify()
531 if (peer == null)
532 peer = getToolkit ().createCheckbox (this);
533 super.addNotify ();
536 public ItemListener[] getItemListeners ()
538 return (ItemListener[])
539 AWTEventMulticaster.getListeners (item_listeners, ItemListener.class);
543 * Adds a new listeners to the list of registered listeners for this object.
545 * @param listener The new listener to add.
547 public synchronized void
548 addItemListener(ItemListener listener)
550 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
553 /*************************************************************************/
556 * Removes a listener from the list of registered listeners for this object.
558 * @param listener The listener to remove.
560 public synchronized void
561 removeItemListener(ItemListener listener)
563 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
566 /*************************************************************************/
569 * Processes this event by calling <code>processItemEvent()</code> if it
570 * is any instance of <code>ItemEvent</code>. Otherwise it is passed to
571 * the superclass for processing.
573 * @param event The event to process.
575 protected void
576 processEvent(AWTEvent event)
578 if (event instanceof ItemEvent)
579 processItemEvent((ItemEvent)event);
580 else
581 super.processEvent(event);
584 /*************************************************************************/
587 * Processes this event by dispatching it to any registered listeners.
589 * @param event The <code>ItemEvent</code> to process.
591 protected void
592 processItemEvent(ItemEvent event)
594 if (item_listeners != null)
595 item_listeners.itemStateChanged(event);
598 void
599 dispatchEventImpl(AWTEvent e)
601 if (e.id <= ItemEvent.ITEM_LAST
602 && e.id >= ItemEvent.ITEM_FIRST
603 && (item_listeners != null
604 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
605 processEvent(e);
606 else
607 super.dispatchEventImpl(e);
610 /*************************************************************************/
613 * Returns a debugging string for this object.
615 protected String
616 paramString()
618 // Note: We cannot add the checkbox group information here because this
619 // would trigger infinite recursion when CheckboxGroup.toString() is
620 // called and the box is in its selected state.
621 return ("label=" + label + ",state=" + state + "," + super.paramString());
625 * Gets the AccessibleContext associated with this <code>Checkbox</code>.
626 * The context is created, if necessary.
628 * @return the associated context
630 public AccessibleContext getAccessibleContext()
632 /* Create the context if this is the first request */
633 if (accessibleContext == null)
635 AccessibleAWTCheckbox ac = new AccessibleAWTCheckbox();
636 accessibleContext = ac;
637 addItemListener(ac);
639 return accessibleContext;
643 * Generate a unique name for this checkbox.
645 * @return A unique name for this checkbox.
647 String generateName()
649 return "checkbox" + getUniqueLong();
652 private static synchronized long getUniqueLong()
654 return next_checkbox_number++;