// A class presenting a menu for the manipulation of a population.
// Barry Cornelius, 20 June 2000
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import javax.swing.    JFrame;
import javax.swing.    JMenu;
import javax.swing.    JMenuBar;
import javax.swing.    JMenuItem;
import javax.swing.    JTextArea;
import javax.swing.    JTextField;
import java.awt.event. KeyEvent;
public class PopMenu implements ActionListener
{
   private JMenuBar iJMenuBar;
   private JFrame iJFrame;
   private Pop iPop;
   private int iX;
   private int iY;
   private JTextField iResultsJTextField;
   private JTextArea iLogJTextArea;

   private void iAddJMenuItem(final JMenu pJMenu, final int pKeyEvent,
                              final String pString)
   {
      final JMenuItem tJMenuItem = new JMenuItem(pString);
      pJMenu.add(tJMenuItem);
      tJMenuItem.setMnemonic(pKeyEvent);
      tJMenuItem.addActionListener(this);
   }

   private void iCreateFileJMenu(final JMenuBar pJMenuBar)
   {
      final JMenu tFileJMenu = new JMenu("File");
      pJMenuBar.add(tFileJMenu);
      iAddJMenuItem(tFileJMenu, KeyEvent.VK_Q, "quit");
   }

   private void iCreateOperationsJMenu(final JMenuBar pJMenuBar)
   {
      final JMenu tOperationsJMenu = new JMenu("Operations");
      pJMenuBar.add(tOperationsJMenu);
      iAddJMenuItem(tOperationsJMenu, KeyEvent.VK_A, "add");
      iAddJMenuItem(tOperationsJMenu, KeyEvent.VK_R, "remove");
      iAddJMenuItem(tOperationsJMenu, KeyEvent.VK_G, "get");
      iAddJMenuItem(tOperationsJMenu, KeyEvent.VK_F, "first");
      iAddJMenuItem(tOperationsJMenu, KeyEvent.VK_N, "next");
   } //BJCHEREFIRST 

   public PopMenu(final JFrame pJFrame, final Pop pPop,
                  final int pX, final int pY,
                  final JTextField pResultsJTextField,
                  final JTextArea pLogJTextArea)
   {
      iJFrame = pJFrame;
      iPop = pPop;
      iX = pX;
      iY = pY;
      iResultsJTextField = pResultsJTextField;
      iLogJTextArea = pLogJTextArea;
      iJMenuBar = new JMenuBar();
      iCreateFileJMenu(iJMenuBar);
      iCreateOperationsJMenu(iJMenuBar);
   }

   private void iDisplayPersonOutputDialog(final Person pPerson)
   {
      if (pPerson==null)
      {
         iResultsJTextField.setText("No person has been found");
      }
      else
      {
         iResultsJTextField.setText("                        ");
         final PersonOutputDialog tPersonOutputDialog =
                            new PersonOutputDialog(iJFrame, pPerson, iX, iY);
      }
   }

   public void actionPerformed(final ActionEvent pActionEvent)
   {
      switch (pActionEvent.getActionCommand().charAt(0))
      {
         case 'a':
         {
            final PersonInputDialog tPersonInputDialog =
                                      new PersonInputDialog(iJFrame, iX, iY);
            final Person tPerson = tPersonInputDialog.getPerson();
            final boolean tResult = iPop.add(tPerson);
            iResultsJTextField.setText("Person:" + tPerson + " OK:" +
                                                                    tResult);
            iLogJTextArea.append("add\n");
         }
         break; //BJCHERECONT
         case 'r':
         {
            final NameInputDialog tNameInputDialog =
                                        new NameInputDialog(iJFrame, iX, iY);
            final String tName = tNameInputDialog.getName();
            final boolean tResult = iPop.remove(tName);
            iResultsJTextField.setText("Name:" + tName + " OK:" + tResult);
            iLogJTextArea.append("remove\n");
         }
         break;
         case 'g':
         {
            final NameInputDialog tNameInputDialog =
                                        new NameInputDialog(iJFrame, iX, iY);
            iDisplayPersonOutputDialog(iPop.get(tNameInputDialog.getName()));
            iLogJTextArea.append("get\n");
         }
         break;
         case 'f':
         {
            iDisplayPersonOutputDialog(iPop.getFirst());
            iLogJTextArea.append("first\n");
         }
         break;
         case 'n':
         {
            iDisplayPersonOutputDialog(iPop.next());
            iLogJTextArea.append("next\n");
         }
         break;
         case 'q':
         {
            System.exit(0);
         }
         break;
      }
   }

   public JMenuBar getJMenuBar()
   {
      return iJMenuBar;
   }
}
