1 package org.simantics.sysdyn.ui.editor.participant;
\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
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
24 public class SysdynPopulateElementDropParticipant extends PopulateElementDropParticipant {
\r
26 public SysdynPopulateElementDropParticipant(GraphToDiagramSynchronizer synchronizer) {
\r
27 super(synchronizer);
\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
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
45 if (!(obj instanceof IStructuredSelection)) {
\r
46 obj = LocalObjectTransfer.getTransfer().getObject();
\r
49 if (obj instanceof IStructuredSelection) {
\r
50 final IStructuredSelection sel = (IStructuredSelection) obj;
\r
51 if (!sel.isEmpty()) {
\r
53 boolean allowed = true;
\r
55 allowed = synchronizer.getSession().syncRequest(new Read<Boolean>(){
\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
81 } catch (DatabaseException e) {
\r
82 e.printStackTrace();
\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
92 super.dragEnter(dtde, dp);
\r