package org.simantics.sysdyn.ui.editor;\r
\r
+import java.awt.dnd.DnDConstants;\r
import java.util.ArrayList;\r
import java.util.Collections;\r
import java.util.Set;\r
import org.simantics.diagram.synchronization.SynchronizationHints;\r
import org.simantics.diagram.ui.DiagramModelHints;\r
import org.simantics.diagram.ui.WorkbenchSelectionProvider;\r
+import org.simantics.g2d.canvas.Hints;\r
import org.simantics.g2d.canvas.ICanvasContext;\r
import org.simantics.g2d.canvas.impl.CanvasContext;\r
import org.simantics.g2d.connection.IConnectionAdvisor;\r
import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
import org.simantics.modeling.mapping.ElementCopyAdvisor;\r
import org.simantics.modeling.mapping.MappedElementCopyAdvisor;\r
+import org.simantics.modeling.ui.diagramEditor.PopulateElementMonitorDropParticipant;\r
import org.simantics.modeling.ui.diagramEditor.handlers.LinkBrowsingHandler;\r
import org.simantics.structural2.modelingRules.IModelingRules;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.ui.editor.participant.CreateVariablesShortcutParticipant;\r
import org.simantics.sysdyn.ui.editor.participant.SysdynComponentCopyAdvisor;\r
+import org.simantics.sysdyn.ui.editor.participant.SysdynPopulateElementDropParticipant;\r
import org.simantics.sysdyn.ui.elements2.CloudFactory;\r
import org.simantics.sysdyn.ui.elements2.SysdynElementFactory;\r
import org.simantics.sysdyn.ui.elements2.connections.ConnectionClasses;\r
ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext));\r
\r
}\r
+ \r
+ protected void addDropParticipants(ICanvasContext ctx) {\r
+ ctx.getDefaultHintContext().setHint(Hints.KEY_ALLOWED_DRAG_ACTIONS, DnDConstants.ACTION_COPY);\r
+\r
+ ctx.add(new SysdynPopulateElementDropParticipant(synchronizer));\r
+ ctx.add(new PopulateElementMonitorDropParticipant(synchronizer, 0.5, 0.5));\r
+ }\r
\r
@Override\r
protected void addOtherParticipants(CanvasContext ctx) {\r
--- /dev/null
+package org.simantics.sysdyn.ui.editor.participant;\r
+\r
+import java.awt.datatransfer.Transferable;\r
+import java.awt.datatransfer.UnsupportedFlavorException;\r
+import java.awt.dnd.DropTargetDragEvent;\r
+import java.io.IOException;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
+import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.g2d.dnd.IDnDContext;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.dnd.LocalObjectTransfer;\r
+import org.simantics.ui.dnd.LocalObjectTransferable;\r
+\r
+public class SysdynPopulateElementDropParticipant extends PopulateElementDropParticipant {\r
+\r
+ public SysdynPopulateElementDropParticipant(GraphToDiagramSynchronizer synchronizer) {\r
+ super(synchronizer);\r
+ }\r
+\r
+ @Override\r
+ public void dragEnter(DropTargetDragEvent dtde, IDnDContext dp) {\r
+ Transferable tr = dtde.getTransferable();\r
+ if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) {\r
+ // This must be done to have SWT transfer set the source data\r
+ Object obj = null;\r
+ try {\r
+ obj = tr.getTransferData(LocalObjectTransferable.FLAVOR);\r
+ } catch (UnsupportedFlavorException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ // Check SWT\r
+ if (!(obj instanceof IStructuredSelection)) {\r
+ obj = LocalObjectTransfer.getTransfer().getObject();\r
+ }\r
+ \r
+ if (obj instanceof IStructuredSelection) {\r
+ final IStructuredSelection sel = (IStructuredSelection) obj;\r
+ if (!sel.isEmpty()) {\r
+ \r
+ boolean allowed = true;\r
+ try {\r
+ allowed = synchronizer.getSession().syncRequest(new Read<Boolean>(){\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ for (Object elm : sel.toList()) {\r
+ if (elm instanceof IAdaptable) {\r
+ Object r = ((IAdaptable) elm).getAdapter(Resource.class);\r
+ if(r != null && r instanceof Resource) {\r
+ Resource resource = (Resource)r;\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(graph.isInheritedFrom(resource, sr.ModuleSymbol)) {\r
+ Resource module = graph.getSingleObject(resource, ModelingResources.getInstance(graph).SymbolToComponentType);\r
+ Resource configuration = graph.getSingleObject(module, StructuralResource2.getInstance(graph).IsDefinedBy);\r
+ Resource dia = graph.getSingleObject(configuration, ModelingResources.getInstance(graph).CompositeToDiagram);\r
+ Resource editorDia = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
+ if(dia.equals(editorDia))\r
+ return false;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+ \r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ // Trying to populate an instance of self to diagram -> infinite recursion\r
+ // This limitation should be removed by allowing differentation (copies) from modules. i.e. detaching from class and creating a new module type\r
+ if(!allowed)\r
+ return;\r
+ }\r
+ }\r
+ }\r
+ super.dragEnter(dtde, dp);\r
+ }\r
+}\r