source: trunk/gcc/libjava/java/awt/TextComponent.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.1 KB
Line 
1/* TextComponent.java -- Widgets for entering text
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.event.TextEvent;
42import java.awt.event.TextListener;
43import java.awt.peer.TextComponentPeer;
44import java.awt.peer.ComponentPeer;
45
46/**
47 * This class provides common functionality for widgets than
48 * contain text.
49 *
50 * @author Aaron M. Renn (arenn@urbanophile.com)
51 */
52public class TextComponent extends Component implements java.io.Serializable
53{
54
55/*
56 * Static Variables
57 */
58
59// Constant for serialization
60private static final long serialVersionUID = -2214773872412987419L;
61
62/*
63 * Instance Variables
64 */
65
66/**
67 * @serial Indicates whether or not this component is editable.
68 */
69private boolean editable;
70
71/**
72 * @serial The starting position of the selected text region.
73 */
74private int selectionStart;
75
76/**
77 * @serial The ending position of the selected text region.
78 */
79private int selectionEnd;
80
81/**
82 * @serial The text in the component
83 */
84private String text;
85
86/**
87 * A list of listeners that will receive events from this object.
88 */
89protected transient TextListener textListener;
90
91/*************************************************************************/
92
93/*
94 * Constructors
95 */
96
97TextComponent(String text)
98{
99 this.text = text;
100 this.editable = true;
101}
102
103/*************************************************************************/
104
105/*
106 * Instance Methods
107 */
108
109/**
110 * Returns the text in this component
111 *
112 * @return The text in this component.
113 */
114public synchronized String
115getText()
116{
117 TextComponentPeer tcp = (TextComponentPeer)getPeer();
118 if (tcp != null)
119 text = tcp.getText();
120
121 return(text);
122}
123
124/*************************************************************************/
125
126/**
127 * Sets the text in this component to the specified string.
128 *
129 * @param text The new text for this component.
130 */
131public synchronized void
132setText(String text)
133{
134 if (text == null)
135 text = "";
136
137 this.text = text;
138
139 TextComponentPeer tcp = (TextComponentPeer)getPeer();
140 if (tcp != null)
141 tcp.setText(text);
142}
143
144/*************************************************************************/
145
146/**
147 * Returns a string that contains the text that is currently selected.
148 *
149 * @return The currently selected text region.
150 */
151public synchronized String
152getSelectedText()
153{
154 String alltext = getText();
155 int start = getSelectionStart();
156 int end = getSelectionEnd();
157
158 return(alltext.substring(start, end));
159}
160
161/*************************************************************************/
162
163/**
164 * Returns the starting position of the selected text region.
165 * // FIXME: What is returned if there is no selected text?
166 *
167 * @return The starting position of the selected text region.
168 */
169public synchronized int
170getSelectionStart()
171{
172 TextComponentPeer tcp = (TextComponentPeer)getPeer();
173 if (tcp != null)
174 selectionStart = tcp.getSelectionStart();
175
176 return(selectionStart);
177}
178
179/*************************************************************************/
180
181/**
182 * Sets the starting position of the selected region to the
183 * specified value. If the specified value is out of range, then it
184 * will be silently changed to the nearest legal value.
185 *
186 * @param selectionStart The new start position for selected text.
187 */
188public synchronized void
189setSelectionStart(int selectionStart)
190{
191 select(selectionStart, getSelectionEnd());
192}
193
194/*************************************************************************/
195
196/**
197 * Returns the ending position of the selected text region.
198 * // FIXME: What is returned if there is no selected text.
199 *
200 * @return The ending position of the selected text region.
201 */
202public synchronized int
203getSelectionEnd()
204{
205 TextComponentPeer tcp = (TextComponentPeer)getPeer();
206 if (tcp != null)
207 selectionEnd = tcp.getSelectionEnd();
208
209 return(selectionEnd);
210}
211
212/*************************************************************************/
213
214/**
215 * Sets the ending position of the selected region to the
216 * specified value. If the specified value is out of range, then it
217 * will be silently changed to the nearest legal value.
218 *
219 * @param selectionEnd The new start position for selected text.
220 */
221public synchronized void
222setSelectionEnd(int selectionEnd)
223{
224 select(getSelectionStart(), selectionEnd);
225}
226
227/*************************************************************************/
228
229/**
230 * This method sets the selected text range to the text between the
231 * specified start and end positions. Illegal values for these
232 * positions are silently fixed.
233 *
234 * @param startSelection The new start position for the selected text.
235 * @param endSelection The new end position for the selected text.
236 */
237public synchronized void
238select(int selectionStart, int endSelection)
239{
240 if (selectionStart < 0)
241 selectionStart = 0;
242
243 if (selectionStart > getText().length())
244 selectionStart = text.length();
245
246 if (selectionEnd > text.length())
247 selectionEnd = text.length();
248
249 if (selectionStart > getSelectionEnd())
250 selectionStart = selectionEnd;
251
252 this.selectionStart = selectionStart;
253 this.selectionEnd = selectionEnd;
254
255 TextComponentPeer tcp = (TextComponentPeer)getPeer();
256 if (tcp != null)
257 tcp.select(selectionStart, selectionEnd);
258}
259
260/*************************************************************************/
261
262/**
263 * Selects all of the text in the component.
264 */
265public synchronized void
266selectAll()
267{
268 select(0, getText().length());
269}
270
271/*************************************************************************/
272
273/**
274 * Returns the current caret position in the text.
275 *
276 * @return The caret position in the text.
277 */
278public synchronized int
279getCaretPosition()
280{
281 TextComponentPeer tcp = (TextComponentPeer)getPeer();
282 if (tcp != null)
283 return(tcp.getCaretPosition());
284 else
285 return(0);
286}
287
288/*************************************************************************/
289
290/**
291 * Sets the caret position to the specified value.
292 *
293 * @param caretPosition The new caret position.
294 *
295 * @exception IllegalArgumentException If the value supplied for position
296 * is less than zero.
297 *
298 * @since 1.1
299 */
300public synchronized void
301setCaretPosition(int caretPosition)
302{
303 if (caretPosition < 0)
304 throw new IllegalArgumentException ();
305
306 TextComponentPeer tcp = (TextComponentPeer)getPeer();
307 if (tcp != null)
308 tcp.setCaretPosition(caretPosition);
309}
310
311/*************************************************************************/
312
313/**
314 * Tests whether or not this component's text can be edited.
315 *
316 * @return <code>true</code> if the text can be edited, <code>false</code>
317 * otherwise.
318 */
319public boolean
320isEditable()
321{
322 return(editable);
323}
324
325/*************************************************************************/
326
327/**
328 * Sets whether or not this component's text can be edited.
329 *
330 * @param editable <code>true</code> to enable editing of the text,
331 * <code>false</code> to disable it.
332 */
333public synchronized void
334setEditable(boolean editable)
335{
336 this.editable = editable;
337
338 TextComponentPeer tcp = (TextComponentPeer)getPeer();
339 if (tcp != null)
340 tcp.setEditable(editable);
341}
342
343/*************************************************************************/
344
345/**
346 * Notifies the component that it should destroy its native peer.
347 */
348public void
349removeNotify()
350{
351 super.removeNotify();
352}
353
354/*************************************************************************/
355
356/**
357 * Adds a new listener to the list of text listeners for this
358 * component.
359 *
360 * @param listener The listener to be added.
361 */
362public synchronized void
363addTextListener(TextListener listener)
364{
365 textListener = AWTEventMulticaster.add(textListener, listener);
366
367 enableEvents(AWTEvent.TEXT_EVENT_MASK);
368}
369
370/*************************************************************************/
371
372/**
373 * Removes the specified listener from the list of listeners
374 * for this component.
375 *
376 * @param listener The listener to remove.
377 */
378public synchronized void
379removeTextListener(TextListener listener)
380{
381 textListener = AWTEventMulticaster.remove(textListener, listener);
382}
383
384/*************************************************************************/
385
386/**
387 * Processes the specified event for this component. Text events are
388 * processed by calling the <code>processTextEvent()</code> method.
389 * All other events are passed to the superclass method.
390 *
391 * @param event The event to process.
392 */
393protected void
394processEvent(AWTEvent event)
395{
396 if (event instanceof TextEvent)
397 processTextEvent((TextEvent)event);
398 else
399 super.processEvent(event);
400}
401
402/*************************************************************************/
403
404/**
405 * Processes the specified text event by dispatching it to any listeners
406 * that are registered. Note that this method will only be called
407 * if text event's are enabled. This will be true if there are any
408 * registered listeners, or if the event has been specifically
409 * enabled using <code>enableEvents()</code>.
410 *
411 * @param event The text event to process.
412 */
413protected void
414processTextEvent(TextEvent event)
415{
416 if (textListener != null)
417 textListener.textValueChanged(event);
418}
419
420void
421dispatchEventImpl(AWTEvent e)
422{
423 if (e.id <= TextEvent.TEXT_LAST
424 && e.id >= TextEvent.TEXT_FIRST
425 && (textListener != null
426 || (eventMask & AWTEvent.TEXT_EVENT_MASK) != 0))
427 processEvent(e);
428 else
429 super.dispatchEventImpl(e);
430}
431
432/*************************************************************************/
433
434/**
435 * Returns a debugging string.
436 *
437 * @return A debugging string.
438 */
439protected String
440paramString()
441{
442 return(getClass().getName() + "(text=" + getText() + ")");
443}
444
445} // class TextComponent
446
Note: See TracBrowser for help on using the repository browser.