Button.java 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libjava.

This software is copyrighted work licensed under the terms of the
Libjava License.  Please consult the file "LIBJAVA_LICENSE" for
details.  */

package java.awt;
import java.awt.peer.ButtonPeer;
import java.awt.peer.ComponentPeer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
14
import java.util.EventListener;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date July 30, 2000
 */

public class Button extends Component
{
  public Button ()
  {
    this (null);
  }

  public Button (String label)
  {
    this.label = label;
  }

  public void addActionListener (ActionListener l)
  {
35
    actionListener = AWTEventMulticaster.add (actionListener, l);
36 37 38 39
  }

  public void addNotify ()
  {
40
    if (peer == null)
41
      peer = getToolkit ().createButton (this);
42
    super.addNotify();
43 44 45 46
  }

  public String getActionCommand ()
  {
47
    return actionCommand;
48 49 50 51 52 53 54 55 56 57 58 59
  }

  public String getLabel ()
  {
    return label;
  }

  protected String paramString ()
  {
    return "Button[" + label + "]";
  }

60 61 62 63 64 65 66 67 68 69 70
  void dispatchEventImpl(AWTEvent e)
  {
      super.dispatchEventImpl(e);
      
      if (e.id <= ActionEvent.ACTION_LAST 
	  && e.id >= ActionEvent.ACTION_FIRST
	  && (actionListener != null 
	      || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
	  processEvent(e);
  }

71 72
  protected void processActionEvent (ActionEvent e)
  {
73 74
    if (actionListener != null)
      actionListener.actionPerformed (e);
75 76 77 78 79 80 81 82 83 84 85 86
  }

  protected void processEvent (AWTEvent e)
  {
    if (e instanceof ActionEvent)
      processActionEvent ((ActionEvent) e);
    else
      super.processEvent (e);
  }

  public void removeActionListener (ActionListener l)
  {
87 88 89 90 91 92 93 94
    actionListener = AWTEventMulticaster.remove (actionListener, l);
  }

  public EventListener[] getListeners(Class listenerType)
  {
    if (listenerType == ActionListener.class)
      return getListenersImpl(listenerType, actionListener);
    return super.getListeners(listenerType);
95 96 97 98
  }

  public void setActionCommand (String command)
  {
99
    this.actionCommand = (command == null) ? label : command;
100 101 102 103 104 105 106 107 108 109 110 111
  }

  public void setLabel (String label)
  {
    this.label = label;
    if (peer != null)
      {
	ButtonPeer bp = (ButtonPeer) peer;
	bp.setLabel (label);
      }
  }

112 113 114 115
  String label;
  String actionCommand;

  transient ActionListener actionListener;
116
}