/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing;

import com.dmdirc.actions.ActionManager;
import com.dmdirc.actions.CoreActionType;
import com.dmdirc.addons.ui_swing.actions.CopyAction;
import com.dmdirc.addons.ui_swing.actions.CutAction;
import com.dmdirc.addons.ui_swing.actions.PasteAction;

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

/**
 * Custom event queue to add common functionality to certain components.
 */
@Singleton
public class DMDircEventQueue extends EventQueue {

    /** Swing Controller. */
    private final SwingController controller;

    /**
     * Instantiates the DMDircEventQueue.
     *
     * @param controller Swing controller
     */
    @Inject
    public DMDircEventQueue(final SwingController controller) {
        super();

        this.controller = controller;
    }

    /** {@inheritDoc} */
    @Override
    protected void dispatchEvent(final AWTEvent event) {
        preDispatchEvent(event);
        super.dispatchEvent(event);
        postDispatchEvent(event);
        if (event instanceof MouseEvent) {
            handleMouseEvent((MouseEvent) event);
        } else if (event instanceof KeyEvent) {
            handleKeyEvent((KeyEvent) event);
        } else if (event instanceof WindowEvent) {
            handleWindowEvent((WindowEvent) event);
        }
    }

    /**
     * Triggered before the event is dispatched to AWT.
     *
     * @param event Event about to be dispatched
     */
    protected void preDispatchEvent(final AWTEvent event) {
        //Do nothing
    }

    /**
     * Called just after an event is dispatched to AWT.
     *
     * @param event Event that has been dispatched
     */
    protected void postDispatchEvent(final AWTEvent event) {
        //Do nothing
    }

    /**
     * Handles key events.
     *
     * @param ke Key event
     */
    private void handleKeyEvent(final KeyEvent ke) {
        ActionManager.getActionManager().triggerEvent(
                CoreActionType.CLIENT_KEY_PRESSED, null,
                KeyStroke.getKeyStroke(ke.getKeyChar(), ke.getModifiers()));
    }

    /**
     * Handles mouse events.
     *
     * @param me Mouse event
     */
    private void handleMouseEvent(final MouseEvent me) {
        if (!me.isPopupTrigger() || me.getComponent() == null) {
            return;
        }

        final Component comp = SwingUtilities.getDeepestComponentAt(
                me.getComponent(), me.getX(), me.getY());

        if (!(comp instanceof JTextComponent) || MenuSelectionManager
                .defaultManager().getSelectedPath().length > 0) {
            return;
        }

        final JTextComponent tc = (JTextComponent) comp;
        final JPopupMenu menu = new JPopupMenu();
        menu.add(new CutAction(tc));
        menu.add(new CopyAction(tc));
        menu.add(new PasteAction(tc));

        final Point pt = SwingUtilities.convertPoint(me.getComponent(),
                me.getPoint(), tc);
        menu.show(tc, pt.x, pt.y);
    }

    /**
     * Handles window events.
     *
     * @param we Window event
     */
    private void handleWindowEvent(final WindowEvent we) {
        if ((we.getSource() instanceof Window) && controller.hasMainFrame()) {
            if (we.getID() == WindowEvent.WINDOW_OPENED) {
                controller.addTopLevelWindow((Window) we.getSource());
            } else if (we.getID() == WindowEvent.WINDOW_CLOSED) {
                controller.delTopLevelWindow((Window) we.getSource());
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void pop() { //NOPMD
        super.pop();
    }

}

@@ -1162,91 +1162,8 @@
 g;%0A%0A
-import com.dmdirc.actions.ActionManager;%0Aimport com.dmdirc.actions.CoreActionType;%0A
 impo
@@ -1319,16 +1319,109 @@
 eAction;
+%0Aimport com.dmdirc.events.ClientKeyPressedEvent;%0A%0Aimport com.google.common.eventbus.EventBus;
 %0A%0Aimport
@@ -2104,115 +2104,89 @@
 er;%0A
-%0A
     /**
-%0A     * Instantiates the DMDircEventQueue.%0A    
+ Event bus to despatch events to.
  *
+/
 %0A    
- * @param controller Swing controller%0A     */
+private final EventBus eventBus;%0A
 %0A   
@@ -2254,16 +2254,41 @@
 ntroller
+, final EventBus eventBus
 ) %7B%0A    
@@ -2347,35 +2347,44 @@
     
-%7D%0A%0A
     
-/** %7B@inheritDoc%7D */
+this.eventBus = eventBus;%0A    %7D%0A
 %0A   
@@ -3438,111 +3438,48 @@
     
-ActionManager.getActionManager().triggerEvent(%0A                CoreActionType.CLIENT_KEY_PRESSED, null,
+eventBus.post(new ClientKeyPressedEvent(
 %0A   
@@ -3550,16 +3550,17 @@
 iers()))
+)
 ;%0A    %7D%0A
@@ -4984,33 +4984,8 @@
  %7D%0A%0A
-    /** %7B@inheritDoc%7D */%0A
     

Use an event bus.