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.
113 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
118 this("", 1, 1, SCROLLBARS_BOTH
);
121 /*************************************************************************/
124 * Initializes a new instance of <code>TextArea</code> that
125 * contains the specified string. Both horizontal and veritcal
126 * scrollbars will be used.
128 * @param text The text to display in this text area.
130 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
133 TextArea(String text
)
135 this(text
, 1, text
.length(), SCROLLBARS_BOTH
);
138 /*************************************************************************/
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.
145 * @param rows The number of rows in this text area.
146 * @param columns The number of columns in this text area.
148 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
151 TextArea(int rows
, int columns
)
153 this("", rows
, columns
, SCROLLBARS_BOTH
);
156 /*************************************************************************/
159 * Initializes a new instance of <code>TextArea</code> that is the
160 * specified size and has the specified text.
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.
166 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
169 TextArea(String text
, int rows
, int columns
)
171 this(text
, rows
, columns
, SCROLLBARS_BOTH
);
174 /*************************************************************************/
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.
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.
186 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
189 TextArea(String text
, int rows
, int columns
, int scrollbarVisibility
)
193 if (GraphicsEnvironment
.isHeadless())
194 throw new HeadlessException ();
196 if ((rows
< 1) || (columns
< 0))
197 throw new IllegalArgumentException("Bad row or column value");
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");
206 this.columns
= columns
;
207 this.scrollbarVisibility
= scrollbarVisibility
;
210 /*************************************************************************/
217 * Returns the number of columns in the field.
219 * @return The number of columns in the field.
227 /*************************************************************************/
230 * Sets the number of columns in this field to the specified value.
232 * @param columns The new number of columns in the field.
234 * @exception IllegalArgumentException If columns is less than zero.
236 public synchronized void
237 setColumns(int columns
)
240 throw new IllegalArgumentException("Value is less than zero: " +
243 this.columns
= columns
;
244 // FIXME: How to we communicate this to our peer?
247 /*************************************************************************/
250 * Returns the number of rows in the field.
252 * @return The number of rows in the field.
260 /*************************************************************************/
263 * Sets the number of rows in this field to the specified value.
265 * @param rows The new number of rows in the field.
267 * @exception IllegalArgumentException If rows is less than zero.
269 public synchronized void
273 throw new IllegalArgumentException("Value is less than one: " +
277 // FIXME: How to we communicate this to our peer?
280 /*************************************************************************/
283 * Returns the minimum size for this text field.
285 * @return The minimum size for this text field.
290 return(getMinimumSize(getRows(), getColumns()));
293 /*************************************************************************/
296 * Returns the minimum size of a text field with the specified number
297 * of rows and columns.
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.
303 getMinimumSize(int rows
, int columns
)
305 TextAreaPeer tap
= (TextAreaPeer
)getPeer();
307 return(null); // FIXME: What do we do if there is no peer?
309 return(tap
.getMinimumSize(rows
, columns
));
312 /*************************************************************************/
315 * Returns the minimum size for this text field.
317 * @return The minimum size for this text field.
319 * @deprecated This method is depcreated in favor of
320 * <code>getMinimumSize()</code>.
325 return(getMinimumSize(getRows(), getColumns()));
328 /*************************************************************************/
331 * Returns the minimum size of a text field with the specified number
332 * of rows and columns.
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.
337 * @deprecated This method is deprecated in favor of
338 * <code>getMinimumSize(int)</code>.
341 minimumSize(int rows
, int columns
)
343 return(getMinimumSize(rows
, columns
));
346 /*************************************************************************/
349 * Returns the preferred size for this text field.
351 * @return The preferred size for this text field.
356 return(getPreferredSize(getRows(), getColumns()));
359 /*************************************************************************/
362 * Returns the preferred size of a text field with the specified number
363 * of rows and columns.
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.
369 getPreferredSize(int rows
, int columns
)
371 TextAreaPeer tap
= (TextAreaPeer
)getPeer();
374 // Sun's JDK just seems to return Dimension(0,0) in this case.
376 return new Dimension(0, 0);
379 return(tap
.getPreferredSize(rows
, columns
));
382 /*************************************************************************/
385 * Returns the preferred size for this text field.
387 * @return The preferred size for this text field.
389 * @deprecated This method is deprecated in favor of
390 * <code>getPreferredSize()</code>.
395 return(getPreferredSize(getRows(), getColumns()));
398 /*************************************************************************/
401 * Returns the preferred size of a text field with the specified number
402 * of rows and columns.
404 * @param rows The number of rows to get the preferred size for.
405 * @param columns The number of columns to get the preferred size for.
407 * @deprecated This method is deprecated in favor of
408 * <code>getPreferredSize(int)</code>.
411 preferredSize(int rows
, int columns
)
413 return(getPreferredSize(rows
, columns
));
416 /*************************************************************************/
419 * Returns one of the constants from this class indicating which
420 * types of scrollbars this object uses, if any.
422 * @return The scrollbar type constant for this object.
425 getScrollbarVisibility()
427 return(scrollbarVisibility
);
430 /*************************************************************************/
433 * Notify this object that it should create its native peer.
438 if (getPeer() != null)
441 setPeer((ComponentPeer
)getToolkit().createTextArea(this));
444 /*************************************************************************/
447 * Appends the specified text to the end of the current text.
449 * @param text The text to append.
454 TextAreaPeer tap
= (TextAreaPeer
)getPeer();
458 tap
.insert(str
, tap
.getText().length());
461 /*************************************************************************/
464 * Appends the specified text to the end of the current text.
466 * @param text The text to append.
468 * @deprecated This method is deprecated in favor of
469 * <code>append()</code>.
472 appendText(String text
)
477 /*************************************************************************/
480 * Inserts the specified text at the specified location.
482 * @param text The text to insert.
483 * @param pos The insert position.
486 insert(String text
, int pos
)
488 TextAreaPeer tap
= (TextAreaPeer
)getPeer();
492 tap
.insert(text
, pos
);
495 /*************************************************************************/
498 * Inserts the specified text at the specified location.
500 * @param text The text to insert.
501 * @param pos The insert position.
503 * @deprecated This method is depcreated in favor of <code>insert()</code>.
506 insertText(String text
, int pos
)
511 /*************************************************************************/
514 * Replaces the text bounded by the specified start and end positions
515 * with the specified text.
517 * @param text The new text for the range.
518 * @param start The start position of the replacement range.
519 * @param end The end position of the replacement range.
522 replaceRange(String text
, int start
, int end
)
524 TextAreaPeer tap
= (TextAreaPeer
)getPeer();
528 tap
.replaceRange(text
, start
, end
);
531 /*************************************************************************/
534 * Replaces the text bounded by the specified start and end positions
535 * with the specified text.
537 * @param text The new text for the range.
538 * @param start The start position of the replacement range.
539 * @param end The end position of the replacement range.
541 * @deprecated This method is deprecated in favor of
542 * <code>replaceRange()</code>.
545 replaceText(String text
, int start
, int end
)
547 replaceRange(text
, start
, end
);
550 /*************************************************************************/
553 * Returns a debugging string for this text area.
555 * @return A debugging string for this text area.
560 return(getClass().getName() + "(rows=" + getRows() + ",columns=" +
561 getColumns() + ",scrollbars=" + getScrollbarVisibility() +