TextField.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* TextField.java -- A one line text entry field
  2. Copyright (C) 1999, 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.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.peer.TextFieldPeer;
  35. import java.awt.peer.TextComponentPeer;
  36. import java.awt.peer.ComponentPeer;
  37. /**
  38. * This class implements a single line text entry field widget
  39. *
  40. * @author Aaron M. Renn (arenn@urbanophile.com)
  41. */
  42. public class TextField extends TextComponent implements java.io.Serializable
  43. {
  44. /*
  45. * Static Variables
  46. */
  47. // Serialization constant
  48. private static final long serialVersionUID = -2966288784432217853L;
  49. /*************************************************************************/
  50. /*
  51. * Instance Variables
  52. */
  53. /**
  54. * @serial The number of columns in the text entry field.
  55. */
  56. private int columns;
  57. /**
  58. * @serial The character that is echoed when doing protected input
  59. */
  60. private char echoChar;
  61. // List of registered ActionListener's for this object.
  62. private ActionListener action_listeners;
  63. /*************************************************************************/
  64. /*
  65. * Constructors
  66. */
  67. /**
  68. * Initializes a new instance of <code>TextField</code> that is empty
  69. * and has one column.
  70. *
  71. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
  72. */
  73. public
  74. TextField()
  75. {
  76. this("", 1);
  77. }
  78. /*************************************************************************/
  79. /**
  80. * Initializes a new instance of <code>TextField</code> containing
  81. * the specified text. The number of columns will be equal to the
  82. * length of the text string.
  83. *
  84. * @param text The text to display in the field.
  85. *
  86. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
  87. */
  88. public
  89. TextField(String text)
  90. {
  91. this(text, text.length());
  92. }
  93. /*************************************************************************/
  94. /**
  95. * Initializes a new instance of <code>TextField</code> that is empty
  96. * and has the specified number of columns.
  97. *
  98. * @param columns The number of columns in the text field.
  99. *
  100. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
  101. */
  102. public
  103. TextField(int columns)
  104. {
  105. this("", columns);
  106. }
  107. /*************************************************************************/
  108. /**
  109. * Initializes a new instance of <code>TextField</code> with the
  110. * specified text and number of columns.
  111. *
  112. * @param text The text to display in the field.
  113. * @param columns The number of columns in the field.
  114. *
  115. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
  116. */
  117. public
  118. TextField(String text, int columns)
  119. {
  120. super(text);
  121. this.columns = columns;
  122. if (GraphicsEnvironment.isHeadless())
  123. throw new HeadlessException ();
  124. }
  125. /*************************************************************************/
  126. /*
  127. * Instance Methods
  128. */
  129. /**
  130. * Returns the number of columns in the field.
  131. *
  132. * @return The number of columns in the field.
  133. */
  134. public int
  135. getColumns()
  136. {
  137. return(columns);
  138. }
  139. /*************************************************************************/
  140. /**
  141. * Sets the number of columns in this field to the specified value.
  142. *
  143. * @param columns The new number of columns in the field.
  144. *
  145. * @exception IllegalArgumentException If columns is less than zero.
  146. */
  147. public synchronized void
  148. setColumns(int columns)
  149. {
  150. if (columns < 0)
  151. throw new IllegalArgumentException("Value is less than zero: " +
  152. columns);
  153. this.columns = columns;
  154. // FIXME: How to we communicate this to our peer?
  155. }
  156. /*************************************************************************/
  157. /**
  158. * Returns the character that is echoed to the screen when a text
  159. * field is protected (such as when a password is being entered).
  160. *
  161. * @return The echo character for this text field.
  162. */
  163. public char
  164. getEchoChar()
  165. {
  166. return(echoChar);
  167. }
  168. /*************************************************************************/
  169. /**
  170. * Sets the character that is echoed when protected input such as
  171. * a password is displayed.
  172. *
  173. * @param echoChar The new echo character.
  174. */
  175. public void
  176. setEchoChar(char echoChar)
  177. {
  178. this.echoChar = echoChar;
  179. TextFieldPeer tfp = (TextFieldPeer)getPeer();
  180. if (tfp != null)
  181. tfp.setEchoChar(echoChar);
  182. }
  183. /*************************************************************************/
  184. /**
  185. * Sets the character that is echoed when protected input such as
  186. * a password is displayed.
  187. *
  188. * @param echoChar The new echo character.
  189. *
  190. * @deprecated This method is deprecated in favor of
  191. * <code>setEchoChar()</code>
  192. */
  193. public void
  194. setEchoCharacter(char echoChar)
  195. {
  196. setEchoChar(echoChar);
  197. }
  198. /*************************************************************************/
  199. /**
  200. * Tests whether or not this text field has an echo character set
  201. * so that characters the user type are not echoed to the screen.
  202. *
  203. * @return <code>true</code> if an echo character is set,
  204. * <code>false</code> otherwise.
  205. */
  206. public boolean
  207. echoCharIsSet()
  208. {
  209. if (echoChar == '\u0000')
  210. return(false);
  211. else
  212. return(true);
  213. }
  214. /*************************************************************************/
  215. /**
  216. * Returns the minimum size for this text field.
  217. *
  218. * @return The minimum size for this text field.
  219. */
  220. public Dimension
  221. getMinimumSize()
  222. {
  223. return(getMinimumSize(getColumns()));
  224. }
  225. /*************************************************************************/
  226. /**
  227. * Returns the minimum size of a text field with the specified number
  228. * of columns.
  229. *
  230. * @param columns The number of columns to get the minimum size for.
  231. */
  232. public Dimension
  233. getMinimumSize(int columns)
  234. {
  235. TextFieldPeer tfp = (TextFieldPeer)getPeer();
  236. if (tfp == null)
  237. return(null); // FIXME: What do we do if there is no peer?
  238. return(tfp.getMinimumSize(columns));
  239. }
  240. /*************************************************************************/
  241. /**
  242. * Returns the minimum size for this text field.
  243. *
  244. * @return The minimum size for this text field.
  245. *
  246. * @deprecated This method is depcreated in favor of
  247. * <code>getMinimumSize()</code>.
  248. */
  249. public Dimension
  250. minimumSize()
  251. {
  252. return(getMinimumSize(getColumns()));
  253. }
  254. /*************************************************************************/
  255. /**
  256. * Returns the minimum size of a text field with the specified number
  257. * of columns.
  258. *
  259. * @param columns The number of columns to get the minimum size for.
  260. *
  261. * @deprecated This method is deprecated in favor of
  262. * <code>getMinimumSize(int)</code>.
  263. */
  264. public Dimension
  265. minimumSize(int columns)
  266. {
  267. return(getMinimumSize(columns));
  268. }
  269. /*************************************************************************/
  270. /**
  271. * Returns the preferred size for this text field.
  272. *
  273. * @return The preferred size for this text field.
  274. */
  275. public Dimension
  276. getPreferredSize()
  277. {
  278. return(getPreferredSize(getColumns()));
  279. }
  280. /*************************************************************************/
  281. /**
  282. * Returns the preferred size of a text field with the specified number
  283. * of columns.
  284. *
  285. * @param columns The number of columns to get the preferred size for.
  286. */
  287. public Dimension
  288. getPreferredSize(int columns)
  289. {
  290. TextFieldPeer tfp = (TextFieldPeer)getPeer();
  291. if (tfp == null)
  292. return(null); // FIXME: What do we do if there is no peer?
  293. return(tfp.getPreferredSize(columns));
  294. }
  295. /*************************************************************************/
  296. /**
  297. * Returns the preferred size for this text field.
  298. *
  299. * @return The preferred size for this text field.
  300. *
  301. * @deprecated This method is deprecated in favor of
  302. * <code>getPreferredSize()</code>.
  303. */
  304. public Dimension
  305. preferredSize()
  306. {
  307. return(getPreferredSize(getColumns()));
  308. }
  309. /*************************************************************************/
  310. /**
  311. * Returns the preferred size of a text field with the specified number
  312. * of columns.
  313. *
  314. * @param columns The number of columns to get the preferred size for.
  315. *
  316. * @deprecated This method is deprecated in favor of
  317. * <code>getPreferredSize(int)</code>.
  318. */
  319. public Dimension
  320. preferredSize(int columns)
  321. {
  322. return(getPreferredSize(columns));
  323. }
  324. /*************************************************************************/
  325. /**
  326. * Notifies this object that it should create its native peer.
  327. */
  328. public void
  329. addNotify()
  330. {
  331. if (getPeer() != null)
  332. return;
  333. setPeer((ComponentPeer)getToolkit().createTextField(this));
  334. }
  335. /*************************************************************************/
  336. /**
  337. * Addes a new listener to the list of action listeners for this
  338. * object.
  339. *
  340. * @param listener The listener to add to the list.
  341. */
  342. public synchronized void
  343. addActionListener(ActionListener listener)
  344. {
  345. action_listeners = AWTEventMulticaster.add(action_listeners, listener);
  346. enableEvents(AWTEvent.ACTION_EVENT_MASK);
  347. }
  348. /*************************************************************************/
  349. /**
  350. * Removes the specified listener from the list of action listeners
  351. * for this object.
  352. *
  353. * @param listener The listener to remove from the list.
  354. */
  355. public synchronized void
  356. removeActionListener(ActionListener listener)
  357. {
  358. action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
  359. }
  360. /*************************************************************************/
  361. /**
  362. * Processes the specified event. If the event is an instance of
  363. * <code>ActionEvent</code> then <code>processActionEvent()</code> is
  364. * called to process it, otherwise the event is sent to the
  365. * superclass.
  366. *
  367. * @param event The event to process.
  368. */
  369. protected void
  370. processEvent(AWTEvent event)
  371. {
  372. if (event instanceof ActionEvent)
  373. processActionEvent((ActionEvent)event);
  374. else
  375. super.processEvent(event);
  376. }
  377. /*************************************************************************/
  378. /**
  379. * Processes an action event by calling any registered listeners.
  380. * Note to subclasses: This method is not called unless action events
  381. * are enabled on this object. This will be true if any listeners
  382. * are registered, or if action events were specifically enabled
  383. * using <code>enableEvents()</code>.
  384. *
  385. * @param event The event to process.
  386. */
  387. protected void
  388. processActionEvent(ActionEvent event)
  389. {
  390. if (action_listeners != null)
  391. action_listeners.actionPerformed(event);
  392. }
  393. void
  394. dispatchEventImpl(AWTEvent e)
  395. {
  396. if (e.id <= ActionEvent.ACTION_LAST
  397. && e.id >= ActionEvent.ACTION_FIRST
  398. && (action_listeners != null
  399. || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
  400. processEvent(e);
  401. else
  402. super.dispatchEventImpl(e);
  403. }
  404. /*************************************************************************/
  405. /**
  406. * Returns a debug string for this object.
  407. *
  408. * @return A debug string for this object.
  409. */
  410. protected String
  411. paramString()
  412. {
  413. return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
  414. getEchoChar());
  415. }
  416. } // class TextField