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