TextComponent.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* TextComponent.java -- Widgets for entering text
  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.TextEvent;
  33. import java.awt.event.TextListener;
  34. import java.awt.peer.TextComponentPeer;
  35. import java.awt.peer.ComponentPeer;
  36. /**
  37. * This class provides common functionality for widgets than
  38. * contain text.
  39. *
  40. * @author Aaron M. Renn (arenn@urbanophile.com)
  41. */
  42. public class TextComponent extends Component implements java.io.Serializable
  43. {
  44. /*
  45. * Static Variables
  46. */
  47. // Constant for serialization
  48. private static final long serialVersionUID = -2214773872412987419L;
  49. /*
  50. * Instance Variables
  51. */
  52. /**
  53. * @serial Indicates whether or not this component is editable.
  54. */
  55. private boolean editable;
  56. /**
  57. * @serial The starting position of the selected text region.
  58. */
  59. private int selectionStart;
  60. /**
  61. * @serial The ending position of the selected text region.
  62. */
  63. private int selectionEnd;
  64. /**
  65. * @serial The text in the component
  66. */
  67. private String text;
  68. /**
  69. * A list of listeners that will receive events from this object.
  70. */
  71. protected transient TextListener textListener;
  72. /*************************************************************************/
  73. /*
  74. * Constructors
  75. */
  76. TextComponent(String text)
  77. {
  78. this.text = text;
  79. this.editable = true;
  80. }
  81. /*************************************************************************/
  82. /*
  83. * Instance Methods
  84. */
  85. /**
  86. * Returns the text in this component
  87. *
  88. * @return The text in this component.
  89. */
  90. public synchronized String
  91. getText()
  92. {
  93. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  94. if (tcp != null)
  95. text = tcp.getText();
  96. return(text);
  97. }
  98. /*************************************************************************/
  99. /**
  100. * Sets the text in this component to the specified string.
  101. *
  102. * @param text The new text for this component.
  103. */
  104. public synchronized void
  105. setText(String text)
  106. {
  107. if (text == null)
  108. text = "";
  109. this.text = text;
  110. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  111. if (tcp != null)
  112. tcp.setText(text);
  113. }
  114. /*************************************************************************/
  115. /**
  116. * Returns a string that contains the text that is currently selected.
  117. *
  118. * @return The currently selected text region.
  119. */
  120. public synchronized String
  121. getSelectedText()
  122. {
  123. String alltext = getText();
  124. int start = getSelectionStart();
  125. int end = getSelectionEnd();
  126. return(alltext.substring(start, end));
  127. }
  128. /*************************************************************************/
  129. /**
  130. * Returns the starting position of the selected text region.
  131. * // FIXME: What is returned if there is no selected text?
  132. *
  133. * @return The starting position of the selected text region.
  134. */
  135. public synchronized int
  136. getSelectionStart()
  137. {
  138. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  139. if (tcp != null)
  140. selectionStart = tcp.getSelectionStart();
  141. return(selectionStart);
  142. }
  143. /*************************************************************************/
  144. /**
  145. * Sets the starting position of the selected region to the
  146. * specified value. If the specified value is out of range, then it
  147. * will be silently changed to the nearest legal value.
  148. *
  149. * @param selectionStart The new start position for selected text.
  150. */
  151. public synchronized void
  152. setSelectionStart(int selectionStart)
  153. {
  154. select(selectionStart, getSelectionEnd());
  155. }
  156. /*************************************************************************/
  157. /**
  158. * Returns the ending position of the selected text region.
  159. * // FIXME: What is returned if there is no selected text.
  160. *
  161. * @return The ending position of the selected text region.
  162. */
  163. public synchronized int
  164. getSelectionEnd()
  165. {
  166. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  167. if (tcp != null)
  168. selectionEnd = tcp.getSelectionEnd();
  169. return(selectionEnd);
  170. }
  171. /*************************************************************************/
  172. /**
  173. * Sets the ending position of the selected region to the
  174. * specified value. If the specified value is out of range, then it
  175. * will be silently changed to the nearest legal value.
  176. *
  177. * @param selectionEnd The new start position for selected text.
  178. */
  179. public synchronized void
  180. setSelectionEnd(int selectionEnd)
  181. {
  182. select(getSelectionStart(), selectionEnd);
  183. }
  184. /*************************************************************************/
  185. /**
  186. * This method sets the selected text range to the text between the
  187. * specified start and end positions. Illegal values for these
  188. * positions are silently fixed.
  189. *
  190. * @param startSelection The new start position for the selected text.
  191. * @param endSelection The new end position for the selected text.
  192. */
  193. public synchronized void
  194. select(int selectionStart, int endSelection)
  195. {
  196. if (selectionStart < 0)
  197. selectionStart = 0;
  198. if (selectionStart > getText().length())
  199. selectionStart = text.length();
  200. if (selectionEnd > text.length())
  201. selectionEnd = text.length();
  202. if (selectionStart > getSelectionEnd())
  203. selectionStart = selectionEnd;
  204. this.selectionStart = selectionStart;
  205. this.selectionEnd = selectionEnd;
  206. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  207. if (tcp != null)
  208. tcp.select(selectionStart, selectionEnd);
  209. }
  210. /*************************************************************************/
  211. /**
  212. * Selects all of the text in the component.
  213. */
  214. public synchronized void
  215. selectAll()
  216. {
  217. select(0, getText().length());
  218. }
  219. /*************************************************************************/
  220. /**
  221. * Returns the current caret position in the text.
  222. *
  223. * @return The caret position in the text.
  224. */
  225. public synchronized int
  226. getCaretPosition()
  227. {
  228. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  229. if (tcp != null)
  230. return(tcp.getCaretPosition());
  231. else
  232. return(0);
  233. }
  234. /*************************************************************************/
  235. /**
  236. * Sets the caret position to the specified value.
  237. *
  238. * @param caretPosition The new caret position.
  239. *
  240. * @exception IllegalArgumentException If the value supplied for position
  241. * is less than zero.
  242. *
  243. * @since 1.1
  244. */
  245. public synchronized void
  246. setCaretPosition(int caretPosition)
  247. {
  248. if (caretPosition < 0)
  249. throw new IllegalArgumentException ();
  250. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  251. if (tcp != null)
  252. tcp.setCaretPosition(caretPosition);
  253. }
  254. /*************************************************************************/
  255. /**
  256. * Tests whether or not this component's text can be edited.
  257. *
  258. * @return <code>true</code> if the text can be edited, <code>false</code>
  259. * otherwise.
  260. */
  261. public boolean
  262. isEditable()
  263. {
  264. return(editable);
  265. }
  266. /*************************************************************************/
  267. /**
  268. * Sets whether or not this component's text can be edited.
  269. *
  270. * @param editable <code>true</code> to enable editing of the text,
  271. * <code>false</code> to disable it.
  272. */
  273. public synchronized void
  274. setEditable(boolean editable)
  275. {
  276. this.editable = editable;
  277. TextComponentPeer tcp = (TextComponentPeer)getPeer();
  278. if (tcp != null)
  279. tcp.setEditable(editable);
  280. }
  281. /*************************************************************************/
  282. /**
  283. * Notifies the component that it should destroy its native peer.
  284. */
  285. public void
  286. removeNotify()
  287. {
  288. super.removeNotify();
  289. }
  290. /*************************************************************************/
  291. /**
  292. * Adds a new listener to the list of text listeners for this
  293. * component.
  294. *
  295. * @param listener The listener to be added.
  296. */
  297. public synchronized void
  298. addTextListener(TextListener listener)
  299. {
  300. textListener = AWTEventMulticaster.add(textListener, listener);
  301. enableEvents(AWTEvent.TEXT_EVENT_MASK);
  302. }
  303. /*************************************************************************/
  304. /**
  305. * Removes the specified listener from the list of listeners
  306. * for this component.
  307. *
  308. * @param listener The listener to remove.
  309. */
  310. public synchronized void
  311. removeTextListener(TextListener listener)
  312. {
  313. textListener = AWTEventMulticaster.remove(textListener, listener);
  314. }
  315. /*************************************************************************/
  316. /**
  317. * Processes the specified event for this component. Text events are
  318. * processed by calling the <code>processTextEvent()</code> method.
  319. * All other events are passed to the superclass method.
  320. *
  321. * @param event The event to process.
  322. */
  323. protected void
  324. processEvent(AWTEvent event)
  325. {
  326. if (event instanceof TextEvent)
  327. processTextEvent((TextEvent)event);
  328. else
  329. super.processEvent(event);
  330. }
  331. /*************************************************************************/
  332. /**
  333. * Processes the specified text event by dispatching it to any listeners
  334. * that are registered. Note that this method will only be called
  335. * if text event's are enabled. This will be true if there are any
  336. * registered listeners, or if the event has been specifically
  337. * enabled using <code>enableEvents()</code>.
  338. *
  339. * @param event The text event to process.
  340. */
  341. protected void
  342. processTextEvent(TextEvent event)
  343. {
  344. if (textListener != null)
  345. textListener.textValueChanged(event);
  346. }
  347. void
  348. dispatchEventImpl(AWTEvent e)
  349. {
  350. if (e.id <= TextEvent.TEXT_LAST
  351. && e.id >= TextEvent.TEXT_FIRST
  352. && (textListener != null
  353. || (eventMask & AWTEvent.TEXT_EVENT_MASK) != 0))
  354. processEvent(e);
  355. else
  356. super.dispatchEventImpl(e);
  357. }
  358. /*************************************************************************/
  359. /**
  360. * Returns a debugging string.
  361. *
  362. * @return A debugging string.
  363. */
  364. protected String
  365. paramString()
  366. {
  367. return(getClass().getName() + "(text=" + getText() + ")");
  368. }
  369. } // class TextComponent