Package pdfdb.app

Source Code of pdfdb.app.EventController$MouseManager

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.app;

import pdfdb.gui.panels.MainPanel;
import pdfdb.gui.customcomponents.ClosableTab;
import java.awt.Component;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.table.JTableHeader;
import pdfdb.data.filetable.*;
import pdfdb.gui.*;
import pdfdb.gui.customcomponents.DarkTextField;
import pdfdb.gui.frames.*;
import pdfdb.structure.IndexableFile;

/** The controller part of the Model-View-Controller architecture. This class
* defines global level actions as constants that can be passed to the
* individual listening classes. Access to the listeners are managed to
* avoid memory issues with instantiating anonymous listeners. This is because
* listeners are accessible to all classes and can be removed when an
* implementation class no longer requires the listener. Note: local level
* actions are not managed by this class as this would inhibit component reuse,
* scalability and is in conflict with a modular design with low coupling.
* @author ug22cmg */
public class EventController
{

    private static MouseListener mouseListener = new MouseManager();
    private static WindowListener windowListener = new WindowManager();
    private static ActionListener actionListener = new ActionManager();
    private static KeyListener keyListener = new KeyManager();

    public static final int OP_CLOSE_TAB = 0;
    public static final int OP_SEARCH = 1;
    public static final int OP_PREFS_CONFIRM = 2;
    public static final int OP_PREFS_CANCEL = 3;
    public static final int OP_PREFS_OPEN = 4;
    public static final int OP_MENU_CLEAR = 5;
    public static final int OP_MENU_EXIT = 6;
    public static final int OP_MENU_ABOUT = 7;
    public static final int OP_MENU_HELP_MAIN = 8;
    public static final int OP_MENU_HELP_DEV = 9;
    public static final int OP_TABLEMENU_OPENED = 11;
    public static final int OP_TABLEMENU_CLICKED = 12;
    public static final int OP_PASSWORD_CONFIRM = 13;
    public static final int OP_PASSWORD_REJECT = 14;
    public static final int OP_HELP_BACK = 15;
    public static final int OP_HELP_NEXT = 16;
    public static final int OP_PDF_OPEN = 17;

    /** Private constructor ensures that the class cannot be instantiated. */
    private EventController()
    {
    }

    /** Gets the mouse listener.
     * @return The mouse listener instance. */
    public static MouseListener getMouseListener()
    {
        return mouseListener;
    }

    /** Gets the action listener.
     * @return The action listener instance. */
    public static ActionListener getActionListener()
    {
        return actionListener;
    }

    /** Gets the window listener.
     * @return The window listener instance. */
    public static WindowListener getWindowListener()
    {
        return windowListener;
    }

    /** Gets the key listener.
     * @return The key listener instance. */
    public static KeyListener getKeyListener()
    {
        return keyListener;
    }

    /** Executes a global action.
     * @param command The command id.
     * @param source The source of the event.
     * @param arg The event arguments. */
    private static void executeCommand(int command, Object source,
            Object arg, ActionEvent evt)
    {
        try
        {
            switch (command)
            {
                case OP_CLOSE_TAB:
                    closeTab(source);
                    break;

                case OP_PDF_OPEN:
                    openPdf(source);
                    break;

                case OP_SEARCH:
                    search(source);
                    break;

                case OP_PASSWORD_REJECT:
                    cancelPassword(source);
                    break;

                case OP_PASSWORD_CONFIRM:
                    confirmPassword(source);
                    break;

                case OP_PREFS_CONFIRM:
                    confirmPrefs(source);
                    break;

                case OP_PREFS_CANCEL:
                    cancelPrefs(source);
                    break;

                case OP_PREFS_OPEN:
                    openPrefs();
                    break;

                case OP_MENU_EXIT:
                    System.exit(0);
                    break;

                case OP_MENU_ABOUT:
                    new AboutFrame().setVisible(true);
                    break;

                case OP_MENU_HELP_MAIN:
                    openHelpFrame("main.html");
                    break;

                case OP_MENU_HELP_DEV:
                    openHelpFrame("dev.html");
                    break;

                case OP_TABLEMENU_OPENED:
                    openTableMenu(source, arg);
                    break;

                case OP_HELP_BACK:
                    goBackOnHelpFrame(source);
                    break;

                case OP_HELP_NEXT:
                    goForwardOnHelpFrame(source);
                    break;

                case OP_TABLEMENU_CLICKED:
                    clickTableMenu(evt);
                    break;
            }
        }
        catch (IOException ex)
        {
        }
    }

    /** Cancels the password dialog
     * @param source The source of the event */
    private static void cancelPassword(Object source)
    {
        JButton button = (JButton) source;
        PasswordRequestFrame f = (PasswordRequestFrame) Hierarchy.
                findDirectAncestor(
                PasswordRequestFrame.class, button);
        f.cancel();
    }

    /** Confirms the password dialog
     * @param source The source of the event */
    private static void confirmPassword(Object source)
    {
        JButton button = (JButton) source;
        PasswordRequestFrame f = (PasswordRequestFrame) Hierarchy.
                findDirectAncestor(
                PasswordRequestFrame.class, button);
        f.confirm();
    }

    /** Opens the preferences window */
    private static void openPrefs()
    {
        MainFrame mf = MainFrame.getMainFrame();
        PreferencesFrame pf = new PreferencesFrame(mf);
        pf.setVisible(true);
    }

    /** Confirms the preferences window.
     * @param source The source of the event.
     * @throws java.io.IOException If an error occurs writing to the
     * configuration file. */
    private static void confirmPrefs(Object source) throws IOException
    {
        JComponent btn = (JComponent) source;
        Class cls = PreferencesFrame.class;
        PreferencesFrame pf = (PreferencesFrame) Hierarchy.findDirectAncestor(
                cls, btn);
        pf.save();
    }

