// A class providing a controller for the CarPark.
// Barry Cornelius, 29th February 2000
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import java.awt.       BorderLayout;
import java.awt.       Container;
import javax.swing.    JButton;
import javax.swing.    JFrame;
import javax.swing.    JTextField;
public class CarParkController implements ActionListener
{
   private CarPark iCarPark;
   private JTextField iJTextField;
   public CarParkController(final CarPark pCarPark,
                            final String pLocation,
                            final int pJFrameX, final int pJFrameY)
   {
      iCarPark = pCarPark;
      final JFrame tJFrame = new JFrame("CarParkController: " + pLocation);
      final JButton tAddJButton = new JButton("Add");
      final JButton tRemoveJButton = new JButton("Remove");
      tAddJButton.addActionListener(this);
      tRemoveJButton.addActionListener(this);
      iJTextField = new JTextField(35);
      final Container tContentPane = tJFrame.getContentPane();
      tContentPane.add(tAddJButton,    BorderLayout.NORTH);
      tContentPane.add(iJTextField,     BorderLayout.CENTER);
      tContentPane.add(tRemoveJButton, BorderLayout.SOUTH);
      final ExitOnWindowClosing tExitOnWindowClosing = 
                             new ExitOnWindowClosing();
      tJFrame.addWindowListener(tExitOnWindowClosing);
      tJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      tJFrame.setLocation(pJFrameX, pJFrameY);
      tJFrame.setSize(300,100);
      tJFrame.setVisible(true);
   }
   public void actionPerformed(final ActionEvent pActionEvent)
   {
      final String tJButtonString = pActionEvent.getActionCommand();
      final String tNumberPlate = iJTextField.getText();
      if (tNumberPlate.equals(""))
      {
          return;
      } 
      if (tJButtonString.equals("Add"))
      {
          iCarPark.add(tNumberPlate);
      }
      else
      {
          iCarPark.remove(tNumberPlate);
      }
      iJTextField.setText("");
   }
}
