Button.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* Button.java -- AWT button widget
  2. Copyright (C) 1999, 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.peer.ButtonPeer;
  35. import java.lang.reflect.Array;
  36. import java.util.EventListener;
  37. import javax.accessibility.Accessible;
  38. import javax.accessibility.AccessibleAction;
  39. import javax.accessibility.AccessibleContext;
  40. import javax.accessibility.AccessibleRole;
  41. import javax.accessibility.AccessibleValue;
  42. /**
  43. * This class provides a button widget for the AWT.
  44. *
  45. * @author Aaron M. Renn (arenn@urbanophile.com)
  46. * @author Tom Tromey (tromey@cygnus.com)
  47. */
  48. public class Button extends Component
  49. implements java.io.Serializable, Accessible
  50. {
  51. /*
  52. * Static Variables
  53. */
  54. // FIXME: Need readObject/writeObject for serialization
  55. // Serialization version constant
  56. private static final long serialVersionUID = -8774683716313001058L;
  57. /*************************************************************************/
  58. /*
  59. * Instance Variables
  60. */
  61. /**
  62. * @serial The action command name for this button.
  63. * This is package-private to avoid an accessor method.
  64. */
  65. String actionCommand;
  66. /**
  67. * @serial The label for this button.
  68. * This is package-private to avoid an accessor method.
  69. */
  70. String label;
  71. // List of ActionListeners for this class.
  72. private transient ActionListener action_listeners;
  73. /*
  74. * The number used to generate the name returned by getName.
  75. */
  76. private static transient long next_button_number;
  77. protected class AccessibleAWTButton extends AccessibleAWTComponent
  78. implements AccessibleAction, AccessibleValue
  79. {
  80. private static final long serialVersionUID = -5932203980244017102L;
  81. protected AccessibleAWTButton()
  82. {
  83. // Do nothing here.
  84. }
  85. /* (non-Javadoc)
  86. * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
  87. */
  88. public int getAccessibleActionCount()
  89. {
  90. // Only 1 action possible
  91. return 1;
  92. }
  93. /* (non-Javadoc)
  94. * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
  95. */
  96. public String getAccessibleActionDescription(int i)
  97. {
  98. // JDK 1.4.2 returns the string "click" for action 0. However, the API
  99. // docs don't say what the string to be returned is, beyond being a
  100. // description of the action. So we return the same thing for
  101. // compatibility with 1.4.2.
  102. if (i == 0)
  103. return "click";
  104. return null;
  105. }
  106. /* (non-Javadoc)
  107. * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
  108. */
  109. public boolean doAccessibleAction(int i)
  110. {
  111. if (i != 0)
  112. return false;
  113. processActionEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
  114. return true;
  115. }
  116. public String getAccessibleName()
  117. {
  118. return label;
  119. }
  120. public AccessibleAction getAccessibleAction()
  121. {
  122. return this;
  123. }
  124. public AccessibleValue getAccessibleValue()
  125. {
  126. return this;
  127. }
  128. /* (non-Javadoc)
  129. * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
  130. */
  131. public Number getCurrentAccessibleValue()
  132. {
  133. // Docs say return 1 if selected, but buttons can't be selected, right?
  134. return new Integer(0);
  135. }
  136. /* (non-Javadoc)
  137. * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
  138. */
  139. public boolean setCurrentAccessibleValue(Number number)
  140. {
  141. // Since there's no selection with buttons, we're ignoring this.
  142. // TODO someone who knows shoulw check this.
  143. return false;
  144. }
  145. /* (non-Javadoc)
  146. * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
  147. */
  148. public Number getMinimumAccessibleValue()
  149. {
  150. return new Integer(0);
  151. }
  152. /* (non-Javadoc)
  153. * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
  154. */
  155. public Number getMaximumAccessibleValue()
  156. {
  157. return new Integer(0);
  158. }
  159. public AccessibleRole getAccessibleRole()
  160. {
  161. return AccessibleRole.PUSH_BUTTON;
  162. }
  163. }
  164. /*************************************************************************/
  165. /*
  166. * Constructors
  167. */
  168. /**
  169. * Initializes a new instance of <code>Button</code> with no label.
  170. *
  171. * @exception HeadlessException If GraphicsEnvironment.isHeadless()
  172. * returns true
  173. */
  174. public
  175. Button()
  176. {
  177. this("");
  178. }
  179. /*************************************************************************/
  180. /**
  181. * Initializes a new instance of <code>Button</code> with the specified
  182. * label. The action command name is also initialized to this value.
  183. *
  184. * @param label The label to display on the button.
  185. *
  186. * @exception HeadlessException If GraphicsEnvironment.isHeadless()
  187. * returns true
  188. */
  189. public
  190. Button(String label)
  191. {
  192. this.label = label;
  193. actionCommand = label;
  194. if (GraphicsEnvironment.isHeadless ())
  195. throw new HeadlessException ();
  196. }
  197. /*************************************************************************/
  198. /*
  199. * Instance Variables
  200. */
  201. /**
  202. * Returns the label for this button.
  203. *
  204. * @return The label for this button.
  205. */
  206. public String
  207. getLabel()
  208. {
  209. return(label);
  210. }
  211. /*************************************************************************/
  212. /**
  213. * Sets the label for this button to the specified value.
  214. *
  215. * @param label The new label for this button.
  216. */
  217. public synchronized void
  218. setLabel(String label)
  219. {
  220. this.label = label;
  221. actionCommand = label;
  222. if (peer != null)
  223. {
  224. ButtonPeer bp = (ButtonPeer) peer;
  225. bp.setLabel (label);
  226. }
  227. }
  228. /*************************************************************************/
  229. /**
  230. * Returns the action command name for this button.
  231. *
  232. * @return The action command name for this button.
  233. */
  234. public String
  235. getActionCommand()
  236. {
  237. return(actionCommand);
  238. }
  239. /*************************************************************************/
  240. /**
  241. * Sets the action command name for this button to the specified value.
  242. *
  243. * @param actionCommand The new action command name.
  244. */
  245. public void
  246. setActionCommand(String actionCommand)
  247. {
  248. this.actionCommand = actionCommand == null ? label : actionCommand;
  249. }
  250. /*************************************************************************/
  251. /**
  252. * Adds a new entry to the list of listeners that will receive
  253. * action events from this button.
  254. *
  255. * @param listener The listener to add.
  256. */
  257. public synchronized void
  258. addActionListener(ActionListener listener)
  259. {
  260. action_listeners = AWTEventMulticaster.add(action_listeners, listener);
  261. }
  262. /*************************************************************************/
  263. /**
  264. * Removes the specified listener from the list of listeners that will
  265. * receive action events from this button.
  266. *
  267. * @param listener The listener to remove.
  268. */
  269. public synchronized void
  270. removeActionListener(ActionListener listener)
  271. {
  272. action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
  273. }
  274. /**
  275. * Returns all added <code>ActionListener</code> objects.
  276. *
  277. * @return an array of listeners
  278. *
  279. * @since 1.4
  280. */
  281. public synchronized ActionListener[] getActionListeners()
  282. {
  283. return (ActionListener[])
  284. AWTEventMulticaster.getListeners(action_listeners,
  285. ActionListener.class);
  286. }
  287. /**
  288. * Returns all registered EventListers of the given listenerType.
  289. * listenerType must be a subclass of EventListener, or a
  290. * ClassClassException is thrown.
  291. *
  292. * @param listenerType the listener type to return
  293. *
  294. * @return an array of listeners
  295. *
  296. * @exception ClassCastException If listenerType doesn't specify a class or
  297. * interface that implements @see java.util.EventListener.
  298. *
  299. * @since 1.3
  300. */
  301. public <T extends EventListener> T[] getListeners(Class<T> listenerType)
  302. {
  303. if (listenerType == ActionListener.class)
  304. return (T[]) getActionListeners();
  305. return (T[]) Array.newInstance(listenerType, 0);
  306. }
  307. /*************************************************************************/
  308. /**
  309. * Notifies this button that it should create its native peer object.
  310. */
  311. public void
  312. addNotify()
  313. {
  314. if (peer == null)
  315. peer = getToolkit ().createButton (this);
  316. super.addNotify();
  317. }
  318. /*************************************************************************/
  319. /**
  320. * Processes an event for this button. If the specified event is an
  321. * instance of <code>ActionEvent</code>, then the
  322. * <code>processActionEvent()</code> method is called to dispatch it
  323. * to any registered listeners. Otherwise, the superclass method
  324. * will be invoked. Note that this method will not be called at all
  325. * unless <code>ActionEvent</code>'s are enabled. This will be done
  326. * implicitly if any listeners are added.
  327. *
  328. * @param event The event to process.
  329. */
  330. protected void
  331. processEvent(AWTEvent event)
  332. {
  333. if (event instanceof ActionEvent)
  334. processActionEvent((ActionEvent)event);
  335. else
  336. super.processEvent(event);
  337. }
  338. /*************************************************************************/
  339. /**
  340. * This method dispatches an action event for this button to any
  341. * registered listeners.
  342. *
  343. * @param event The event to process.
  344. */
  345. protected void
  346. processActionEvent(ActionEvent event)
  347. {
  348. if (action_listeners != null)
  349. action_listeners.actionPerformed(event);
  350. }
  351. void
  352. dispatchEventImpl(AWTEvent e)
  353. {
  354. if (e.id <= ActionEvent.ACTION_LAST
  355. && e.id >= ActionEvent.ACTION_FIRST
  356. && (action_listeners != null
  357. || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
  358. processEvent(e);
  359. else
  360. super.dispatchEventImpl(e);
  361. }
  362. /*************************************************************************/
  363. /**
  364. * Returns a debugging string for this button.
  365. *
  366. * @return A debugging string for this button.
  367. */
  368. protected String
  369. paramString()
  370. {
  371. return getName () + "," + getX () + "," + getY () + ","
  372. + getWidth () + "x" + getHeight () + ",label=" + getLabel ();
  373. }
  374. /**
  375. * Gets the AccessibleContext associated with this <code>Button</code>.
  376. * The context is created, if necessary.
  377. *
  378. * @return the associated context
  379. */
  380. public AccessibleContext getAccessibleContext()
  381. {
  382. /* Create the context if this is the first request */
  383. if (accessibleContext == null)
  384. accessibleContext = new AccessibleAWTButton();
  385. return accessibleContext;
  386. }
  387. /**
  388. * Generate a unique name for this button.
  389. *
  390. * @return A unique name for this button.
  391. */
  392. String generateName ()
  393. {
  394. return "button" + getUniqueLong ();
  395. }
  396. private static synchronized long getUniqueLong ()
  397. {
  398. return next_button_number++;
  399. }
  400. } // class Button