Sunday, January 13, 2013

Paranoid Mouse Listener

package utility;

import java.awt.Toolkit;
import java.awt.event.*;
import java.util.TimerTask;
import javax.swing.Timer;

/**
 *
 * @author Rudi Theunissen
 */
public class ParanoidMouseListener extends MouseAdapter {

    public static final int LEFT = MouseEvent.BUTTON1;
    public static final int MIDDLE = MouseEvent.BUTTON2;
    public static final int RIGHT = MouseEvent.BUTTON3;
    private MouseEvent event;

    /**
     * This method should be safe as far as I know, which allows you to ignore the MouseEvent when you don't need it.
     *
     * @return
     */
    public MouseEvent getEvent() {
        return event;
    }

    @Override
    public final void mouseClicked(MouseEvent event) {
        this.event = event;
        switch (event.getButton()) {
            case LEFT:
                if (leftDouble.isRunning()) {
                    leftDouble.stop();
                    onLeftDoubleClick();
                } else {
                    leftDouble.start();
                    onLeftClick();
                }
                break;
            case MIDDLE:
                if (middleDouble.isRunning()) {
                    middleDouble.stop();
                    onMiddleDoubleClick();
                } else {
                    middleDouble.start();
                    onMiddleClick();
                }
                break;
            case RIGHT:
                if (rightDouble.isRunning()) {
                    rightDouble.stop();
                    onRightDoubleClick();
                } else {
                    rightDouble.start();
                    onRightClick();
                }
                break;
        }
    }

    @Override
    public final void mousePressed(MouseEvent event) {
        this.event = event;
        switch (event.getButton()) {
            case LEFT:
                leftHold.start();
                onLeftPress();
                break;
            case MIDDLE:
                middleHold.start();
                onMiddlePress();
                break;
            case RIGHT:
                rightHold.start();
                onRightPress();
                break;
        }
    }

    @Override
    public final void mouseReleased(MouseEvent event) {
        this.event = event;
        switch (event.getButton()) {
            case LEFT:
                leftHold.stop();
                onLeftRelease();
                break;
            case MIDDLE:
                middleHold.stop();
                onMiddleRelease();
                break;
            case RIGHT:
                rightHold.stop();
                onRightRelease();
                break;
        }
    }

    @Override
    public void mouseMoved(MouseEvent event) {
        this.event = event;
        moved();
    }

    @Override
    public void mouseDragged(MouseEvent event) {
        this.event = event;
        switch (event.getButton()) {
            case LEFT:
                onLeftDrag();
                break;
            case MIDDLE:
                onMiddleDrag();
                break;
            case RIGHT:
                onRightDrag();
                break;
        }
    }

    @Override
    public final void mouseEntered(MouseEvent event) {
        this.event = event;
        entered();
    }

    @Override
    public final void mouseExited(MouseEvent event) {
        this.event = event;
        exited();
    }

    public void moved() {
    }

    public void entered() {
    }

    public void exited() {
    }

    public void onLeftHold() {
    }

    public void onMiddleHold() {
    }

    public void onRightHold() {
    }

    public void onLeftClick() {
    }

    public void onMiddleClick() {
    }

    public void onRightClick() {
    }

    public void onLeftDoubleClick() {
    }

    public void onMiddleDoubleClick() {
    }

    public void onRightDoubleClick() {
    }

    public void onLeftPress() {
    }

    public void onMiddlePress() {
    }

    public void onRightPress() {
    }

    public void onLeftRelease() {
    }

    public void onMiddleRelease() {
    }

    public void onRightRelease() {
    }

    public void onLeftDrag() {
    }

    public void onMiddleDrag() {
    }

    public void onRightDrag() {
    }

    private int getDoubleClickInterval() {
        String property = "awt.multiClickInterval";
        return (int) Toolkit.getDefaultToolkit().getDesktopProperty(property);
    }

    private class DoubleClickTimer extends Timer {

        public DoubleClickTimer() {
            super(getDoubleClickInterval(), null);
            this.setRepeats(false);
        }
    }

    private class HoldTimer extends Timer {

        public static final int ACTIVATION_DELAY = 300;
        public static final int HOLD_INTER_DELAY = 60;

        public HoldTimer() {
            super(HOLD_INTER_DELAY, null);

            this.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    perform();
                }
            });
            this.setInitialDelay(ACTIVATION_DELAY);
        }

        public void perform() {
        }
    }
    private HoldTimer leftHold = new HoldTimer() {

        @Override
        public void perform() {
            onLeftHold();
        }
    };
    private HoldTimer middleHold = new HoldTimer() {

        @Override
        public void perform() {
            onMiddleHold();
        }
    };
    private HoldTimer rightHold = new HoldTimer() {

        @Override
        public void perform() {
            onRightHold();
        }
    };
    private DoubleClickTimer leftDouble, middleDouble, rightDouble;

    public ParanoidMouseListener() {
        leftDouble = new DoubleClickTimer();
        middleDouble = new DoubleClickTimer();
        rightDouble = new DoubleClickTimer();
    }
}

No comments:

Post a Comment