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

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