home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / textarea.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.9 KB  |  182 lines

  1. /*
  2.  * @(#)TextArea.java    1.14 95/08/17 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.TextAreaPeer;
  22.  
  23. /**
  24.  * A TextArea object is a multi-line area that displays text. It can
  25.  * be set to allow editing or read-only modes.
  26.  *
  27.  * @version    1.14, 08/17/95
  28.  * @author     Sami Shaio
  29.  */
  30. public class TextArea extends TextComponent {
  31.  
  32.     /**
  33.      * The number of rows in the TextArea.
  34.      */
  35.     int    rows;
  36.  
  37.     /**
  38.      * The number of columns in the TextArea.
  39.      */
  40.     int    cols;
  41.  
  42.     /**
  43.      * Constructs a new TextArea.
  44.      */
  45.     public TextArea() {
  46.     super("");
  47.     }
  48.  
  49.     /**
  50.      * Constructs a new TextArea with the specified number of rows and columns.
  51.      * @param rows the number of rows
  52.      * @param cols the number of columns
  53.      */
  54.     public TextArea(int rows, int cols) {
  55.     super("");
  56.     this.rows = rows;
  57.     this.cols = cols;
  58.     }
  59.  
  60.     /**
  61.      * Constructs a new TextArea with the specified text displayed.
  62.      * @param text the text to be displayed 
  63.      */
  64.     public TextArea(String text) {
  65.     super(text);
  66.     }
  67.  
  68.     /**
  69.      * Constructs a new TextArea with the specified text and the specified number of rows 
  70.      * and columns.
  71.      * @param text the text to be displayed
  72.      * @param rows the number of rows
  73.      * @param cols the number of cols
  74.      */
  75.     public TextArea(String text, int rows, int cols) {
  76.     super(text);
  77.     this.rows = rows;
  78.     this.cols = cols;
  79.     }
  80.  
  81.     /**
  82.      * Creates the TextArea's peer.  The peer allows us to modify the appearance of
  83.      * the TextArea without changing any of its functionality.
  84.      */
  85.     public synchronized void addNotify() {
  86.     peer = getToolkit().createTextArea(this);
  87.     super.addNotify();
  88.     }
  89.  
  90.     /**
  91.      * Inserts the specified text at the specified position.
  92.      * @param str the text to insert.
  93.      * @param pos the position to insert at.
  94.      * @see #setText
  95.      * @see #replaceText
  96.      */
  97.     public void insertText(String str, int pos) {
  98.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  99.     if (peer != null) {
  100.         peer.insertText(str, pos);
  101.     } else {
  102.         text = text.substring(0, pos) + str + text.substring(pos);
  103.     }
  104.     }
  105.  
  106.     /**
  107.      * Replaces text from the indicated start to end position with the specified new text.
  108.      * @param str the text to use as the replacement.
  109.      * @param start the start position.
  110.      * @param end the end position.
  111.      * @see #insertText
  112.      * @see #replaceText
  113.      */
  114.     public void replaceText(String str, int start, int end) {
  115.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  116.     if (peer != null) {
  117.         peer.replaceText(str, start, end);
  118.     } else {
  119.         text = text.substring(0, start) + str + text.substring(end);
  120.     }
  121.     }
  122.  
  123.     /**
  124.      * Returns the number of rows in the TextArea.
  125.      */
  126.     public int getRows() {
  127.     return rows;
  128.     }
  129.  
  130.     /**
  131.      * Returns the number of columns in the TextArea.
  132.      */
  133.     public int getColumns() {
  134.     return cols;
  135.     }
  136.  
  137.     /**
  138.      * Returns the specified row and column Dimensions of the TextArea.
  139.      * @param rows the preferred rows amount
  140.      * @param cols the preferred columns amount
  141.      */
  142.     public Dimension preferredSize(int rows, int cols) {
  143.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  144.     return (peer != null) ? 
  145.         peer.preferredSize(rows, cols) : super.preferredSize();
  146.     }
  147.  
  148.     /**
  149.      * Returns the preferred size Dimensions of the TextArea.
  150.      */
  151.     public Dimension preferredSize() {
  152.     return ((rows > 0) && (cols > 0)) ? 
  153.         preferredSize(rows, cols) : super.preferredSize();
  154.     }
  155.  
  156.     /**
  157.      * Returns the specified minimum size Dimensions of the TextArea.
  158.      * @param rows the minimum row size
  159.      * @param cols the minimum column size
  160.      */
  161.     public Dimension minimumSize(int rows, int cols) {
  162.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  163.     return (peer != null) ? 
  164.         peer.minimumSize(rows, cols) : super.minimumSize();
  165.     }
  166.  
  167.     /**
  168.      * Returns the minimum size Dimensions of the TextArea.
  169.      */
  170.     public Dimension minimumSize() {
  171.     return ((rows > 0) && (cols > 0)) ? 
  172.         minimumSize(rows, cols) : super.minimumSize();
  173.     }
  174.  
  175.     /**
  176.      * Returns the String of parameters for this TextArea.
  177.      */
  178.     protected String paramString() {
  179.     return super.paramString() + ",rows=" + rows + ",cols=" + cols;
  180.     }
  181. }
  182.