]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/dnd/DragInteractor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / dnd / DragInteractor.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/dnd/DragInteractor.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/dnd/DragInteractor.java
new file mode 100644 (file)
index 0000000..85db571
--- /dev/null
@@ -0,0 +1,283 @@
+/*******************************************************************************\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
+package org.simantics.g2d.dnd;\r
+\r
+import java.awt.Component;\r
+import java.awt.Cursor;\r
+import java.awt.Point;\r
+import java.awt.datatransfer.Transferable;\r
+import java.awt.dnd.DnDConstants;\r
+import java.awt.dnd.DragGestureEvent;\r
+import java.awt.dnd.DragGestureListener;\r
+import java.awt.dnd.DragGestureRecognizer;\r
+import java.awt.dnd.DragSource;\r
+import java.awt.dnd.DragSourceDragEvent;\r
+import java.awt.dnd.DragSourceDropEvent;\r
+import java.awt.dnd.DragSourceEvent;\r
+import java.awt.dnd.DragSourceListener;\r
+import java.awt.dnd.InvalidDnDOperationException;\r
+import java.awt.event.InputEvent;\r
+import java.awt.event.MouseEvent;\r
+import java.util.Collection;\r
+import java.util.TooManyListenersException;\r
+\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.participant.MouseUtil;\r
+import org.simantics.g2d.participant.MouseUtil.ButtonInfo;\r
+import org.simantics.g2d.participant.MouseUtil.MouseInfo;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDragBegin;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * This participant handles drop operations.\r
+ * \r
+ * To implement a drop operation add an implementation of IDragParticipant to the canvas.\r
+ * \r
+ * Drag interactor is added by chassis.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class DragInteractor extends AbstractCanvasParticipant implements DragSourceListener {\r
+\r
+    private final Component component;\r
+    private IDragSourceParticipant dragParticipant;\r
+    @Dependency\r
+    MouseUtil mouseUtil;\r
+\r
+    public DragInteractor(Component component) {\r
+        super();\r
+        this.component = component;\r
+    }\r
+\r
+    @Override\r
+    public void addedToContext(ICanvasContext ctx) {\r
+        super.addedToContext(ctx);\r
+        installDragSource();\r
+    }\r
+\r
+    @Override\r
+    public void removedFromContext(ICanvasContext ctx) {\r
+        DRAG_SOURCE.removeDragSourceListener(this);\r
+        super.removedFromContext(ctx);\r
+    }\r
+\r
+    int getAllowedOps() {\r
+        int result = 0;\r
+        for (IDragSourceParticipant p : getDragParticipants())\r
+            result |= p.getAllowedOps();\r
+        return result;\r
+    }\r
+\r
+    class MyDragGestureRecognizer extends DragGestureRecognizer {\r
+\r
+        private static final long serialVersionUID = 920532285869166322L;\r
+\r
+        protected MyDragGestureRecognizer(DragSource ds, Component c, int actionMask) {\r
+            super(ds, c, actionMask);\r
+        }\r
+\r
+        @Override\r
+        protected void registerListeners() {\r
+        }\r
+\r
+        @Override\r
+        protected void unregisterListeners() {\r
+        }\r
+        public boolean handleDrag(MouseDragBegin me) {\r
+            for (IDragSourceParticipant p : getDragParticipants()) {\r
+                int op = p.canDrag(me);\r
+                if (op==0) continue;\r
+                int x = (int)me.controlPosition.getX();\r
+                int y = (int)me.controlPosition.getY();\r
+                InputEvent awtie = new MouseEvent(component, 0, me.time, 0, x, y, 1, false);\r
+                appendEvent(awtie);\r
+                fireDragGestureRecognized(op, new Point(x, y));\r
+                return true;\r
+            }\r
+            return false;\r
+        }\r
+    }\r
+\r
+    static DragSource DRAG_SOURCE = new DragSource();\r
+    MyDragGestureRecognizer dgr;\r
+\r
+    @EventHandler(priority = -1000)\r
+    public boolean handleDrag(MouseDragBegin me) {\r
+        return dgr.handleDrag(me);\r
+    }\r
+\r
+    void installDragSource() {\r
+        Integer actionMask = getHint(Hints.KEY_ALLOWED_DRAG_ACTIONS);\r
+        if (actionMask == null)\r
+            actionMask = DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_REFERENCE;\r
+\r
+        dgr = new MyDragGestureRecognizer(DRAG_SOURCE, component, actionMask);\r
+        try {\r
+            dgr.addDragGestureListener(new DragGestureListener() {\r
+                @Override\r
+                public void dragGestureRecognized(DragGestureEvent e) {\r
+                    // Start drag\r
+                    try {\r
+                        // Create transferable from selection\r
+                        Transferable transferable = null;\r
+                        IDragSourceParticipant activeParticipant = null;\r
+                        for (IDragSourceParticipant p : getDragParticipants())\r
+                        {\r
+                            transferable = p.dragStart(e);\r
+                            if (transferable!=null) {\r
+                                activeParticipant = p;\r
+                                break;\r
+                            }\r
+                        }\r
+\r
+                        if (transferable==null)\r
+                            return;\r
+\r
+                        //initial cursor, transferable, dsource listener\r
+\r
+//                        int allowedOps = activeParticipant.getAllowedOps();\r
+//                        int action = e.getDragAction();\r
+//                        Cursor cursor = getCursor(action, allowedOps);\r
+//                        DRAG_SOURCE.startDrag(e, cursor, transferable, DragInteractor.this);\r
+\r
+                        DRAG_SOURCE.startDrag(e, null, transferable, DragInteractor.this);\r
+                        DragInteractor.this.dragParticipant = activeParticipant;\r
+\r
+                        // synthetisize mouse button release\r
+                        MouseInfo mi = mouseUtil.getMouseInfo(0);\r
+                        if (mi!=null) {\r
+                            long time = System.currentTimeMillis();\r
+                            for (ButtonInfo bi : mi.getButtonInfos())\r
+                            {\r
+                                if (!bi.down) continue;\r
+                                // FIXME : screenPos null (requires changes in MouseInfo)\r
+                                MouseButtonReleasedEvent mbre = new MouseButtonReleasedEvent(null, time, mi.mouseId, mi.buttons, bi.stateMask, bi.button, time-bi.eventTime, mi.controlPosition, null);\r
+                                getContext().getEventQueue().queueEvent(mbre);\r
+                            }\r
+                        }\r
+\r
+                    } catch( InvalidDnDOperationException idoe ) {\r
+                        ErrorLogger.defaultLogError(idoe);\r
+                    }\r
+                }});\r
+        } catch (TooManyListenersException e) {\r
+            // Should not happen\r
+            e.printStackTrace();\r
+        }\r
+        DRAG_SOURCE.addDragSourceListener(this);\r
+    }\r
+\r
+    protected Collection<IDragSourceParticipant> getDragParticipants() {\r
+        return getContext().getItemsByClass(IDragSourceParticipant.class);\r
+    }\r
+\r
+    @Override\r
+    public void dragEnter(final DragSourceDragEvent dsde) {\r
+        if (dragParticipant==null) return;\r
+        syncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (dragParticipant==null) return;\r
+\r
+//                int allowedOps = dragParticipant.getAllowedOps();\r
+//                int action = dsde.getDropAction();\r
+//                Cursor cursor = getCursor(action, allowedOps);\r
+//                dsde.getDragSourceContext().setCursor( cursor );\r
+\r
+                dragParticipant.dragEnter(dsde);\r
+            }});\r
+    }\r
+\r
+    @Override\r
+    public void dragDropEnd(final DragSourceDropEvent dsde) {\r
+        if (dragParticipant==null) return;\r
+        syncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (dragParticipant==null) return;\r
+                dragParticipant.dragDropEnd(dsde);\r
+                endDrag();\r
+            }\r
+        });\r
+    }\r
+\r
+    @Override\r
+    public void dragExit(final DragSourceEvent dse) {\r
+        if (dragParticipant==null) return;\r
+        syncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (dragParticipant==null) return;\r
+                dragParticipant.dragExit(dse);\r
+                endDrag();\r
+            }\r
+        });\r
+    }\r
+\r
+    @Override\r
+    public void dragOver(final DragSourceDragEvent dsde) {\r
+        syncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (dragParticipant==null) return;\r
+                dragParticipant.dragOver(dsde);\r
+            }\r
+        });\r
+    }\r
+\r
+    public static Cursor getCursor(int action, int allowedOps)\r
+    {\r
+        boolean allowed = (action & allowedOps) != 0;\r
+        if (action == DnDConstants.ACTION_LINK)\r
+            return allowed ? DragSource.DefaultLinkDrop : DragSource.DefaultLinkNoDrop;\r
+        else if (action == DnDConstants.ACTION_COPY)\r
+            return allowed ? DragSource.DefaultCopyDrop : DragSource.DefaultCopyNoDrop;\r
+        else if (action == DnDConstants.ACTION_MOVE)\r
+            return allowed ? DragSource.DefaultMoveDrop : DragSource.DefaultMoveNoDrop;\r
+        return DragSource.DefaultCopyNoDrop;\r
+    }\r
+\r
+    @Override\r
+    public void dropActionChanged(final DragSourceDragEvent dsde) {\r
+\r
+        if (dragParticipant==null) return;\r
+        syncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (dragParticipant==null) return;\r
+\r
+//                int allowedOps = dragParticipant.getAllowedOps();\r
+//                int action = dsde.getDropAction();\r
+//                Cursor cursor = getCursor(action, allowedOps);\r
+//                dsde.getDragSourceContext().setCursor( cursor );\r
+\r
+                dragParticipant.dropActionChanged(dsde);\r
+            }\r
+        });\r
+    }\r
+\r
+    public void endDrag()\r
+    {\r
+        Collection<DragPainter> dragPainters = getContext().getItemsByClass(DragPainter.class);\r
+        if (dragPainters.size()==0) return;\r
+        for (DragPainter dp : dragPainters)\r
+            dp.remove();\r
+        dragParticipant = null;\r
+        getContext().getContentContext().setDirty();\r
+    }\r
+\r
+}\r