package indyjug;

import java.awt.*;
import java.awt.event.*;

public class ShapeGUI extends Frame {
  private Label radiusLbl = new Label("Radius:");
  private TextField radiusTxt = new TextField();
  private Label perimeterLbl = new Label("Perimeter:");
  private Label perimeterValueLbl = new Label();
  private Label areaLbl = new Label("Area:");
  private Label areaValueLbl = new Label();
  private Button computePerimeterAreaBtn = new Button("Compute Perimeter & Area");

  Dialog dialog = new Dialog(this, "Error", true);
  Label messageLbl = new Label("Please enter a numeric value");
  Button okBtn = new Button("OK");

  public ShapeGUI() {
    setTitle("Shape GUI");
    createLayout();
    createListeners();    
    setSize(350, 150);
  }

  private class PerimeterAreaActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      try {
        double radius = Double.parseDouble(radiusTxt.getText());
        Circle circle = new Circle(radius);
        perimeterValueLbl.setText("" + circle.computePerimeter());
        areaValueLbl.setText("" + circle.computeArea());
      }
      catch (NumberFormatException ex) {
        showError();
      }
    }
  }

  private void createLayout() {
    Insets insets = new Insets(0, 0, 0, 0);
    setLayout(new GridBagLayout());
    add(radiusLbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                                          GridBagConstraints.NONE, insets, 0, 0));
    add(radiusTxt, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
                                          GridBagConstraints.HORIZONTAL, insets, 0, 0));
    add(perimeterLbl, new GridBagConstraints(0, 1, 1, 1, 0.5, 0.0, GridBagConstraints.WEST,
                                          GridBagConstraints.NONE, insets, 0, 0));
    add(perimeterValueLbl, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.0, GridBagConstraints.WEST,
                                          GridBagConstraints.HORIZONTAL, insets, 0, 0));
    add(areaLbl, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                                          GridBagConstraints.NONE, insets, 0, 0));
    add(areaValueLbl, new GridBagConstraints(1, 2, 1, 1, 0.5, 0.0, GridBagConstraints.WEST,
                                          GridBagConstraints.HORIZONTAL, insets, 0, 0));
    add(computePerimeterAreaBtn, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                          GridBagConstraints.NONE, insets, 0, 0));
    dialog.setLayout(new BorderLayout());
    dialog.add(messageLbl, BorderLayout.CENTER);
    Panel btnPnl = new Panel();
    btnPnl.add(okBtn);
    dialog.add(btnPnl, BorderLayout.SOUTH);
    dialog.pack();
    okBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        radiusTxt.setText("");
        perimeterValueLbl.setText("");
        areaValueLbl.setText("");
        dialog.setVisible(false);
        radiusTxt.requestFocus();
      }
    });
  }

  private void createListeners() {
    computePerimeterAreaBtn.addActionListener(new PerimeterAreaActionListener());
  }

  private void showError() {
    dialog.setVisible(true);
  }

  public static void main(String[] args) {
    ShapeGUI gui = new ShapeGUI();
    gui.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
        System.exit(0); 
      } 
    });
    gui.setVisible(true);
  }
}