// ShapeInputDialog2.java

// Dialog box which prompts the user for
// data for a ShapeOutline object.  The
// showDialog method returns a ShapeOutline
// object directly -- not a string
// representation.
//
// This version does not need to handle
// any exceptions.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

// Also uses class DimensionDisplayField,
// defined in DimensionDisplayField.java

public class ShapeInputDialog2 extends JDialog
                              implements ActionListener
{
   // Displayed regions of the dialog box:
   private JScrollBar widthScrollBar;
   private JScrollBar heightScrollBar;
   private JScrollBar spokesScrollBar;
   private JButton okButton;
   private JButton cancelButton;

   private static ShapeOutline shapeData = null;

   // Group of radio buttons to select shape:
   ButtonGroup shapeSelector;

   public ShapeInputDialog2(JFrame owner)
   {
      super(owner,
            /* modal= */ true);

      // Active components to display on window:
      JRadioButton rbCircle = new JRadioButton("Circle");
      rbCircle.getModel().setActionCommand("Circle");
      JRadioButton rbWheel = new JRadioButton("Wheel");
      rbWheel.getModel().setActionCommand("Wheel");
      JRadioButton rbRightTriangle = new JRadioButton("RightTriangle");
      rbRightTriangle.getModel().setActionCommand("RightTriangle");
      widthScrollBar = new JScrollBar(JScrollBar.HORIZONTAL,
                                       50, 1, 0, 100);
      heightScrollBar = new JScrollBar(JScrollBar.HORIZONTAL,
                                       50, 1, 0, 100);
      spokesScrollBar = new JScrollBar(JScrollBar.HORIZONTAL,
                                       10, 1, 0, 30);
      DimensionDisplayField widthField
                              = new DimensionDisplayField("width");
      DimensionDisplayField spokesField
                              = new DimensionDisplayField("spokes");
      DimensionDisplayField heightField
                              = new DimensionDisplayField("height");
      okButton = new JButton("  OK  ");
      okButton.setBorder(
                      BorderFactory.createBevelBorder(
                                             BevelBorder.RAISED));
      cancelButton = new JButton(" Cancel ");
      cancelButton.setBorder(
                      BorderFactory.createBevelBorder(
                                             BevelBorder.RAISED));


      // Arrange components on window:
      Container contentPane = getContentPane();

      JPanel southPanel = new JPanel();
      contentPane.add(southPanel, BorderLayout.SOUTH);
      southPanel.setLayout(new GridLayout(1, 4));
      southPanel.add(new JPanel());
      southPanel.add(okButton);
      southPanel.add(cancelButton);
      southPanel.add(new JPanel());

      JPanel westPanel = new JPanel();
      contentPane.add(westPanel, BorderLayout.WEST);
      westPanel.setBorder(
                      BorderFactory.createLineBorder(
                                             Color.black));
      westPanel.setLayout(new GridLayout(4, 1));
      westPanel.add(new Label("  Select shape below:  "));
      westPanel.add(rbCircle);
      westPanel.add(rbWheel);
      westPanel.add(rbRightTriangle);

      JPanel centerPanel = new JPanel();
      contentPane.add(centerPanel, BorderLayout.CENTER);
      centerPanel.setBorder(
                      BorderFactory.createLineBorder(
                                             Color.black));
      centerPanel.setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      centerPanel.add(labelPanel, BorderLayout.WEST);
      labelPanel.setLayout(new GridLayout(3, 1));
      labelPanel.add(new Label("  Adjust width:"));
      labelPanel.add(new Label("  spokes, if needed:"));
      labelPanel.add(new Label("  height, if needed:"));

      JPanel textEntryPanel = new JPanel();
      centerPanel.add(textEntryPanel, BorderLayout.CENTER);
      textEntryPanel.setLayout(new GridLayout(3, 1));
      textEntryPanel.add(widthScrollBar);
      textEntryPanel.add(spokesScrollBar);
      textEntryPanel.add(heightScrollBar);

      JPanel dimensionDisplayPanel = new JPanel();
      centerPanel.add(dimensionDisplayPanel, BorderLayout.SOUTH);
      dimensionDisplayPanel.setLayout(new GridLayout(1, 3));
      dimensionDisplayPanel.add(widthField);
      dimensionDisplayPanel.add(spokesField);
      dimensionDisplayPanel.add(heightField);

      // Group radio buttons so that only one can be checked at a time:
      shapeSelector = new ButtonGroup();
      shapeSelector.add(rbCircle);
      shapeSelector.add(rbWheel);
      shapeSelector.add(rbRightTriangle);

      // Enable this dialog box to respond to "OK" and "Cancel" buttons:
      okButton.addActionListener(this);
      cancelButton.addActionListener(this);

      // Enable dimension display fields to respond to
      // scroll bars:
      widthScrollBar.addAdjustmentListener(widthField);
      spokesScrollBar.addAdjustmentListener(spokesField);
      heightScrollBar.addAdjustmentListener(heightField);

      // Routine necessities with JDialog windows:
      setSize(500, 150);
      setLocation(200, 470);
      setTitle("Shape data input");
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      setVisible(true);
   } // constructor

   public void actionPerformed(ActionEvent e)
   {
      if ( e.getSource() == okButton )
      {
         shapeData = getData();
         if ( shapeData != null )
            dispose();
      }
      else if ( e.getSource() == cancelButton )
         dispose();
      else
         throw new IllegalStateException(
                            "This dialog box listens only to "
                            + "its own OK and Cancel buttons.");
   }  // method actionPerformed

   public static ShapeOutline showDialog(JFrame owner)
   {
      new ShapeInputDialog2(owner);

      // Return current value of static variable shapeData,
      // resetting its value to null for the next time:
      ShapeOutline toReturn = shapeData;
      shapeData = null;
      return toReturn;
   }  // method showDialog

   private ShapeOutline getData()
   {
      if ( shapeSelector.getSelection() == null )
      {
         JOptionPane.showMessageDialog(this,
                                       "You must select a shape.",
                                       "Shape not selected!",
                                       JOptionPane.ERROR_MESSAGE);
         return null;
      }  // if

      String className;
      className = shapeSelector.getSelection().getActionCommand();

      if ( className.equals("Circle") )
         return new Circle(widthScrollBar.getValue());
      if ( className.equals("Wheel") )
         return new Wheel(widthScrollBar.getValue(),
                          spokesScrollBar.getValue());
      if ( className.equals("RightTriangle") )
         return new RightTriangle(widthScrollBar.getValue(),
                                  heightScrollBar.getValue());

      // If we haven't exhausted the possibilities for the
      // class name, we made a programming error:
      throw new IllegalStateException("Unaccounted for: "
                                         + className);
   }  // method getData

   // For test purposes only:
   public static void main(String[] args)
   {
      System.out.println(showDialog(null));
      System.exit(0);
   }  // method main
} // class ShapeInputDialog2