Checkbox.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Checkbox.java -- An AWT checkbox widget
  2. Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.peer.CheckboxPeer;
  33. import java.awt.peer.ComponentPeer;
  34. import java.awt.event.ItemEvent;
  35. import java.awt.event.ItemListener;
  36. import java.io.Serializable;
  37. /**
  38. * This class implements a component which has an on/off state. Two
  39. * or more Checkboxes can be grouped by a CheckboxGroup.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. * @author Tom Tromey <tromey@redhat.com>
  43. */
  44. public class Checkbox extends Component implements ItemSelectable, Serializable
  45. {
  46. // FIXME: Need readObject/writeObject for this.
  47. /*
  48. * Static Variables
  49. */
  50. // Serialization Constant
  51. private static final long serialVersionUID = 7270714317450821763L;
  52. /*************************************************************************/
  53. /*
  54. * Instance Variables
  55. */
  56. /**
  57. * @serial The checkbox group for this checkbox.
  58. */
  59. private CheckboxGroup group;
  60. /**
  61. * @serial The label on this checkbox.
  62. */
  63. private String label;
  64. /**
  65. * @serial The state of this checkbox.
  66. */
  67. private boolean state;
  68. // The list of listeners for this object.
  69. private transient ItemListener item_listeners;
  70. /*************************************************************************/
  71. /*
  72. * Constructors
  73. */
  74. /**
  75. * Initializes a new instance of <code>Checkbox</code> with no label,
  76. * an initial state of off, and that is not part of any checkbox group.
  77. */
  78. public
  79. Checkbox()
  80. {
  81. this("", false, null);
  82. }
  83. /*************************************************************************/
  84. /**
  85. * Initializes a new instance of <code>Checkbox</code> with the specified
  86. * label, an initial state of off, and that is not part of any checkbox
  87. * group.
  88. *
  89. * @param label The label for this checkbox.
  90. */
  91. public
  92. Checkbox(String label)
  93. {
  94. this(label, false, null);
  95. }
  96. /*************************************************************************/
  97. /**
  98. * Initializes a new instance of <code>Checkbox</code> with the specified
  99. * label and initial state, and that is not part of any checkbox
  100. * group.
  101. *
  102. * @param label The label for this checkbox.
  103. * @param state The initial state of the checkbox, <code>true</code> for
  104. * on, <code>false</code> for off.
  105. */
  106. public
  107. Checkbox(String label, boolean state)
  108. {
  109. this(label, state, null);
  110. }
  111. /*************************************************************************/
  112. /**
  113. * Initializes a new instance of <code>Checkbox</code> with the specified
  114. * label, initial state, and checkbox group.
  115. *
  116. * @param label The label for this checkbox.
  117. * @param group The checkbox group for this box, or <code>null</code>
  118. * if there is no checkbox group.
  119. * @param state The initial state of the checkbox, <code>true</code> for
  120. * on, <code>false</code> for off.
  121. */
  122. public
  123. Checkbox(String label, CheckboxGroup group, boolean state)
  124. {
  125. this(label, state, group);
  126. }
  127. /*************************************************************************/
  128. /**
  129. * Initializes a new instance of <code>Checkbox</code> with the specified
  130. * label, initial state, and checkbox group.
  131. *
  132. * @param label The label for this checkbox.
  133. * @param state The initial state of the checkbox, <code>true</code> for
  134. * on, <code>false</code> for off.
  135. * @param group The checkbox group for this box, or <code>null</code>
  136. * if there is no checkbox group.
  137. */
  138. public
  139. Checkbox(String label, boolean state, CheckboxGroup group)
  140. {
  141. this.label = label;
  142. this.state = state;
  143. this.group = group;
  144. }
  145. /*************************************************************************/
  146. /*
  147. * Instance Variables
  148. */
  149. /**
  150. * Returns the label for this checkbox.
  151. *
  152. * @return The label for this checkbox.
  153. */
  154. public String
  155. getLabel()
  156. {
  157. return(label);
  158. }
  159. /*************************************************************************/
  160. /**
  161. * Sets the label for this checkbox to the specified value.
  162. *
  163. * @param label The new checkbox label.
  164. */
  165. public synchronized void
  166. setLabel(String label)
  167. {
  168. this.label = label;
  169. if (peer != null)
  170. {
  171. CheckboxPeer cp = (CheckboxPeer) peer;
  172. cp.setLabel(label);
  173. }
  174. }
  175. /*************************************************************************/
  176. /**
  177. * Returns the state of this checkbox.
  178. *
  179. * @return The state of this checkbox, which will be <code>true</code> for
  180. * on and <code>false</code> for off.
  181. */
  182. public boolean
  183. getState()
  184. {
  185. return(state);
  186. }
  187. /*************************************************************************/
  188. /**
  189. * Sets the state of this checkbox to the specified value.
  190. *
  191. * @param state The new state of the checkbox, which will be <code>true</code>
  192. * for on or <code>false</code> for off.
  193. */
  194. public synchronized void
  195. setState(boolean state)
  196. {
  197. this.state = state;
  198. if (peer != null)
  199. {
  200. CheckboxPeer cp = (CheckboxPeer) peer;
  201. cp.setState (state);
  202. }
  203. }
  204. /*************************************************************************/
  205. /**
  206. * Returns an array of length one containing the checkbox label if this
  207. * checkbox is selected. Otherwise <code>null</code> is returned.
  208. *
  209. * @return The selection state of this checkbox.
  210. */
  211. public Object[]
  212. getSelectedObjects()
  213. {
  214. if (state == false)
  215. return(null);
  216. Object[] objs = new Object[1];
  217. objs[0] = label;
  218. return(objs);
  219. }
  220. /*************************************************************************/
  221. /**
  222. * Returns the checkbox group this object is a member of, if any.
  223. *
  224. * @return This object's checkbox group, of <code>null</code> if it is
  225. * not a member of any group.
  226. */
  227. public CheckboxGroup
  228. getCheckboxGroup()
  229. {
  230. return(group);
  231. }
  232. /*************************************************************************/
  233. /**
  234. * Sets this object's checkbox group to the specified group.
  235. *
  236. * @param group The new checkbox group, or <code>null</code> to make this
  237. * object part of no checkbox group.
  238. */
  239. public synchronized void
  240. setCheckboxGroup(CheckboxGroup group)
  241. {
  242. this.group = group;
  243. if (peer != null)
  244. {
  245. CheckboxPeer cp = (CheckboxPeer) peer;
  246. cp.setCheckboxGroup (group);
  247. }
  248. }
  249. /*************************************************************************/
  250. /**
  251. * Creates this object's native peer.
  252. */
  253. public void
  254. addNotify()
  255. {
  256. if (peer == null)
  257. peer = getToolkit ().createCheckbox (this);
  258. super.addNotify ();
  259. }
  260. /*************************************************************************/
  261. /**
  262. * Adds a new listeners to the list of registered listeners for this object.
  263. *
  264. * @param listener The new listener to add.
  265. */
  266. public synchronized void
  267. addItemListener(ItemListener listener)
  268. {
  269. item_listeners = AWTEventMulticaster.add(item_listeners, listener);
  270. }
  271. /*************************************************************************/
  272. /**
  273. * Removes a listener from the list of registered listeners for this object.
  274. *
  275. * @param listener The listener to remove.
  276. */
  277. public synchronized void
  278. removeItemListener(ItemListener listener)
  279. {
  280. item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
  281. }
  282. /*************************************************************************/
  283. /**
  284. * Processes this event by calling <code>processItemEvent()</code> if it
  285. * is any instance of <code>ItemEvent</code>. Otherwise it is passed to
  286. * the superclass for processing.
  287. *
  288. * @param event The event to process.
  289. */
  290. protected void
  291. processEvent(AWTEvent event)
  292. {
  293. if (event instanceof ItemEvent)
  294. processItemEvent((ItemEvent)event);
  295. else
  296. super.processEvent(event);
  297. }
  298. /*************************************************************************/
  299. /**
  300. * Processes this event by dispatching it to any registered listeners.
  301. *
  302. * @param event The <code>ItemEvent</code> to process.
  303. */
  304. protected void
  305. processItemEvent(ItemEvent event)
  306. {
  307. if (item_listeners != null)
  308. item_listeners.itemStateChanged(event);
  309. }
  310. void
  311. dispatchEventImpl(AWTEvent e)
  312. {
  313. if (e.id <= ItemEvent.ITEM_LAST
  314. && e.id >= ItemEvent.ITEM_FIRST
  315. && (item_listeners != null
  316. || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
  317. processEvent(e);
  318. else
  319. super.dispatchEventImpl(e);
  320. }
  321. /*************************************************************************/
  322. /**
  323. * Returns a debugging string for this object.
  324. */
  325. protected String
  326. paramString()
  327. {
  328. return ("label=" + label + ",state=" + state + ",group=" + group
  329. + "," + super.paramString());
  330. }
  331. } // class Checkbox