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

  1. /*
  2.  * @(#)TextComponent.java    1.8 95/11/02 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1995 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.TextComponentPeer;
  22.  
  23. /**
  24.  * A TextComponent is a component that allows the editing of some text.
  25.  *
  26.  * @version    1.8, 02 Nov 1995
  27.  * @author     Sami Shaio
  28.  * @author     Arthur van Hoff
  29.  */
  30. public class TextComponent extends Component {
  31.  
  32.     /**
  33.      * The value of the text.
  34.      */
  35.     String text;
  36.  
  37.     /**
  38.      * A boolean indicating whether or not this TextComponent is editable.
  39.      */
  40.     boolean editable = true;
  41.  
  42.     /**
  43.      * The selection start.
  44.      */
  45.     int selStart;
  46.  
  47.     /**
  48.      * The selection end.
  49.      */
  50.     int selEnd;
  51.  
  52.     /**
  53.      * Constructs a new TextComponent initialized with the specified text.
  54.      * @param text the initial text of the field.
  55.      */
  56.     TextComponent(String text) {
  57.     this.text = text;
  58.     }
  59.  
  60.     /**
  61.      * Removes the TextComponent's peer.  The peer allows us to modify the appearance
  62.      * of the TextComponent without changing its functionality.
  63.      */
  64.     public synchronized void removeNotify() {
  65.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  66.     if (peer != null) {
  67.         text = peer.getText();
  68.         selStart = peer.getSelectionStart();
  69.         selEnd = peer.getSelectionEnd();
  70.     }
  71.     super.removeNotify();
  72.     }
  73.  
  74.     /**
  75.      * Sets the text of this TextComponent to the specified text.
  76.      * @param t the new text to be set
  77.      * @see #getText
  78.      */
  79.     public void setText(String t) {
  80.     text = t;
  81.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  82.     if (peer != null) {
  83.         peer.setText(t);
  84.     }
  85.     }
  86.  
  87.     /**
  88.      * Returns the text contained in this TextComponent.
  89.      * @see #setText
  90.      */
  91.     public String getText() {
  92.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  93.     if (peer != null) {
  94.         text = peer.getText();
  95.     }
  96.     return text;
  97.     }
  98.  
  99.     /**
  100.      * Returns the selected text contained in this TextComponent.
  101.      * @see #setText
  102.      */
  103.     public String getSelectedText() {
  104.     return getText().substring(getSelectionStart(), getSelectionEnd());
  105.     }
  106.  
  107.     /**
  108.      * Returns the boolean indicating whether this TextComponent is editable or not.
  109.      * @see #setEditable
  110.      */
  111.     public boolean isEditable() {
  112.     return editable;
  113.     }
  114.  
  115.     /**
  116.      * Sets the specified boolean to indicate whether or not this TextComponent should be 
  117.      * editable.
  118.      * @param t the boolean to be set
  119.      * @see #isEditable
  120.      */
  121.     public void setEditable(boolean t) {
  122.     editable = t;
  123.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  124.     if (peer != null) {
  125.         peer.setEditable(t);
  126.     }
  127.     }
  128.  
  129.     /**
  130.      * Returns the selected text's start position.
  131.      */
  132.     public int getSelectionStart() {
  133.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  134.     if (peer != null) {
  135.         selStart = peer.getSelectionStart();
  136.     }
  137.     return selStart;
  138.     }
  139.  
  140.     /**
  141.      * Returns the selected text's end position.
  142.      */
  143.     public int getSelectionEnd() {
  144.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  145.     if (peer != null) {
  146.         selEnd = peer.getSelectionEnd();
  147.     }
  148.     return selEnd;
  149.     }
  150.     
  151.     /**
  152.      * Selects the text found between the specified start and end locations.
  153.      * @param selStart the start position of the text
  154.      * @param selEnd the end position of the text
  155.      */
  156.     public void select(int selStart, int selEnd) {
  157.     String text = getText();
  158.     if (selStart < 0) {
  159.         selStart = 0;
  160.     }
  161.     if (selEnd > text.length()) {
  162.         selEnd = text.length();
  163.     }
  164.     if (selEnd < selStart) {
  165.         selEnd = selStart;
  166.     }
  167.     if (selStart > selEnd) {
  168.         selStart = selEnd;
  169.     }
  170.  
  171.     this.selStart = selStart;
  172.     this.selEnd = selEnd;
  173.  
  174.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  175.     if (peer != null) {
  176.         peer.select(selStart, selEnd);
  177.     }
  178.     }
  179.  
  180.     /**
  181.      * Selects all the text in the TextComponent.
  182.      */
  183.     public void selectAll() {
  184.     String text = getText();
  185.     this.selStart = 0;
  186.     this.selEnd = getText().length();
  187.  
  188.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  189.     if (peer != null) {
  190.         peer.select(selStart, selEnd);
  191.     }
  192.     }
  193.  
  194.     /**
  195.      * Returns the String of parameters for this TextComponent.
  196.      */
  197.     protected String paramString() {
  198.     String str = super.paramString() + ",text=" + getText();
  199.     if (editable) {
  200.         str += ",editable";
  201.     }
  202.     return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
  203.     }
  204. }
  205.