]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/dnd/BasicDragSource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / BasicDragSource.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/dnd/BasicDragSource.java b/bundles/org.simantics.ui/src/org/simantics/ui/dnd/BasicDragSource.java
new file mode 100644 (file)
index 0000000..056a274
--- /dev/null
@@ -0,0 +1,148 @@
+/*******************************************************************************\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.ui.dnd;\r
+\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.swt.dnd.DND;\r
+import org.eclipse.swt.dnd.DragSource;\r
+import org.eclipse.swt.dnd.DragSourceEvent;\r
+import org.eclipse.swt.dnd.DragSourceListener;\r
+import org.eclipse.swt.dnd.TextTransfer;\r
+import org.eclipse.swt.dnd.Transfer;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+/**\r
+ * A basic combined implementation of an SWT {@link DragSource} and\r
+ * {@link DragSourceListener} that supports the following transfers:\r
+ * <ul>\r
+ * <li>{@link TextTransfer}</li>\r
+ * <li>{@link ResourceReferenceTransfer}</li>\r
+ * <li>{@link LocalObjectTransfer}</li>\r
+ * </ul>\r
+ * \r
+ * <p>\r
+ * Initialization requires the SWT {@link Control} which the drag source is\r
+ * attached, an {@link ISelectionProvider} for the control and a database\r
+ * session.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class BasicDragSource implements DragSourceListener, SessionContainer {\r
+\r
+    private Transfer[]           transferAgents;\r
+\r
+    private ISelectionProvider   selectionProvider;\r
+\r
+    private IStructuredSelection sel;\r
+\r
+    private ResourceArray[]      resources;\r
+\r
+    private Session              session;\r
+\r
+    private String               purpose;\r
+\r
+    /**\r
+     * @param selectionProvider\r
+     * @param sourceControl\r
+     * @param session the database Session to be used for serialization by this\r
+     *        drag source or <code>null</code> to put this drag source in\r
+     *        disabled state at construction time. To later enable this drag\r
+     *        source, use {@link #setSession(Session)} to set the resource\r
+     *        serializer.\r
+     */\r
+    public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session) {\r
+        this(selectionProvider, sourceControl, session, null);\r
+    }\r
+\r
+    /**\r
+     * @param selectionProvider\r
+     * @param sourceControl\r
+     * @param session the database Session to be used for serialization by this\r
+     *        drag source or <code>null</code> to put this drag source in\r
+     *        disabled state at construction time. To later enable this drag\r
+     *        source, use {@link #setSession(Session)} to set the resource\r
+     *        serializer.\r
+     * @param purpose a string for defining a designation of the purpose of this\r
+     *        drag source. <code>null</code> indicates no special purpose.\r
+     */\r
+    public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session, String purpose) {\r
+        this.selectionProvider = selectionProvider;\r
+        this.transferAgents = new Transfer[] { \r
+                TextTransfer.getInstance(), \r
+                ResourceReferenceTransfer.createInstance(purpose),\r
+                LocalObjectTransfer.getTransfer()\r
+        };\r
+        this.session = session;\r
+        this.purpose = purpose;\r
+        DragSource source = new DragSource(sourceControl, DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);\r
+        source.setTransfer(transferAgents);\r
+        source.addDragListener(this);\r
+\r
+        // NOTE: This will disable SWT DND selection screenshot painting.\r
+        source.setDragSourceEffect(new NoImageDragSourceEffect(sourceControl));\r
+    }\r
+\r
+    @Override\r
+    public Session getSession() {\r
+        return session;\r
+    }\r
+\r
+    @Override\r
+    public void setSession(Session session) {\r
+        this.session = session;\r
+    }\r
+\r
+    public void dragStart(DragSourceEvent event) {\r
+        // Don't start dragging by default.\r
+        event.doit = false;\r
+\r
+        // Drag won't work without a database session.\r
+        if (session == null)\r
+            return;\r
+\r
+        // Allow drag to start only if the selection is non-empty.\r
+        sel = (IStructuredSelection) selectionProvider.getSelection();\r
+        if (sel == null || sel.isEmpty())\r
+            return;\r
+\r
+        resources = ResourceAdaptionUtils.toResourceArrays(sel);\r
+\r
+        event.doit = resources.length > 0; \r
+    }\r
+\r
+    public void dragSetData(DragSourceEvent event) {\r
+        if (ResourceReferenceTransfer.getInstance().isSupportedType(event.dataType)) {\r
+            event.data = resources;  \r
+        } else if (LocalObjectTransfer.getTransfer().isSupportedType(event.dataType)) {\r
+            event.data = sel;\r
+        } else if (TextTransfer.getInstance().isSupportedType(event.dataType)) {\r
+            try {\r
+                event.data = ResourceTransferUtils.createStringTransferable(session, resources, purpose);\r
+            } catch (DatabaseException e) {\r
+                event.doit = false;\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+\r
+    public void dragFinished(DragSourceEvent event) {\r
+        // release resources\r
+        sel = null;\r
+        resources = null;\r
+    }\r
+\r
+}\r