package file;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/****
 *
 * Class FileMenu is the "File" pulldown menu in the CalendarToolUI prototype.
 * Anonymous instances of JMenuItem are defined for each item in the menu.
 *                                                                         <p>
 * This class serves as a good example for how items are added to a menu, and
 * how an item's "action listener" is given a simple prototype implementation.
 *                                                                         <p>
 * As explained in the Java Swing Tutorial, an action listener is implemented
 * in a method named "actionPerformed".  This method is specialized for all
 * clickable GUI components, such as menu items and buttons.  In a complete
 * implementation, action listeners perform actual computations, by calling
 * methods in companion computational classes.
 *                                                                         <p>
 * In a GUI prototype, action listeners don't do any real work.  Instead, they
 * do one of two things:
 *                                                                     <ol><li>
 * print a simple message to stdout, to indicate that a menu item or button has
 * been clicked on
 *                                                                         <li>
 * bring up another prototype GUI, such as a dialog or display window
 *                                                                        </ol>
 *                                                                         <p>
 * In the full implementation next quarter in 309, these simple prototype
 * actions will be replaced by calls to methods that do real work.
 *
 */
public class FileMenu extends JMenu {

    /**
     * Construct this by inserting each of its nine menu items into the
     * pulldown menu.  The items are New, Open, Close, Close All, Save, Save
     * As, Save All, Print, and Exit.  JSeparators are placed after the 'Close
     * All', 'Save All', and 'Print' items.
     */
    public FileMenu() {

        super("File");

        /*
         * Add each of the items to the menu, with separators in the
         * appropriate places (per the requirements).
         */
        addNewItem();
        addOpenItem();
        add(new JSeparator());

        addCloseItem();
        addCloseAllItem();
        add(new JSeparator());

        addSaveItem();
        addSaveAsItem();
        addSaveAllItem();
        add(new JSeparator());

        addLoadSettingsItem();
        addSaveSettingsItem();
        add(new JSeparator());

        addPageSetupItem();
        addPrintItem();
        add(new JSeparator());

        addExitItem();
    }

    /**
     * Add the 'New' menu item.  This and all other file menu items are created
     * using the following pattern:
     *                                                                    <pre>
     *     JMenu.add(new JMenuItem("<em>Item name</em>")).addActionListener(
     *         new ActionListener() {
     *             public void actionPerformed(ActionEvent e) {
     *                 <em>print a message to stdout, or bring up another GUI</em>
     *             }
     *         }
     *     );
     *                                                                   </pre>
     */
    protected void addNewItem() {

        /*
         * Use the standard menu item pattern for the item.
         */
        add(new JMenuItem("New")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    /*
                     * Print a message
                     */
                    System.out.println("File->New selected.");
                }
            }
        );
    }

    /**
     * Add the 'Open ...' menu item.  Its action listener brings up a standard
     * Swing file choose, provided by the JFileChooser class.  method.  See the
     * description of the <a href= FileMenu.html#addNewItem> addNewItem </a>
     * method for further info.
     */
    protected void addOpenItem() {

        final JMenuItem item;           // Used by JFileChooser for placement

        add(
          item = new JMenuItem("Open ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JFileChooser chooser = new JFileChooser();
                    chooser.showOpenDialog(item);
                }
            }
        );
    }

    /**
     * Add the 'Close' menu item.  Its action listener just prints a message.
     * See the description of the <a href= FileMenu.html#addNewItem> addNewItem
     * </a> method for further info.
     */
    protected void addCloseItem() {
        add(new JMenuItem("Close")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("File->Close selected.");
                }
            }
        );
    }

    /**
     * Add the 'Close All menu item.  Its action listener just prints a
     * message.  See the description of the <a href= FileMenu.html#addNewItem>
     * addNewItem </a> method for further info.
     */
    protected void addCloseAllItem() {
        add(new JMenuItem("Close All")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("File->CloseAll selected.");
                }
            }
        );
    }

    /**
     * Add the 'Save' menu item.  Its action listener just prints a message.
     * See the description of the <a href= FileMenu.html#addNewItem> addNewItem
     * </a> method for further info.
     */
    protected void addSaveItem() {
        add(new JMenuItem("Save")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("File->Save selected.");
                }
            }
        );
    }

    /**
     * Add the 'Save As ...' menu item.  Its action listener displays a file
     * chooser to select the file on which to save.  See the description of the
     * <a href= FileMenu.html#addNewItem> addNewItem </a> method for further
     * info.
     */
    protected void addSaveAsItem() {
        final JMenuItem item;           // Used by JFileChooser for placement

        add(
          item = new JMenuItem("Save As ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    (new JFileChooser()).showSaveDialog(item);
                }
            }
        );
    }

    /**
     * Add the 'Save All' menu item.  Its action listener just prints a
     * message.  method.  See the description of the <a href=
     * FileMenu.html#addNewItem()> addNewItem </a> method for further info.
     */
    protected void addSaveAllItem() {
        add(new JMenuItem("Save All")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("File->Save All selected.");
                }
            }
        );
    }

    /**
     * Add the 'Load Settings' menu item.  Its action listener displays a
     * simple prototype of the load settings dialog.  See the description of the
     * <a href= FileMenu.html#addNewItem()> addNewItem </a> method for further
     * info.
     */
    protected void addLoadSettingsItem() {
        final JMenuItem item;           // Used by JFileChooser for placement

        add(
          item = new JMenuItem("Load Settings ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    /*
                     * For the prototype, just display a quickie dialog that
                     * will be refined later.
                     */
                    JOptionPane.showInputDialog("... Load Settings stuff goes here ...");
                }
            }
        );
    }

    /**
     * Add the 'Save Settings' menu item.  Its action listener displays a
     * simple prototype of the save settings dialog.  See the description of
     * the <a href= FileMenu.html#addNewItem()> addNewItem </a> method for
     * further info.
     */
    protected void addSaveSettingsItem() {
        final JMenuItem item;           // Used by JFileChooser for placement

        add(
          item = new JMenuItem("Save Settings ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    /*
                     * For the prototype, just display a quickie dialog
                     * that will be refined later.
                     */
                    JOptionPane.showInputDialog("... Save Settings stuff goes here ...");
                }
            }
        );
    }

    /**
     * Add the 'Page Setup ...' menu item.  Its action listener displays a Page
     * Setup dialog, from which the File.pageSetup method is invoked.  See the
     * description of the <a href= FileMenu.html#addNewItem()> addNewItem </a>
     * method for further info.
     */
    protected void addPageSetupItem() {
        final JMenuItem item;           // Used by JFileChooser for placement

        add(new JMenuItem("Page Setup ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    /*
                     * For the prototype, just display a quickie dialog
                     * that will be refined later.
                     */
                    JOptionPane.showInputDialog("... Page Setup stuff goes here ...");
                }
            }
        );
    }

    /**
     * Add the 'Print ...' menu item.  Its action listener invokes the
     * File.print method.  See the description of the <a href=
     * FileMenu.html#addNewItem()> addNewItem </a> method for further info.
     */
    protected void addPrintItem() {
        final JMenuItem item;           // Used by JFileChooser for placement

        add(new JMenuItem("Print ...")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    /*
                     * For the prototype, just display a quickie dialog
                     * that will be refined later.
                     */
                    JOptionPane.showInputDialog("Choose a printer: ");
                }
            }
        );
    }

    /**
     * Add the 'Exit' menu item.  Its action listener calls System.exit.  As
     * such it's the only action listener that does any real work.
     */
    protected void addExitItem() {
        add(new JMenuItem("Exit")).addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            }
        );
    }

}