1 /* TextArea.java -- A multi-line text entry widget
2 Copyright (C) 1999 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.peer.TextAreaPeer;
42 import java.awt.peer.TextComponentPeer;
43 import java.awt.peer.ComponentPeer;
46 * This implements a multi-line text entry widget.
48 * @author Aaron M. Renn (arenn@urbanophile.com)
50 public class TextArea extends TextComponent implements java.io.Serializable
58 * Use both horiztonal and vertical scroll bars.
60 public static final int SCROLLBARS_BOTH = 0;
63 * Use vertical scroll bars only.
65 public static final int SCROLLBARS_VERTICAL_ONLY = 1;
68 * Use horizatonal scroll bars only.
70 public static final int SCROLLBARS_HORIZONTAL_ONLY = 2;
75 public static final int SCROLLBARS_NONE = 3;
77 // Serialization constant
78 private static final long serialVersionUID = 3692302836626095722L;
80 /*************************************************************************/
87 * @serial The number of columns in this text area.
92 * @serial The number of rows in this text area.
97 * @serial The type of scrollbars to display, which will be one of
98 * the contstants from this class.
100 private int scrollbarVisibility;
102 /*************************************************************************/
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.
116 this("", 1, 1, SCROLLBARS_BOTH);
119 /*************************************************************************/
122 * Initializes a new instance of <code>TextArea</code> that
123 * contains the specified string. Both horizontal and veritcal
124 * scrollbars will be used.
126 * @param text The text to display in this text area.
129 TextArea(String text)
131 this(text, 1, text.length(), SCROLLBARS_BOTH);
134 /*************************************************************************/
137 * Initializes a new instance of <code>TextArea</code> that is empty
138 * and has the specified number of rows and columns. Both
139 * horizontal and vertical scrollbars will be used.
141 * @param rows The number of rows in this text area.
142 * @param columns The number of columns in this text area.
145 TextArea(int rows, int columns)
147 this("", rows, columns, SCROLLBARS_BOTH);
150 /*************************************************************************/
153 * Initializes a new instance of <code>TextArea</code> that is the
154 * specified size and has the specified text.
156 * @param text The text to display in this text area.
157 * @param rows The number of rows in this text area.
158 * @param columns The number of columns in this text area.
161 TextArea(String text, int rows, int columns)
163 this(text, rows, columns, SCROLLBARS_BOTH);
166 /*************************************************************************/
169 * Initializes a new instance of <code>TextArea</code> with the
170 * specified values. The scrollbar visibility value must be one
171 * of the constants in this class.
173 * @param text The text to display in this text area.
174 * @param rows The number of rows in this text area.
175 * @param columns The number of columns in this text area.
176 * @param scrollbarVisibility Which scrollbars to display.
179 TextArea(String text, int rows, int columns, int scrollbarVisibility)
183 if ((rows < 1) || (columns < 0))
184 throw new IllegalArgumentException("Bad row or column value");
186 if ((scrollbarVisibility != SCROLLBARS_BOTH) &&
187 (scrollbarVisibility != SCROLLBARS_VERTICAL_ONLY) &&
188 (scrollbarVisibility != SCROLLBARS_HORIZONTAL_ONLY) &&
189 (scrollbarVisibility != SCROLLBARS_NONE))
190 throw new IllegalArgumentException("Bad scrollbar visibility value");
193 this.columns = columns;
194 this.scrollbarVisibility = scrollbarVisibility;
197 /*************************************************************************/
204 * Returns the number of columns in the field.
206 * @return The number of columns in the field.
214 /*************************************************************************/
217 * Sets the number of columns in this field to the specified value.
219 * @param columns The new number of columns in the field.
221 * @exception IllegalArgumentException If columns is less than zero.
223 public synchronized void
224 setColumns(int columns)
227 throw new IllegalArgumentException("Value is less than zero: " +
230 this.columns = columns;
231 // FIXME: How to we communicate this to our peer?
234 /*************************************************************************/
237 * Returns the number of rows in the field.
239 * @return The number of rows in the field.
247 /*************************************************************************/
250 * Sets the number of rows in this field to the specified value.
252 * @param rows The new number of rows in the field.
254 * @exception IllegalArgumentException If rows is less than zero.
256 public synchronized void
260 throw new IllegalArgumentException("Value is less than one: " +
264 // FIXME: How to we communicate this to our peer?
267 /*************************************************************************/
270 * Returns the minimum size for this text field.
272 * @return The minimum size for this text field.
277 return(getMinimumSize(getRows(), getColumns()));
280 /*************************************************************************/
283 * Returns the minimum size of a text field with the specified number
284 * of rows and columns.
286 * @param rows The number of rows to get the minimum size for.
287 * @param columns The number of columns to get the minimum size for.
290 getMinimumSize(int rows, int columns)
292 TextAreaPeer tap = (TextAreaPeer)getPeer();
294 return(null); // FIXME: What do we do if there is no peer?
296 return(tap.getMinimumSize(rows, columns));
299 /*************************************************************************/
302 * Returns the minimum size for this text field.
304 * @return The minimum size for this text field.
306 * @deprecated This method is depcreated in favor of
307 * <code>getMinimumSize()</code>.
312 return(getMinimumSize(getRows(), getColumns()));
315 /*************************************************************************/
318 * Returns the minimum size of a text field with the specified number
319 * of rows and columns.
321 * @param rows The number of rows to get the minimum size for.
322 * @param columns The number of columns to get the minimum size for.
324 * @deprecated This method is deprecated in favor of
325 * <code>getMinimumSize(int)</code>.
328 minimumSize(int rows, int columns)
330 return(getMinimumSize(rows, columns));
333 /*************************************************************************/
336 * Returns the preferred size for this text field.
338 * @return The preferred size for this text field.
343 return(getPreferredSize(getRows(), getColumns()));
346 /*************************************************************************/
349 * Returns the preferred size of a text field with the specified number
350 * of rows and columns.
352 * @param rows The number of rows to get the preferred size for.
353 * @param columns The number of columns to get the preferred size for.
356 getPreferredSize(int rows, int columns)
358 TextAreaPeer tap = (TextAreaPeer)getPeer();
360 return(null); // FIXME: What do we do if there is no peer?
362 return(tap.getPreferredSize(rows, columns));
365 /*************************************************************************/
368 * Returns the preferred size for this text field.
370 * @return The preferred size for this text field.
372 * @deprecated This method is deprecated in favor of
373 * <code>getPreferredSize()</code>.
378 return(getPreferredSize(getRows(), getColumns()));
381 /*************************************************************************/
384 * Returns the preferred size of a text field with the specified number
385 * of rows and columns.
387 * @param rows The number of rows to get the preferred size for.
388 * @param columns The number of columns to get the preferred size for.
390 * @deprecated This method is deprecated in favor of
391 * <code>getPreferredSize(int)</code>.
394 preferredSize(int columns)
396 return(getPreferredSize(rows, columns));
399 /*************************************************************************/
402 * Returns one of the constants from this class indicating which
403 * types of scrollbars this object uses, if any.
405 * @return The scrollbar type constant for this object.
408 getScrollbarVisibility()
410 return(scrollbarVisibility);
413 /*************************************************************************/
416 * Notify this object that it should create its native peer.
421 if (getPeer() != null)
424 setPeer((ComponentPeer)getToolkit().createTextArea(this));
427 /*************************************************************************/
430 * Appends the specified text to the end of the current text.
432 * @param text The text to append.
437 TextAreaPeer tap = (TextAreaPeer)getPeer();
441 tap.insert(str, tap.getText().length());
444 /*************************************************************************/
447 * Appends the specified text to the end of the current text.
449 * @param text The text to append.
451 * @deprecated This method is deprecated in favor of
452 * <code>append()</code>.
455 appendText(String text)
460 /*************************************************************************/
463 * Inserts the specified text at the specified location.
465 * @param text The text to insert.
466 * @param pos The insert position.
469 insert(String text, int pos)
471 TextAreaPeer tap = (TextAreaPeer)getPeer();
475 tap.insert(text, pos);
478 /*************************************************************************/
481 * Inserts the specified text at the specified location.
483 * @param text The text to insert.
484 * @param pos The insert position.
486 * @deprecated This method is depcreated in favor of <code>insert()</code>.
489 insertText(String text, int pos)
494 /*************************************************************************/
497 * Replaces the text bounded by the specified start and end positions
498 * with the specified text.
500 * @param text The new text for the range.
501 * @param start The start position of the replacement range.
502 * @param end The end position of the replacement range.
505 replaceRange(String text, int start, int end)
507 TextAreaPeer tap = (TextAreaPeer)getPeer();
511 tap.replaceRange(text, start, end);
514 /*************************************************************************/
517 * Replaces the text bounded by the specified start and end positions
518 * with the specified text.
520 * @param text The new text for the range.
521 * @param start The start position of the replacement range.
522 * @param end The end position of the replacement range.
524 * @deprecated This method is deprecated in favor of
525 * <code>replaceRange()</code>.
528 replaceText(String text, int start, int end)
530 replaceRange(text, start, end);
533 /*************************************************************************/
536 * Returns a debugging string for this text area.
538 * @return A debugging string for this text area.
543 return(getClass().getName() + "(rows=" + getRows() + ",columns=" +
544 getColumns() + ",scrollbars=" + getScrollbarVisibility() +