]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
6c0cbf61d363676fe45aeb82e50322e55a71de61
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.editor.participant;\r
2 \r
3 import java.awt.datatransfer.Transferable;\r
4 import java.awt.datatransfer.UnsupportedFlavorException;\r
5 import java.awt.dnd.DropTargetDragEvent;\r
6 import java.io.IOException;\r
7 \r
8 import org.eclipse.core.runtime.IAdaptable;\r
9 import org.eclipse.jface.viewers.IStructuredSelection;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.exception.DatabaseException;\r
13 import org.simantics.db.request.Read;\r
14 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
15 import org.simantics.diagram.ui.DiagramModelHints;\r
16 import org.simantics.g2d.dnd.IDnDContext;\r
17 import org.simantics.modeling.ModelingResources;\r
18 import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant;\r
19 import org.simantics.structural.stubs.StructuralResource2;\r
20 import org.simantics.sysdyn.SysdynResource;\r
21 import org.simantics.ui.dnd.LocalObjectTransfer;\r
22 import org.simantics.ui.dnd.LocalObjectTransferable;\r
23 \r
24 public class SysdynPopulateElementDropParticipant extends PopulateElementDropParticipant {\r
25 \r
26         public SysdynPopulateElementDropParticipant(GraphToDiagramSynchronizer synchronizer) {\r
27                 super(synchronizer);\r
28         }\r
29 \r
30     @Override\r
31     public void dragEnter(DropTargetDragEvent dtde, IDnDContext dp) {\r
32         Transferable tr = dtde.getTransferable();\r
33         if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {\r
34             // This must be done to have SWT transfer set the source data\r
35             Object obj = null;\r
36             try {\r
37                 obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);\r
38             } catch (UnsupportedFlavorException e) {\r
39                 e.printStackTrace();\r
40             } catch (IOException e) {\r
41                 e.printStackTrace();\r
42             }\r
43             \r
44             // Check SWT\r
45             if (!(obj instanceof IStructuredSelection)) {\r
46                 obj = LocalObjectTransfer.getTransfer().getObject();\r
47             }\r
48             \r
49             if (obj instanceof IStructuredSelection) {\r
50                 final IStructuredSelection sel = (IStructuredSelection) obj;\r
51                 if (!sel.isEmpty()) {\r
52                         \r
53                         boolean allowed = true;\r
54                         try {\r
55                                 allowed = synchronizer.getSession().syncRequest(new Read<Boolean>(){\r
56 \r
57                                                         @Override\r
58                                                         public Boolean perform(ReadGraph graph)\r
59                                                                         throws DatabaseException {\r
60                                             for (Object elm : sel.toList()) {\r
61                                                 if (elm instanceof IAdaptable) {\r
62                                                         Object r = ((IAdaptable) elm).getAdapter(Resource.class);\r
63                                                         if(r != null && r instanceof Resource) {\r
64                                                                 Resource resource = (Resource)r;\r
65                                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
66                                                                 if(graph.isInheritedFrom(resource, sr.ModuleSymbol)) {\r
67                                                                         Resource module = graph.getSingleObject(resource, ModelingResources.getInstance(graph).SymbolToComponentType);\r
68                                                                         Resource configuration = graph.getSingleObject(module, StructuralResource2.getInstance(graph).IsDefinedBy);\r
69                                                                         Resource dia = graph.getSingleObject(configuration, ModelingResources.getInstance(graph).CompositeToDiagram);\r
70                                                                         Resource editorDia = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
71                                                                         if(dia.equals(editorDia))\r
72                                                                                 return false;\r
73                                                                 }\r
74                                                         }\r
75                                                 }\r
76                                             }\r
77                                                                 return true;\r
78                                                         }\r
79                                                         \r
80                                                 });\r
81                                         } catch (DatabaseException e) {\r
82                                                 e.printStackTrace();\r
83                                         }\r
84                                         \r
85                                         // Trying to populate an instance of self to diagram -> infinite recursion\r
86                                         // This limitation should be removed by allowing differentation (copies) from modules. i.e. detaching from class and creating a new module type\r
87                                         if(!allowed)\r
88                                                 return;\r
89                 }\r
90             }\r
91         }\r
92         super.dragEnter(dtde, dp);\r
93     }\r
94 }\r