1 | package de.uka.ipd.sdq.sensorframework.visualisation.views; |
2 | |
3 | import org.eclipse.jface.util.LocalSelectionTransfer; |
4 | import org.eclipse.jface.viewers.IStructuredSelection; |
5 | import org.eclipse.jface.viewers.TreeViewer; |
6 | import org.eclipse.swt.dnd.DragSourceEvent; |
7 | import org.eclipse.swt.dnd.DragSourceListener; |
8 | import org.eclipse.ui.part.EditorInputTransfer; |
9 | |
10 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
11 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
12 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.ViewAndAdapterFactory; |
13 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.ConfigEditorInput; |
14 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.ConfigEntry; |
15 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.SensorValidationToView; |
16 | |
17 | /** |
18 | * @author admin |
19 | */ |
20 | public class TreeDragSourceListener implements DragSourceListener { |
21 | |
22 | TreeViewer viewer; |
23 | |
24 | public TreeDragSourceListener(TreeViewer viewer) { |
25 | this.viewer = viewer; |
26 | } |
27 | |
28 | /* (non-Javadoc) |
29 | * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData(org.eclipse.swt.dnd.DragSourceEvent) |
30 | */ |
31 | public void dragSetData(DragSourceEvent event) { |
32 | IStructuredSelection selection = (IStructuredSelection) viewer |
33 | .getSelection(); |
34 | Object object = selection.getFirstElement(); |
35 | Object[] viewers = null; |
36 | |
37 | if (EditorInputTransfer.getInstance().isSupportedType(event.dataType)) { |
38 | TreeObject treeObject = (TreeObject) object; |
39 | Object innerObject = treeObject.getObject(); |
40 | |
41 | /** Sensor */ |
42 | if (innerObject instanceof Sensor) { |
43 | Sensor sensor = (Sensor) innerObject; |
44 | /** return all view, which can represent the sensor */ |
45 | viewers = SensorValidationToView.findViews(treeObject.getRun() |
46 | .getMeasurementsOfSensor(sensor)); |
47 | } |
48 | /** Experiment Run */ |
49 | if (innerObject instanceof ExperimentRun) { |
50 | /** return all view */ |
51 | viewers = SensorValidationToView.getConfigurationElements(); |
52 | } |
53 | |
54 | ViewAndAdapterFactory selectedView = SensorValidationToView.getSelectedAction(event.display |
55 | .getActiveShell(), viewers); |
56 | if (selectedView != null) { |
57 | |
58 | ConfigEditorInput editorInput = new ConfigEditorInput(selectedView.getFactory().getAdapterFactoryID()); |
59 | /** Sensor */ |
60 | if (innerObject instanceof Sensor) { |
61 | Sensor sensor = (Sensor) innerObject; |
62 | ConfigEntry configEntry = new ConfigEntry( |
63 | treeObject.getDatasource(), |
64 | treeObject.getRun(), |
65 | treeObject.getExperiment(), sensor); |
66 | editorInput.addConfigEntry(configEntry); |
67 | } |
68 | /** Experiment Run */ |
69 | if (innerObject instanceof ExperimentRun) { |
70 | ExperimentRun run = (ExperimentRun) innerObject; |
71 | ConfigEntry configEntry = new ConfigEntry(treeObject.getDatasource(), |
72 | run, treeObject |
73 | .getExperiment(), null); |
74 | editorInput.addConfigEntry(configEntry); |
75 | } |
76 | EditorInputTransfer.EditorInputData[] transferArray = new EditorInputTransfer.EditorInputData[] { EditorInputTransfer |
77 | .createEditorInputData(selectedView.getView().getAttribute("editorID"), |
78 | editorInput) }; |
79 | event.data = transferArray; |
80 | } else { |
81 | event.data = null; |
82 | event.doit = false; |
83 | } |
84 | } else if (LocalSelectionTransfer.getTransfer().isSupportedType( |
85 | event.dataType)) { |
86 | LocalSelectionTransfer.getTransfer().setSelection(selection); |
87 | } |
88 | } |
89 | |
90 | /* (non-Javadoc) |
91 | * @see org.eclipse.swt.dnd.DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent) |
92 | */ |
93 | public void dragStart(DragSourceEvent event) { |
94 | IStructuredSelection selection = (IStructuredSelection) viewer |
95 | .getSelection(); |
96 | Object object = selection.getFirstElement(); |
97 | |
98 | event.doit = false; |
99 | |
100 | if (object instanceof TreeObject) { |
101 | event.doit = true; |
102 | // if the sensor is not a child from ExperimentRun |
103 | TreeObject treeObject = (TreeObject) object; |
104 | ExperimentRun run = treeObject.getRun(); |
105 | if (treeObject.getObject() instanceof Sensor && run == null) |
106 | event.doit = false; |
107 | } |
108 | } |
109 | |
110 | public void dragFinished(DragSourceEvent event) { |
111 | } |
112 | } |