1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.dnd;
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;
29 * A basic combined implementation of an SWT {@link DragSource} and
30 * {@link DragSourceListener} that supports the following transfers:
32 * <li>{@link TextTransfer}</li>
33 * <li>{@link ResourceReferenceTransfer}</li>
34 * <li>{@link LocalObjectTransfer}</li>
38 * Initialization requires the SWT {@link Control} which the drag source is
39 * attached, an {@link ISelectionProvider} for the control and a database
42 * @author Tuukka Lehtonen
44 public class BasicDragSource implements DragSourceListener, SessionContainer {
46 private Transfer[] transferAgents;
48 private ISelectionProvider selectionProvider;
50 private IStructuredSelection sel;
52 private ResourceArray[] resources;
54 private Session session;
56 private String purpose;
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
67 public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session) {
68 this(selectionProvider, sourceControl, session, null);
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
79 * @param purpose a string for defining a designation of the purpose of this
80 * drag source. <code>null</code> indicates no special purpose.
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()
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);
95 // NOTE: This will disable SWT DND selection screenshot painting.
96 source.setDragSourceEffect(new NoImageDragSourceEffect(sourceControl));
100 public Session getSession() {
105 public void setSession(Session session) {
106 this.session = session;
109 public void dragStart(DragSourceEvent event) {
110 // Don't start dragging by default.
113 // Drag won't work without a database session.
117 // Allow drag to start only if the selection is non-empty.
118 sel = (IStructuredSelection) selectionProvider.getSelection();
119 if (sel == null || sel.isEmpty())
122 resources = ResourceAdaptionUtils.toResourceArrays(sel);
124 event.doit = resources.length > 0;
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)) {
132 } else if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
134 event.data = ResourceTransferUtils.createStringTransferable(session, resources, purpose);
135 } catch (DatabaseException e) {
142 public void dragFinished(DragSourceEvent event) {