]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/MouseEvent.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / events / MouseEvent.java
diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/MouseEvent.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/MouseEvent.java
new file mode 100644 (file)
index 0000000..8284342
--- /dev/null
@@ -0,0 +1,330 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+package org.simantics.scenegraph.g2d.events;\r
+\r
+import java.awt.datatransfer.Transferable;\r
+import java.awt.event.InputEvent;\r
+import java.awt.geom.Point2D;\r
+\r
+/**\r
+ * TODO Remove hold time\r
+ * @see Event\r
+ * @see MouseClickEvent\r
+ * @see MouseDragBegin\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public abstract class MouseEvent extends Event {\r
+\r
+    private static final long serialVersionUID = -2090477028406944454L;\r
+\r
+    public static final int   LEFT_BUTTON        = 1;\r
+    public static final int   RIGHT_BUTTON       = 2;\r
+    public static final int   MIDDLE_BUTTON      = 3;\r
+\r
+    /** Time for mouse press + mouse release to be interpreted as mouse click */\r
+    public static final long  CLICK_TIME         = 250;\r
+\r
+    public static final int   LEFT_MASK          = 1 << (LEFT_BUTTON - 1);\r
+    public static final int   RIGHT_MASK         = 1 << (RIGHT_BUTTON - 1);\r
+    public static final int   MIDDLE_MASK        = 1 << (MIDDLE_BUTTON - 1);\r
+    public static final int   ALL_BUTTONS_MASK   = LEFT_MASK | RIGHT_MASK | MIDDLE_MASK;\r
+\r
+    public static final int   CTRL_MASK          = InputEvent.CTRL_DOWN_MASK;\r
+    public static final int   ALT_MASK           = InputEvent.ALT_DOWN_MASK;\r
+    public static final int   ALT_GRAPH_MASK     = InputEvent.ALT_GRAPH_DOWN_MASK;\r
+    public static final int   SHIFT_MASK         = InputEvent.SHIFT_DOWN_MASK;\r
+    public static final int   ALL_MODIFIERS_MASK = CTRL_MASK | ALT_MASK | ALT_GRAPH_MASK | SHIFT_MASK;\r
+\r
+    /**\r
+     * The identifier of the mouse of this event.\r
+     * 0 = the main mouse.\r
+     */\r
+    public final int mouseId;\r
+\r
+    /** Position in the coordinate system of the UI component */\r
+    public final Point2D controlPosition;\r
+\r
+    /** Position in the coordinate system of the screen */\r
+    public final Point2D screenPosition;\r
+\r
+    /**\r
+     * The status of mouse buttons\r
+     * @see #LEFT_MASK\r
+     * @see #MIDDLE_MASK\r
+     * @see #RIGHT_MASK\r
+     */\r
+    public final int buttons;\r
+\r
+    /**\r
+     * The state of the keyboard modifier keys at the time the event was\r
+     * generated.\r
+     * \r
+     * @see #ALT_MASK\r
+     * @see #ALT_GRAPH_MASK\r
+     * @see #CTRL_MASK\r
+     * @see #SHIFT_MASK\r
+     */\r
+    public int stateMask;\r
+\r
+    private MouseEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+            Point2D controlPosition, Point2D screenPosition) {\r
+        super(context, time);\r
+        this.mouseId = mouseId;\r
+        this.buttons = buttons;\r
+        this.controlPosition = controlPosition;\r
+        this.screenPosition = screenPosition;\r
+        this.stateMask = stateMask;\r
+    }\r
+\r
+    public boolean hasAnyButton(int mask) {\r
+        return (buttons & mask) != 0;\r
+    }\r
+\r
+    public boolean hasAllButtons(int mask) {\r
+        return (buttons & mask) == mask;\r
+    }\r
+\r
+    public boolean hasAnyModifier(int mask) {\r
+        return (stateMask & mask) != 0;\r
+    }\r
+\r
+    public boolean hasAllModifiers(int mask) {\r
+        return (stateMask & mask) == mask;\r
+    }\r
+\r
+    /**\r
+     * Returns whether or not the Shift modifier is down on this event.\r
+     */\r
+    public boolean isShiftDown() {\r
+        return (stateMask & MouseEvent.SHIFT_MASK) != 0;\r
+    }\r
+\r
+    /**\r
+     * Returns whether or not the Control modifier is down on this event.\r
+     */\r
+    public boolean isControlDown() {\r
+        return (stateMask & MouseEvent.CTRL_MASK) != 0;\r
+    }\r
+\r
+    /**\r
+     * Returns whether or not the Alt modifier is down on this event.\r
+     */\r
+    public boolean isAltDown() {\r
+        return (stateMask & MouseEvent.ALT_MASK) != 0;\r
+    }\r
+\r
+    /**\r
+     * Returns whether or not the Alt Graph modifier is down on this event.\r
+     */\r
+    public boolean isAltGraphDown() {\r
+        return (stateMask & MouseEvent.ALT_GRAPH_MASK) != 0;\r
+    }\r
+\r
+    public abstract static class MouseButtonEvent extends MouseEvent {\r
+\r
+        private static final long serialVersionUID = 3540032494506535841L;\r
+\r
+        /**\r
+         * button of this event\r
+         */\r
+        public final int button;\r
+\r
+        public MouseButtonEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                int button, Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);\r
+            this.button  = button;\r
+        }\r
+    }\r
+\r
+    public static class MouseWheelMovedEvent extends MouseEvent {\r
+\r
+        private static final long serialVersionUID = -7896477913481842708L;\r
+\r
+        /**\r
+         * Constant representing scrolling by "units" (like scrolling with the\r
+         * arrow keys)\r
+         * \r
+         * @see #getScrollType\r
+         */\r
+        public static final int WHEEL_UNIT_SCROLL = 0;\r
+\r
+        /**\r
+         * Constant representing scrolling by a "block" (like scrolling\r
+         * with page-up, page-down keys)\r
+         *\r
+         * @see #getScrollType\r
+         */\r
+        public static final int WHEEL_BLOCK_SCROLL = 1;\r
+\r
+        /**\r
+         * Indicates what sort of scrolling should take place in response to this\r
+         * event, based on platform settings.  Legal values are:\r
+         * <ul>\r
+         * <li> WHEEL_UNIT_SCROLL\r
+         * <li> WHEEL_BLOCK_SCROLL\r
+         * </ul>\r
+         * \r
+         * @see #getScrollType\r
+         */\r
+        public final int scrollType;\r
+\r
+        /**\r
+         * Only valid for scrollType WHEEL_UNIT_SCROLL.\r
+         * Indicates number of units that should be scrolled per\r
+         * click of mouse wheel rotation, based on platform settings.\r
+         *\r
+         * @see #getScrollAmount\r
+         * @see #getScrollType\r
+         */\r
+        public final int scrollAmount;\r
+\r
+        /**\r
+         * Indicates how far the mouse wheel was rotated.\r
+         *\r
+         * @see #getWheelRotation\r
+         */\r
+        public final int wheelRotation;\r
+\r
+        public MouseWheelMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                Point2D controlPosition, Point2D screenPosition, int scrollType, int scrollAmount,\r
+                int wheelRotation) {\r
+            super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);\r
+            this.scrollType = scrollType;\r
+            this.scrollAmount = scrollAmount;\r
+            this.wheelRotation = wheelRotation;\r
+        }\r
+\r
+    }\r
+\r
+    public static class MouseButtonPressedEvent extends MouseButtonEvent {\r
+\r
+        private static final long serialVersionUID = -4687294674299429690L;\r
+\r
+        public MouseButtonPressedEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                int button, Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, button, controlPosition,screenPosition);\r
+        }\r
+\r
+    }\r
+\r
+    public static class MouseButtonReleasedEvent extends MouseButtonEvent {\r
+\r
+        private static final long serialVersionUID = -2303672225339491858L;\r
+\r
+        /** Time in milliseconds how long the button was held down */\r
+        public final long holdTime;\r
+\r
+        public MouseButtonReleasedEvent(Object context, long time,\r
+                int mouseId, int buttons, int stateMask, int button, long holdTime,\r
+                Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);\r
+            this.holdTime = holdTime;\r
+        }\r
+    }\r
+\r
+    public static class MouseMovedEvent extends MouseEvent {\r
+        private static final long serialVersionUID = 1463958776335678155L;\r
+\r
+        public MouseMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);\r
+        }\r
+    }\r
+\r
+    public static class MouseEnterEvent extends MouseEvent {\r
+        private static final long serialVersionUID = 6074648747865556588L;\r
+\r
+        public MouseEnterEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);\r
+        }\r
+    }\r
+\r
+    public static class MouseExitEvent extends MouseEvent {\r
+        private static final long serialVersionUID = 8596801599996844789L;\r
+\r
+        public MouseExitEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);\r
+        }\r
+    }\r
+\r
+    public static class MouseDoubleClickedEvent extends MouseButtonEvent {\r
+        private static final long serialVersionUID = -8046967155607105912L;\r
+\r
+        public MouseDoubleClickedEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                int button, Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);\r
+        }\r
+    }\r
+\r
+    public static class MouseClickEvent extends MouseButtonEvent {\r
+        private static final long serialVersionUID = -1737712792090986607L;\r
+        /**\r
+         * Indicates the number of quick consecutive clicks of\r
+         * a mouse button.\r
+         */\r
+        public final int clickCount;\r
+    \r
+        public MouseClickEvent(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                int button, int clickCount, Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);\r
+            this.clickCount = clickCount;\r
+        }\r
+    }\r
+\r
+    public static class MouseDragBegin extends MouseButtonEvent {\r
+        private static final long serialVersionUID = 3882552912240236450L;\r
+        public final Point2D startCanvasPos;\r
+        public final Point2D startControlPos;\r
+        public Transferable transferable = null;\r
+        public MouseDragBegin(Object context, long time, int mouseId, int buttons, int stateMask,\r
+                int button, Point2D startCanvasPos, Point2D startControlPos, Point2D controlPosition, Point2D screenPosition) {\r
+            super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);\r
+            this.startCanvasPos = startCanvasPos;\r
+            this.startControlPos = startControlPos;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Spans button bit mask into individual button ids (starting from 1)\r
+     * @param buttons\r
+     * @return\r
+     */\r
+    public static int[] getButtonIds(int buttons)\r
+    {\r
+        // calculate 1 bits\r
+        int size = 0;\r
+        for (int i=0; i<32; i++)\r
+            if ((buttons & (1<<i))!=0)\r
+                size++;\r
+        int result[] = new int[size];\r
+        int j=0;\r
+        for (int i=0; i<32; i++)\r
+            if ((buttons & (1<<i))!=0)\r
+                result[j++] = i+1;\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return super.toString() + "[mouse#=" + mouseId + ", control pos=" + controlPosition + ", screen pos="\r
+                + screenPosition + ", buttons=" + Integer.toBinaryString(buttons) + ", stateMask=0x"\r
+                + Integer.toHexString(stateMask) + "]";\r
+    }\r
+\r
+}\r