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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.dnd;
\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
29 * A basic combined implementation of an SWT {@link DragSource} and
\r
30 * {@link DragSourceListener} that supports the following transfers:
\r
32 * <li>{@link TextTransfer}</li>
\r
33 * <li>{@link ResourceReferenceTransfer}</li>
\r
34 * <li>{@link LocalObjectTransfer}</li>
\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
42 * @author Tuukka Lehtonen
\r
44 public class BasicDragSource implements DragSourceListener, SessionContainer {
\r
46 private Transfer[] transferAgents;
\r
48 private ISelectionProvider selectionProvider;
\r
50 private IStructuredSelection sel;
\r
52 private ResourceArray[] resources;
\r
54 private Session session;
\r
56 private String purpose;
\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
67 public BasicDragSource(ISelectionProvider selectionProvider, Control sourceControl, Session session) {
\r
68 this(selectionProvider, sourceControl, session, null);
\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
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
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
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
95 // NOTE: This will disable SWT DND selection screenshot painting.
\r
96 source.setDragSourceEffect(new NoImageDragSourceEffect(sourceControl));
\r
100 public Session getSession() {
\r
105 public void setSession(Session session) {
\r
106 this.session = session;
\r
109 public void dragStart(DragSourceEvent event) {
\r
110 // Don't start dragging by default.
\r
111 event.doit = false;
\r
113 // Drag won't work without a database session.
\r
114 if (session == null)
\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
122 resources = ResourceAdaptionUtils.toResourceArrays(sel);
\r
124 event.doit = resources.length > 0;
\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
132 } else if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
\r
134 event.data = ResourceTransferUtils.createStringTransferable(session, resources, purpose);
\r
135 } catch (DatabaseException e) {
\r
136 event.doit = false;
\r
137 e.printStackTrace();
\r
142 public void dragFinished(DragSourceEvent event) {
\r
143 // release resources
\r