import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** DemoDisabledButton - program to demonstrate disabling buttons.<p>
*
* Like <code>DemoIconButton</code>, this program uses a button to increment a count
* and another button to decrement a count.  The count is displayed in a text field.
*
* However, this program adds a new twist.  The count is constrained to the range
* 0 to 9, inclusive.  When the count is decremented to 0, the decrement button is
* "disabled" as follows:<p>
*
* <pre>
*     decrementButton.setEnabled(false);
* </pre>
*
* Not only is the button disabled, the image changes to a faded look.  Since the button
* is embellished with an image, rather than text, a faded version of the original
* icon must be supplied.  This is setup where the button is configured 
* in the constructor:<p>
*
* <pre>
*     ImageIcon decIconDisabled = new ImageIcon("decrementIcon-disabled.gif");
*     decrementButton.setDisabledIcon(decIconDisabled);
* </pre>
*
* A similar process is used for the increment button.<p>
*
* When a focused button becomes disabled,
* focus is automatically transferred to the next focusable component.  
* It is a minor nuisance that the count text field acquires focus when the increment button is
* disabled.  This occurs because the count text field is the next focusable component in 
* sequence following the increment button.  To prevent the count text field
* from gaining focus, the following statement is included in the constructor:<p>
*
* <pre>
*	   countField.setFocusable(false);
* </pre>
*
* With this, focus is transferred to the exit button when the increment button is
* disabled.<p>
*
* Note: To "click on" a focused component using the keyboard, use the spacebar.
* <p>
*
* Screen snap upon launch...<br>
* <center><img src="DemoDisabledButton-1.gif"></center><p>
*
* Screen snap after reaching max count...<br>
* <center><img src="DemoDisabledButton-2.gif"></center><p>
*
* @see <a href="DemoDisabledButton.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoDisabledButton
{
   public static void main(String[] args)
   {
      DemoDisabledButtonFrame frame = new DemoDisabledButtonFrame();
      frame.setTitle("DemoDisabledButton");
      frame.pack();
      frame.show();
   }
}

class DemoDisabledButtonFrame extends JFrame implements ActionListener 
{
   final int INITIAL_COUNT = 5;
   final int MIN_COUNT = 0;
   final int MAX_COUNT = 9;

   private int count;
   private JTextField countField;
   private JButton decrementButton;
   private JButton incrementButton;
   private JButton exitButton;

   // constructor

   public DemoDisabledButtonFrame()
   {
      count = INITIAL_COUNT;

      // -------------------------------
      // create and configure components
      // -------------------------------

      countField = new JTextField("" + count);
      countField.setFont(new Font("Serif", Font.BOLD, 28));
      countField.setForeground(Color.blue);
      countField.setBackground(Color.pink);
      countField.setHorizontalAlignment(SwingConstants.CENTER);
		countField.setEditable(false);

      ImageIcon decIcon = new ImageIcon("decrementIcon.gif");
      ImageIcon incIcon = new ImageIcon("incrementIcon.gif");

      decrementButton = new JButton(decIcon);
      incrementButton = new JButton(incIcon);

      ImageIcon decIconDisabled = new ImageIcon("decrementIcon-disabled.gif");
      decrementButton.setDisabledIcon(decIconDisabled);

      ImageIcon incIconDisabled = new ImageIcon("incrementIcon-disabled.gif");
      incrementButton.setDisabledIcon(incIconDisabled);

      exitButton = new JButton("Exit");

		countField.setFocusable(false);

      // -------------
      // add listeners
      // -------------

      decrementButton.addActionListener(this);
      incrementButton.addActionListener(this);
      exitButton.addActionListener(this);
      this.addWindowListener(new WindowCloser());

      // ------------------
      // arrange components
      // ------------------

      // put components in panels

      JPanel topPanel = new JPanel();
      topPanel.setLayout(new GridLayout(1, 2));
      topPanel.add(decrementButton);
      topPanel.add(incrementButton);

      JPanel bottomPanel = new JPanel();
      bottomPanel.setBorder(BorderFactory.createEmptyBorder(50, 30, 50, 30));
      bottomPanel.setLayout(new GridLayout(3, 1));
      bottomPanel.add(topPanel);
      bottomPanel.add(countField);
      bottomPanel.add(exitButton);

      // create a container for this (extended) JFrame's content pane

      Container contentPane = this.getContentPane();

      //  add panels to the container

      contentPane.add(bottomPanel, "Center");
   }

   // -------------------------------
   // implement ActionListener method
   // -------------------------------

   public void actionPerformed(ActionEvent ae)
   {
      if (ae.getSource() == decrementButton)
      {
         count--;
         countField.setText("" + count);
         switch (count)
         {
            case MIN_COUNT:
               decrementButton.setEnabled(false);
               break;
            case (MAX_COUNT - 1):
               incrementButton.setEnabled(true);
               break;
         }
      }
      else if (ae.getSource() == incrementButton)
      {
         count++;
         countField.setText("" + count);
         switch (count)
         {
            case (MIN_COUNT + 1):
               decrementButton.setEnabled(true);
               break;
            case MAX_COUNT:
               incrementButton.setEnabled(false);
               break;
         }
      }

      else if (ae.getSource() == exitButton)
         System.exit(0);   
   }

   // Note: WindowAdapter implements WindowListener

   private class WindowCloser extends WindowAdapter
   {
      public void windowClosing(WindowEvent event)
      {
         System.exit(0);
      }
   }
}

