]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/dnd/BasicDragSource.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / BasicDragSource.java
index 056a2749b5a01788176aa4606be9c21112c02c4f..e3c735388ee2d7666e6f9d88948433c0caa737ca 100644 (file)
-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.dnd;
+
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DragSource;
+import org.eclipse.swt.dnd.DragSourceEvent;
+import org.eclipse.swt.dnd.DragSourceListener;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.widgets.Control;
+import org.simantics.db.Session;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+
+/**
+ * A basic combined implementation of an SWT {@link DragSource} and
+ * {@link DragSourceListener} that supports the following transfers:
+ * <ul>
+ * <li>{@link TextTransfer}</li>
+ * <li>{@link ResourceReferenceTransfer}</li>
+ * <li>{@link LocalObjectTransfer}</li>
+ * </ul>
+ * 
+ * <p>
+ * Initialization requires the SWT {@link Control} which the drag source is
+ * attached, an {@link ISelectionProvider} for the control and a database
+ * session.
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class BasicDragSource implements DragSourceListener, SessionContainer {
+
+    private Transfer[]           transferAgents;
+
+    private ISelectionProvider   selectionProvider;
+
+    private IStructuredSelection sel;
+
+    private ResourceArray[]      resources;
+
+    private Session              session;
+
+    private String               purpose;
+
+    /**
+     * @param selectionProvider
+     * @param sourceControl
+     * @param session the database Session to be used for serialization by this
+     *        drag source or <code>null</code> to put this drag source in
+     *        disabled state at construction time. To later enable this drag
+     *        source, use {@link #setSession(Session)} to set the resource
+     *        serializer.
+     */
+    public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session) {
+        this(selectionProvider, sourceControl, session, null);
+    }
+
+    /**
+     * @param selectionProvider
+     * @param sourceControl
+     * @param session the database Session to be used for serialization by this
+     *        drag source or <code>null</code> to put this drag source in
+     *        disabled state at construction time. To later enable this drag
+     *        source, use {@link #setSession(Session)} to set the resource
+     *        serializer.
+     * @param purpose a string for defining a designation of the purpose of this
+     *        drag source. <code>null</code> indicates no special purpose.
+     */
+    public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session, String purpose) {
+        this.selectionProvider = selectionProvider;
+        this.transferAgents = new Transfer[] { 
+                TextTransfer.getInstance(), 
+                ResourceReferenceTransfer.createInstance(purpose),
+                LocalObjectTransfer.getTransfer()
+        };
+        this.session = session;
+        this.purpose = purpose;
+        DragSource source = new DragSource(sourceControl, DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
+        source.setTransfer(transferAgents);
+        source.addDragListener(this);
+
+        // NOTE: This will disable SWT DND selection screenshot painting.
+        source.setDragSourceEffect(new NoImageDragSourceEffect(sourceControl));
+    }
+
+    @Override
+    public Session getSession() {
+        return session;
+    }
+
+    @Override
+    public void setSession(Session session) {
+        this.session = session;
+    }
+
+    public void dragStart(DragSourceEvent event) {
+        // Don't start dragging by default.
+        event.doit = false;
+
+        // Drag won't work without a database session.
+        if (session == null)
+            return;
+
+        // Allow drag to start only if the selection is non-empty.
+        sel = (IStructuredSelection) selectionProvider.getSelection();
+        if (sel == null || sel.isEmpty())
+            return;
+
+        resources = ResourceAdaptionUtils.toResourceArrays(sel);
+
+        event.doit = resources.length > 0; 
+    }
+
+    public void dragSetData(DragSourceEvent event) {
+        if (ResourceReferenceTransfer.getInstance().isSupportedType(event.dataType)) {
+            event.data = resources;  
+        } else if (LocalObjectTransfer.getTransfer().isSupportedType(event.dataType)) {
+            event.data = sel;
+        } else if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
+            try {
+                event.data = ResourceTransferUtils.createStringTransferable(session, resources, purpose);
+            } catch (DatabaseException e) {
+                event.doit = false;
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void dragFinished(DragSourceEvent event) {
+        // release resources
+        sel = null;
+        resources = null;
+    }
+
+}