]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Disabled populating modules to themselves thus disabling infinite recursion
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Oct 2010 09:44:30 +0000 (09:44 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Oct 2010 09:44:30 +0000 (09:44 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18383 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPopulateElementDropParticipant.java [new file with mode: 0644]

index 8c0eadff2129634a54720dc462935a5e19a89791..de5af9932e84158b46ee4a712722f3432e3807cc 100644 (file)
@@ -1,5 +1,6 @@
 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
@@ -19,6 +20,7 @@ import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
 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
@@ -34,11 +36,13 @@ import org.simantics.g2d.element.IElementClassProvider;
 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
@@ -153,6 +157,13 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr
                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
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPopulateElementDropParticipant.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPopulateElementDropParticipant.java
new file mode 100644 (file)
index 0000000..6c0cbf6
--- /dev/null
@@ -0,0 +1,94 @@
+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