home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / TextComponent.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  15.0 KB  |  480 lines

  1. /*
  2.  * @(#)TextComponent.java    1.37 98/03/18
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.TextComponentPeer;
  17. import java.awt.event.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21. import sun.awt.SunToolkit;
  22.  
  23.  
  24. /**
  25.  * The <code>TextComponent</code> class is the superclass of 
  26.  * any component that allows the editing of some text. 
  27.  * <p>
  28.  * A text component embodies a string of text.  The 
  29.  * <code>TextComponent</code> class defines a set of methods 
  30.  * that determine whether or not this text is editable. If the
  31.  * component is editable, it defines another set of methods
  32.  * that supports a text insertion caret. 
  33.  * <p>
  34.  * In addition, the class defines methods that are used 
  35.  * to maintain a current <em>selection</em> from the text. 
  36.  * The text selection, a substring of the component's text, 
  37.  * is the target of editing operations. It is also referred
  38.  * to as the <em>selected text</em>.
  39.  *
  40.  * @version    1.37, 03/18/98
  41.  * @author     Sami Shaio
  42.  * @author     Arthur van Hoff
  43.  * @since       JDK1.0
  44.  */
  45. public class TextComponent extends Component {
  46.  
  47.     /**
  48.      * The value of the text.
  49.      */
  50.     String text;
  51.  
  52.     /**
  53.      * A boolean indicating whether or not this TextComponent is editable.
  54.      */
  55.     boolean editable = true;
  56.  
  57.     /**
  58.      * The selection start.
  59.      */
  60.     int selectionStart;
  61.  
  62.     /**
  63.      * The selection end.
  64.      */
  65.     int selectionEnd;
  66.  
  67.     transient protected TextListener textListener;
  68.  
  69.     /*
  70.      * JDK 1.1 serialVersionUID 
  71.      */
  72.     private static final long serialVersionUID = -2214773872412987419L;
  73.  
  74.     /**
  75.      * Constructs a new text component initialized with the 
  76.      * specified text. Sets the value of the cursor to 
  77.      * <code>Cursor.TEXT_CURSOR</code>.
  78.      * @param      text the initial text that the component presents.
  79.      * @see        java.awt.Cursor
  80.      */
  81.     TextComponent(String text) {
  82.     this.text = text;
  83.     setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
  84.     enableInputMethodsIfNecessary();
  85.     }
  86.  
  87.     private void enableInputMethodsIfNecessary() {
  88.     try {
  89.             Toolkit toolkit = Toolkit.getDefaultToolkit();
  90.             enableInputMethods(((SunToolkit) toolkit).enableInputMethodsForTextComponent());
  91.         } catch (Exception e) {
  92.             // if something bad happens, just don't enable input methods
  93.     }
  94.     }
  95.  
  96.     boolean areInputMethodsEnabled() {
  97.         // TextComponent handles key events without touching the eventMask or
  98.         // having a key listener, so just check inputMethodsEnabled
  99.         return inputMethodsEnabled;
  100.     }
  101.  
  102.     /**
  103.      * Removes the TextComponent's peer.  The peer allows us to modify
  104.      * the appearance of the TextComponent without changing its
  105.      * functionality.
  106.      */
  107.     public void removeNotify() {
  108.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  109.     if (peer != null) {
  110.         text = peer.getText();
  111.         selectionStart = peer.getSelectionStart();
  112.         selectionEnd = peer.getSelectionEnd();
  113.     }
  114.     super.removeNotify();
  115.     }
  116.  
  117.     /**
  118.      * Sets the text that is presented by this 
  119.      * text component to be the specified text. 
  120.      * @param       t   the new text.
  121.      * @see         java.awt.TextComponent#getText  
  122.      */
  123.     public synchronized void setText(String t) {
  124.     text = t;
  125.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  126.     if (peer != null) {
  127.         peer.setText(t);
  128.     }
  129.     }
  130.  
  131.     /**
  132.      * Gets the text that is presented by this text component.
  133.      * @see     java.awt.TextComponent#setText
  134.      */
  135.     public synchronized String getText() {
  136.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  137.     if (peer != null) {
  138.         text = peer.getText();
  139.     }
  140.     return text;
  141.     }
  142.  
  143.     /**
  144.      * Gets the selected text from the text that is
  145.      * presented by this text component.  
  146.      * @return      the selected text of this text component.
  147.      * @see         java.awt.TextComponent#select
  148.      */
  149.     public synchronized String getSelectedText() {
  150.     return getText().substring(getSelectionStart(), getSelectionEnd());
  151.     }
  152.  
  153.     /**
  154.      * Indicates whether or not this text component is editable.
  155.      * @return     <code>true</code> if this text component is
  156.      *                  editable; <code>false</code> otherwise.
  157.      * @see        java.awt.TextComponent#setEditable
  158.      * @since      JDK1ble
  159.      */
  160.     public boolean isEditable() {
  161.     return editable;
  162.     }
  163.  
  164.     /**
  165.      * Sets the flag that determines whether or not this
  166.      * text component is editable.
  167.      * <p>
  168.      * If the flag is set to <code>true</code>, this text component 
  169.      * becomes user editable. If the flag is set to <code>false</code>, 
  170.      * the user cannot change the text of this text component. 
  171.      * @param     t   a flag indicating whether this text component 
  172.      *                      should be user editable.
  173.      * @see       java.awt.TextComponent#isEditable
  174.      */
  175.     public synchronized void setEditable(boolean b) {
  176.     editable = b;
  177.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  178.     if (peer != null) {
  179.         peer.setEditable(b);
  180.     }
  181.     }
  182.  
  183.     /**
  184.      * Gets the start position of the selected text in 
  185.      * this text component. 
  186.      * @return      the start position of the selected text. 
  187.      * @see         java.awt.TextComponent#setSelectionStart
  188.      * @see         java.awt.TextComponent#getSelectionEnd
  189.      */
  190.     public synchronized int getSelectionStart() {
  191.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  192.     if (peer != null) {
  193.         selectionStart = peer.getSelectionStart();
  194.     }
  195.     return selectionStart;
  196.     }
  197.  
  198.     /**
  199.      * Sets the selection start for this text component to  
  200.      * the specified position. The new start point is constrained 
  201.      * to be at or before the current selection end. It also
  202.      * cannot be set to less than zero, the beginning of the 
  203.      * component's text.
  204.      * If the caller supplies a value for <code>selectionStart</code>
  205.      * that is out of bounds, the method enforces these constraints
  206.      * silently, and without failure.
  207.      * @param       selectionStart   the start position of the 
  208.      *                        selected text.
  209.      * @see         java.awt.TextComponent#getSelectionStart
  210.      * @see         java.awt.TextComponent#setSelectionEnd
  211.      * @since       JDK1.1
  212.      */
  213.     public synchronized void setSelectionStart(int selectionStart) {
  214.     /* Route through select method to enforce consistent policy
  215.          * between selectionStart and selectionEnd.
  216.          */
  217.     select(selectionStart, getSelectionEnd());
  218.     }
  219.  
  220.     /**
  221.      * Gets the end position of the selected text in 
  222.      * this text component. 
  223.      * @return      the end position of the selected text. 
  224.      * @see         java.awt.TextComponent#setSelectionEnd
  225.      * @see         java.awt.TextComponent#getSelectionStart
  226.      */
  227.     public synchronized int getSelectionEnd() {
  228.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  229.     if (peer != null) {
  230.         selectionEnd = peer.getSelectionEnd();
  231.     }
  232.     return selectionEnd;
  233.     }
  234.  
  235.     /**
  236.      * Sets the selection end for this text component to  
  237.      * the specified position. The new end point is constrained 
  238.      * to be at or after the current selection start. It also
  239.      * cannot be set beyond the end of the component's text.
  240.      * If the caller supplies a value for <code>selectionEnd</code>
  241.      * that is out of bounds, the method enforces these constraints
  242.      * silently, and without failure.
  243.      * @param       selectionEnd   the end position of the 
  244.      *                        selected text.
  245.      * @see         java.awt.TextComponent#getSelectionEnd
  246.      * @see         java.awt.TextComponent#setSelectionStart
  247.      * @since       JDK1.1
  248.      */
  249.     public synchronized void setSelectionEnd(int selectionEnd) {
  250.     /* Route through select method to enforce consistent policy
  251.          * between selectionStart and selectionEnd.
  252.          */
  253.     select(getSelectionStart(), selectionEnd);
  254.     }
  255.     
  256.     /**
  257.      * Selects the text between the specified start and end positions.
  258.      * <p>
  259.      * This method sets the start and end positions of the 
  260.      * selected text, enforcing the restriction that the end 
  261.      * position must be greater than or equal to the start position.
  262.      * The start position must be greater than zero, and the 
  263.      * end position must be less that or equal to the length
  264.      * of the text component's text. If the caller supplies values
  265.      * that are inconsistent or out of bounds, the method enforces 
  266.      * these constraints silently, and without failure.
  267.      * @param        selectionStart the start position of the 
  268.      *                             text to select.
  269.      * @param        selectionEnd the end position of the 
  270.      *                             text to select.
  271.      * @see          java.awt.TextComponent#setSelectionStart
  272.      * @see          java.awt.TextComponent#setSelectionEnd
  273.      * @see          java.awt.TextComponent#selectAll
  274.      */
  275.     public synchronized void select(int selectionStart, int selectionEnd) {
  276.     String text = getText();
  277.     if (selectionStart < 0) {
  278.         selectionStart = 0;
  279.     }
  280.     if (selectionEnd > text.length()) {
  281.         selectionEnd = text.length();
  282.     }
  283.     if (selectionEnd < selectionStart) {
  284.         selectionEnd = selectionStart;
  285.     }
  286.     if (selectionStart > selectionEnd) {
  287.         selectionStart = selectionEnd;
  288.     }
  289.  
  290.     this.selectionStart = selectionStart;
  291.     this.selectionEnd = selectionEnd;
  292.  
  293.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  294.     if (peer != null) {
  295.         peer.select(selectionStart, selectionEnd);
  296.     }
  297.     }
  298.  
  299.     /**
  300.      * Selects all the text in this text component.
  301.      * @see        java.awt.TextComponent@select
  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.      */
  428.     protected String paramString() {
  429.     String str = super.paramString() + ",text=" + getText();
  430.     if (editable) {
  431.         str += ",editable";
  432.     }
  433.     return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
  434.     }
  435.  
  436.  
  437.     /* 
  438.      * Serialization support.  Since the value of the fields
  439.      * selectionStart, and selectionEnd, and text aren't neccessarily
  440.      * up to date we sync them up with the peer before serializing.
  441.      */
  442.  
  443.     private int textComponentSerializedDataVersion = 1;
  444.  
  445.  
  446.     private void writeObject(java.io.ObjectOutputStream s)
  447.       throws java.io.IOException 
  448.     {
  449.       TextComponentPeer peer = (TextComponentPeer)this.peer;
  450.       if (peer != null) {
  451.     text = peer.getText();
  452.     selectionStart = peer.getSelectionStart();
  453.     selectionEnd = peer.getSelectionEnd();
  454.       }
  455.       s.defaultWriteObject();
  456.  
  457.       AWTEventMulticaster.save(s, textListenerK, textListener);
  458.       s.writeObject(null);
  459.     }
  460.  
  461.  
  462.     private void readObject(ObjectInputStream s)
  463.         throws ClassNotFoundException, IOException 
  464.     {
  465.         s.defaultReadObject();
  466.  
  467.         Object keyOrNull;
  468.         while(null != (keyOrNull = s.readObject())) {
  469.         String key = ((String)keyOrNull).intern();
  470.  
  471.         if (textListenerK == key) 
  472.             addTextListener((TextListener)(s.readObject()));
  473.  
  474.         else // skip value for unrecognized key
  475.             s.readObject();
  476.         }
  477.     enableInputMethodsIfNecessary();
  478.     }
  479. }
  480.