TextArea.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* TextArea.java -- A multi-line text entry component
  2. Copyright (C) 1999, 2004 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.KeyEvent;
  33. import java.awt.peer.ComponentPeer;
  34. import java.awt.peer.TextAreaPeer;
  35. import java.util.HashSet;
  36. import java.util.Set;
  37. import javax.accessibility.AccessibleContext;
  38. import javax.accessibility.AccessibleStateSet;
  39. /**
  40. * A TextArea is a text component capable of displaying multiple lines
  41. * of user-editable text. A TextArea handles its own scrolling and
  42. * can display vertical and horizontal scrollbars as navigation aids.
  43. *
  44. * @author Aaron M. Renn (arenn@urbanophile.com)
  45. */
  46. public class TextArea extends TextComponent implements java.io.Serializable
  47. {
  48. /**
  49. * Display both horiztonal and vertical scroll bars.
  50. */
  51. public static final int SCROLLBARS_BOTH = 0;
  52. /**
  53. * Display vertical scroll bar only.
  54. */
  55. public static final int SCROLLBARS_VERTICAL_ONLY = 1;
  56. /**
  57. * Display horizatonal scroll bar only.
  58. */
  59. public static final int SCROLLBARS_HORIZONTAL_ONLY = 2;
  60. /**
  61. * Do not display scrollbars.
  62. */
  63. public static final int SCROLLBARS_NONE = 3;
  64. /**
  65. * Serialization constant.
  66. */
  67. private static final long serialVersionUID = 3692302836626095722L;
  68. /**
  69. * @serial The number of columns used in this text area's preferred
  70. * and minimum size calculations.
  71. */
  72. private int columns;
  73. /**
  74. * @serial The number of rows used in this text area's preferred and
  75. * minimum size calculations.
  76. */
  77. private int rows;
  78. /**
  79. * @serial The scrollbar display policy. One of SCROLLBARS_BOTH,
  80. * SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY,
  81. * SCROLLBARS_NONE.
  82. */
  83. private int scrollbarVisibility;
  84. /*
  85. * The number used to generate the name returned by getName.
  86. */
  87. private static transient long next_text_number;
  88. /**
  89. * Initialize a new instance of <code>TextArea</code> that is empty.
  90. * Conceptually the <code>TextArea</code> has 0 rows and 0 columns
  91. * but its initial bounds are defined by its peer or by the
  92. * container in which it is packed. Both horizontal and vertical
  93. * scrollbars will be displayed.
  94. *
  95. * @exception HeadlessException if GraphicsEnvironment.isHeadless () is true
  96. */
  97. public TextArea ()
  98. {
  99. this ("", 0, 0, SCROLLBARS_BOTH);
  100. }
  101. /**
  102. * Initialize a new instance of <code>TextArea</code> that contains
  103. * the specified text. Conceptually the <code>TextArea</code> has 0
  104. * rows and 0 columns but its initial bounds are defined by its peer
  105. * or by the container in which it is packed. Both horizontal and
  106. * veritcal scrollbars will be displayed. The TextArea initially contains
  107. * the specified text. If text specified as <code>null<code>, it will
  108. * be set to "".
  109. *
  110. * @param text The text to display in this text area (<code>null</code> permitted).
  111. *
  112. * @exception HeadlessException if GraphicsEnvironment.isHeadless () is true
  113. */
  114. public TextArea (String text)
  115. {
  116. this (text, 0, 0, SCROLLBARS_BOTH);
  117. }
  118. /**
  119. * Initialize a new instance of <code>TextArea</code> that is empty
  120. * and can display the specified number of rows and columns of text,
  121. * without the need to scroll. Both horizontal and vertical
  122. * scrollbars will be displayed.
  123. *
  124. * @param rows The number of rows in this text area.
  125. * @param columns The number of columns in this text area.
  126. *
  127. * @exception HeadlessException if GraphicsEnvironment.isHeadless () is true
  128. */
  129. public TextArea (int rows, int columns)
  130. {
  131. this ("", rows, columns, SCROLLBARS_BOTH);
  132. }
  133. /**
  134. * Initialize a new instance of <code>TextArea</code> that can
  135. * display the specified number of rows and columns of text, without
  136. * the need to scroll. The TextArea initially contains the
  137. * specified text. If text specified as <code>null<code>, it will
  138. * be set to "".
  139. *
  140. * @param text The text to display in this text area (<code>null</code> permitted).
  141. * @param rows The number of rows in this text area.
  142. * @param columns The number of columns in this text area.
  143. *
  144. * @exception HeadlessException if GraphicsEnvironment.isHeadless () is true
  145. */
  146. public TextArea (String text, int rows, int columns)
  147. {
  148. this (text, rows, columns, SCROLLBARS_BOTH);
  149. }
  150. /**
  151. * Initialize a new instance of <code>TextArea</code> that initially
  152. * contains the specified text. The TextArea can display the
  153. * specified number of rows and columns of text, without the need to
  154. * scroll. This constructor allows specification of the scroll bar
  155. * display policy. The TextArea initially contains the specified text.
  156. * If text specified as <code>null<code>, it will be set to "".
  157. *
  158. * @param text The text to display in this text area (<code>null</code> permitted).
  159. * @param rows The number of rows in this text area.
  160. * @param columns The number of columns in this text area.
  161. * @param scrollbarVisibility The scroll bar display policy. One of
  162. * SCROLLBARS_BOTH, SCROLLBARS_VERTICAL_ONLY,
  163. * SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE.
  164. *
  165. * @exception HeadlessException if GraphicsEnvironment.isHeadless () is true
  166. */
  167. public TextArea (String text, int rows, int columns, int scrollbarVisibility)
  168. {
  169. super (text);
  170. if (GraphicsEnvironment.isHeadless ())
  171. throw new HeadlessException ();
  172. if (rows < 0)
  173. this.rows = 0;
  174. else
  175. this.rows = rows;
  176. if (columns < 0)
  177. this.columns = 0;
  178. else
  179. this.columns = columns;
  180. if (scrollbarVisibility < 0 || scrollbarVisibility > 4)
  181. this.scrollbarVisibility = SCROLLBARS_BOTH;
  182. else
  183. this.scrollbarVisibility = scrollbarVisibility;
  184. // TextAreas need to receive tab key events so we override the
  185. // default forward and backward traversal key sets.
  186. Set s = new HashSet ();
  187. s.add (AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_TAB,
  188. KeyEvent.CTRL_DOWN_MASK));
  189. setFocusTraversalKeys (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, s);
  190. s = new HashSet ();
  191. s.add (AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_TAB,
  192. KeyEvent.SHIFT_DOWN_MASK
  193. | KeyEvent.CTRL_DOWN_MASK));
  194. setFocusTraversalKeys (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, s);
  195. }
  196. /**
  197. * Retrieve the number of columns that this text area would prefer
  198. * to display. This value may or may not correspond to the number
  199. * of columns that are actually displayed.
  200. *
  201. * @return The preferred number of columns.
  202. */
  203. public int getColumns ()
  204. {
  205. return columns;
  206. }
  207. /**
  208. * Set the preferred number of columns for this text area. This
  209. * method does not cause the number of columns displayed by the text
  210. * area to be updated, if the text area is currently visible.
  211. *
  212. * @param columns The preferred number of columns.
  213. *
  214. * @exception IllegalArgumentException If columns is less than zero.
  215. */
  216. public synchronized void setColumns (int columns)
  217. {
  218. if (columns < 0)
  219. throw new IllegalArgumentException ("Value is less than zero: "
  220. + columns);
  221. this.columns = columns;
  222. }
  223. /**
  224. * Retrieve the number of rows that this text area would prefer to
  225. * display. This value may or may not correspond to the number of
  226. * rows that are actually displayed.
  227. *
  228. * @return The preferred number of rows.
  229. */
  230. public int getRows ()
  231. {
  232. return rows;
  233. }
  234. /**
  235. * Set the preferred number of rows for this text area. This method
  236. * does not cause the number of columns displayed by the text area
  237. * to be updated, if the text area is currently visible.
  238. *
  239. * @param rows The preferred number of rows.
  240. *
  241. * @exception IllegalArgumentException If rows is less than zero.
  242. */
  243. public synchronized void setRows (int rows)
  244. {
  245. if (rows < 1)
  246. throw new IllegalArgumentException ("Value is less than one: " + rows);
  247. this.rows = rows;
  248. }
  249. /**
  250. * Retrieve the minimum size for this text area.
  251. *
  252. * @return The minimum size for this text field.
  253. */
  254. public Dimension getMinimumSize ()
  255. {
  256. return getMinimumSize (getRows (), getColumns ());
  257. }
  258. /**
  259. * Retrieve the minimum size for this text area. If the minimum
  260. * size has been set, then rows and columns are used in the calculation.
  261. *
  262. * @param rows The number of rows to use in the minimum size
  263. * calculation.
  264. * @param columns The number of columns to use in the minimum size
  265. * calculation.
  266. *
  267. * @return The minimum size for this text area.
  268. */
  269. public Dimension getMinimumSize (int rows, int columns)
  270. {
  271. return minimumSize (rows, columns);
  272. }
  273. /**
  274. * Retrieve the minimum size for this text area.
  275. *
  276. * @return The minimum size for this text area.
  277. *
  278. * @deprecated This method is deprecated in favor of
  279. * <code>getMinimumSize ()</code>.
  280. */
  281. public Dimension minimumSize ()
  282. {
  283. return minimumSize (getRows (), getColumns ());
  284. }
  285. /**
  286. * Retrieve the minimum size for this text area. If the minimum
  287. * size has been set, then rows and columns are used in the calculation.
  288. *
  289. * @param rows The number of rows to use in the minimum size
  290. * calculation.
  291. * @param columns The number of columns to use in the minimum size
  292. * calculation.
  293. *
  294. * @return The minimum size for this text area.
  295. *
  296. * @deprecated This method is deprecated in favor of
  297. * <code>getMinimumSize (int, int)</code>.
  298. */
  299. public Dimension minimumSize (int rows, int columns)
  300. {
  301. if (isMinimumSizeSet())
  302. return new Dimension(minSize);
  303. TextAreaPeer peer = (TextAreaPeer) getPeer ();
  304. if (peer == null)
  305. return new Dimension (getWidth(), getHeight());
  306. return peer.getMinimumSize (rows, columns);
  307. }
  308. /**
  309. * Retrieve the preferred size for this text area.
  310. *
  311. * @return The preferred size for this text field.
  312. */
  313. public Dimension getPreferredSize ()
  314. {
  315. return getPreferredSize (getRows (), getColumns ());
  316. }
  317. /**
  318. * Retrieve the preferred size for this text area. If the preferred
  319. * size has been set, then rows and columns are used in the calculation.
  320. *
  321. * @param rows The number of rows to use in the preferred size
  322. * calculation.
  323. * @param columns The number of columns to use in the preferred size
  324. * calculation.
  325. *
  326. * @return The preferred size for this text area.
  327. */
  328. public Dimension getPreferredSize (int rows, int columns)
  329. {
  330. return preferredSize (rows, columns);
  331. }
  332. /**
  333. * Retrieve the preferred size for this text area.
  334. *
  335. * @return The preferred size for this text field.
  336. *
  337. * @deprecated This method is deprecated in favor of
  338. * <code>getPreferredSize ()</code>.
  339. */
  340. public Dimension preferredSize ()
  341. {
  342. return preferredSize (getRows (), getColumns ());
  343. }
  344. /**
  345. * Retrieve the preferred size for this text area. If the preferred
  346. * size has been set, then rows and columns are used in the calculation.
  347. *
  348. * @param rows The number of rows to use in the preferred size
  349. * calculation.
  350. * @param columns The number of columns to use in the preferred size
  351. * calculation.
  352. *
  353. * @return The preferred size for this text area.
  354. *
  355. * @deprecated This method is deprecated in favor of
  356. * <code>getPreferredSize (int, int)</code>.
  357. */
  358. public Dimension preferredSize (int rows, int columns)
  359. {
  360. if (isPreferredSizeSet())
  361. return new Dimension(prefSize);
  362. TextAreaPeer peer = (TextAreaPeer) getPeer ();
  363. if (peer == null)
  364. return new Dimension (getWidth(), getHeight());
  365. return peer.getPreferredSize (rows, columns);
  366. }
  367. /**
  368. * Retrieve the scroll bar display policy -- one of SCROLLBARS_BOTH,
  369. * SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY,
  370. * SCROLLBARS_NONE.
  371. *
  372. * @return The current scroll bar display policy.
  373. */
  374. public int getScrollbarVisibility ()
  375. {
  376. return scrollbarVisibility;
  377. }
  378. /**
  379. * Notify this object that it should create its native peer.
  380. */
  381. public void addNotify ()
  382. {
  383. if (getPeer () == null)
  384. setPeer ((ComponentPeer) getToolkit().createTextArea (this));
  385. }
  386. /**
  387. * Append the specified text to the end of the current text.
  388. *
  389. * @param str The text to append.
  390. */
  391. public void append (String str)
  392. {
  393. appendText (str);
  394. }
  395. /**
  396. * Append the specified text to the end of the current text.
  397. *
  398. * @param str The text to append.
  399. *
  400. * @deprecated This method is deprecated in favor of
  401. * <code>append ()</code>.
  402. */
  403. public void appendText (String str)
  404. {
  405. TextAreaPeer peer = (TextAreaPeer) getPeer ();
  406. if (peer != null)
  407. peer.insert (str, peer.getText().length ());
  408. else
  409. setText(getText() + str);
  410. }
  411. /**
  412. * Insert the specified text at the specified position. The first
  413. * character in the text area is at position zero.
  414. *
  415. * @param str The text to insert.
  416. * @param pos The position at which to insert text.
  417. */
  418. public void insert (String str, int pos)
  419. {
  420. insertText (str, pos);
  421. }
  422. /**
  423. * Insert the specified text at the specified position. The first
  424. * character in the text area is at position zero.
  425. *
  426. * @param str The text to insert.
  427. * @param pos The position at which to insert text.
  428. *
  429. * @deprecated This method is deprecated in favor of
  430. * <code>insert ()</code>.
  431. */
  432. public void insertText (String str, int pos)
  433. {
  434. String tmp1 = null;
  435. String tmp2 = null;
  436. TextAreaPeer peer = (TextAreaPeer) getPeer ();
  437. if (peer != null)
  438. peer.insert (str, pos);
  439. else
  440. {
  441. tmp1 = getText().substring(0, pos);
  442. tmp2 = getText().substring(pos, getText().length());
  443. setText(tmp1 + str + tmp2);
  444. }
  445. }
  446. /**
  447. * Replace a range of characters with the specified text. The
  448. * character at the start position will be replaced, unless start ==
  449. * end. The character at the end posistion will not be replaced.
  450. * The first character in the text area is at position zero. The
  451. * length of the replacement text may differ from the length of the
  452. * text that is replaced.
  453. *
  454. * @param str The new text for the range.
  455. * @param start The start position of the replacement range.
  456. * @param end The end position of the replacement range.
  457. */
  458. public void replaceRange (String str, int start, int end)
  459. {
  460. replaceText (str, start, end);
  461. }
  462. /**
  463. * Replace a range of characters with the specified text. The
  464. * character at the start position will be replaced, unless start ==
  465. * end. The character at the end posistion will not be replaced.
  466. * The first character in the text area is at position zero. The
  467. * length of the replacement text may differ from the length of the
  468. * text that is replaced.
  469. *
  470. * @param str The new text for the range.
  471. * @param start The start position of the replacement range.
  472. * @param end The end position of the replacement range.
  473. *
  474. * @deprecated This method is deprecated in favor of
  475. * <code>replaceRange ()</code>.
  476. */
  477. public void replaceText (String str, int start, int end)
  478. {
  479. String tmp1 = null;
  480. String tmp2 = null;
  481. TextAreaPeer peer = (TextAreaPeer) getPeer();
  482. if (peer != null)
  483. peer.replaceRange(str, start, end);
  484. else
  485. {
  486. tmp1 = getText().substring(0, start);
  487. tmp2 = getText().substring(end, getText().length());
  488. setText(tmp1 + str + tmp2);
  489. }
  490. }
  491. /**
  492. * Retrieve a debugging string for this text area.
  493. *
  494. * @return A debugging string for this text area.
  495. */
  496. protected String paramString ()
  497. {
  498. String sbVisibility = "";
  499. switch (scrollbarVisibility)
  500. {
  501. case SCROLLBARS_BOTH:
  502. sbVisibility = "both";
  503. break;
  504. case SCROLLBARS_VERTICAL_ONLY:
  505. sbVisibility = "vertical-only";
  506. break;
  507. case SCROLLBARS_HORIZONTAL_ONLY:
  508. sbVisibility = "horizontal-only";
  509. break;
  510. case SCROLLBARS_NONE:
  511. sbVisibility = "none";
  512. break;
  513. }
  514. String editable = "";
  515. if (isEditable ())
  516. editable = "editable,";
  517. return getName () + "," + getX () + "," + getY () + "," + getWidth ()
  518. + "x" + getHeight () + "," + "text=" + getText () + "," + editable
  519. + "selection=" + getSelectionStart () + "-" + getSelectionEnd ()
  520. + ",rows=" + rows + ",columns=" + columns + ",scrollbarVisibility="
  521. + sbVisibility;
  522. }
  523. /**
  524. * Generate a unique name for this text area.
  525. *
  526. * @return A unique name for this text area.
  527. */
  528. String generateName ()
  529. {
  530. return "text" + getUniqueLong ();
  531. }
  532. private static synchronized long getUniqueLong ()
  533. {
  534. return next_text_number++;
  535. }
  536. protected class AccessibleAWTTextArea extends AccessibleAWTTextComponent
  537. {
  538. private static final long serialVersionUID = 3472827823632144419L;
  539. protected AccessibleAWTTextArea()
  540. {
  541. }
  542. public AccessibleStateSet getAccessibleStateSet()
  543. {
  544. return super.getAccessibleStateSet();
  545. }
  546. }
  547. /**
  548. * Gets the AccessibleContext associated with this <code>TextArea</code>.
  549. * The context is created, if necessary.
  550. *
  551. * @return the associated context
  552. */
  553. public AccessibleContext getAccessibleContext()
  554. {
  555. /* Create the context if this is the first request */
  556. if (accessibleContext == null)
  557. accessibleContext = new AccessibleAWTTextArea();
  558. return accessibleContext;
  559. }
  560. }