1 /* TextComponent.java -- Widgets for entering text
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.event.TextEvent;
42 import java.awt.event.TextListener;
43 import java.awt.peer.TextComponentPeer;
44 import java.awt.peer.ComponentPeer;
47 * This class provides common functionality for widgets than
50 * @author Aaron M. Renn (arenn@urbanophile.com)
52 public class TextComponent extends Component implements java.io.Serializable
59 // Constant for serialization
60 private static final long serialVersionUID = -2214773872412987419L;
67 * @serial Indicates whether or not this component is editable.
69 private boolean editable;
72 * @serial The starting position of the selected text region.
74 private int selectionStart;
77 * @serial The ending position of the selected text region.
79 private int selectionEnd;
82 * @serial The text in the component
87 * A list of listeners that will receive events from this object.
89 protected transient TextListener textListener;
91 /*************************************************************************/
97 TextComponent(String text)
100 this.editable = true;
103 /*************************************************************************/
110 * Returns the text in this component
112 * @return The text in this component.
114 public synchronized String
117 TextComponentPeer tcp = (TextComponentPeer)getPeer();
119 text = tcp.getText();
124 /*************************************************************************/
127 * Sets the text in this component to the specified string.
129 * @param text The new text for this component.
131 public synchronized void
139 TextComponentPeer tcp = (TextComponentPeer)getPeer();
144 /*************************************************************************/
147 * Returns a string that contains the text that is currently selected.
149 * @return The currently selected text region.
151 public synchronized String
154 String alltext = getText();
155 int start = getSelectionStart();
156 int end = getSelectionEnd();
158 return(alltext.substring(start, end));
161 /*************************************************************************/
164 * Returns the starting position of the selected text region.
165 * // FIXME: What is returned if there is no selected text?
167 * @return The starting position of the selected text region.
169 public synchronized int
172 TextComponentPeer tcp = (TextComponentPeer)getPeer();
174 selectionStart = tcp.getSelectionStart();
176 return(selectionStart);
179 /*************************************************************************/
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.
186 * @param selectionStart The new start position for selected text.
188 public synchronized void
189 setSelectionStart(int selectionStart)
191 select(selectionStart, getSelectionEnd());
194 /*************************************************************************/
197 * Returns the ending position of the selected text region.
198 * // FIXME: What is returned if there is no selected text.
200 * @return The ending position of the selected text region.
202 public synchronized int
205 TextComponentPeer tcp = (TextComponentPeer)getPeer();
207 selectionEnd = tcp.getSelectionEnd();
209 return(selectionEnd);
212 /*************************************************************************/
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.
219 * @param selectionEnd The new start position for selected text.
221 public synchronized void
222 setSelectionEnd(int selectionEnd)
224 select(getSelectionStart(), selectionEnd);
227 /*************************************************************************/
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.
234 * @param startSelection The new start position for the selected text.
235 * @param endSelection The new end position for the selected text.
237 public synchronized void
238 select(int selectionStart, int endSelection)
240 if (selectionStart < 0)
243 if (selectionStart > getText().length())
244 selectionStart = text.length();
246 if (selectionEnd > text.length())
247 selectionEnd = text.length();
249 if (selectionStart > getSelectionEnd())
250 selectionStart = selectionEnd;
252 this.selectionStart = selectionStart;
253 this.selectionEnd = selectionEnd;
255 TextComponentPeer tcp = (TextComponentPeer)getPeer();
257 tcp.select(selectionStart, selectionEnd);
260 /*************************************************************************/
263 * Selects all of the text in the component.
265 public synchronized void
268 select(0, getText().length());
271 /*************************************************************************/
274 * Returns the current caret position in the text.
276 * @return The caret position in the text.
278 public synchronized int
281 TextComponentPeer tcp = (TextComponentPeer)getPeer();
283 return(tcp.getCaretPosition());
288 /*************************************************************************/
291 * Sets the caret position to the specified value.
293 * @param caretPosition The new caret position.
295 * @exception IllegalArgumentException If the value supplied for position
300 public synchronized void
301 setCaretPosition(int caretPosition)
303 if (caretPosition < 0)
304 throw new IllegalArgumentException ();
306 TextComponentPeer tcp = (TextComponentPeer)getPeer();
308 tcp.setCaretPosition(caretPosition);
311 /*************************************************************************/
314 * Tests whether or not this component's text can be edited.
316 * @return <code>true</code> if the text can be edited, <code>false</code>
325 /*************************************************************************/
328 * Sets whether or not this component's text can be edited.
330 * @param editable <code>true</code> to enable editing of the text,
331 * <code>false</code> to disable it.
333 public synchronized void
334 setEditable(boolean editable)
336 this.editable = editable;
338 TextComponentPeer tcp = (TextComponentPeer)getPeer();
340 tcp.setEditable(editable);
343 /*************************************************************************/
346 * Notifies the component that it should destroy its native peer.
351 super.removeNotify();
354 /*************************************************************************/
357 * Adds a new listener to the list of text listeners for this
360 * @param listener The listener to be added.
362 public synchronized void
363 addTextListener(TextListener listener)
365 textListener = AWTEventMulticaster.add(textListener, listener);
367 enableEvents(AWTEvent.TEXT_EVENT_MASK);
370 /*************************************************************************/
373 * Removes the specified listener from the list of listeners
374 * for this component.
376 * @param listener The listener to remove.
378 public synchronized void
379 removeTextListener(TextListener listener)
381 textListener = AWTEventMulticaster.remove(textListener, listener);
384 /*************************************************************************/
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.
391 * @param event The event to process.
394 processEvent(AWTEvent event)
396 if (event instanceof TextEvent)
397 processTextEvent((TextEvent)event);
399 super.processEvent(event);
402 /*************************************************************************/
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>.
411 * @param event The text event to process.
414 processTextEvent(TextEvent event)
416 if (textListener != null)
417 textListener.textValueChanged(event);
421 dispatchEventImpl(AWTEvent e)
423 if (e.id <= TextEvent.TEXT_LAST
424 && e.id >= TextEvent.TEXT_FIRST
425 && (textListener != null
426 || (eventMask & AWTEvent.TEXT_EVENT_MASK) != 0))
429 super.dispatchEventImpl(e);
432 /*************************************************************************/
435 * Returns a debugging string.
437 * @return A debugging string.
442 return(getClass().getName() + "(text=" + getText() + ")");
445 } // class TextComponent