1 | /* TextArea.java -- A multi-line text entry widget
|
---|
2 | Copyright (C) 1999 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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)
|
---|
9 | any later version.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
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
|
---|
24 | combination.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.awt;
|
---|
40 |
|
---|
41 | import java.awt.peer.TextAreaPeer;
|
---|
42 | import java.awt.peer.TextComponentPeer;
|
---|
43 | import java.awt.peer.ComponentPeer;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * This implements a multi-line text entry widget.
|
---|
47 | *
|
---|
48 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
49 | */
|
---|
50 | public class TextArea extends TextComponent implements java.io.Serializable
|
---|
51 | {
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Static Variables
|
---|
55 | */
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Use both horiztonal and vertical scroll bars.
|
---|
59 | */
|
---|
60 | public static final int SCROLLBARS_BOTH = 0;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Use vertical scroll bars only.
|
---|
64 | */
|
---|
65 | public static final int SCROLLBARS_VERTICAL_ONLY = 1;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Use horizatonal scroll bars only.
|
---|
69 | */
|
---|
70 | public static final int SCROLLBARS_HORIZONTAL_ONLY = 2;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Use no scrollbars.
|
---|
74 | */
|
---|
75 | public static final int SCROLLBARS_NONE = 3;
|
---|
76 |
|
---|
77 | // Serialization constant
|
---|
78 | private static final long serialVersionUID = 3692302836626095722L;
|
---|
79 |
|
---|
80 | /*************************************************************************/
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Instance Variables
|
---|
84 | */
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @serial The number of columns in this text area.
|
---|
88 | */
|
---|
89 | private int columns;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * @serial The number of rows in this text area.
|
---|
93 | */
|
---|
94 | private int rows;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * @serial The type of scrollbars to display, which will be one of
|
---|
98 | * the contstants from this class.
|
---|
99 | */
|
---|
100 | private int scrollbarVisibility;
|
---|
101 |
|
---|
102 | /*************************************************************************/
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Constructors
|
---|
106 | */
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Initialize a new instance of <code>TextArea</code> that is empty
|
---|
110 | * and is one row and one column. Both horizontal and vertical
|
---|
111 | * scrollbars will be used.
|
---|
112 | *
|
---|
113 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
|
---|
114 | */
|
---|
115 | public
|
---|
116 | TextArea()
|
---|
117 | {
|
---|
118 | this("", 1, 1, SCROLLBARS_BOTH);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /*************************************************************************/
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Initializes a new instance of <code>TextArea</code> that
|
---|
125 | * contains the specified string. Both horizontal and veritcal
|
---|
126 | * scrollbars will be used.
|
---|
127 | *
|
---|
128 | * @param text The text to display in this text area.
|
---|
129 | *
|
---|
130 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
|
---|
131 | */
|
---|
132 | public
|
---|
133 | TextArea(String text)
|
---|
134 | {
|
---|
135 | this(text, 1, text.length(), SCROLLBARS_BOTH);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*************************************************************************/
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Initializes a new instance of <code>TextArea</code> that is empty
|
---|
142 | * and has the specified number of rows and columns. Both
|
---|
143 | * horizontal and vertical scrollbars will be used.
|
---|
144 | *
|
---|
145 | * @param rows The number of rows in this text area.
|
---|
146 | * @param columns The number of columns in this text area.
|
---|
147 | *
|
---|
148 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
|
---|
149 | */
|
---|
150 | public
|
---|
151 | TextArea(int rows, int columns)
|
---|
152 | {
|
---|
153 | this("", rows, columns, SCROLLBARS_BOTH);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /*************************************************************************/
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Initializes a new instance of <code>TextArea</code> that is the
|
---|
160 | * specified size and has the specified text.
|
---|
161 | *
|
---|
162 | * @param text The text to display in this text area.
|
---|
163 | * @param rows The number of rows in this text area.
|
---|
164 | * @param columns The number of columns in this text area.
|
---|
165 | *
|
---|
166 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
|
---|
167 | */
|
---|
168 | public
|
---|
169 | TextArea(String text, int rows, int columns)
|
---|
170 | {
|
---|
171 | this(text, rows, columns, SCROLLBARS_BOTH);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /*************************************************************************/
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Initializes a new instance of <code>TextArea</code> with the
|
---|
178 | * specified values. The scrollbar visibility value must be one
|
---|
179 | * of the constants in this class.
|
---|
180 | *
|
---|
181 | * @param text The text to display in this text area.
|
---|
182 | * @param rows The number of rows in this text area.
|
---|
183 | * @param columns The number of columns in this text area.
|
---|
184 | * @param scrollbarVisibility Which scrollbars to display.
|
---|
185 | *
|
---|
186 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
|
---|
187 | */
|
---|
188 | public
|
---|
189 | TextArea(String text, int rows, int columns, int scrollbarVisibility)
|
---|
190 | {
|
---|
191 | super(text);
|
---|
192 |
|
---|
193 | if (GraphicsEnvironment.isHeadless())
|
---|
194 | throw new HeadlessException ();
|
---|
195 |
|
---|
196 | if ((rows < 1) || (columns < 0))
|
---|
197 | throw new IllegalArgumentException("Bad row or column value");
|
---|
198 |
|
---|
199 | if ((scrollbarVisibility != SCROLLBARS_BOTH) &&
|
---|
200 | (scrollbarVisibility != SCROLLBARS_VERTICAL_ONLY) &&
|
---|
201 | (scrollbarVisibility != SCROLLBARS_HORIZONTAL_ONLY) &&
|
---|
202 | (scrollbarVisibility != SCROLLBARS_NONE))
|
---|
203 | throw new IllegalArgumentException("Bad scrollbar visibility value");
|
---|
204 |
|
---|
205 | this.rows = rows;
|
---|
206 | this.columns = columns;
|
---|
207 | this.scrollbarVisibility = scrollbarVisibility;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*************************************************************************/
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * Instance Variables
|
---|
214 | */
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Returns the number of columns in the field.
|
---|
218 | *
|
---|
219 | * @return The number of columns in the field.
|
---|
220 | */
|
---|
221 | public int
|
---|
222 | getColumns()
|
---|
223 | {
|
---|
224 | return(columns);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*************************************************************************/
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Sets the number of columns in this field to the specified value.
|
---|
231 | *
|
---|
232 | * @param columns The new number of columns in the field.
|
---|
233 | *
|
---|
234 | * @exception IllegalArgumentException If columns is less than zero.
|
---|
235 | */
|
---|
236 | public synchronized void
|
---|
237 | setColumns(int columns)
|
---|
238 | {
|
---|
239 | if (columns < 0)
|
---|
240 | throw new IllegalArgumentException("Value is less than zero: " +
|
---|
241 | columns);
|
---|
242 |
|
---|
243 | this.columns = columns;
|
---|
244 | // FIXME: How to we communicate this to our peer?
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*************************************************************************/
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Returns the number of rows in the field.
|
---|
251 | *
|
---|
252 | * @return The number of rows in the field.
|
---|
253 | */
|
---|
254 | public int
|
---|
255 | getRows()
|
---|
256 | {
|
---|
257 | return(rows);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /*************************************************************************/
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Sets the number of rows in this field to the specified value.
|
---|
264 | *
|
---|
265 | * @param rows The new number of rows in the field.
|
---|
266 | *
|
---|
267 | * @exception IllegalArgumentException If rows is less than zero.
|
---|
268 | */
|
---|
269 | public synchronized void
|
---|
270 | setRows(int rows)
|
---|
271 | {
|
---|
272 | if (rows < 1)
|
---|
273 | throw new IllegalArgumentException("Value is less than one: " +
|
---|
274 | rows);
|
---|
275 |
|
---|
276 | this.rows = rows;
|
---|
277 | // FIXME: How to we communicate this to our peer?
|
---|
278 | }
|
---|
279 |
|
---|
280 | /*************************************************************************/
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Returns the minimum size for this text field.
|
---|
284 | *
|
---|
285 | * @return The minimum size for this text field.
|
---|
286 | */
|
---|
287 | public Dimension
|
---|
288 | getMinimumSize()
|
---|
289 | {
|
---|
290 | return(getMinimumSize(getRows(), getColumns()));
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*************************************************************************/
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Returns the minimum size of a text field with the specified number
|
---|
297 | * of rows and columns.
|
---|
298 | *
|
---|
299 | * @param rows The number of rows to get the minimum size for.
|
---|
300 | * @param columns The number of columns to get the minimum size for.
|
---|
301 | */
|
---|
302 | public Dimension
|
---|
303 | getMinimumSize(int rows, int columns)
|
---|
304 | {
|
---|
305 | TextAreaPeer tap = (TextAreaPeer)getPeer();
|
---|
306 | if (tap == null)
|
---|
307 | return(null); // FIXME: What do we do if there is no peer?
|
---|
308 |
|
---|
309 | return(tap.getMinimumSize(rows, columns));
|
---|
310 | }
|
---|
311 |
|
---|
312 | /*************************************************************************/
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Returns the minimum size for this text field.
|
---|
316 | *
|
---|
317 | * @return The minimum size for this text field.
|
---|
318 | *
|
---|
319 | * @deprecated This method is depcreated in favor of
|
---|
320 | * <code>getMinimumSize()</code>.
|
---|
321 | */
|
---|
322 | public Dimension
|
---|
323 | minimumSize()
|
---|
324 | {
|
---|
325 | return(getMinimumSize(getRows(), getColumns()));
|
---|
326 | }
|
---|
327 |
|
---|
328 | /*************************************************************************/
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Returns the minimum size of a text field with the specified number
|
---|
332 | * of rows and columns.
|
---|
333 | *
|
---|
334 | * @param rows The number of rows to get the minimum size for.
|
---|
335 | * @param columns The number of columns to get the minimum size for.
|
---|
336 | *
|
---|
337 | * @deprecated This method is deprecated in favor of
|
---|
338 | * <code>getMinimumSize(int)</code>.
|
---|
339 | */
|
---|
340 | public Dimension
|
---|
341 | minimumSize(int rows, int columns)
|
---|
342 | {
|
---|
343 | return(getMinimumSize(rows, columns));
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*************************************************************************/
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Returns the preferred size for this text field.
|
---|
350 | *
|
---|
351 | * @return The preferred size for this text field.
|
---|
352 | */
|
---|
353 | public Dimension
|
---|
354 | getPreferredSize()
|
---|
355 | {
|
---|
356 | return(getPreferredSize(getRows(), getColumns()));
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*************************************************************************/
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Returns the preferred size of a text field with the specified number
|
---|
363 | * of rows and columns.
|
---|
364 | *
|
---|
365 | * @param rows The number of rows to get the preferred size for.
|
---|
366 | * @param columns The number of columns to get the preferred size for.
|
---|
367 | */
|
---|
368 | public Dimension
|
---|
369 | getPreferredSize(int rows, int columns)
|
---|
370 | {
|
---|
371 | TextAreaPeer tap = (TextAreaPeer)getPeer();
|
---|
372 | if (tap == null)
|
---|
373 | return(null); // FIXME: What do we do if there is no peer?
|
---|
374 |
|
---|
375 | return(tap.getPreferredSize(rows, columns));
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*************************************************************************/
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Returns the preferred size for this text field.
|
---|
382 | *
|
---|
383 | * @return The preferred size for this text field.
|
---|
384 | *
|
---|
385 | * @deprecated This method is deprecated in favor of
|
---|
386 | * <code>getPreferredSize()</code>.
|
---|
387 | */
|
---|
388 | public Dimension
|
---|
389 | preferredSize()
|
---|
390 | {
|
---|
391 | return(getPreferredSize(getRows(), getColumns()));
|
---|
392 | }
|
---|
393 |
|
---|
394 | /*************************************************************************/
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Returns the preferred size of a text field with the specified number
|
---|
398 | * of rows and columns.
|
---|
399 | *
|
---|
400 | * @param rows The number of rows to get the preferred size for.
|
---|
401 | * @param columns The number of columns to get the preferred size for.
|
---|
402 | *
|
---|
403 | * @deprecated This method is deprecated in favor of
|
---|
404 | * <code>getPreferredSize(int)</code>.
|
---|
405 | */
|
---|
406 | public Dimension
|
---|
407 | preferredSize(int columns)
|
---|
408 | {
|
---|
409 | return(getPreferredSize(rows, columns));
|
---|
410 | }
|
---|
411 |
|
---|
412 | /*************************************************************************/
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Returns one of the constants from this class indicating which
|
---|
416 | * types of scrollbars this object uses, if any.
|
---|
417 | *
|
---|
418 | * @return The scrollbar type constant for this object.
|
---|
419 | */
|
---|
420 | public int
|
---|
421 | getScrollbarVisibility()
|
---|
422 | {
|
---|
423 | return(scrollbarVisibility);
|
---|
424 | }
|
---|
425 |
|
---|
426 | /*************************************************************************/
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Notify this object that it should create its native peer.
|
---|
430 | */
|
---|
431 | public void
|
---|
432 | addNotify()
|
---|
433 | {
|
---|
434 | if (getPeer() != null)
|
---|
435 | return;
|
---|
436 |
|
---|
437 | setPeer((ComponentPeer)getToolkit().createTextArea(this));
|
---|
438 | }
|
---|
439 |
|
---|
440 | /*************************************************************************/
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Appends the specified text to the end of the current text.
|
---|
444 | *
|
---|
445 | * @param text The text to append.
|
---|
446 | */
|
---|
447 | public void
|
---|
448 | append(String str)
|
---|
449 | {
|
---|
450 | TextAreaPeer tap = (TextAreaPeer)getPeer();
|
---|
451 | if (tap == null)
|
---|
452 | return;
|
---|
453 |
|
---|
454 | tap.insert(str, tap.getText().length());
|
---|
455 | }
|
---|
456 |
|
---|
457 | /*************************************************************************/
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Appends the specified text to the end of the current text.
|
---|
461 | *
|
---|
462 | * @param text The text to append.
|
---|
463 | *
|
---|
464 | * @deprecated This method is deprecated in favor of
|
---|
465 | * <code>append()</code>.
|
---|
466 | */
|
---|
467 | public void
|
---|
468 | appendText(String text)
|
---|
469 | {
|
---|
470 | append(text);
|
---|
471 | }
|
---|
472 |
|
---|
473 | /*************************************************************************/
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Inserts the specified text at the specified location.
|
---|
477 | *
|
---|
478 | * @param text The text to insert.
|
---|
479 | * @param pos The insert position.
|
---|
480 | */
|
---|
481 | public void
|
---|
482 | insert(String text, int pos)
|
---|
483 | {
|
---|
484 | TextAreaPeer tap = (TextAreaPeer)getPeer();
|
---|
485 | if (tap == null)
|
---|
486 | return;
|
---|
487 |
|
---|
488 | tap.insert(text, pos);
|
---|
489 | }
|
---|
490 |
|
---|
491 | /*************************************************************************/
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Inserts the specified text at the specified location.
|
---|
495 | *
|
---|
496 | * @param text The text to insert.
|
---|
497 | * @param pos The insert position.
|
---|
498 | *
|
---|
499 | * @deprecated This method is depcreated in favor of <code>insert()</code>.
|
---|
500 | */
|
---|
501 | public void
|
---|
502 | insertText(String text, int pos)
|
---|
503 | {
|
---|
504 | insert(text, pos);
|
---|
505 | }
|
---|
506 |
|
---|
507 | /*************************************************************************/
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Replaces the text bounded by the specified start and end positions
|
---|
511 | * with the specified text.
|
---|
512 | *
|
---|
513 | * @param text The new text for the range.
|
---|
514 | * @param start The start position of the replacement range.
|
---|
515 | * @param end The end position of the replacement range.
|
---|
516 | */
|
---|
517 | public void
|
---|
518 | replaceRange(String text, int start, int end)
|
---|
519 | {
|
---|
520 | TextAreaPeer tap = (TextAreaPeer)getPeer();
|
---|
521 | if (tap == null)
|
---|
522 | return;
|
---|
523 |
|
---|
524 | tap.replaceRange(text, start, end);
|
---|
525 | }
|
---|
526 |
|
---|
527 | /*************************************************************************/
|
---|
528 |
|
---|
529 | /**
|
---|
530 | * Replaces the text bounded by the specified start and end positions
|
---|
531 | * with the specified text.
|
---|
532 | *
|
---|
533 | * @param text The new text for the range.
|
---|
534 | * @param start The start position of the replacement range.
|
---|
535 | * @param end The end position of the replacement range.
|
---|
536 | *
|
---|
537 | * @deprecated This method is deprecated in favor of
|
---|
538 | * <code>replaceRange()</code>.
|
---|
539 | */
|
---|
540 | public void
|
---|
541 | replaceText(String text, int start, int end)
|
---|
542 | {
|
---|
543 | replaceRange(text, start, end);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /*************************************************************************/
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * Returns a debugging string for this text area.
|
---|
550 | *
|
---|
551 | * @return A debugging string for this text area.
|
---|
552 | */
|
---|
553 | protected String
|
---|
554 | paramString()
|
---|
555 | {
|
---|
556 | return(getClass().getName() + "(rows=" + getRows() + ",columns=" +
|
---|
557 | getColumns() + ",scrollbars=" + getScrollbarVisibility() +
|
---|
558 | ")");
|
---|
559 | }
|
---|
560 |
|
---|
561 | } // class TextArea
|
---|
562 |
|
---|