package caltool.view.admin;

import caltool.model.admin.*;
import caltool.view.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import mvp.*;

/****
 *
 * Class AdminMenu is the pulldown menu view of the <a href = "Admin.html"
 * Admin </a> model class.  The AdminMenu widget is a Java JMenu.  Anonymous
 * instances of JMenuItem are defined for each item in the menu.
 *
 */
public class AdminMenu extends mvp.View {

    /**
     * Construct this with the give Admin model.
     */
    public AdminMenu(Screen screen, Admin admin, AdminUI adminUI) {
        super(null, admin);
    }

    /**
     * Compose this by inserting each of its four menu items into the pulldown
     * menu.  The items are Users, Groups, Locations, Central Host, List
     * Admins, and Login.  A JSeparator is placed after the 'Locations' item.
     */
    public Component compose() {

        /*
         * Make the widget of this the JMenu.
         */
        widget = new JMenu("Admin");

        /*
         * Create a constant reference to the Admin model for use in the
         * menu item action listeners.  This is necessary due to the subtle
         * rules of anonymous inner classes in Java.
         */
        final Admin admin = (Admin) model;

        /*
         * Create the menu items using the following pattern:
         *                                                              <pre>
         *     JMenu.add(new JMenuItem("<em>Item name</em>").addActionListener(
         *         new ActionListener() {
         *             public void actionPerformed(ActionEvent e) {
         *                 <em>Model.method()</em>
         *                                                              </pre>
         */


        /*
         * Add the 'Connect ...' menu item.
         */
        ((JMenu) widget).add(new JMenuItem("Connect ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("In Admin.connect.");
                }
            }
        );

        /*
         * Add a separator.
         */
        ((JMenu) widget).add(new JSeparator());

        /*
         * Add the 'Users ...' menu item.
         */
        ((JMenu) widget).add(new JMenuItem("Users ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("In Admin.users.");
                }
            }
        );

        /*
         * Add the 'Groups ...' menu item.);
         */
        ((JMenu) widget).add(new JMenuItem("Groups ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("In Admin.groups.");
                }
            }
        );

        /*
         * Add the 'Locations ...' menu item.);
         */
        ((JMenu) widget).add(new JMenuItem("Locations ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("In Admin.locations.");
                }
            }
        );

        /*
         * Add a separator.
         */
        ((JMenu) widget).add(new JSeparator());

        /*
         * Add the 'Password ...' menu item.);
         */
        ((JMenu) widget).add(new JMenuItem("Password ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("In Admin.password.");
                }
            }
        );

        /*
         * Add the 'Contact Admin ...' menu item.);
         */
        ((JMenu) widget).add(new JMenuItem("Contact Admin ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("In Admin.conctactAdmin.");
                }
            }
        );

        return widget;
    }

}