JTextArea.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* JTextArea.java --
  2. Copyright (C) 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 javax.swing;
  32. import java.awt.Dimension;
  33. import java.awt.FontMetrics;
  34. import java.awt.Rectangle;
  35. import javax.accessibility.AccessibleContext;
  36. import javax.accessibility.AccessibleStateSet;
  37. import javax.swing.text.BadLocationException;
  38. import javax.swing.text.Document;
  39. import javax.swing.text.Element;
  40. import javax.swing.text.JTextComponent;
  41. import javax.swing.text.PlainDocument;
  42. import javax.swing.text.View;
  43. /**
  44. * The <code>JTextArea</code> component provides a multi-line area for displaying
  45. * and editing plain text. The component is designed to act as a lightweight
  46. * replacement for the heavyweight <code>java.awt.TextArea</code> component,
  47. * which provides similar functionality using native widgets.
  48. * <p>
  49. *
  50. * This component has additional functionality to the AWT class. It follows
  51. * the same design pattern as seen in other text components, such as
  52. * <code>JTextField</code>, <code>JTextPane</code> and <code>JEditorPane</code>,
  53. * and embodied in <code>JTextComponent</code>. These classes separate the text
  54. * (the model) from its appearance within the onscreen component (the view). The
  55. * text is held within a <code>javax.swing.text.Document</code> object, which can
  56. * also maintain relevant style information where necessary. As a result, it is the
  57. * document that should be monitored for textual changes, via
  58. * <code>DocumentEvent</code>s delivered to registered
  59. * <code>DocumentListener</code>s, rather than this component.
  60. * <p>
  61. *
  62. * Unlike <code>java.awt.TextArea</code>, <code>JTextArea</code> does not
  63. * handle scrolling. Instead, this functionality is delegated to a
  64. * <code>JScrollPane</code>, which can contain the text area and handle
  65. * scrolling when required. Likewise, the word wrapping functionality
  66. * of the AWT component is converted to a property of this component
  67. * and the <code>rows</code> and <code>columns</code> properties
  68. * are used in calculating the preferred size of the scroll pane's
  69. * view port.
  70. *
  71. * @author Michael Koch (konqueror@gmx.de)
  72. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  73. * @see java.awt.TextArea
  74. * @see javax.swing.text.JTextComponent
  75. * @see javax.swing.JTextField
  76. * @see javax.swing.JTextPane
  77. * @see javax.swing.JEditorPane
  78. * @see javax.swing.text.Document
  79. * @see javax.swing.event.DocumentEvent
  80. * @see javax.swing.event.DocumentListener
  81. */
  82. public class JTextArea extends JTextComponent
  83. {
  84. /**
  85. * Provides accessibility support for <code>JTextArea</code>.
  86. *
  87. * @author Roman Kennke (kennke@aicas.com)
  88. */
  89. protected class AccessibleJTextArea extends AccessibleJTextComponent
  90. {
  91. /**
  92. * Creates a new <code>AccessibleJTextArea</code> object.
  93. */
  94. protected AccessibleJTextArea()
  95. {
  96. super();
  97. }
  98. /**
  99. * Returns the accessible state of this <code>AccessibleJTextArea</code>.
  100. *
  101. * @return the accessible state of this <code>AccessibleJTextArea</code>
  102. */
  103. public AccessibleStateSet getAccessibleStateSet()
  104. {
  105. AccessibleStateSet state = super.getAccessibleStateSet();
  106. // TODO: Figure out what state must be added here to the super's state.
  107. return state;
  108. }
  109. }
  110. /**
  111. * Compatible with Sun's JDK
  112. */
  113. private static final long serialVersionUID = -6141680179310439825L;
  114. /**
  115. * The number of rows used by the component.
  116. */
  117. private int rows;
  118. /**
  119. * The number of columns used by the component.
  120. */
  121. private int columns;
  122. /**
  123. * Whether line wrapping is enabled or not.
  124. */
  125. private boolean lineWrap;
  126. /**
  127. * The number of characters equal to a tab within the text.
  128. */
  129. private int tabSize = 8;
  130. private boolean wrapStyleWord;
  131. /**
  132. * Creates a new <code>JTextArea</code> object.
  133. */
  134. public JTextArea()
  135. {
  136. this(null, null, 0, 0);
  137. }
  138. /**
  139. * Creates a new <code>JTextArea</code> object.
  140. *
  141. * @param text the initial text
  142. */
  143. public JTextArea(String text)
  144. {
  145. this(null, text, 0, 0);
  146. }
  147. /**
  148. * Creates a new <code>JTextArea</code> object.
  149. *
  150. * @param rows the number of rows
  151. * @param columns the number of cols
  152. *
  153. * @exception IllegalArgumentException if rows or columns are negative
  154. */
  155. public JTextArea(int rows, int columns)
  156. {
  157. this(null, null, rows, columns);
  158. }
  159. /**
  160. * Creates a new <code>JTextArea</code> object.
  161. *
  162. * @param text the initial text
  163. * @param rows the number of rows
  164. * @param columns the number of cols
  165. *
  166. * @exception IllegalArgumentException if rows or columns are negative
  167. */
  168. public JTextArea(String text, int rows, int columns)
  169. {
  170. this(null, text, rows, columns);
  171. }
  172. /**
  173. * Creates a new <code>JTextArea</code> object.
  174. *
  175. * @param doc the document model to use
  176. */
  177. public JTextArea(Document doc)
  178. {
  179. this(doc, null, 0, 0);
  180. }
  181. /**
  182. * Creates a new <code>JTextArea</code> object.
  183. *
  184. * @param doc the document model to use
  185. * @param text the initial text
  186. * @param rows the number of rows
  187. * @param columns the number of cols
  188. *
  189. * @exception IllegalArgumentException if rows or columns are negative
  190. */
  191. public JTextArea(Document doc, String text, int rows, int columns)
  192. {
  193. setDocument(doc == null ? createDefaultModel() : doc);
  194. // Only explicitly setText() when there is actual text since
  195. // setText() might be overridden and not expected to be called
  196. // from the constructor (as in JEdit).
  197. if (text != null)
  198. setText(text);
  199. setRows(rows);
  200. setColumns(columns);
  201. }
  202. /**
  203. * Appends the supplied text to the current contents
  204. * of the document model.
  205. *
  206. * @param toAppend the text to append
  207. */
  208. public void append(String toAppend)
  209. {
  210. try
  211. {
  212. getDocument().insertString(getText().length(), toAppend, null);
  213. }
  214. catch (BadLocationException exception)
  215. {
  216. /* This shouldn't happen in theory -- but, if it does... */
  217. throw new RuntimeException("Unexpected exception occurred.", exception);
  218. }
  219. if (toAppend != null && toAppend.length() > 0)
  220. revalidate();
  221. }
  222. /**
  223. * Creates the default document model.
  224. *
  225. * @return a new default model
  226. */
  227. protected Document createDefaultModel()
  228. {
  229. return new PlainDocument();
  230. }
  231. /**
  232. * Returns true if the width of this component should be forced
  233. * to match the width of a surrounding view port. When line wrapping
  234. * is turned on, this method returns true.
  235. *
  236. * @return true if lines are wrapped.
  237. */
  238. public boolean getScrollableTracksViewportWidth()
  239. {
  240. return lineWrap ? true : super.getScrollableTracksViewportWidth();
  241. }
  242. /**
  243. * Returns the increment that is needed to expose exactly one new line
  244. * of text. This is implemented here to return the values of
  245. * {@link #getRowHeight} and {@link #getColumnWidth}, depending on
  246. * the value of the argument <code>direction</code>.
  247. *
  248. * @param visibleRect the view area that is visible in the viewport
  249. * @param orientation either {@link SwingConstants#VERTICAL} or
  250. * {@link SwingConstants#HORIZONTAL}
  251. * @param direction less than zero for up/left scrolling, greater
  252. * than zero for down/right scrolling
  253. *
  254. * @return the increment that is needed to expose exactly one new row
  255. * or column of text
  256. *
  257. * @throws IllegalArgumentException if <code>orientation</code> is invalid
  258. */
  259. public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
  260. int direction)
  261. {
  262. if (orientation == SwingConstants.VERTICAL)
  263. return getRowHeight();
  264. else if (orientation == SwingConstants.HORIZONTAL)
  265. return getColumnWidth();
  266. else
  267. throw new IllegalArgumentException("orientation must be either "
  268. + "javax.swing.SwingConstants.VERTICAL "
  269. + "or "
  270. + "javax.swing.SwingConstants.HORIZONTAL"
  271. );
  272. }
  273. /**
  274. * Returns the preferred size of that text component in the case
  275. * it is embedded within a JScrollPane. This uses the column and
  276. * row settings if they are explicitly set, or fall back to
  277. * the superclass's behaviour.
  278. *
  279. * @return the preferred size of that text component in the case
  280. * it is embedded within a JScrollPane
  281. */
  282. public Dimension getPreferredScrollableViewportSize()
  283. {
  284. if ((rows > 0) && (columns > 0))
  285. return new Dimension(columns * getColumnWidth(), rows * getRowHeight());
  286. else
  287. return super.getPreferredScrollableViewportSize();
  288. }
  289. /**
  290. * Returns the UI class ID string.
  291. *
  292. * @return the string "TextAreaUI"
  293. */
  294. public String getUIClassID()
  295. {
  296. return "TextAreaUI";
  297. }
  298. /**
  299. * Returns the current number of columns.
  300. *
  301. * @return number of columns
  302. */
  303. public int getColumns()
  304. {
  305. return columns;
  306. }
  307. /**
  308. * Sets the number of rows.
  309. *
  310. * @param columns number of columns
  311. *
  312. * @exception IllegalArgumentException if columns is negative
  313. */
  314. public void setColumns(int columns)
  315. {
  316. if (columns < 0)
  317. throw new IllegalArgumentException();
  318. if (columns != this.columns)
  319. {
  320. this.columns = columns;
  321. revalidate();
  322. }
  323. }
  324. /**
  325. * Returns the current number of rows.
  326. *
  327. * @return number of rows
  328. */
  329. public int getRows()
  330. {
  331. return rows;
  332. }
  333. /**
  334. * Sets the number of rows.
  335. *
  336. * @param rows number of rows
  337. *
  338. * @exception IllegalArgumentException if rows is negative
  339. */
  340. public void setRows(int rows)
  341. {
  342. if (rows < 0)
  343. throw new IllegalArgumentException();
  344. if (rows != this.rows)
  345. {
  346. this.rows = rows;
  347. revalidate();
  348. }
  349. }
  350. /**
  351. * Checks whether line wrapping is enabled.
  352. *
  353. * @return <code>true</code> if line wrapping is enabled,
  354. * <code>false</code> otherwise
  355. */
  356. public boolean getLineWrap()
  357. {
  358. return lineWrap;
  359. }
  360. /**
  361. * Enables/disables line wrapping.
  362. *
  363. * @param flag <code>true</code> to enable line wrapping,
  364. * <code>false</code> otherwise
  365. */
  366. public void setLineWrap(boolean flag)
  367. {
  368. if (lineWrap == flag)
  369. return;
  370. boolean oldValue = lineWrap;
  371. lineWrap = flag;
  372. firePropertyChange("lineWrap", oldValue, lineWrap);
  373. }
  374. /**
  375. * Checks whether word style wrapping is enabled.
  376. *
  377. * @return <code>true</code> if word style wrapping is enabled,
  378. * <code>false</code> otherwise
  379. */
  380. public boolean getWrapStyleWord()
  381. {
  382. return wrapStyleWord;
  383. }
  384. /**
  385. * Enables/Disables word style wrapping.
  386. *
  387. * @param flag <code>true</code> to enable word style wrapping,
  388. * <code>false</code> otherwise
  389. */
  390. public void setWrapStyleWord(boolean flag)
  391. {
  392. if (wrapStyleWord == flag)
  393. return;
  394. boolean oldValue = wrapStyleWord;
  395. wrapStyleWord = flag;
  396. firePropertyChange("wrapStyleWord", oldValue, wrapStyleWord);
  397. }
  398. /**
  399. * Returns the number of characters used for a tab.
  400. * This defaults to 8.
  401. *
  402. * @return the current number of spaces used for a tab.
  403. */
  404. public int getTabSize()
  405. {
  406. return tabSize;
  407. }
  408. /**
  409. * Sets the number of characters used for a tab to the
  410. * supplied value. If a change to the tab size property
  411. * occurs (i.e. newSize != tabSize), a property change event
  412. * is fired.
  413. *
  414. * @param newSize The new number of characters to use for a tab.
  415. */
  416. public void setTabSize(int newSize)
  417. {
  418. if (tabSize == newSize)
  419. return;
  420. int oldValue = tabSize;
  421. tabSize = newSize;
  422. firePropertyChange("tabSize", oldValue, tabSize);
  423. }
  424. protected int getColumnWidth()
  425. {
  426. FontMetrics metrics = getToolkit().getFontMetrics(getFont());
  427. return metrics.charWidth('m');
  428. }
  429. public int getLineCount()
  430. {
  431. return getDocument().getDefaultRootElement().getElementCount();
  432. }
  433. public int getLineStartOffset(int line)
  434. throws BadLocationException
  435. {
  436. int lineCount = getLineCount();
  437. if (line < 0 || line > lineCount)
  438. throw new BadLocationException("Non-existing line number", line);
  439. Element lineElem = getDocument().getDefaultRootElement().getElement(line);
  440. return lineElem.getStartOffset();
  441. }
  442. public int getLineEndOffset(int line)
  443. throws BadLocationException
  444. {
  445. int lineCount = getLineCount();
  446. if (line < 0 || line > lineCount)
  447. throw new BadLocationException("Non-existing line number", line);
  448. Element lineElem = getDocument().getDefaultRootElement().getElement(line);
  449. return lineElem.getEndOffset();
  450. }
  451. public int getLineOfOffset(int offset)
  452. throws BadLocationException
  453. {
  454. Document doc = getDocument();
  455. if (offset < doc.getStartPosition().getOffset()
  456. || offset >= doc.getEndPosition().getOffset())
  457. throw new BadLocationException("offset outside of document", offset);
  458. return doc.getDefaultRootElement().getElementIndex(offset);
  459. }
  460. protected int getRowHeight()
  461. {
  462. FontMetrics metrics = getToolkit().getFontMetrics(getFont());
  463. return metrics.getHeight();
  464. }
  465. /**
  466. * Inserts the supplied text at the specified position. Nothing
  467. * happens in the case that the model or the supplied string is null
  468. * or of zero length.
  469. *
  470. * @param string The string of text to insert.
  471. * @param position The position at which to insert the supplied text.
  472. * @throws IllegalArgumentException if the position is &lt; 0 or greater
  473. * than the length of the current text.
  474. */
  475. public void insert(String string, int position)
  476. {
  477. // Retrieve the document model.
  478. Document doc = getDocument();
  479. // Check the model and string for validity.
  480. if (doc == null
  481. || string == null
  482. || string.length() == 0)
  483. return;
  484. // Insert the text into the model.
  485. try
  486. {
  487. doc.insertString(position, string, null);
  488. }
  489. catch (BadLocationException e)
  490. {
  491. throw new IllegalArgumentException("The supplied position, "
  492. + position + ", was invalid.");
  493. }
  494. }
  495. public void replaceRange(String text, int start, int end)
  496. {
  497. Document doc = getDocument();
  498. if (start > end
  499. || start < doc.getStartPosition().getOffset()
  500. || end >= doc.getEndPosition().getOffset())
  501. throw new IllegalArgumentException();
  502. try
  503. {
  504. doc.remove(start, end - start);
  505. doc.insertString(start, text, null);
  506. }
  507. catch (BadLocationException e)
  508. {
  509. // This cannot happen as we check offset above.
  510. }
  511. }
  512. /**
  513. * Returns the preferred size for the JTextArea. This is the maximum of
  514. * the size that is needed to display the content and the requested size
  515. * as per {@link #getColumns} and {@link #getRows}.
  516. *
  517. * @return the preferred size of the JTextArea
  518. */
  519. public Dimension getPreferredSize()
  520. {
  521. int reqWidth = getColumns() * getColumnWidth();
  522. int reqHeight = getRows() * getRowHeight();
  523. View view = getUI().getRootView(this);
  524. int neededWidth = (int) view.getPreferredSpan(View.HORIZONTAL);
  525. int neededHeight = (int) view.getPreferredSpan(View.VERTICAL);
  526. return new Dimension(Math.max(reqWidth, neededWidth),
  527. Math.max(reqHeight, neededHeight));
  528. }
  529. /**
  530. * Returns the accessible context associated with the <code>JTextArea</code>.
  531. *
  532. * @return the accessible context associated with the <code>JTextArea</code>
  533. */
  534. public AccessibleContext getAccessibleContext()
  535. {
  536. if (accessibleContext == null)
  537. accessibleContext = new AccessibleJTextArea();
  538. return accessibleContext;
  539. }
  540. }