home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / TextArea.java < prev    next >
Text File  |  1997-10-01  |  14KB  |  466 lines

  1. /*
  2.  * @(#)TextArea.java    1.36 97/06/17
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. import java.awt.peer.TextAreaPeer;
  25.  
  26. /**
  27.  * A <code>TextArea</code> object is a multi-line region 
  28.  * that displays text. It can be set to allow editing or 
  29.  * to be read-only.
  30.  * <p>
  31.  * The following image shows the appearance of a text area:
  32.  * <p>
  33.  * <img src="images-awt/TextArea-1.gif" 
  34.  * ALIGN=center HSPACE=10 VSPACE=7>
  35.  * <p>
  36.  * This text area could be created by the following line of code:
  37.  * <p>
  38.  * <hr><blockquote><pre>
  39.  * new TextArea("Hello", 5, 40);
  40.  * </pre></blockquote><hr>
  41.  * <p>
  42.  * @version    1.36, 06/17/97
  43.  * @author     Sami Shaio
  44.  * @since       JDK1.0
  45.  */
  46. public class TextArea extends TextComponent {
  47.  
  48.     /**
  49.      * The number of rows in the TextArea.
  50.      */
  51.     int    rows;
  52.  
  53.     /**
  54.      * The number of columns in the TextArea.
  55.      */
  56.     int    columns;
  57.  
  58.     private static final String base = "text";
  59.     private static int nameCounter = 0;
  60.  
  61.     /**
  62.      * Create and display both vertical and horizontal scrollbars.
  63.      * @since JDK1.1
  64.      */
  65.     public static final int SCROLLBARS_BOTH = 0;
  66.  
  67.     /**
  68.      * Create and display vertical scrollbar only.
  69.      * @since JDK1.1
  70.      */
  71.     public static final int SCROLLBARS_VERTICAL_ONLY = 1;
  72.  
  73.     /**
  74.      * Create and display horizontal scrollbar only.
  75.      * @since JDK1.1
  76.      */
  77.     public static final int SCROLLBARS_HORIZONTAL_ONLY = 2;
  78.  
  79.     /**
  80.      * Do not create or display any scrollbars for the text area.
  81.      * @since JDK1.1
  82.      */
  83.     public static final int SCROLLBARS_NONE = 3;
  84.  
  85.     /**
  86.      * Determines which scrollbars are created for the 
  87.      * text area.
  88.      */
  89.     private int scrollbarVisibility;
  90.  
  91.     /*
  92.      * JDK 1.1 serialVersionUID 
  93.      */
  94.      private static final long serialVersionUID = 3692302836626095722L;
  95.  
  96.     /**
  97.      * Constructs a new text area.
  98.      * This text area is created with both vertical and
  99.      * horizontal scroll bars.
  100.      * @since     JDK1.0
  101.      */
  102.     public TextArea() {
  103.     this("", 0, 0, SCROLLBARS_BOTH);
  104.     }
  105.  
  106.     /**
  107.      * Constructs a new text area with the specified text.
  108.      * This text area is created with both vertical and
  109.      * horizontal scroll bars.
  110.      * @param     text the text to be displayed. 
  111.      * @since     JDK1.0 
  112.      */
  113.     public TextArea(String text) {
  114.     this(text, 0, 0, SCROLLBARS_BOTH);
  115.     }
  116.  
  117.     /**
  118.      * Constructs a new empty TextArea with the specified number of
  119.      * rows and columns.
  120.      * @param rows the number of rows
  121.      * @param columns the number of columns
  122.      */
  123.     public TextArea(int rows, int columns) {
  124.     this("", rows, columns);
  125.     }
  126.  
  127.     /**
  128.      * Constructs a new text area with the specified text,
  129.      * and with the specified number of rows and columns.
  130.      * This text area is created with both vertical and
  131.      * horizontal scroll bars.
  132.      * @param     text      the text to be displayed.
  133.      * @param     rows      the number of rows.
  134.      * @param     columns   the number of columns.
  135.      * @since     JDK1.0
  136.      */
  137.     public TextArea(String text, int rows, int columns) {
  138.         this(text, rows, columns, SCROLLBARS_BOTH);
  139.     }
  140.  
  141.     /**
  142.      * Constructs a new text area with the specified text, 
  143.      * and with the rows, columns, and scroll bar visibility
  144.      * as specified. 
  145.      * <p>
  146.      * The <code>TextArea</code> class defines several constants
  147.      * that can be supplied as values for the
  148.      * <code>scrollbars</code> argument: 
  149.      * <code>SCROLLBARS_BOTH</code>, 
  150.      * <code>SCROLLBARS_VERTICAL_ONLY</code>, 
  151.      * <code>SCROLLBARS_HORIZONTAL_ONLY</code>, 
  152.      * and <code>SCROLLBARS_NEITHER</code>. 
  153.      * @param     text the text to be displayed.
  154.      * @param     rows the number of rows.
  155.      * @param     columns the number of columns.
  156.      * @param     scrollbars a constant that determines what 
  157.      *                scrollbars are created to view the text area.
  158.      * @since     JDK1.1
  159.      */
  160.     public TextArea(String text, int rows, int columns, int scrollbars) {
  161.     super(text);
  162.     this.name = base + nameCounter++;
  163.     this.rows = rows;
  164.     this.columns = columns;
  165.     this.scrollbarVisibility = scrollbars;
  166.     }
  167.  
  168.     /**
  169.      * Creates the TextArea's peer.  The peer allows us to modify
  170.      * the appearance of the TextArea without changing any of its
  171.      * functionality.
  172.      */
  173.     public void addNotify() {
  174.     peer = getToolkit().createTextArea(this);
  175.     super.addNotify();
  176.     }
  177.  
  178.     /**
  179.      * Inserts the specified text at the specified position
  180.      * in this text area.
  181.      * @param      str the text to insert.
  182.      * @param      pos the position at which to insert.
  183.      * @see        java.awt.TextComponent#setText
  184.      * @see        java.awt.TextArea#replaceRange
  185.      * @see        java.awt.TextArea#append
  186.      * @since      JDK1.1
  187.      */
  188.     public synchronized void insert(String str, int pos) {
  189.         insertText(str, pos);
  190.     }
  191.  
  192.     /**
  193.      * @deprecated As of JDK version 1.1,
  194.      * replaced by <code>insert(String, int)</code>.
  195.      */
  196.     public void insertText(String str, int pos) {
  197.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  198.     if (peer != null) {
  199.         peer.insertText(str, pos);
  200.     } else {
  201.         text = text.substring(0, pos) + str + text.substring(pos);
  202.     }
  203.     }
  204.  
  205.     /**
  206.      * Appends the given text to the text area's current text.
  207.      * @param     str the text to append.
  208.      * @see       java.awt.TextArea#insert
  209.      */
  210.     public synchronized void append(String str) {
  211.         appendText(str);
  212.     }
  213.  
  214.     /**
  215.      * @deprecated As of JDK version 1.1,
  216.      * replaced by <code>append(String)</code>.
  217.      */
  218.     public void appendText(String str) {
  219.     if (peer != null) {
  220.         insertText(str, getText().length());
  221.     } else {
  222.         text = text + str;
  223.     }
  224.     }
  225.  
  226.     /**
  227.      * Replaces text between the indicated start and end positions 
  228.      * with the specified replacement text.
  229.      * @param     str      the text to use as the replacement.
  230.      * @param     start    the start position.
  231.      * @param     end      the end position.
  232.      * @see       java.awt.TextArea#insert
  233.      * @since     JDK1.1
  234.      */
  235.     public synchronized void replaceRange(String str, int start, int end) {
  236.     replaceText(str, start, end);
  237.     }
  238.  
  239.     /**
  240.      * @deprecated As of JDK version 1.1,
  241.      * replaced by <code>replaceRange(String, int, int)</code>.
  242.      */
  243.     public void replaceText(String str, int start, int end) {
  244.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  245.     if (peer != null) {
  246.         peer.replaceText(str, start, end);
  247.     } else {
  248.         text = text.substring(0, start) + str + text.substring(end);
  249.     }
  250.     }
  251.  
  252.     /**
  253.      * Gets the number of rows in the text area.
  254.      * @return    the number of rows in the text area.
  255.      * @see       java.awt.TextArea#setRows
  256.      * @see       java.awt.TextArea#getColumns
  257.      * @since     JDK1
  258.      */
  259.     public int getRows() {
  260.     return rows;
  261.     }
  262.  
  263.     /**
  264.      * Sets the number of rows for this text area.
  265.      * @param       rows   the number of rows.
  266.      * @see         java.awt.TextArea#getRows
  267.      * @see         java.awt.TextArea#setColumns
  268.      * @exception   IllegalArgumentException if the value supplied
  269.      *                  for <code>rows</code> is less than zero.
  270.      * @since       JDK1.1
  271.      */
  272.     public void setRows(int rows) {
  273.     int oldVal = this.rows;
  274.     if (rows < 0) {
  275.         throw new IllegalArgumentException("rows less than zero.");
  276.     }
  277.     if (rows != oldVal) {
  278.         this.rows = rows;
  279.         invalidate();
  280.     }
  281.     }
  282.  
  283.     /**
  284.      * Gets the number of columns in this text area.
  285.      * @return    the number of columns in the text area.
  286.      * @see       java.awt.TextArea#setColumns
  287.      * @see       java.awt.TextArea#getRows
  288.      * @since     JDK1.0
  289.      */
  290.     public int getColumns() {
  291.     return columns;
  292.     }
  293.  
  294.     /**
  295.      * Sets the number of columns for this text area.
  296.      * @param       columns   the number of columns.
  297.      * @see         java.awt.TextArea#getColumns
  298.      * @see         java.awt.TextArea#setRows
  299.      * @exception   IllegalArgumentException if the value supplied
  300.      *                  for <code>columns</code> is less than zero.
  301.      * @since       JDK1.1
  302.      */
  303.     public void setColumns(int columns) {
  304.     int oldVal = this.columns;
  305.     if (columns < 0) {
  306.         throw new IllegalArgumentException("columns less than zero.");
  307.     }
  308.     if (columns != oldVal) {
  309.         this.columns = columns;
  310.         invalidate();
  311.     }
  312.     }
  313.  
  314.     /**
  315.      * Gets an enumerated value that indicates which scroll bars
  316.      * the text area uses. 
  317.      * <p>
  318.      * The <code>TextArea</code> class defines four integer constants 
  319.      * that are used to specify which scroll bars are available. 
  320.      * <code>TextArea</code> has one constructor that gives the 
  321.      * application discretion over scroll bars.
  322.      * @return     an integer that indicates which scroll bars are used.
  323.      * @see        java.awt.TextArea#SCROLLBARS_BOTH 
  324.      * @see        java.awt.TextArea#SCROLLBARS_VERTICAL_ONLY
  325.      * @see        java.awt.TextArea#SCROLLBARS_HORIZONTAL_ONLY
  326.      * @see        java.awt.TextArea#SCROLLBARS_NEITHER
  327.      * @see        java.awt.TextArea#TextArea(java.lang.String, int, int, int)
  328.      * @since      JDK1.1
  329.      */
  330.     public int getScrollbarVisibility() {
  331.         return scrollbarVisibility;
  332.     }
  333.  
  334.  
  335.     /**
  336.      * Determines the preferred size of a text area with the specified 
  337.      * number of rows and columns. 
  338.      * @param     rows   the number of rows.
  339.      * @param     cols   the number of columns.
  340.      * @return    the preferred dimensions required to display 
  341.      *                       the text area with the specified 
  342.      *                       number of rows and columns.
  343.      * @see       java.awt.Component#getPreferredSize
  344.      * @since     JDK1.1
  345.      */
  346.     public Dimension getPreferredSize(int rows, int columns) {
  347.         return preferredSize(rows, columns);
  348.     }
  349.  
  350.     /**
  351.      * @deprecated As of JDK version 1.1,
  352.      * replaced by <code>getPreferredSize(int, int)</code>.
  353.      */
  354.     public Dimension preferredSize(int rows, int columns) {
  355.     synchronized (Component.LOCK) {
  356.         TextAreaPeer peer = (TextAreaPeer)this.peer;
  357.         return (peer != null) ? 
  358.                peer.preferredSize(rows, columns) :
  359.                super.preferredSize();
  360.     }
  361.     }
  362.  
  363.     /**
  364.      * Determines the preferred size of this text area.  
  365.      * @return    the preferred dimensions needed for this text area.
  366.      * @see       java.awt.Component#getPreferredSize
  367.      * @since     JDK1.1
  368.      */
  369.     public Dimension getPreferredSize() {
  370.     return preferredSize();
  371.     }
  372.  
  373.     /**
  374.      * @deprecated As of JDK version 1.1,
  375.      * replaced by <code>getPreferredSize()</code>.
  376.      */
  377.     public Dimension preferredSize() {
  378.     synchronized (Component.LOCK) {
  379.         return ((rows > 0) && (columns > 0)) ? 
  380.                preferredSize(rows, columns) :
  381.                super.preferredSize();
  382.     }
  383.     }
  384.  
  385.     /**
  386.      * Determines the minimum size of a text area with the specified 
  387.      * number of rows and columns.
  388.      * @param     rows   the number of rows.
  389.      * @param     cols   the number of columns.
  390.      * @return    the minimum dimensions required to display 
  391.      *                       the text area with the specified 
  392.      *                       number of rows and columns.
  393.      * @see       java.awt.Component#getMinimumSize
  394.      * @since     JDK1.1
  395.      */
  396.     public Dimension getMinimumSize(int rows, int columns) {
  397.         return minimumSize(rows, columns);
  398.     }
  399.  
  400.     /**
  401.      * @deprecated As of JDK version 1.1,
  402.      * replaced by <code>getMinimumSize(int, int)</code>.
  403.      */
  404.     public Dimension minimumSize(int rows, int columns) {
  405.     synchronized (Component.LOCK) {
  406.         TextAreaPeer peer = (TextAreaPeer)this.peer;
  407.         return (peer != null) ? 
  408.                peer.minimumSize(rows, columns) :
  409.                super.minimumSize();
  410.     }
  411.     }
  412.  
  413.     /**
  414.      * Determines the minimum size of this text area. 
  415.      * @return    the preferred dimensions needed for this text area.
  416.      * @see       java.awt.Component#getPreferredSize
  417.      * @since     JDK1.1
  418.      */
  419.     public Dimension getMinimumSize() {
  420.     return minimumSize();
  421.     }
  422.  
  423.     /**
  424.      * @deprecated As of JDK version 1.1,
  425.      * replaced by <code>getMinimumSize()</code>.
  426.      */
  427.     public Dimension minimumSize() {
  428.     synchronized (Component.LOCK) {
  429.         return ((rows > 0) && (columns > 0)) ? 
  430.                minimumSize(rows, columns) :
  431.                super.minimumSize();
  432.     }
  433.     }
  434.  
  435.     /**
  436.      * Returns the parameter string representing the state of
  437.      * this text area. This string is useful for debugging.
  438.      * @return      the parameter string of this text area.
  439.      * @since       JDK1.0
  440.      */
  441.     protected String paramString() {
  442.     String sbVisStr;
  443.     switch (scrollbarVisibility) {
  444.         case SCROLLBARS_BOTH:
  445.         sbVisStr = "both";
  446.         break;
  447.         case SCROLLBARS_VERTICAL_ONLY:
  448.         sbVisStr = "vertical-only";
  449.         break;
  450.         case SCROLLBARS_HORIZONTAL_ONLY:
  451.         sbVisStr = "horizontal-only";
  452.         break;
  453.         case SCROLLBARS_NONE:
  454.         sbVisStr = "none";
  455.         break;
  456.         default:
  457.         sbVisStr = "invalid display policy";
  458.     }
  459.       
  460.     return super.paramString() + ",rows=" + rows + 
  461.         ",columns=" + columns + 
  462.       ", scrollbarVisibility=" + sbVisStr;
  463.     }
  464.  
  465. }
  466.