aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/awt/Checkbox.java
blob: 10c6744376ccb6eabf493b8b387b656f579d48dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.awt;
import java.awt.event.*;
import java.awt.peer.CheckboxPeer;

/** This class implements a component which has an on/off state.  Two
 * or more Checkboxes can be grouped by a CheckboxGroup.
 * @author Tom Tromey <tromey@redhat.com>
 * @date December 25, 2000
 */
public class Checkbox extends Component implements ItemSelectable
{
  /** Create a new checkbox.
   * @param label The checkbox label.  A null value is the same as "";
   *              this is the default.
   * @param state The initial check state; defaults to false.
   * @param group The CheckboxGroup.  Defaults to null.
   */
  public Checkbox ()
  {
    this (null, null, false);
  }

  public Checkbox (String label)
  {
    this (label, null, false);
  }

  public Checkbox (String label, boolean state)
  {
    this (label, null, state);
  }

  public Checkbox (String label, boolean state, CheckboxGroup group)
  {
    this (label, group, state);
  }

  public Checkbox (String label, CheckboxGroup group, boolean state)
  {
    this.label = label;
    this.group = group;
    this.state = state;
  }

  /** Add a listener for item events.
   * @param listener The listener to add.
   */
  public synchronized void addItemListener (ItemListener listener)
  {
    listeners = AWTEventMulticaster.add (listeners, listener);
  }

  /** This creates the component's peer.  */
  public void addNotify ()
  {
    if (peer == null)
      peer = getToolkit ().createCheckbox (this);
  }

  /** Returns the current CheckboxGroup associated with this
   * Checkbox.  */
  public CheckboxGroup getCheckboxGroup ()
  {
    return group;
  }

  /** Returns the current label; might be null.  */
  public String getLabel ()
  {
    return label;
  }

  /** Returns this checkbox's label if this checkbox is selected.  */
  public Object[] getSelectedObjects ()
  {
    Object[] r;
    if (state)
      {
	r = new Object[1];
	r[0] = label;
      }
    else
      r = new Object[0];
    return r;
  }

  /** Returns the current state of this checkbox.  */
  public boolean getState ()
  {
    return state;
  }

  /** Generates a String representation of this Checkbox's state.  */
  protected String paramString ()
  {
    return ("Checkbox["
	    + "state=" + state + ","
	    + "label=" + label + ","
	    + "group=" + group + "]");
  }

  /** Process an event for this Checkbox.
   * @param event The event the process.
   */
  protected void processEvent (AWTEvent event) 
  {
    if (event instanceof ItemEvent)
      processItemEvent ((ItemEvent) event);
    else
      super.processEvent (event);
  }

  /** Process an item event for this Checkbox.
   * @param event The ItemEvent to process
   */
  protected void processItemEvent (ItemEvent event)
  {
    if (listeners != null)
      listeners.itemStateChanged (event);
  }

  /** Remove an item listener.
   * @param listener Item listener to remove.
   */
  public synchronized void removeItemListener (ItemListener listener)
  {
    listeners = AWTEventMulticaster.remove (listeners, listener);
  }

  /** Set this checkbox's group.
   * @param group The new group.  null means remove the Checkbox from
   *              its group.
   */
  public void setCheckboxGroup (CheckboxGroup group)
  {
    this.group = group;
    if (peer != null)
      {
	CheckboxPeer cp = (CheckboxPeer) peer;
	cp.setCheckboxGroup (group);
      }
  }

  /** Set the checkbox's label.
   * @param label The new label
   */
  public synchronized void setLabel (String label)
  {
    this.label = label;
    if (peer != null)
      {
	CheckboxPeer cp = (CheckboxPeer) peer;
	// FIXME: unclear what to do here; we err on the side of
	// caution.
	cp.setLabel (label == null ? "" : label);
      }
  }

  /** Set the checkbox's state.
   * @param state The new state.
   */
  public void setState (boolean state)
  {
    this.state = state;
    if (peer != null)
      {
	CheckboxPeer cp = (CheckboxPeer) peer;
	cp.setState (state);
      }
  }

  private ItemListener listeners;

  String label;
  CheckboxGroup group;
  boolean state;
}