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