// ShapeInputDialog.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 needs to handle some
// exceptions.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ShapeInputDialog extends JDialog
implements ActionListener
{
// Displayed regions of the dialog box:
private JTextField widthField;
private JTextField heightField;
private JTextField spokesField;
private JButton okButton;
private JButton cancelButton;
private static ShapeOutline shapeData = null;
// Group of radio buttons to select shape:
ButtonGroup shapeSelector;
public ShapeInputDialog(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");
widthField = new JTextField();
heightField = new JTextField();
spokesField = new JTextField();
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(" Enter 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(widthField);
textEntryPanel.add(spokesField);
textEntryPanel.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);
// 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 ShapeInputDialog(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 ( widthField.getText().equals("") )
{
JOptionPane.showMessageDialog(this,
"You must enter a width.",
"Width not entered!",
JOptionPane.ERROR_MESSAGE);
return null;
} // if
if ( className.equals("Wheel")
&& spokesField.getText().equals("") )
{
JOptionPane.showMessageDialog(
this,
"You must enter a number of spokes.",
"Spokes not entered!",
JOptionPane.ERROR_MESSAGE);
return null;
} // if
if ( className.equals("RightTriangle")
&& heightField.getText().equals("") )
{
JOptionPane.showMessageDialog(this,
"You must enter a height.",
"Height not entered!",
JOptionPane.ERROR_MESSAGE);
return null;
} // if
try {
if ( className.equals("Circle") )
return new Circle(Integer.parseInt(widthField.getText()));
if ( className.equals("Wheel") )
return new Wheel(Integer.parseInt(widthField.getText()),
Integer.parseInt(spokesField.getText()));
if ( className.equals("RightTriangle") )
return new RightTriangle(
Integer.parseInt(widthField.getText()),
Integer.parseInt(heightField.getText()));
// If we haven't exhausted the possibilities for the
// class name, we made a programming error:
throw new IllegalStateException("Unaccounted for: "
+ className);
} // try
catch ( NumberFormatException nfe ) {
JOptionPane.showMessageDialog(
this,
"Not an integer: " + nfe.getMessage(),
"Non-integer size entry!",
JOptionPane.ERROR_MESSAGE);
} // catch
catch ( NegativeSizeException nze ) {
JOptionPane.showMessageDialog(
this,
nze.getMessage(),
"Negative size entry!",
JOptionPane.ERROR_MESSAGE);
} // catch
// This point will be reached only if we
// haven't yet returned a valid ShapeOutline:
return null;
} // method getData
// For test purposes only:
public static void main(String[] args)
{
System.out.println(showDialog(null));
System.exit(0);
} // method main
} // class ShapeInputDialog