// Import statements for the JComponents -- the visual widgets that will be
// employed
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComponent;

// Import statement for the layout manager used in this program
import javax.swing.Box;

// Import statements to support actions and responses
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Demo of event processing in Swing.
 *
 * @author Terran Lane
 * @version 1.0
 */
public class buttonDemo {

  public static void main(String[] args) {
    buttonDemo bd=new buttonDemo();
    bd._init();
  }

  /* ******************** end of public interface ******************** */

  private int _stateVar;
  private JLabel _display=null;

  private buttonDemo() {
    _stateVar=0;
    _buttonProcessor=new _ButtonProcClass();
  }

  private void _init() {
    JFrame jf=new JFrame("CS351 Button Demo App");
    jf.getContentPane().add(_buildDemoGUI());
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.pack();
    jf.setVisible(true);
  }

  private JComponent _buildDemoGUI() {
    Box vb=Box.createVerticalBox();
    vb.add(Box.createHorizontalStrut(300));
    vb.add(_display=new JLabel("i=" + _stateVar));
    Box hb=Box.createHorizontalBox();
    hb.add(_buildIncButton());
    hb.add(_buildDecButton());
    hb.add(_buildQuitButton());
    vb.add(hb);
    return vb;
  }

  // String constants identifing button functionality
  private static final String ACT_INC="INC";
  private static final String ACT_DEC="DEC";
  private static final String ACT_QUIT="QUIT";

  // The listener that will process each of the button clicks
  private ActionListener _buttonProcessor;

  private JButton _buildIncButton() {
    JButton b=new JButton("inc");
    // When the button is clicked, the action generated will have this string
    // as its "action command"; the receiving listener can query the event
    // with event.getActionCommand() and get back this string to identify
    // where the event came from.
    b.setActionCommand(ACT_INC);
    // When the button is clicked, an action event is generated and sent to
    // each listener registered on its listener list (via the listener's
    // actionPerformed() method).  Add a listener for this button that
    // understands its action and handles it appropriately.
    b.addActionListener(_buttonProcessor);
    return b;
  }

  private JButton _buildDecButton() {
    JButton b=new JButton("dec");
    b.setActionCommand(ACT_DEC);
    b.addActionListener(_buttonProcessor);
    return b;
  }

  private JButton _buildQuitButton() {
    JButton b=new JButton("quit");
    b.setActionCommand(ACT_QUIT);
    b.addActionListener(_buttonProcessor);
    return b;
  }

  // This class implements the "ActionListener" protocol for this class.  It
  // contains only a single method -- the actionPerformed() method -- that is
  // invoked whenever the Component for which this listener is assigned is
  // triggered.  Note that this is a _class_, so it must be instantiated (via
  // new) before it can be added as the listener for a component.
  private class _ButtonProcClass implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals(ACT_INC)) {
	++_stateVar;
	assert _display!=null;
	_display.setText("i=" + _stateVar);
	return;
      }
      if (e.getActionCommand().equals(ACT_DEC)) {
	--_stateVar;
	assert _display!=null;
	_display.setText("i=" + _stateVar);
	return;
      }
      if (e.getActionCommand().equals(ACT_QUIT)) {
	System.exit(0);
      }
      assert false;
    }
  }
}
