home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / TextArea.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  202 lines

  1. /*
  2.  * @(#)TextArea.java    1.19 96/04/04 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.19, 04 Apr 1996
  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 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 at which to insert.
  94.      * @see TextComponent#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.      * Appends the given text to the end.
  108.      * @param str the text to insert
  109.      * @see #insertText
  110.      */
  111.     public void appendText(String str) {
  112.     if (peer != null) {
  113.         insertText(str, getText().length());
  114.     } else {
  115.         text = text + str;
  116.     }
  117.     }
  118.  
  119.     /**
  120.      * Replaces text from the indicated start to end position with the
  121.      * new text specified.
  122.  
  123.      * @param str the text to use as the replacement.
  124.      * @param start the start position.
  125.      * @param end the end position.
  126.      * @see #insertText
  127.      * @see #replaceText
  128.      */
  129.     public void replaceText(String str, int start, int end) {
  130.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  131.     if (peer != null) {
  132.         peer.replaceText(str, start, end);
  133.     } else {
  134.         text = text.substring(0, start) + str + text.substring(end);
  135.     }
  136.     }
  137.  
  138.     /**
  139.      * Returns the number of rows in the TextArea.
  140.      */
  141.     public int getRows() {
  142.     return rows;
  143.     }
  144.  
  145.     /**
  146.      * Returns the number of columns in the TextArea.
  147.      */
  148.     public int getColumns() {
  149.     return cols;
  150.     }
  151.  
  152.     /**
  153.      * Returns the specified row and column Dimensions of the TextArea.
  154.      * @param rows the preferred rows amount
  155.      * @param cols the preferred columns amount
  156.      */
  157.     public Dimension preferredSize(int rows, int cols) {
  158.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  159.     return (peer != null) ? 
  160.         peer.preferredSize(rows, cols) : super.preferredSize();
  161.     }
  162.  
  163.     /**
  164.      * Returns the preferred size Dimensions of the TextArea.
  165.      */
  166.     public Dimension preferredSize() {
  167.     return ((rows > 0) && (cols > 0)) ? 
  168.         preferredSize(rows, cols) : super.preferredSize();
  169.     }
  170.  
  171.     /**
  172.      * Returns the specified minimum size Dimensions of the TextArea.
  173.      * @param rows the minimum row size
  174.      * @param cols the minimum column size
  175.      */
  176.     public Dimension minimumSize(int rows, int cols) {
  177.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  178.     return (peer != null) ? 
  179.         peer.minimumSize(rows, cols) : super.minimumSize();
  180.     }
  181.  
  182.     /**
  183.      * Returns the minimum size Dimensions of the TextArea.
  184.      */
  185.     public Dimension minimumSize() {
  186.     return ((rows > 0) && (cols > 0)) ? 
  187.         minimumSize(rows, cols) : super.minimumSize();
  188.     }
  189.  
  190.     /**
  191.      * Returns the String of parameters for this TextArea.
  192.      */
  193.     protected String paramString() {
  194.     return super.paramString() + ",rows=" + rows + ",cols=" + cols;
  195.     }
  196.     
  197.     /* Can this field be tabbed to? */
  198.     boolean tabbable() {
  199.         return true;
  200.     }
  201. }
  202.