]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalSelectionDragSourceListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / LocalSelectionDragSourceListener.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.ISelection;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16 import org.eclipse.swt.dnd.DragSourceEvent;
17 import org.eclipse.swt.dnd.DragSourceListener;
18 import org.eclipse.swt.dnd.DropTargetListener;
19
20 /**
21  * An SWT drag source listener that provides the specified selection provider's
22  * {@link ISelection} as the dragged data to {@link DropTargetListener}s.
23  * 
24  * <p>
25  * Only guaranteed to work with {@link LocalObjectTransfer} which will only work
26  * within a single application.
27  * 
28  * @author Tuukka Lehtonen
29  */
30 public class LocalSelectionDragSourceListener implements DragSourceListener {
31
32     ISelectionProvider selectionProvider;
33     ISelection         selection;
34
35     public LocalSelectionDragSourceListener(ISelectionProvider selectionProvider) {
36         this.selectionProvider = selectionProvider;
37     }
38
39     public ISelection getSelection() {
40         return selection;
41     }
42
43     @Override
44     public void dragFinished(DragSourceEvent event) {
45         //System.out.println("SOURCE FINISH " + event);
46         LocalObjectTransfer.getTransfer().clear();
47         selection = null;
48     }
49
50     @Override
51     public void dragSetData(DragSourceEvent event) {
52         //System.out.println("SOURCE PUT " + event + " " + selection);
53         event.data = selection;
54         LocalObjectTransfer.getTransfer().setObject(selection);
55         
56     }
57
58     @Override
59     public void dragStart(DragSourceEvent event) {
60         //System.out.println("SOURCE START " + event + " " + selectionProvider.getSelection());
61         selection = selectionProvider.getSelection();
62         if (selection == null || selection.isEmpty())
63             event.doit = false;
64         else {
65             event.doit = true;
66             LocalObjectTransfer.getTransfer().setObject(selection);
67         }
68     }
69
70 }