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

Last change on this file since 3254 was 1392, checked in by bird, 22 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 ([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 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{