]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.dnd;\r
13 \r
14 import org.eclipse.jface.viewers.ISelectionProvider;\r
15 import org.eclipse.jface.viewers.IStructuredSelection;\r
16 import org.eclipse.swt.dnd.DND;\r
17 import org.eclipse.swt.dnd.DragSource;\r
18 import org.eclipse.swt.dnd.DragSourceEvent;\r
19 import org.eclipse.swt.dnd.DragSourceListener;\r
20 import org.eclipse.swt.dnd.TextTransfer;\r
21 import org.eclipse.swt.dnd.Transfer;\r
22 import org.eclipse.swt.widgets.Control;\r
23 import org.simantics.db.Session;\r
24 import org.simantics.db.common.ResourceArray;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
27 \r
28 /**\r
29  * A basic combined implementation of an SWT {@link DragSource} and\r
30  * {@link DragSourceListener} that supports the following transfers:\r
31  * <ul>\r
32  * <li>{@link TextTransfer}</li>\r
33  * <li>{@link ResourceReferenceTransfer}</li>\r
34  * <li>{@link LocalObjectTransfer}</li>\r
35  * </ul>\r
36  * \r
37  * <p>\r
38  * Initialization requires the SWT {@link Control} which the drag source is\r
39  * attached, an {@link ISelectionProvider} for the control and a database\r
40  * session.\r
41  * \r
42  * @author Tuukka Lehtonen\r
43  */\r
44 public class BasicDragSource implements DragSourceListener, SessionContainer {\r
45 \r
46     private Transfer[]           transferAgents;\r
47 \r
48     private ISelectionProvider   selectionProvider;\r
49 \r
50     private IStructuredSelection sel;\r
51 \r
52     private ResourceArray[]      resources;\r
53 \r
54     private Session              session;\r
55 \r
56     private String               purpose;\r
57 \r
58     /**\r
59      * @param selectionProvider\r
60      * @param sourceControl\r
61      * @param session the database Session to be used for serialization by this\r
62      *        drag source or <code>null</code> to put this drag source in\r
63      *        disabled state at construction time. To later enable this drag\r
64      *        source, use {@link #setSession(Session)} to set the resource\r
65      *        serializer.\r
66      */\r
67     public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session) {\r
68         this(selectionProvider, sourceControl, session, null);\r
69     }\r
70 \r
71     /**\r
72      * @param selectionProvider\r
73      * @param sourceControl\r
74      * @param session the database Session to be used for serialization by this\r
75      *        drag source or <code>null</code> to put this drag source in\r
76      *        disabled state at construction time. To later enable this drag\r
77      *        source, use {@link #setSession(Session)} to set the resource\r
78      *        serializer.\r
79      * @param purpose a string for defining a designation of the purpose of this\r
80      *        drag source. <code>null</code> indicates no special purpose.\r
81      */\r
82     public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session, String purpose) {\r
83         this.selectionProvider = selectionProvider;\r
84         this.transferAgents = new Transfer[] { \r
85                 TextTransfer.getInstance(), \r
86                 ResourceReferenceTransfer.createInstance(purpose),\r
87                 LocalObjectTransfer.getTransfer()\r
88         };\r
89         this.session = session;\r
90         this.purpose = purpose;\r
91         DragSource source = new DragSource(sourceControl, DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);\r
92         source.setTransfer(transferAgents);\r
93         source.addDragListener(this);\r
94 \r
95         // NOTE: This will disable SWT DND selection screenshot painting.\r
96         source.setDragSourceEffect(new NoImageDragSourceEffect(sourceControl));\r
97     }\r
98 \r
99     @Override\r
100     public Session getSession() {\r
101         return session;\r
102     }\r
103 \r
104     @Override\r
105     public void setSession(Session session) {\r
106         this.session = session;\r
107     }\r
108 \r
109     public void dragStart(DragSourceEvent event) {\r
110         // Don't start dragging by default.\r
111         event.doit = false;\r
112 \r
113         // Drag won't work without a database session.\r
114         if (session == null)\r
115             return;\r
116 \r
117         // Allow drag to start only if the selection is non-empty.\r
118         sel = (IStructuredSelection) selectionProvider.getSelection();\r
119         if (sel == null || sel.isEmpty())\r
120             return;\r
121 \r
122         resources = ResourceAdaptionUtils.toResourceArrays(sel);\r
123 \r
124         event.doit = resources.length > 0; \r
125     }\r
126 \r
127     public void dragSetData(DragSourceEvent event) {\r
128         if (ResourceReferenceTransfer.getInstance().isSupportedType(event.dataType)) {\r
129             event.data = resources;  \r
130         } else if (LocalObjectTransfer.getTransfer().isSupportedType(event.dataType)) {\r
131             event.data = sel;\r
132         } else if (TextTransfer.getInstance().isSupportedType(event.dataType)) {\r
133             try {\r
134                 event.data = ResourceTransferUtils.createStringTransferable(session, resources, purpose);\r
135             } catch (DatabaseException e) {\r
136                 event.doit = false;\r
137                 e.printStackTrace();\r
138             }\r
139         }\r
140     }\r
141 \r
142     public void dragFinished(DragSourceEvent event) {\r
143         // release resources\r
144         sel = null;\r
145         resources = null;\r
146     }\r
147 \r
148 }\r