source: trunk/src/gcc/libjava/java/awt/TextComponent.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.7 KB
Line 
1/* TextComponent.java -- Widgets for entering text
2 Copyright (C) 1999 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 ([email protected])
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}
101
102/*************************************************************************/
103
104/*
105 * Instance Methods
106 */
107
108/**
109 * Returns the text in this component
110 *
111 * @return The text in this component.
112 */
113public synchronized String
114getText()
115{
116 TextComponentPeer tcp = (TextComponentPeer)getPeer();
117 if (tcp != null)
118 text = tcp.getText();
119
120 return(text);
121}
122
123/*************************************************************************/
124
125/**
126 * Sets the text in this component to the specified string.
127 *
128 * @param text The new text for this component.
129 */
130public synchronized void
131setText(String text)
132{
133 if (text == null)
134 text = "";
135
136 this.text = text;
137
138 TextComponentPeer tcp = (TextComponentPeer)getPeer();
139 if (tcp != null)
140 tcp.setText(text);
141}
142
143/*************************************************************************/
144
145/**
146 * Returns a string that contains the text that is currently selected.
147 *
148 * @return The currently selected text region.
149 */
150public synchronized String
151getSelectedText()
152{
153 String alltext = getText();
154 int start = getSelectionStart();
155 int end = getSelectionEnd();
156
157 return(alltext.substring(start, end));
158}
159
160/*************************************************************************/
161
162/**
163 * Returns the starting position of the selected text region.
164 * // FIXME: What is returned if there is no selected text?
165 *
166 * @return The starting position of the selected text region.
167 */
168public synchronized int
169getSelectionStart()
170{
171 TextComponentPeer tcp = (TextComponentPeer)getPeer();
172 if (tcp != null)
173 selectionStart = tcp.getSelectionStart();
174
175 return(selectionStart);
176}
177
178/*************************************************************************/
179
180/**
181 * Sets the starting position of the selected region to the
182 * specified value. If the specified value is out of range, then it
183 * will be silently changed to the nearest legal value.
184 *
185 * @param selectionStart The new start position for selected text.
186 */
187public synchronized void
188setSelectionStart(int selectionStart)
189{
190 select(selectionStart, getSelectionEnd());
191}
192
193/*************************************************************************/
194
195/**
196 * Returns the ending position of the selected text region.
197 * // FIXME: What is returned if there is no selected text.
198 *
199 * @return The ending position of the selected text region.
200 */
201public synchronized int
202getSelectionEnd()
203{
204 TextComponentPeer tcp = (TextComponentPeer)getPeer();
205 if (tcp != null)
206 selectionEnd = tcp.getSelectionEnd();
207
208 return(selectionEnd);
209}
210
211/*************************************************************************/
212
213/**
214 * Sets the ending position of the selected region to the
215 * specified value. If the specified value is out of range, then it
216 * will be silently changed to the nearest legal value.
217 *
218 * @param selectionEnd The new start position for selected text.
219 */
220public synchronized void
221setSelectionEnd(int selectionEnd)
222{
223 select(getSelectionStart(), selectionEnd);
224}
225
226/*************************************************************************/
227
228/**
229 * This method sets the selected text range to the text between the
230 * specified start and end positions. Illegal values for these
231 * positions are silently fixed.
232 *
233 * @param startSelection The new start position for the selected text.
234 * @param endSelection The new end position for the selected text.
235 */
236public synchronized void
237select(int selectionStart, int endSelection)
238{
239 if (selectionStart < 0)
240 selectionStart = 0;
241
242 if (selectionStart > getText().length())
243 selectionStart = text.length();
244
245 if (selectionEnd > text.length())
246 selectionEnd = text.length();
247
248 if (selectionStart > getSelectionEnd())
249 selectionStart = selectionEnd;
250
251 this.selectionStart = selectionStart;
252 this.selectionEnd = selectionEnd;
253
254 TextComponentPeer tcp = (TextComponentPeer)getPeer();
255 if (tcp != null)
256 tcp.select(selectionStart, selectionEnd);
257}
258
259/*************************************************************************/
260
261/**
262 * Selects all of the text in the component.
263 */
264public synchronized void
265selectAll()
266{
267 select(0, getText().length());
268}
269
270/*************************************************************************/
271
272/**
273 * Returns the current caret position in the text.
274 *
275 * @return The caret position in the text.
276 */
277public synchronized int
278getCaretPosition()
279{
280 TextComponentPeer tcp = (TextComponentPeer)getPeer();
281 if (tcp != null)
282 return(tcp.getCaretPosition());
283 else
284 return(0);
285}
286
287/*************************************************************************/
288
289/**
290 * Sets the caret position to the specified value.
291 *
292 * @param caretPosition The new caret position.
293 */
294public synchronized void
295setCaretPosition(int caretPosition)
296{
297 TextComponentPeer tcp = (TextComponentPeer)getPeer();
298 if (tcp != null)
299 tcp.setCaretPosition(caretPosition);
300}
301
302/*************************************************************************/
303
304/**
305 * Tests whether or not this component's text can be edited.
306 *
307 * @return <code>true</code> if the text can be edited, <code>false</code>
308 * otherwise.
309 */
310public boolean
311isEditable()
312{
313 return(editable);
314}
315
316/*************************************************************************/
317
318/**
319 * Sets whether or not this component's text can be edited.
320 *
321 * @param editable <code>true</code> to enable editing of the text,
322 * <code>false</code> to disable it.
323 */
324public synchronized void
325setEditable(boolean editable)
326{
327 this.editable = editable;
328
329 TextComponentPeer tcp = (TextComponentPeer)getPeer();
330 if (tcp != null)
331 tcp.setEditable(editable);
332}
333
334/*************************************************************************/
335
336/**
337 * Notifies the component that it should destroy its native peer.
338 */
339public void
340removeNotify()
341{
342 super.removeNotify();
343}
344
345/*************************************************************************/
346
347/**
348 * Adds a new listener to the list of text listeners for this
349 * component.
350 *
351 * @param listener The listener to be added.
352 */
353public synchronized void
354addTextListener(TextListener listener)
355{
356 textListener = AWTEventMulticaster.add(textListener, listener);
357
358 enableEvents(AWTEvent.TEXT_EVENT_MASK);
359}
360
361/*************************************************************************/
362
363/**
364 * Removes the specified listener from the list of listeners
365 * for this component.
366 *
367 * @param listener The listener to remove.
368 */
369public synchronized void
370removeTextListener(TextListener listener)
371{
372 textListener = AWTEventMulticaster.remove(textListener, listener);
373}
374
375/*************************************************************************/
376
377/**
378 * Processes the specified event for this component. Text events are
379 * processed by calling the <code>processTextEvent()</code> method.
380 * All other events are passed to the superclass method.
381 *
382 * @param event The event to process.
383 */
384protected void
385processEvent(AWTEvent event)
386{
387 if (event instanceof TextEvent)
388 processTextEvent((TextEvent)event);
389 else
390 super.processEvent(event);
391}
392
393/*************************************************************************/
394
395/**
396 * Processes the specified text event by dispatching it to any listeners
397 * that are registered. Note that this method will only be called
398 * if text event's are enabled. This will be true if there are any
399 * registered listeners, or if the event has been specifically
400 * enabled using <code>enableEvents()</code>.
401 *
402 * @param event The text event to process.
403 */
404protected void
405processTextEvent(TextEvent event)
406{
407 if (textListener != null)
408 textListener.textValueChanged(event);
409}
410
411/*************************************************************************/
412
413/**
414 * Returns a debugging string.
415 *
416 * @return A debugging string.
417 */
418protected String
419paramString()
420{
421 return(getClass().getName() + "(text=" + getText() + ")");
422}
423
424} // class TextComponent
425
Note: See TracBrowser for help on using the repository browser.