    /** Cancels the preferences window.
     * @param source The event source. */
    private static void cancelPrefs(Object source)
    {
        JComponent btn = (JComponent) source;
        Class cls = PreferencesFrame.class;
        PreferencesFrame pf2 = (PreferencesFrame) Hierarchy.findDirectAncestor(
                cls,
                btn);
        pf2.cancel();
    }

    /** Performs a search.
     * @param source The source of the event. */
    private static void search(Object source)
    {
        JComponent comp = (JComponent) source;
        Component mPanel = Hierarchy.findDirectAncestor(MainPanel.class, comp);
        MainPanel panel = (MainPanel) mPanel;
        panel.addSearchTab(panel.getSearchString(), true);
    }

    /** Opens the table menu.
     * @param source The source of the event.
     * @param arg The event arguments. */
    private static void openTableMenu(Object source, Object arg)
    {
        MenuManager.showMenu(source, (MouseEvent) arg);
    }

    /** Clicks the table menu.
     * @param evt The event. */
    private static void clickTableMenu(ActionEvent evt)
    {
        SavedColumnModel.getInstance().menuUpdate(evt);
    }

    /** Opens a PDF.
     * @param source The source of the event. */
    private static void openPdf(Object source)
    {
        JComponent comp = (JComponent) source;
        MainPanel mp1 = (MainPanel) Hierarchy.findDirectAncestor(MainPanel.class,
                comp);
        FileTable table = mp1.getTableForTab(mp1.getSelectedTab());
        IndexableFile f = table.getFile(table.getSelectedRow());
        if (f != null)
        {
            HelperApplicationAccess appAccess = HelperApplicationAccess.
                    getInstance();
            appAccess.executePdf(f);
        }
    }

    /** Closes a tab.
     * @param source The source of the event. */
    private static void closeTab(Object source)
    {
        ClosableTab tab = null;
        if (source instanceof ClosableTab)
        {
            tab = (ClosableTab) source;
        }
        else
        {
            tab = (ClosableTab) ((JComponent) source).getParent();
        }
        Component c = Hierarchy.findDirectAncestor(JTabbedPane.class, tab);
        JTabbedPane jtp = (JTabbedPane) c;
        jtp.remove(tab.getIndex());
    }

    /** Opens the help frame.
     * @param path The path to the help file.
     * @throws java.io.IOException If an error occurs reading the help file. */
    private static void openHelpFrame(String path) throws IOException
    {
        File f = new File(path);
        URL fileUrl = f.toURI().toURL();
        new HelpFrame(fileUrl).setVisible(true);
    }

    /** Moves the help window back.
     * @param source The source of the event.
     * @throws java.io.IOException If an error occurs reading the help file. */
    private static void goBackOnHelpFrame(Object source) throws IOException
    {
        JComponent comp = (JComponent) source;
        Object o2 = Hierarchy.findDirectAncestor(HelpFrame.class, comp);
        HelpFrame frame = (HelpFrame) o2;
        frame.back();
    }

    /** Moves the help window forward.
     * @param source The source of the event.
     * @throws java.io.IOException If an error occurs reading the help file. */
    private static void goForwardOnHelpFrame(Object source) throws IOException
    {
        JComponent comp = (JComponent) source;
        Object o = Hierarchy.findDirectAncestor(HelpFrame.class, comp);
        HelpFrame frame = (HelpFrame) o;
        frame.forward();
    }

    /** The action listener class */
    private static class ActionManager implements ActionListener
    {

        /** Manages the action listener action event, specifically for searching
         *  when, the search button is pressed.
         * @param e The event arguments. */
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                String cmd = e.getActionCommand();
                String cmdInt = null;
                Object src = null;
                if (cmd.contains(">>"))
                {
                    Object tmpArg = null;
                    String[] tmp = cmd.split(">>");
                    if (tmp.length != 2)
                    {
                        throw new IllegalArgumentException();
                    }
                    cmdInt = tmp[0];
                    src = e.getSource();
                    tmpArg = tmp[1];
                    executeCommand(Integer.valueOf(cmdInt), src, tmpArg, e);
                }
                else
                {
                    cmdInt = cmd;
                    src = e.getSource();
                    executeCommand(Integer.valueOf(cmdInt), src, null, e);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    /** The window listener class */
    private static class WindowManager extends WindowAdapter
    {

        /** Manages application shutdown when the main frame is closed.
         * @param e The event arguments. */
        @Override
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }

    /** The key listener class */
    private static class KeyManager extends KeyAdapter
    {

        /** Handles the key-press event
         * @param e The event arguments. */
        @Override
        public void keyPressed(KeyEvent e)
        {
            if(e.getSource() instanceof DarkTextField && e.getKeyCode() == 10)
            {
                executeCommand(EventController.OP_SEARCH, e.getSource(), null, null);
                e.consume();
            }
            else if(e.getSource() instanceof FileTable && e.getKeyCode() == 10)
            {
                executeCommand(EventController.OP_PDF_OPEN, e.getSource(), null, null);
                e.consume();
            }
            super.keyPressed(e);
        }
    }

    /** The mouse listener class. */
    private static class MouseManager extends MouseAdapter
    {

        /** Executed when the mouse clicked event is raised on a component that
         *  is managed by the event controller.
         * @param e The event arguments */
        @Override
        public void mouseClicked(MouseEvent e)
        {
            if (e.getSource() instanceof JTableHeader)
            {
                if (e.getButton() == MouseEvent.BUTTON3)
                {
                    executeCommand(OP_TABLEMENU_OPENED, e.getSource(), e, null);
                }
            }
            else
            {
                System.out.println("Done!");
            }
        }
    }
}
TOP

Related Classes of pdfdb.app.EventController$MouseManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.