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

  1. /*
  2.  * @(#)TextComponent.java    1.30 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.TextComponentPeer;
  25. import java.awt.event.*;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * The <code>TextComponent</code> class is the superclass of 
  33.  * any component that allows the editing of some text. 
  34.  * <p>
  35.  * A text component embodies a string of text.  The 
  36.  * <code>TextComponent</code> class defines a set of methods 
  37.  * that determine whether or not this text is editable. If the
  38.  * component is editable, it defines another set of methods
  39.  * that supports a text insertion caret. 
  40.  * <p>
  41.  * In addition, the class defines methods that are used 
  42.  * to maintain a current <em>selection</em> from the text. 
  43.  * The text selection, a substring of the component's text, 
  44.  * is the target of editing operations. It is also referred
  45.  * to as the <em>selected text</em>.
  46.  *
  47.  * @version    1.30, 06/17/97
  48.  * @author     Sami Shaio
  49.  * @author     Arthur van Hoff
  50.  * @since       JDK1.0
  51.  */
  52. public class TextComponent extends Component {
  53.  
  54.     /**
  55.      * The value of the text.
  56.      */
  57.     String text;
  58.  
  59.     /**
  60.      * A boolean indicating whether or not this TextComponent is editable.
  61.      */
  62.     boolean editable = true;
  63.  
  64.     /**
  65.      * The selection start.
  66.      */
  67.     int selectionStart;
  68.  
  69.     /**
  70.      * The selection end.
  71.      */
  72.     int selectionEnd;
  73.  
  74.     transient protected TextListener textListener;
  75.  
  76.     /*
  77.      * JDK 1.1 serialVersionUID 
  78.      */
  79.     private static final long serialVersionUID = -2214773872412987419L;
  80.  
  81.     /**
  82.      * Constructs a new text component initialized with the 
  83.      * specified text. Sets the value of the cursor to 
  84.      * <code>Cursor.TEXT_CURSOR</code>.
  85.      * @param      text the initial text that the component presents.
  86.      * @see        java.awt.Cursor
  87.      * @since      JDK1.0
  88.      */
  89.     TextComponent(String text) {
  90.     this.text = text;
  91.     setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
  92.     }
  93.  
  94.     /**
  95.      * Removes the TextComponent's peer.  The peer allows us to modify
  96.      * the appearance of the TextComponent without changing its
  97.      * functionality.
  98.      */
  99.     public void removeNotify() {
  100.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  101.     if (peer != null) {
  102.         text = peer.getText();
  103.         selectionStart = peer.getSelectionStart();
  104.         selectionEnd = peer.getSelectionEnd();
  105.     }
  106.     super.removeNotify();
  107.     }
  108.  
  109.     /**
  110.      * Sets the text that is presented by this 
  111.      * text component to be the specified text. 
  112.      * @param       t   the new text.
  113.      * @see         java.awt.TextComponent#getText  
  114.      * @since       JDK1.0
  115.      */
  116.     public synchronized void setText(String t) {
  117.     text = t;
  118.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  119.     if (peer != null) {
  120.         peer.setText(t);
  121.     }
  122.     }
  123.  
  124.     /**
  125.      * Gets the text that is presented by this text component.
  126.      * @see     java.awt.TextComponent#setText
  127.      * @since   JDK1.0
  128.      */
  129.     public synchronized String getText() {
  130.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  131.     if (peer != null) {
  132.         text = peer.getText();
  133.     }
  134.     return text;
  135.     }
  136.  
  137.     /**
  138.      * Gets the selected text from the text that is
  139.      * presented by this text component.  
  140.      * @return      the selected text of this text component.
  141.      * @see         java.awt.TextComponent#select
  142.      * @since       JDK1.0
  143.      */
  144.     public synchronized String getSelectedText() {
  145.     return getText().substring(getSelectionStart(), getSelectionEnd());
  146.     }
  147.  
  148.     /**
  149.      * Indicates whether or not this text component is editable.
  150.      * @return     <code>true</code> if this text component is
  151.      *                  editable; <code>false</code> otherwise.
  152.      * @see        java.awt.TextComponent#setEditable
  153.      * @since      JDK1ble
  154.      */
  155.     public boolean isEditable() {
  156.     return editable;
  157.     }
  158.  
  159.     /**
  160.      * Sets the flag that determines whether or not this
  161.      * text component is editable.
  162.      * <p>
  163.      * If the flag is set to <code>true</code>, this text component 
  164.      * becomes user editable. If the flag is set to <code>false</code>, 
  165.      * the user cannot change the text of this text component. 
  166.      * @param     t   a flag indicating whether this text component 
  167.      *                      should be user editable.
  168.      * @see       java.awt.TextComponent#isEditable
  169.      * @since     JDK1.0
  170.      */
  171.     public synchronized void setEditable(boolean b) {
  172.     editable = b;
  173.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  174.     if (peer != null) {
  175.         peer.setEditable(b);
  176.     }
  177.     }
  178.  
  179.     /**
  180.      * Gets the start position of the selected text in 
  181.      * this text component. 
  182.      * @return      the start position of the selected text. 
  183.      * @see         java.awt.TextComponent#setSelectionStart
  184.      * @see         java.awt.TextComponent#getSelectionEnd
  185.      * @since       JDK1.0
  186.      */
  187.     public synchronized int getSelectionStart() {
  188.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  189.     if (peer != null) {
  190.         selectionStart = peer.getSelectionStart();
  191.     }
  192.     return selectionStart;
  193.     }
  194.  
  195.     /**
  196.      * Sets the selection start for this text component to  
  197.      * the specified position. The new start point is constrained 
  198.      * to be at or before the current selection end. It also
  199.      * cannot be set to less than zero, the beginning of the 
  200.      * component's text.
  201.      * If the caller supplies a value for <code>selectionStart</code>
  202.      * that is out of bounds, the method enforces these constraints
  203.      * silently, and without failure.
  204.      * @param       selectionStart   the start position of the 
  205.      *                        selected text.
  206.      * @see         java.awt.TextComponent#getSelectionStart
  207.      * @see         java.awt.TextComponent#setSelectionEnd
  208.      * @since       JDK1.1
  209.      */
  210.     public synchronized void setSelectionStart(int selectionStart) {
  211.     /* Route through select method to enforce consistent policy
  212.          * between selectionStart and selectionEnd.
  213.          */
  214.     select(selectionStart, getSelectionEnd());
  215.     }
  216.  
  217.     /**
  218.      * Gets the end position of the selected text in 
  219.      * this text component. 
  220.      * @return      the end position of the selected text. 
  221.      * @see         java.awt.TextComponent#setSelectionEnd
  222.      * @see         java.awt.TextComponent#getSelectionStart
  223.      * @since       JDK1.0
  224.      */
  225.     public synchronized int getSelectionEnd() {
  226.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  227.     if (peer != null) {
  228.         selectionEnd = peer.getSelectionEnd();
  229.     }
  230.     return selectionEnd;
  231.     }
  232.  
  233.     /**
  234.      * Sets the selection end for this text component to  
  235.      * the specified position. The new end point is constrained 
  236.      * to be at or after the current selection start. It also
  237.      * cannot be set beyond the end of the component's text.
  238.      * If the caller supplies a value for <code>selectionEnd</code>
  239.      * that is out of bounds, the method enforces these constraints
  240.      * silently, and without failure.
  241.      * @param       selectionEnd   the end position of the 
  242.      *                        selected text.
  243.      * @see         java.awt.TextComponent#getSelectionEnd
  244.      * @see         java.awt.TextComponent#setSelectionStart
  245.      * @since       JDK1.1
  246.      */
  247.     public synchronized void setSelectionEnd(int selectionEnd) {
  248.     /* Route through select method to enforce consistent policy
  249.          * between selectionStart and selectionEnd.
  250.          */
  251.     select(getSelectionStart(), selectionEnd);
  252.     }
  253.     
  254.     /**
  255.      * Selects the text between the specified start and end positions.
  256.      * <p>
  257.      * This method sets the start and end positions of the 
  258.      * selected text, enforcing the restriction that the end 
  259.      * position must be greater than or equal to the start position.
  260.      * The start position must be greater than zero, and the 
  261.      * end position must be less that or equal to the length
  262.      * of the text component's text. If the caller supplies values
  263.      * that are inconsistent or out of bounds, the method enforces 
  264.      * these constraints silently, and without failure.
  265.      * @param        selectionStart the start position of the 
  266.      *                             text to select.
  267.      * @param        selectionEnd the end position of the 
  268.      *                             text to select.
  269.      * @see          java.awt.TextComponent#setSelectionStart
  270.      * @see          java.awt.TextComponent#setSelectionEnd
  271.      * @see          java.awt.TextComponent#selectAll
  272.      * @since        JDK1.0
  273.      */
  274.     public synchronized void select(int selectionStart, int selectionEnd) {
  275.     String text = getText();
  276.     if (selectionStart < 0) {
  277.         selectionStart = 0;
  278.     }
  279.     if (selectionEnd > text.length()) {
  280.         selectionEnd = text.length();
  281.     }
  282.     if (selectionEnd < selectionStart) {
  283.         selectionEnd = selectionStart;
  284.     }
  285.     if (selectionStart > selectionEnd) {
  286.         selectionStart = selectionEnd;
  287.     }
  288.  
  289.     this.selectionStart = selectionStart;
  290.     this.selectionEnd = selectionEnd;
  291.  
  292.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  293.     if (peer != null) {
  294.         peer.select(selectionStart, selectionEnd);
  295.     }
  296.     }
  297.  
  298.     /**
  299.      * Selects all the text in this text component.
  300.      * @see        java.awt.TextComponent@select
  301.      * @since      JDK1.0
  302.      */
  303.     public synchronized void selectAll() {
  304.     String text = getText();
  305.     this.selectionStart = 0;
  306.     this.selectionEnd = getText().length();
  307.  
  308.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  309.     if (peer != null) {
  310.         peer.select(selectionStart, selectionEnd);
  311.     }
  312.     }
  313.  
  314.     /**
  315.      * Sets the position of the text insertion caret for 
  316.      * this text component.
  317.      * 
  318.      * @param        position the position of the text insertion caret.
  319.      * @exception    IllegalArgumentException if the value supplied
  320.      *                   for <code>position</code> is less than zero.
  321.      * @since        JDK1.1
  322.      */
  323.     public void setCaretPosition(int position) {
  324.     if (position < 0) {
  325.         throw new IllegalArgumentException("position less than zero.");
  326.     }
  327.  
  328.     int maxposition = getText().length();
  329.     if (position > maxposition) {
  330.         position = maxposition;
  331.     }
  332.  
  333.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  334.     if (peer != null) {
  335.         peer.setCaretPosition(position);
  336.     } else {
  337.         throw new IllegalComponentStateException("Cannot set caret position until after the peer has been created");
  338.     }
  339.     }
  340.  
  341.     /**
  342.      * Gets the position of the text insertion caret for 
  343.      * this text component.
  344.      * @return       the position of the text insertion caret.
  345.      * @since        JDK1.1
  346.      */
  347.     public int getCaretPosition() {
  348.         TextComponentPeer peer = (TextComponentPeer)this.peer;
  349.     int position = 0;
  350.  
  351.     if (peer != null) {
  352.         position = peer.getCaretPosition();
  353.     } 
  354.     return position;
  355.     }
  356.  
  357.     /**
  358.      * Adds the specified text event listener to recieve text events 
  359.      * from this textcomponent.
  360.      * @param l the text event listener
  361.      */ 
  362.     public synchronized void addTextListener(TextListener l) {
  363.     textListener = AWTEventMulticaster.add(textListener, l);
  364.         newEventsOnly = true;
  365.     }
  366.  
  367.     /**
  368.      * Removes the specified text event listener so that it no longer
  369.      * receives text events from this textcomponent
  370.      */ 
  371.     public void removeTextListener(TextListener l) {
  372.     textListener = AWTEventMulticaster.remove(textListener, l);
  373.     }
  374.  
  375.     // REMIND: remove when filtering is done at lower level
  376.     boolean eventEnabled(AWTEvent e) {
  377.         if (e.id == TextEvent.TEXT_VALUE_CHANGED) {
  378.             if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 ||
  379.                 textListener != null) {
  380.                 return true;
  381.             } 
  382.             return false;
  383.         }
  384.         return super.eventEnabled(e);
  385.     }     
  386.  
  387.     /**
  388.      * Processes events on this textcomponent. If the event is a
  389.      * TextEvent, it invokes the processTextEvent method,
  390.      * else it invokes its superclass's processEvent.
  391.      * @param e the event
  392.      */
  393.     protected void processEvent(AWTEvent e) {
  394.         if (e instanceof TextEvent) {
  395.             processTextEvent((TextEvent)e);
  396.             return;
  397.         }
  398.     super.processEvent(e);
  399.     }
  400.  
  401.     /** 
  402.      * Processes text events occurring on this text component by
  403.      * dispatching them to any registered TextListener objects.
  404.      * NOTE: This method will not be called unless text events
  405.      * are enabled for this component; this happens when one of the
  406.      * following occurs:
  407.      * a) A TextListener object is registered via addTextListener()
  408.      * b) Text events are enabled via enableEvents()
  409.      * @see Component#enableEvents
  410.      * @param e the text event
  411.      */ 
  412.     protected void processTextEvent(TextEvent e) {
  413.         if (textListener != null) {
  414.             int id = e.getID();
  415.         switch (id) {
  416.         case TextEvent.TEXT_VALUE_CHANGED:
  417.         textListener.textValueChanged(e);
  418.         break;
  419.         }
  420.         }
  421.     }
  422.  
  423.     /**
  424.      * Returns the parameter string representing the state of this text 
  425.      * component. This string is useful for debugging. 
  426.      * @return      the parameter string of this text component.
  427.      * @since       JDK1.0
  428.      */
  429.     protected String paramString() {
  430.     String str = super.paramString() + ",text=" + getText();
  431.     if (editable) {
  432.         str += ",editable";
  433.     }
  434.     return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
  435.     }
  436.  
  437.  
  438.     /* 
  439.      * Serialization support.  Since the value of the fields
  440.      * selectionStart, and selectionEnd, and text aren't neccessarily
  441.      * up to date we sync them up with the peer before serializing.
  442.      */
  443.  
  444.     private int textComponentSerializedDataVersion = 1;
  445.  
  446.  
  447.     private void writeObject(java.io.ObjectOutputStream s)
  448.       throws java.io.IOException 
  449.     {
  450.       TextComponentPeer peer = (TextComponentPeer)this.peer;
  451.       if (peer != null) {
  452.     text = peer.getText();
  453.     selectionStart = peer.getSelectionStart();
  454.     selectionEnd = peer.getSelectionEnd();
  455.       }
  456.       s.defaultWriteObject();
  457.  
  458.       AWTEventMulticaster.save(s, textListenerK, textListener);
  459.       s.writeObject(null);
  460.     }
  461.  
  462.  
  463.     private void readObject(ObjectInputStream s)
  464.       throws ClassNotFoundException, IOException 
  465.     {
  466.       s.defaultReadObject();
  467.  
  468.       Object keyOrNull;
  469.       while(null != (keyOrNull = s.readObject())) {
  470.     String key = ((String)keyOrNull).intern();
  471.  
  472.     if (textListenerK == key) 
  473.       addTextListener((TextListener)(s.readObject()));
  474.  
  475.     else // skip value for unrecognized key
  476.       s.readObject();
  477.       }
  478.     }
  479. }
  480.