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

  1. /*
  2.  * @(#)TextField.java    1.41 98/03/18
  3.  *
  4.  * Copyright 1995-1997 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.TextFieldPeer;
  17. import java.awt.event.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21.  
  22.  
  23. /**
  24.  * A <code>TextField</code> object is a text component 
  25.  * that allows for the editing of a single line of text.
  26.  * <p>
  27.  * For example, the following image depicts a frame with four
  28.  * text fields of varying widths. Two of these text fields
  29.  * display the predefined text <code>"Hello"</code>.
  30.  * <p>
  31.  * <img src="images-awt/TextField-1.gif"
  32.  * ALIGN=center HSPACE=10 VSPACE=7>
  33.  * <p>
  34.  * Here is the code that produces these four text fields:
  35.  * <p>
  36.  * <hr><blockquote><pre>
  37.  * TextField tf1, tf2, tf3, tf4; 
  38.  * // a blank text field
  39.  * tf1 = new TextField();
  40.  * // blank field of 20 columns
  41.  * tf2 = new TextField("", 20);
  42.  * // predefined text displayed
  43.  * tf3 = new TextField("Hello!");
  44.  * // predefined text in 30 columns
  45.  * tf4 = new TextField("Hello", 30);
  46.  * </pre></blockquote><hr>
  47.  * <p>
  48.  * Every time the user types a key in the text field, AWT 
  49.  * sends two action events to the text field. The first 
  50.  * one represents the key press and the second one, 
  51.  * the key release. Each action event embodies the state 
  52.  * of the system at the time that some action occurred.
  53.  * The properties of an action event indicate which 
  54.  * key was pressed, what modifier keys were also pressed,
  55.  * and the time at which the event occurred. 
  56.  * <p>
  57.  * Since the event is an instance of <code>ActionEvent</code>, 
  58.  * the <code>TextField</code> class's <code>processEvent</code> 
  59.  * method examines the event and passes it along to 
  60.  * <code>processActionEvent</code>. The latter method redirects the
  61.  * event to any <code>ActionListener</code> objects that have
  62.  * registered an interest in action events generated by this
  63.  * text field. 
  64.  *
  65.  * @version    1.41, 03/18/98
  66.  * @author     Sami Shaio
  67.  * @see         java.awt.event.ActionEvent
  68.  * @see         java.awt.TextField#processEvent
  69.  * @see         java.awt.TextField#processActionEvent
  70.  * @since       JDK1.0
  71.  */
  72. public class TextField extends TextComponent {
  73.  
  74.     /**
  75.      * The number of columns in the TextField.
  76.      */
  77.     int columns;
  78.  
  79.     /**
  80.      * The echo character.
  81.      */
  82.     char echoChar;
  83.  
  84.     transient ActionListener actionListener;
  85.  
  86.     private static final String base = "textfield";
  87.     private static int nameCounter = 0;
  88.  
  89.     /*
  90.      * JDK 1.1 serialVersionUID 
  91.      */
  92.     private static final long serialVersionUID = -2966288784432217853L;
  93.  
  94.     /**
  95.      * Constructs a new text field.
  96.      */
  97.     public TextField() {
  98.     this("", 0);
  99.     }
  100.  
  101.     /**
  102.      * Constructs a new text field initialized with the specified text.
  103.      * @param      text       the text to be displayed.
  104.      */
  105.     public TextField(String text) {
  106.     this(text, text.length());
  107.     }
  108.  
  109.     /**
  110.      * Constructs a new empty TextField with the specified number of columns.
  111.      * @param columns the number of columns
  112.      */ 
  113.     public TextField(int columns) {
  114.     this("", columns);
  115.     }
  116.  
  117.     /**
  118.      * Constructs a new text field initialized with the specified text
  119.      * to be displayed, and wide enough to hold the specified 
  120.      * number of characters.
  121.      * @param      text       the text to be displayed.
  122.      * @param      columns    the number of characters.
  123.      */
  124.     public TextField(String text, int columns) {
  125.     super(text);
  126.     this.name = base + nameCounter++;
  127.     this.columns = columns;
  128.     }
  129.  
  130.     /**
  131.      * Creates the TextField's peer.  The peer allows us to modify the
  132.      * appearance of the TextField without changing its functionality.
  133.      */
  134.     public void addNotify() {
  135.     peer = getToolkit().createTextField(this);
  136.     super.addNotify();
  137.     }
  138.  
  139.     /**
  140.      * Gets the character that is to be used for echoing.
  141.      * <p>
  142.      * An echo character is useful for text fields where 
  143.      * user input should not be echoed to the screen, as in 
  144.      * the case of a text field for entering a password.
  145.      * @return      the echo character for this text field.
  146.      * @see         java.awt.TextField#echoCharIsSet
  147.      * @see         java.awt.TextField#setEchoChar
  148.      */
  149.     public char getEchoChar() {
  150.     return echoChar;
  151.     }
  152.  
  153.     /**
  154.      * Sets the echo character for this text field. 
  155.      * <p>
  156.      * An echo character is useful for text fields where 
  157.      * user input should not be echoed to the screen, as in 
  158.      * the case of a text field for entering a password.
  159.      * @param       c   the echo character for this text field.
  160.      * @see         java.awt.TextField#echoCharIsSet
  161.      * @see         java.awt.TextField#getEchoChar
  162.      * @since       JDK1.1
  163.      */
  164.     public void setEchoChar(char c) {
  165.     setEchoCharacter(c);
  166.     }
  167.  
  168.     /**
  169.      * @deprecated As of JDK version 1.1,
  170.      * replaced by <code>setEchoChar(char)</code>.
  171.      */
  172.     public void setEchoCharacter(char c) {
  173.     echoChar = c;
  174.     TextFieldPeer peer = (TextFieldPeer)this.peer;
  175.     if (peer != null) {
  176.         peer.setEchoCharacter(c);
  177.     }
  178.     }
  179.  
  180.     /**
  181.      * Indicates whether or not this text field has a 
  182.      * character set for echoing.
  183.      * <p>
  184.      * An echo character is useful for text fields where 
  185.      * user input should not be echoed to the screen, as in 
  186.      * the case of a text field for entering a password.
  187.      * @return     <code>true</code> if this text field has 
  188.      *                 a character set for echoing; 
  189.      *                 <code>false</code> otherwise.
  190.      * @see        java.awt.TextField#setEchoChar
  191.      * @see        java.awt.TextField#getEchoChar
  192.      */
  193.     public boolean echoCharIsSet() {
  194.     return echoChar != 0;
  195.     }
  196.  
  197.     /**
  198.      * Gets the number of columns in this text field. 
  199.      * @return     the number of columns.
  200.      * @see        java.awt.TextField#setColumns
  201.      * @since      JDK1.1ld.
  202.      */
  203.     public int getColumns() {
  204.     return columns;
  205.     }
  206.  
  207.     /**
  208.      * Sets the number of columns in this text field.
  209.      * @param      columns   the number of columns.
  210.      * @see        java.awt.TextField#getColumns
  211.      * @exception  IllegalArgumentException   if the value
  212.      *                 supplied for <code>columns</code> 
  213.      *                 is less than zero.
  214.      * @since      JDK1.1
  215.      */
  216.     public void setColumns(int columns) {
  217.     int oldVal = this.columns;
  218.     if (columns < 0) {
  219.         throw new IllegalArgumentException("columns less than zero.");
  220.     }
  221.     if (columns != oldVal) {
  222.         this.columns = columns;
  223.         invalidate();
  224.     }
  225.     }
  226.  
  227.     /**
  228.      * Gets the preferred size of this text field 
  229.      * with the specified number of columns.
  230.      * @param     columns the number of columns 
  231.      *                 in this text field. 
  232.      * @return    the preferred dimensions for 
  233.      *                 displaying this text field.
  234.      * @since     JDK1.1
  235.      */
  236.     public Dimension getPreferredSize(int columns) {
  237.         return preferredSize(columns);
  238.     }
  239.  
  240.     /**
  241.      * @deprecated As of JDK version 1.1,
  242.      * replaced by <code>getPreferredSize(int)</code>.
  243.      */
  244.     public Dimension preferredSize(int columns) {
  245.     synchronized (Component.LOCK) {
  246.         TextFieldPeer peer = (TextFieldPeer)this.peer;
  247.         return (peer != null) ?
  248.                peer.preferredSize(columns) :
  249.                super.preferredSize();
  250.     }
  251.     }
  252.  
  253.     /**
  254.      * Gets the preferred size of this text field. 
  255.      * @return     the preferred dimensions for 
  256.      *                         displaying this text field.
  257.      * @since      JDK1.1
  258.      */
  259.     public Dimension getPreferredSize() {
  260.         return preferredSize();
  261.     }
  262.  
  263.     /**
  264.      * @deprecated As of JDK version 1.1,
  265.      * replaced by <code>getPreferredSize()</code>.
  266.      */
  267.     public Dimension preferredSize() {
  268.     synchronized (Component.LOCK) {
  269.         return (columns > 0) ?
  270.                preferredSize(columns) :
  271.                super.preferredSize();
  272.     }
  273.     }
  274.  
  275.     /**
  276.      * Gets the minumum dimensions for a text field with 
  277.      * the specified number of columns.
  278.      * @param    columns   the number of columns in 
  279.      *                          this text field.
  280.      * @since    JDK1.1
  281.      */
  282.     public Dimension getMinimumSize(int columns) {
  283.         return minimumSize(columns);
  284.     }
  285.  
  286.     /**
  287.      * @deprecated As of JDK version 1.1,
  288.      * replaced by <code>getMinimumSize(int)</code>.
  289.      */
  290.     public Dimension minimumSize(int columns) {
  291.     synchronized (Component.LOCK) {
  292.         TextFieldPeer peer = (TextFieldPeer)this.peer;
  293.         return (peer != null) ?
  294.                peer.minimumSize(columns) :
  295.                super.minimumSize();
  296.     }
  297.     }
  298.  
  299.     /**
  300.      * Gets the minumum dimensions for this text field.
  301.      * @return     the minimum dimensions for 
  302.      *                  displaying this text field.
  303.      * @since      JDK1.1
  304.      */
  305.     public Dimension getMinimumSize() {
  306.         return minimumSize();
  307.     }
  308.  
  309.     /**
  310.      * @deprecated As of JDK version 1.1,
  311.      * replaced by <code>getMinimumSize()</code>.
  312.      */
  313.     public Dimension minimumSize() {
  314.     synchronized (Component.LOCK) {
  315.         return (columns > 0) ?
  316.                minimumSize(columns) :
  317.                super.minimumSize();
  318.     }
  319.     }
  320.  
  321.     /**
  322.      * Adds the specified action listener to recieve 
  323.      * action events from this text field.
  324.      * @param      l the action listener.
  325.      * @see        java.awt.event#ActionListener
  326.      * @see        java.awt.TextField#removeActionListener
  327.      * @since      JDK1.1
  328.      */ 
  329.     public synchronized void addActionListener(ActionListener l) {
  330.     actionListener = AWTEventMulticaster.add(actionListener, l);
  331.         newEventsOnly = true;    
  332.     }
  333.  
  334.     /**
  335.      * Removes the specified action listener so that it no longer
  336.      * receives action events from this text field.
  337.      * @param      l the action listener.
  338.      * @see        java.awt.event#ActionListener
  339.      * @see        java.awt.TextField#addActionListener
  340.      * @since      JDK1.1 
  341.      */ 
  342.     public synchronized void removeActionListener(ActionListener l) {
  343.     actionListener = AWTEventMulticaster.remove(actionListener, l);
  344.     }
  345.  
  346.     // REMIND: remove when filtering is done at lower level
  347.     boolean eventEnabled(AWTEvent e) {
  348.         if (e.id == ActionEvent.ACTION_PERFORMED) {
  349.             if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
  350.                 actionListener != null) {
  351.                 return true;
  352.             } 
  353.             return false;
  354.         }
  355.         return super.eventEnabled(e);
  356.     }          
  357.  
  358.     /**
  359.      * Processes events on this text field. If the event 
  360.      * is an instance of <code>ActionEvent</code>,
  361.      * it invokes the <code>processActionEvent</code> 
  362.      * method. Otherwise, it invokes <code>processEvent</code> 
  363.      * on the superclass.
  364.      * @param      e the event.
  365.      * @see        java.awt.event.ActionEvent
  366.      * @see        java.awt.TextField#processActionEvent
  367.      * @since      JDK1.1
  368.      */
  369.     protected void processEvent(AWTEvent e) {
  370.         if (e instanceof ActionEvent) {
  371.             processActionEvent((ActionEvent)e);     
  372.             return;
  373.         }
  374.     super.processEvent(e);
  375.     }
  376.  
  377.     /** 
  378.      * Processes action events occurring on this text field by
  379.      * dispatching them to any registered 
  380.      * <code>ActionListener</code> objects. 
  381.      * <p>
  382.      * This method is not called unless action events are 
  383.      * enabled for this component. Action events are enabled 
  384.      * when one of the following occurs:
  385.      * <p><ul>
  386.      * <li>An <code>ActionListener</code> object is registered 
  387.      * via <code>addActionListener</code>.
  388.      * <li>Action events are enabled via <code>enableEvents</code>.
  389.      * </ul>
  390.      * @param       e the action event.
  391.      * @see         java.awt.event.ActionListener
  392.      * @see         java.awt.TextField#addActionListener
  393.      * @see         java.awt.Component#enableEvents
  394.      * @since       JDK1.1
  395.      */  
  396.     protected void processActionEvent(ActionEvent e) {
  397.         if (actionListener != null) {
  398.             actionListener.actionPerformed(e);
  399.         }
  400.     }
  401.  
  402.     /**
  403.      * Returns the parameter string representing the state of this 
  404.      * text field. This string is useful for debugging. 
  405.      * @return      the parameter string of this text field. 
  406.      */
  407.     protected String paramString() {
  408.     String str = super.paramString();
  409.     if (echoChar != 0) {
  410.         str += ",echo=" + echoChar;
  411.     }
  412.     return str;
  413.     }
  414.  
  415.  
  416.     /* Serialization support. 
  417.      */
  418.  
  419.     private int textFieldSerializedDataVersion = 1;
  420.  
  421.  
  422.     private void writeObject(ObjectOutputStream s)
  423.       throws IOException 
  424.     {
  425.       s.defaultWriteObject();
  426.  
  427.       AWTEventMulticaster.save(s, actionListenerK, actionListener);
  428.       s.writeObject(null);
  429.     }
  430.  
  431.  
  432.     private void readObject(ObjectInputStream s)
  433.       throws ClassNotFoundException, IOException 
  434.     {
  435.       s.defaultReadObject();
  436.  
  437.       Object keyOrNull;
  438.       while(null != (keyOrNull = s.readObject())) {
  439.     String key = ((String)keyOrNull).intern();
  440.  
  441.     if (actionListenerK == key) 
  442.       addActionListener((ActionListener)(s.readObject()));
  443.  
  444.     else // skip value for unrecognized key
  445.       s.readObject();
  446.       }
  447.     }
  448.  
  449. }
  450.