1 /* TextArea.java -- A multi-line text entry component
2 Copyright (C) 1999, 2004 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. */
40 import java
.awt
.peer
.ComponentPeer
;
41 import java
.awt
.peer
.TextAreaPeer
;
45 * A TextArea is a text component capable of displaying multiple lines
46 * of user-editable text. A TextArea handles its own scrolling and
47 * can display vertical and horizontal scrollbars as navigation aids.
49 * @author Aaron M. Renn (arenn@urbanophile.com)
51 public class TextArea
extends TextComponent
implements java
.io
.Serializable
54 * Display both horiztonal and vertical scroll bars.
56 public static final int SCROLLBARS_BOTH
= 0;
59 * Display vertical scroll bar only.
61 public static final int SCROLLBARS_VERTICAL_ONLY
= 1;
64 * Display horizatonal scroll bar only.
66 public static final int SCROLLBARS_HORIZONTAL_ONLY
= 2;
69 * Do not display scrollbars.
71 public static final int SCROLLBARS_NONE
= 3;
74 * Serialization constant.
76 private static final long serialVersionUID
= 3692302836626095722L;
79 * @serial The number of columns used in this text area's preferred
80 * and minimum size calculations.
85 * @serial The number of rows used in this text area's preferred and
86 * minimum size calculations.
91 * @serial The scrollbar display policy. One of SCROLLBARS_BOTH,
92 * SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY,
95 private int scrollbarVisibility
;
98 * The number used to generate the name returned by getName.
100 private static transient long next_text_number
= 0;
103 * Initialize a new instance of <code>TextArea</code> that is empty
104 * and is one row by one column. Both horizontal and vertical
105 * scrollbars will be displayed.
107 * @exception HeadlessException If GraphicsEnvironment.isHeadless () is true,
111 this ("", 1, 1, SCROLLBARS_BOTH
);
115 * Initialize a new instance of <code>TextArea</code> that initially
116 * contains the specified text. Both horizontal and veritcal
117 * scrollbars will be displayed.
119 * @param text The text to display in this text area.
121 * @exception HeadlessException If GraphicsEnvironment.isHeadless () is true,
123 public TextArea (String text
)
125 this (text
, 1, text
.length (), SCROLLBARS_BOTH
);
129 * Initialize a new instance of <code>TextArea</code> that is empty
130 * and can display the specified number of rows and columns of text,
131 * without the need to scroll. Both horizontal and vertical
132 * scrollbars will be displayed.
134 * @param rows The number of rows in this text area.
135 * @param columns The number of columns in this text area.
137 * @exception HeadlessException If GraphicsEnvironment.isHeadless () is true,
139 public TextArea (int rows
, int columns
)
141 this ("", rows
, columns
, SCROLLBARS_BOTH
);
145 * Initialize a new instance of <code>TextArea</code> that can
146 * display the specified number of rows and columns of text, without
147 * the need to scroll. The TextArea initially contains the
150 * @param text The text to display in this text area.
151 * @param rows The number of rows in this text area.
152 * @param columns The number of columns in this text area.
154 * @exception HeadlessException If GraphicsEnvironment.isHeadless () is true,
156 public TextArea (String text
, int rows
, int columns
)
158 this (text
, rows
, columns
, SCROLLBARS_BOTH
);
162 * Initialize a new instance of <code>TextArea</code> that initially
163 * contains the specified text. The TextArea can display the
164 * specified number of rows and columns of text, without the need to
165 * scroll. This constructor allows specification of the scroll bar
168 * @param text The text to display in this text area.
169 * @param rows The number of rows in this text area.
170 * @param columns The number of columns in this text area.
171 * @param scrollbarVisibility The scroll bar display policy. One of
172 * SCROLLBARS_BOTH, SCROLLBARS_VERTICAL_ONLY,
173 * SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE.
175 * @exception HeadlessException If GraphicsEnvironment.isHeadless () is true,
177 public TextArea (String text
, int rows
, int columns
, int scrollbarVisibility
)
181 if (GraphicsEnvironment
.isHeadless ())
182 throw new HeadlessException ();
184 if (rows
< 1 || columns
< 0)
185 throw new IllegalArgumentException ("Bad row or column value");
187 if (scrollbarVisibility
!= SCROLLBARS_BOTH
188 && scrollbarVisibility
!= SCROLLBARS_VERTICAL_ONLY
189 && scrollbarVisibility
!= SCROLLBARS_HORIZONTAL_ONLY
190 && scrollbarVisibility
!= SCROLLBARS_NONE
)
191 throw new IllegalArgumentException ("Bad scrollbar visibility value");
194 this.columns
= columns
;
195 this.scrollbarVisibility
= scrollbarVisibility
;
203 * Retrieve the number of columns that this text area would prefer
204 * to display. This value may or may not correspond to the number
205 * of columns that are actually displayed.
207 * @return The preferred number of columns.
209 public int getColumns ()
215 * Set the preferred number of columns for this text area. This
216 * method does not cause the number of columns displayed by the text
217 * area to be updated, if the text area is currently visible.
219 * @param columns The preferred number of columns.
221 * @exception IllegalArgumentException If columns is less than zero.
223 public synchronized void setColumns (int columns
)
226 throw new IllegalArgumentException ("Value is less than zero: "
229 this.columns
= columns
;
233 * Retrieve the number of rows that this text area would prefer to
234 * display. This value may or may not correspond to the number of
235 * rows that are actually displayed.
237 * @return The preferred number of rows.
239 public int getRows ()
245 * Set the preferred number of rows for this text area. This method
246 * does not cause the number of columns displayed by the text area
247 * to be updated, if the text area is currently visible.
249 * @param rows The preferred number of rows.
251 * @exception IllegalArgumentException If rows is less than zero.
253 public synchronized void setRows (int rows
)
256 throw new IllegalArgumentException ("Value is less than one: " + rows
);
262 * Retrieve the minimum size for this text area, considering the
263 * text area's current row and column values. A text area's minimum
264 * size depends on the number of rows and columns of text it would
265 * prefer to display, and on the size of the font in which the text
266 * would be displayed.
268 * @return The minimum size for this text field.
270 public Dimension
getMinimumSize ()
272 return getMinimumSize (getRows (), getColumns ());
276 * Retrieve the minimum size that this text area would have if its
277 * row and column values were equal to those specified. A text
278 * area's minimum size depends on the number of rows and columns of
279 * text it would prefer to display, and on the size of the font in
280 * which the text would be displayed.
282 * @param rows The number of rows to use in the minimum size
284 * @param columns The number of columns to use in the minimum size
287 * @return The minimum size for this text area.
289 public Dimension
getMinimumSize (int rows
, int columns
)
291 return minimumSize (rows
, columns
);
295 * Retrieve the minimum size for this text area, considering the
296 * text area's current row and column values. A text area's minimum
297 * size depends on the number of rows and columns of text it would
298 * prefer to display, and on the size of the font in which the text
299 * would be displayed.
301 * @return The minimum size for this text area.
303 * @deprecated This method is deprecated in favor of
304 * <code>getMinimumSize ()</code>.
306 public Dimension
minimumSize ()
308 return minimumSize (getRows (), getColumns ());
312 * Retrieve the minimum size that this text area would have if its
313 * row and column values were equal to those specified. A text
314 * area's minimum size depends on the number of rows and columns of
315 * text it would prefer to display, and on the size of the font in
316 * which the text would be displayed.
318 * @param rows The number of rows to use in the minimum size
320 * @param columns The number of columns to use in the minimum size
323 * @return The minimum size for this text area.
325 * @deprecated This method is deprecated in favor of
326 * <code>getMinimumSize (int, int)</code>.
328 public Dimension
minimumSize (int rows
, int columns
)
330 TextAreaPeer peer
= (TextAreaPeer
) getPeer ();
332 // Sun returns Dimension (0,0) in this case.
334 return new Dimension (0, 0);
336 return peer
.getMinimumSize (rows
, columns
);
340 * Retrieve the preferred size for this text area, considering the
341 * text area's current row and column values. A text area's preferred
342 * size depends on the number of rows and columns of text it would
343 * prefer to display, and on the size of the font in which the text
344 * would be displayed.
346 * @return The preferred size for this text field.
348 public Dimension
getPreferredSize ()
350 return getPreferredSize (getRows (), getColumns ());
354 * Retrieve the preferred size that this text area would have if its
355 * row and column values were equal to those specified. A text
356 * area's preferred size depends on the number of rows and columns
357 * of text it would prefer to display, and on the size of the font
358 * in which the text would be displayed.
360 * @param rows The number of rows to use in the preferred size
362 * @param columns The number of columns to use in the preferred size
365 * @return The preferred size for this text area.
367 public Dimension
getPreferredSize (int rows
, int columns
)
369 return preferredSize (rows
, columns
);
373 * Retrieve the preferred size for this text area, considering the
374 * text area's current row and column values. A text area's preferred
375 * size depends on the number of rows and columns of text it would
376 * prefer to display, and on the size of the font in which the text
377 * would be displayed.
379 * @return The preferred size for this text field.
381 * @deprecated This method is deprecated in favor of
382 * <code>getPreferredSize ()</code>.
384 public Dimension
preferredSize ()
386 return preferredSize (getRows (), getColumns ());
390 * Retrieve the preferred size that this text area would have if its
391 * row and column values were equal to those specified. A text
392 * area's preferred size depends on the number of rows and columns
393 * of text it would prefer to display, and on the size of the font
394 * in which the text would be displayed.
396 * @param rows The number of rows to use in the preferred size
398 * @param columns The number of columns to use in the preferred size
401 * @return The preferred size for this text area.
403 * @deprecated This method is deprecated in favor of
404 * <code>getPreferredSize (int, int)</code>.
406 public Dimension
preferredSize (int rows
, int columns
)
408 TextAreaPeer peer
= (TextAreaPeer
) getPeer ();
410 // Sun returns Dimension (0,0) in this case.
412 return new Dimension (0, 0);
414 return peer
.getPreferredSize (rows
, columns
);
418 * Retrieve the scroll bar display policy -- one of SCROLLBARS_BOTH,
419 * SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY,
422 * @return The current scroll bar display policy.
424 public int getScrollbarVisibility ()
426 return scrollbarVisibility
;
430 * Notify this object that it should create its native peer.
432 public void addNotify ()
434 if (getPeer () != null)
437 setPeer ((ComponentPeer
) getToolkit().createTextArea (this));
441 * Append the specified text to the end of the current text.
443 * @param str The text to append.
445 public void append (String str
)
451 * Append the specified text to the end of the current text.
453 * @param str The text to append.
455 * @deprecated This method is deprecated in favor of
456 * <code>append ()</code>.
458 public void appendText (String str
)
460 TextAreaPeer peer
= (TextAreaPeer
) getPeer ();
464 peer
.insert (str
, peer
.getText().length ());
468 * Insert the specified text at the specified position. The first
469 * character in the text area is at position zero.
471 * @param str The text to insert.
472 * @param pos The position at which to insert text.
474 public void insert (String str
, int pos
)
476 insertText (str
, pos
);
480 * Insert the specified text at the specified position. The first
481 * character in the text area is at position zero.
483 * @param str The text to insert.
484 * @param pos The position at which to insert text.
486 * @deprecated This method is deprecated in favor of
487 * <code>insert ()</code>.
489 public void insertText (String str
, int pos
)
491 TextAreaPeer peer
= (TextAreaPeer
) getPeer ();
495 peer
.insert (str
, pos
);
499 * Replace a range of characters with the specified text. The
500 * character at the start position will be replaced, unless start ==
501 * end. The character at the end posistion will not be replaced.
502 * The first character in the text area is at position zero. The
503 * length of the replacement text may differ from the length of the
504 * text that is replaced.
506 * @param str The new text for the range.
507 * @param start The start position of the replacement range.
508 * @param end The end position of the replacement range.
510 public void replaceRange (String str
, int start
, int end
)
512 replaceText (str
, start
, end
);
516 * Replace a range of characters with the specified text. The
517 * character at the start position will be replaced, unless start ==
518 * end. The character at the end posistion will not be replaced.
519 * The first character in the text area is at position zero. The
520 * length of the replacement text may differ from the length of the
521 * text that is replaced.
523 * @param str The new text for the range.
524 * @param start The start position of the replacement range.
525 * @param end The end position of the replacement range.
527 * @deprecated This method is deprecated in favor of
528 * <code>replaceRange ()</code>.
530 public void replaceText (String str
, int start
, int end
)
532 TextAreaPeer peer
= (TextAreaPeer
) getPeer ();
536 peer
.replaceRange (str
, start
, end
);
540 * Retrieve a debugging string for this text area.
542 * @return A debugging string for this text area.
544 protected String
paramString ()
546 String sbVisibility
= "";
548 switch (scrollbarVisibility
)
550 case SCROLLBARS_BOTH
:
551 sbVisibility
= "both";
553 case SCROLLBARS_VERTICAL_ONLY
:
554 sbVisibility
= "vertical-only";
556 case SCROLLBARS_HORIZONTAL_ONLY
:
557 sbVisibility
= "horizontal-only";
559 case SCROLLBARS_NONE
:
560 sbVisibility
= "none";
564 String editable
= "";
566 editable
= "editable,";
568 return getName () + "," + getX () + "," + getY () + "," + getWidth ()
569 + "x" + getHeight () + "," + "text=" + getText () + "," + editable
570 + "selection=" + getSelectionStart () + "-" + getSelectionEnd ()
571 + ",rows=" + rows
+ ",columns=" + columns
+ ",scrollbarVisibility="
576 * Generate a unique name for this text area.
578 * @return A unique name for this text area.
580 String
generateName ()
582 return "text" + getUniqueLong ();
585 private static synchronized long getUniqueLong ()
587 return next_text_number
++;