import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** DemoButtons - program to demonstrate the thee basic types of
* buttons: push buttons, checkboxes, and radio buttons.<p>
*
* Each button is configured to support keyboard accessibility using
* the <code>setMnemonic</code> method.  The argument to the
* <code>setMnemonic</code> method is a <code>char</code> -- the character
* used to 'press' the button using the keyboard, typically by
* pressing ALT-char.  The character should be one of the characters in the
* button's label (e.g., 'B' for the Bold button).  It will
* appear underlined in the button.<p>
*
* To ensure platform independence and compatibility with future releases
* of Java, express the character argument to <code>setMnemonic</code>
* as <code>VK_?</code>, where '?' is the desired character.  See the
* <code>KeyEvent</code> API specfication for a complete list of VK (virtual
* key) assignments.<p>
*
* Screen snap:<br>
* <center><img src="DemoButtons.gif"></center><p>
*
* @see <a href="DemoButtons.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoButtons
{
   public static void main(String[] args)
   {
      // use look and feel for my system (Win32)
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoButtonsFrame frame = new DemoButtonsFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoButtons");
      frame.pack();
      frame.show();
   }
}

class DemoButtonsFrame extends JFrame
implements ActionListener, ItemListener
{
   private JTextField message;
   private JButton exitButton;
   private JButton restoreButton;
   private JCheckBox italicCheckBox;
   private JCheckBox boldCheckBox;
   private JRadioButton smallButton;
   private JRadioButton mediumButton;
   private JRadioButton largeButton;
   private JToggleButton showHideButton;

   final Color FOREGROUND = Color.blue;
   final Color BACKGROUND = Color.pink;
   final int SMALL = 12;
   final int MEDIUM = 24;
   final int LARGE = 48;
   final Font DEFAULT_FONT = new Font("Serif", Font.PLAIN, MEDIUM);

   private int fontSize;
   private int fontStyle;

   public DemoButtonsFrame()
   {
      fontStyle = Font.PLAIN;
      fontSize = MEDIUM;

      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      message = new JTextField("Hello Java World");
      message.setFont(DEFAULT_FONT);
      message.setEditable(false);
      message.setBackground(BACKGROUND);
      message.setForeground(FOREGROUND);
      message.setHorizontalAlignment(SwingConstants.CENTER);
      message.setPreferredSize(new Dimension(400, 150));

      restoreButton = new JButton("Restore");
      exitButton = new JButton("Exit");

      italicCheckBox = new JCheckBox("Italic");
      boldCheckBox = new JCheckBox("Bold");

      smallButton = new JRadioButton("Small");
      mediumButton = new JRadioButton("Medium");
      largeButton = new JRadioButton("Large");
      mediumButton.setSelected(true);

      showHideButton = new JToggleButton("Hide");

      // set up mnemonics for keyboard accessibility

      restoreButton.setMnemonic(KeyEvent.VK_R);
      exitButton.setMnemonic(KeyEvent.VK_E);
      italicCheckBox.setMnemonic(KeyEvent.VK_I);
      boldCheckBox.setMnemonic(KeyEvent.VK_B);
      smallButton.setMnemonic(KeyEvent.VK_S);
      mediumButton.setMnemonic(KeyEvent.VK_M);
      largeButton.setMnemonic(KeyEvent.VK_L);
      showHideButton.setMnemonic(KeyEvent.VK_H);

      // add radio buttons to button group

      ButtonGroup sizeGroup = new ButtonGroup();
      sizeGroup.add(smallButton);
      sizeGroup.add(mediumButton);
      sizeGroup.add(largeButton);

      // -------------
      // add listeners
      // -------------

      restoreButton.addActionListener(this);
      exitButton.addActionListener(this);
      italicCheckBox.addItemListener(this);
      boldCheckBox.addItemListener(this);
      smallButton.addActionListener(this);
      mediumButton.addActionListener(this);
      largeButton.addActionListener(this);
      showHideButton.addActionListener(this);

      // -----------------
      // layout components
      // -----------------

      // add components to panels
      
      JPanel commandPanel = new JPanel();
      commandPanel.setLayout(new GridLayout(2, 1));
      commandPanel.add(restoreButton);
      commandPanel.add(exitButton);
      commandPanel.setBorder(new TitledBorder(new EtchedBorder(), "Commands"));

      JPanel sizeGroupPanel = new JPanel();
      sizeGroupPanel.setLayout(new GridLayout(3, 1));
      sizeGroupPanel.add(smallButton);
      sizeGroupPanel.add(mediumButton);
      sizeGroupPanel.add(largeButton);
      sizeGroupPanel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));

      JPanel styleGroupPanel = new JPanel();
      styleGroupPanel.setLayout(new GridLayout(2, 1));
      styleGroupPanel.add(italicCheckBox);
      styleGroupPanel.add(boldCheckBox);
      styleGroupPanel.setBorder(new TitledBorder(new EtchedBorder(), "Style"));

      JPanel showHidePanel = new JPanel();
      showHidePanel.setLayout(new GridLayout(2, 1));
      showHidePanel.add(showHideButton);
      showHidePanel.setBorder(new TitledBorder(new EtchedBorder(), "View"));

      // arrange panels

      JPanel southPanel = new JPanel();
      southPanel.setLayout(new GridLayout(1, 4));
      southPanel.add(commandPanel);
      southPanel.add(sizeGroupPanel);
      southPanel.add(styleGroupPanel);
      southPanel.add(showHidePanel);

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());
      contentPane.add(message, "Center");
      contentPane.add(southPanel, "South");
      contentPane.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

      // make panel this JFrame's content pane

      this.setContentPane(contentPane);
   }

   // -------------------------------
   // implement ActionListener method
   // -------------------------------

   public void actionPerformed(ActionEvent ae)
   {
      // for in-class demo...
      //System.out.println(ae.paramString());

      Object source = ae.getSource();

      // 'Command' - check if a command button was pressed
      if (source == restoreButton)
      {
         fontSize = MEDIUM;
         fontStyle = Font.PLAIN;
         mediumButton.setSelected(true);
         italicCheckBox.setSelected(false);
         boldCheckBox.setSelected(false);
         showHideButton.setText("Hide");
         showHideButton.setSelected(false);
         message.setForeground(FOREGROUND);
      }
      else if (source == exitButton)
         System.exit(0);

      // 'Size' - check if a size radio button was pressed
      else if (source == smallButton)
         fontSize = SMALL;
      else if (source == mediumButton)
         fontSize = MEDIUM;
      else if (source == largeButton)
         fontSize = LARGE;

      // 'Show/Hide' - check if show/hide button was pressed
      else if (source == showHideButton)
      {
         if (ae.getActionCommand().equals("Show"))
         {
            message.setForeground(FOREGROUND);
            showHideButton.setText("Hide");
         }
         else if (ae.getActionCommand().equals("Hide"))
         {
            message.setForeground(BACKGROUND);
            showHideButton.setText("Show");
         }
      }

      // update message size and style
      message.setFont(new Font("Serif", fontStyle, fontSize));
   }

   // -----------------------------
   // implement ItemListener method
   // -----------------------------

   public void itemStateChanged(ItemEvent ie)
   {
      Object source = ie.getSource();

      // 'Style' - check if the font style was change via a checkbox
      if (source == italicCheckBox)
         if (italicCheckBox.isSelected())
            fontStyle = fontStyle | Font.ITALIC;  // turn italic on
         else
            fontStyle = fontStyle & ~Font.ITALIC; // turn italic off
      else if (source == boldCheckBox)
         if (boldCheckBox.isSelected())
            fontStyle = fontStyle | Font.BOLD;    // turn bold on
         else
            fontStyle = fontStyle & ~Font.BOLD;   // turn bold off

      // update message size and style
      message.setFont(new Font("Serif", fontStyle, fontSize));
  }
}

