OSDN Git Service

2002-04-16 David S. Miller <davem@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / TextArea.java
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 public
114 TextArea()
115 {
116   this("", 1, 1, SCROLLBARS_BOTH);
117 }
118
119 /*************************************************************************/
120
121 /**
122   * Initializes a new instance of <code>TextArea</code> that 
123   * contains the specified string.  Both horizontal and veritcal
124   * scrollbars will be used.
125   *
126   * @param text The text to display in this text area.
127   */
128 public
129 TextArea(String text)
130 {
131   this(text, 1, text.length(), SCROLLBARS_BOTH);
132 }
133
134 /*************************************************************************/
135
136 /**
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.
140   *
141   * @param rows The number of rows in this text area.
142   * @param columns The number of columns in this text area.
143   */
144 public
145 TextArea(int rows, int columns)
146 {
147   this("", rows, columns, SCROLLBARS_BOTH);
148 }
149
150 /*************************************************************************/
151
152 /**
153   * Initializes a new instance of <code>TextArea</code> that is the
154   * specified size and has the specified text.
155   *
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.
159   */
160 public
161 TextArea(String text, int rows, int columns)
162 {
163   this(text, rows, columns, SCROLLBARS_BOTH);
164 }
165
166 /*************************************************************************/
167
168 /** 
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.
172   *
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.
177   */
178 public
179 TextArea(String text, int rows, int columns, int scrollbarVisibility)
180 {
181   super(text);
182
183   if ((rows < 1) || (columns < 0))
184     throw new IllegalArgumentException("Bad row or column value");
185
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");
191
192   this.rows = rows;
193   this.columns = columns;
194   this.scrollbarVisibility = scrollbarVisibility;
195 }
196
197 /*************************************************************************/
198
199 /*
200  * Instance Variables
201  */
202
203 /**
204   * Returns the number of columns in the field.
205   *
206   * @return The number of columns in the field.
207   */
208 public int
209 getColumns()
210 {
211   return(columns);
212 }
213
214 /*************************************************************************/
215
216 /**
217   * Sets the number of columns in this field to the specified value.
218   *
219   * @param columns The new number of columns in the field.
220   *
221   * @exception IllegalArgumentException If columns is less than zero.
222   */
223 public synchronized void
224 setColumns(int columns)
225 {
226   if (columns < 0)
227     throw new IllegalArgumentException("Value is less than zero: " +
228                                        columns);
229
230   this.columns = columns;
231   // FIXME: How to we communicate this to our peer?
232 }
233
234 /*************************************************************************/
235
236 /**
237   * Returns the number of rows in the field.
238   *
239   * @return The number of rows in the field.
240   */
241 public int
242 getRows()
243 {
244   return(rows);
245 }
246
247 /*************************************************************************/
248
249 /**
250   * Sets the number of rows in this field to the specified value.
251   *
252   * @param rows The new number of rows in the field.
253   *
254   * @exception IllegalArgumentException If rows is less than zero.
255   */
256 public synchronized void
257 setRows(int rows)
258 {
259   if (rows < 1)
260     throw new IllegalArgumentException("Value is less than one: " +
261                                        rows);
262
263   this.rows = rows;
264   // FIXME: How to we communicate this to our peer?
265 }
266
267 /*************************************************************************/
268
269 /**
270   * Returns the minimum size for this text field.
271   *
272   * @return The minimum size for this text field.
273   */
274 public Dimension
275 getMinimumSize()
276 {
277   return(getMinimumSize(getRows(), getColumns()));
278 }
279
280 /*************************************************************************/
281
282 /**
283   * Returns the minimum size of a text field with the specified number
284   * of rows and columns.
285   *
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.
288   */
289 public Dimension
290 getMinimumSize(int rows, int columns)
291 {
292   TextAreaPeer tap = (TextAreaPeer)getPeer();
293   if (tap == null)
294     return(null); // FIXME: What do we do if there is no peer?
295
296   return(tap.getMinimumSize(rows, columns));
297 }
298
299 /*************************************************************************/
300
301 /**
302   * Returns the minimum size for this text field.
303   *
304   * @return The minimum size for this text field.
305   *
306   * @deprecated This method is depcreated in favor of
307   * <code>getMinimumSize()</code>.
308   */
309 public Dimension
310 minimumSize()
311 {
312   return(getMinimumSize(getRows(), getColumns()));
313 }
314
315 /*************************************************************************/
316
317 /**
318   * Returns the minimum size of a text field with the specified number
319   * of rows and columns.
320   *
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.
323   *
324   * @deprecated This method is deprecated in favor of 
325   * <code>getMinimumSize(int)</code>.
326   */
327 public Dimension
328 minimumSize(int rows, int columns)
329 {
330   return(getMinimumSize(rows, columns));
331 }
332
333 /*************************************************************************/
334
335 /**
336   * Returns the preferred size for this text field.
337   *
338   * @return The preferred size for this text field.
339   */
340 public Dimension
341 getPreferredSize()
342 {
343   return(getPreferredSize(getRows(), getColumns()));
344 }
345
346 /*************************************************************************/
347
348 /**
349   * Returns the preferred size of a text field with the specified number
350   * of rows and columns.
351   *
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.
354   */
355 public Dimension
356 getPreferredSize(int rows, int columns)
357 {
358   TextAreaPeer tap = (TextAreaPeer)getPeer();
359   if (tap == null)
360     return(null); // FIXME: What do we do if there is no peer?
361
362   return(tap.getPreferredSize(rows, columns));
363 }
364
365 /*************************************************************************/
366
367 /**
368   * Returns the preferred size for this text field.
369   *
370   * @return The preferred size for this text field.
371   *
372   * @deprecated This method is deprecated in favor of 
373   * <code>getPreferredSize()</code>.
374   */
375 public Dimension
376 preferredSize()
377 {
378   return(getPreferredSize(getRows(), getColumns()));
379 }
380
381 /*************************************************************************/
382
383 /**
384   * Returns the preferred size of a text field with the specified number
385   * of rows and columns.
386   *
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.
389   *
390   * @deprecated This method is deprecated in favor of 
391   * <code>getPreferredSize(int)</code>.
392   */
393 public Dimension
394 preferredSize(int columns)
395 {
396   return(getPreferredSize(rows, columns));
397 }
398
399 /*************************************************************************/
400
401 /**
402   * Returns one of the constants from this class indicating which
403   * types of scrollbars this object uses, if any.
404   *
405   * @return The scrollbar type constant for this object.
406   */
407 public int
408 getScrollbarVisibility()
409 {
410   return(scrollbarVisibility);
411 }
412
413 /*************************************************************************/
414
415 /**
416   * Notify this object that it should create its native peer.
417   */
418 public void
419 addNotify()
420 {
421   if (getPeer() != null)
422     return;
423
424   setPeer((ComponentPeer)getToolkit().createTextArea(this));
425 }
426
427 /*************************************************************************/
428
429 /**
430   * Appends the specified text to the end of the current text.
431   *
432   * @param text The text to append.
433   */
434 public void
435 append(String str)
436 {
437   TextAreaPeer tap = (TextAreaPeer)getPeer();
438   if (tap == null) 
439     return;
440
441   tap.insert(str, tap.getText().length());
442 }
443
444 /*************************************************************************/
445
446 /**
447   * Appends the specified text to the end of the current text.
448   *
449   * @param text The text to append.
450   *
451   * @deprecated This method is deprecated in favor of 
452   * <code>append()</code>.
453   */
454 public void
455 appendText(String text)
456 {
457   append(text);
458 }
459  
460 /*************************************************************************/
461
462 /**
463   * Inserts the specified text at the specified location.
464   *
465   * @param text The text to insert.
466   * @param pos The insert position.
467   */
468 public void
469 insert(String text, int pos)
470 {
471   TextAreaPeer tap = (TextAreaPeer)getPeer();
472   if (tap == null)
473     return;
474
475   tap.insert(text, pos);
476 }
477
478 /*************************************************************************/
479
480 /**
481   * Inserts the specified text at the specified location.
482   *
483   * @param text The text to insert.
484   * @param pos The insert position.
485   *
486   * @deprecated This method is depcreated in favor of <code>insert()</code>.
487   */
488 public void
489 insertText(String text, int pos)
490 {
491   insert(text, pos);
492 }
493
494 /*************************************************************************/
495
496 /**
497   * Replaces the text bounded by the specified start and end positions
498   * with the specified text.
499   *
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.
503   */
504 public void
505 replaceRange(String text, int start, int end)
506 {
507   TextAreaPeer tap = (TextAreaPeer)getPeer();
508   if (tap == null)
509     return;
510
511   tap.replaceRange(text, start, end);
512 }
513
514 /*************************************************************************/
515
516 /**
517   * Replaces the text bounded by the specified start and end positions
518   * with the specified text.
519   *
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.
523   *
524   * @deprecated This method is deprecated in favor of
525   * <code>replaceRange()</code>.
526   */
527 public void
528 replaceText(String text, int start, int end)
529 {
530   replaceRange(text, start, end);
531 }
532
533 /*************************************************************************/
534
535 /**
536   * Returns a debugging string for this text area.
537   *
538   * @return A debugging string for this text area.
539   */
540 protected String
541 paramString()
542 {
543   return(getClass().getName() + "(rows=" + getRows() + ",columns=" +
544          getColumns() + ",scrollbars=" + getScrollbarVisibility() +
545          ")");
546 }
547
548 } // class TextArea 
549