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

  1. /*
  2.  * @(#)Button.java    1.40 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.  
  15. package java.awt;
  16.  
  17. import java.awt.peer.ButtonPeer;
  18. import java.awt.event.*;
  19. import java.io.ObjectOutputStream;
  20. import java.io.ObjectInputStream;
  21. import java.io.IOException;
  22.  
  23.  
  24. /**
  25.  * This class creates a labeled button. The application can cause 
  26.  * some action to happen when the button is pushed. This image 
  27.  * depicts three views of a "<code>Quit</code>" button as it appears
  28.  * under the Solaris operating system: 
  29.  * <p> 
  30.  * <img src="images-awt/Button-1.gif"
  31.  * ALIGN=center HSPACE=10 VSPACE=7>  
  32.  * <p>
  33.  * The first view shows the button as it appears normally. 
  34.  * The second view shows the button 
  35.  * when it has input focus. Its outline is darkened to let the 
  36.  * user know that it is an active object. The third view shows the 
  37.  * button when the user clicks the mouse over the button, and thus 
  38.  * requests that an action be performed.
  39.  * <p>
  40.  * The gesture of clicking on a button with the mouse
  41.  * is associated with one instance of <code>ActionEvent</code>, 
  42.  * which is sent out when the mouse is both pressed and released 
  43.  * over the button. If an application is interested in knowing
  44.  * when the button has been pressed but not released, as a separate 
  45.  * gesture, it can specialize <code>processMouseEvent</code>, 
  46.  * or it can register itself as a listener for mouse events by
  47.  * calling <code>addMouseListener</code>. Both of these methods are
  48.  * defined by <code>Component</code>, the abstract superclass of
  49.  * all components.
  50.  * <p>
  51.  * When a button is pressed and released, AWT sends an instance  
  52.  * of <code>ActionEvent</code> to the button, by calling 
  53.  * <code>processEvent</code> on the button. The button's 
  54.  * <code>processEvent</code> method receives all events
  55.  * for the button; it passes an action event along by
  56.  * calling its own <code>processActionEvent</code> method.
  57.  * The latter method passes the action event on to any action
  58.  * listeners that have registered an interest in action
  59.  * events generated by this button.
  60.  * <p>
  61.  * If an application wants to perform some action based on  
  62.  * a button being pressed and released, it should implement 
  63.  * <code>ActionListener</code> and register the new listener 
  64.  * to receive events from this button, by calling the button's
  65.  * <code>addActionListener</code> method. The application can
  66.  * make use of the button's action command as a messaging protocol.
  67.  *
  68.  * @version     1.40 03/18/98
  69.  * @author     Sami Shaio
  70.  * @see         java.awt.event.ActionEvent
  71.  * @see         java.awt.event.ActionListener
  72.  * @see         java.awt.Component#processMouseEvent
  73.  * @see         java.awt.Component#addMouseListener
  74.  * @since       JDK1.0
  75.  */
  76. public class Button extends Component {
  77.  
  78.     String label;
  79.     String actionCommand;
  80.  
  81.     transient ActionListener actionListener;
  82.     
  83.     private static final String base = "button";
  84.     private static int nameCounter = 0;
  85.     
  86.     /*
  87.      * JDK 1.1 serialVersionUID 
  88.      */
  89.     private static final long serialVersionUID = -8774683716313001058L;
  90.  
  91.     /**
  92.      * Constructs a Button with no label.
  93.      */
  94.     public Button() {
  95.     this("");
  96.     }
  97.  
  98.     /**
  99.      * Constructs a Button with the specified label.
  100.      * @param label A string label for the button.
  101.      */
  102.     public Button(String label) {
  103.     this.name = base + nameCounter++;
  104.     this.label = label;
  105.     }
  106.     
  107.     /**
  108.      * Creates the peer of the button.  The button's peer allows the
  109.      * application to change the look of the button without changing 
  110.      * its functionality.
  111.      * @see     java.awt.Toolkit#createButton(java.awt.Button)
  112.      * @see     java.awt.Component#getToolkit()
  113.      */
  114.     public void addNotify() {
  115.     peer = getToolkit().createButton(this);
  116.     super.addNotify();
  117.     }
  118.  
  119.     /**
  120.      * Gets the label of this button.
  121.      * @return    the button's label, or <code>null</code> 
  122.      *                if the button has no label.
  123.      * @see       java.awt.Button#setLabel
  124.      */
  125.     public String getLabel() {
  126.     return label;
  127.     }
  128.  
  129.     /**
  130.      * Sets the button's label to be the specified string.
  131.      * @param     label   the new label, or <code>null</code> 
  132.      *                if the button has no label.
  133.      * @see       java.awt.Button#getLabel
  134.      */
  135.     public synchronized void setLabel(String label) {
  136.     this.label = label;
  137.     ButtonPeer peer = (ButtonPeer)this.peer;
  138.     if (peer != null) {
  139.         peer.setLabel(label);
  140.     }
  141.     }
  142.  
  143.     /**
  144.      * Sets the command name for the action event fired 
  145.      * by this button. By default this action command is 
  146.      * set to match the label of the button.
  147.      * @param     command  A string used to set the button's 
  148.      *                  action command.
  149.      * @see       java.awt.event.ActionEvent
  150.      * @since     JDK1.1
  151.      */
  152.     public void setActionCommand(String command) {
  153.         actionCommand = command;
  154.     }
  155.  
  156.     /**
  157.      * Returns the command name of the action event fired by this button.
  158.      */
  159.     public String getActionCommand() {
  160.         return (actionCommand == null? label : actionCommand);
  161.     }
  162.  
  163.     /**
  164.      * Adds the specified action listener to receive action events from
  165.      * this button. Action events occur when a user presses or releases
  166.      * the mouse over this button.
  167.      * @param         l the action listener.
  168.      * @see           java.awt.event.ActionListener
  169.      * @see           java.awt.Button#removeActionListener
  170.      * @since         JDK1.1
  171.      */ 
  172.     public synchronized void addActionListener(ActionListener l) {
  173.     actionListener = AWTEventMulticaster.add(actionListener, l);
  174.         newEventsOnly = true;    
  175.     }
  176.  
  177.     /**
  178.      * Removes the specified action listener so that it no longer 
  179.      * receives action events from this button. Action events occur  
  180.      * when a user presses or releases the mouse over this button.
  181.      * @param         l     the action listener.
  182.      * @see           java.awt.event.ActionListener
  183.      * @see           java.awt.Button#addActionListener
  184.      * @since         JDK1.1
  185.      */ 
  186.     public synchronized void removeActionListener(ActionListener l) {
  187.     actionListener = AWTEventMulticaster.remove(actionListener, l);
  188.     }
  189.  
  190.     // REMIND: remove when filtering is done at lower level
  191.     boolean eventEnabled(AWTEvent e) {
  192.         if (e.id == ActionEvent.ACTION_PERFORMED) {
  193.             if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
  194.                 actionListener != null) {
  195.                 return true;
  196.             }
  197.             return false;
  198.         }
  199.         return super.eventEnabled(e);
  200.     }          
  201.  
  202.     /**
  203.      * Processes events on this button. If an event is 
  204.      * an instance of <code>ActionEvent</code>, this method invokes  
  205.      * the <code>processActionEvent</code> method. Otherwise,
  206.      * it invokes <code>processEvent</code> on the superclass.
  207.      * @param        e the event.
  208.      * @see          java.awt.event.ActionEvent
  209.      * @see          java.awt.Button#processActionEvent
  210.      * @since        JDK1.1
  211.      */
  212.     protected void processEvent(AWTEvent e) {
  213.         if (e instanceof ActionEvent) {
  214.             processActionEvent((ActionEvent)e);     
  215.             return;
  216.         }
  217.     super.processEvent(e);
  218.     }
  219.  
  220.     /** 
  221.      * Processes action events occurring on this button 
  222.      * by dispatching them to any registered 
  223.      * <code>ActionListener</code> objects.
  224.      * <p>
  225.      * This method is not called unless action events are 
  226.      * enabled for this button. Action events are enabled 
  227.      * when one of the following occurs:
  228.      * <p><ul>
  229.      * <li>An <code>ActionListener</code> object is registered 
  230.      * via <code>addActionListener</code>.
  231.      * <li>Action events are enabled via <code>enableEvents</code>.
  232.      * </ul>
  233.      * @param       e the action event.
  234.      * @see         java.awt.event.ActionListener
  235.      * @see         java.awt.Button#addActionListener
  236.      * @see         java.awt.Component#enableEvents
  237.      * @since       JDK1.1
  238.      */  
  239.     protected void processActionEvent(ActionEvent e) {
  240.         if (actionListener != null) {
  241.             actionListener.actionPerformed(e);
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * Returns the parameter string representing the state of this 
  247.      * button. This string is useful for debugging. 
  248.      * @return     the parameter string of this button.
  249.      */
  250.     protected String paramString() {
  251.     return super.paramString() + ",label=" + label;
  252.     }
  253.  
  254.  
  255.     /* Serialization support. 
  256.      */
  257.  
  258.     private int buttonSerializedDataVersion = 1;
  259.  
  260.  
  261.     private void writeObject(ObjectOutputStream s)
  262.       throws IOException 
  263.     {
  264.       s.defaultWriteObject();
  265.  
  266.       AWTEventMulticaster.save(s, actionListenerK, actionListener);
  267.       s.writeObject(null);
  268.     }
  269.  
  270.  
  271.     private void readObject(ObjectInputStream s)
  272.       throws ClassNotFoundException, IOException 
  273.     {
  274.       s.defaultReadObject();
  275.  
  276.       Object keyOrNull;
  277.       while(null != (keyOrNull = s.readObject())) {
  278.     String key = ((String)keyOrNull).intern();
  279.  
  280.     if (actionListenerK == key) 
  281.       addActionListener((ActionListener)(s.readObject()));
  282.  
  283.     else // skip value for unrecognized key
  284.       s.readObject();
  285.       }
  286.     }
  287.   
  288. }
  289.