From: lempinen Date: Tue, 12 Oct 2010 09:44:30 +0000 (+0000) Subject: Disabled populating modules to themselves thus disabling infinite recursion X-Git-Tag: simantics-1.2.1~31 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=e7d7e6cc1153f08c3c91c8936cce5cfaecff34f3;p=simantics%2Fsysdyn.git Disabled populating modules to themselves thus disabling infinite recursion git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18383 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java index 8c0eadff..de5af993 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java @@ -1,5 +1,6 @@ package org.simantics.sysdyn.ui.editor; +import java.awt.dnd.DnDConstants; import java.util.ArrayList; import java.util.Collections; import java.util.Set; @@ -19,6 +20,7 @@ import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; import org.simantics.diagram.synchronization.SynchronizationHints; import org.simantics.diagram.ui.DiagramModelHints; import org.simantics.diagram.ui.WorkbenchSelectionProvider; +import org.simantics.g2d.canvas.Hints; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.canvas.impl.CanvasContext; import org.simantics.g2d.connection.IConnectionAdvisor; @@ -34,11 +36,13 @@ import org.simantics.g2d.element.IElementClassProvider; import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; import org.simantics.modeling.mapping.ElementCopyAdvisor; import org.simantics.modeling.mapping.MappedElementCopyAdvisor; +import org.simantics.modeling.ui.diagramEditor.PopulateElementMonitorDropParticipant; import org.simantics.modeling.ui.diagramEditor.handlers.LinkBrowsingHandler; import org.simantics.structural2.modelingRules.IModelingRules; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.editor.participant.CreateVariablesShortcutParticipant; import org.simantics.sysdyn.ui.editor.participant.SysdynComponentCopyAdvisor; +import org.simantics.sysdyn.ui.editor.participant.SysdynPopulateElementDropParticipant; import org.simantics.sysdyn.ui.elements2.CloudFactory; import org.simantics.sysdyn.ui.elements2.SysdynElementFactory; import org.simantics.sysdyn.ui.elements2.connections.ConnectionClasses; @@ -153,6 +157,13 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext)); } + + protected void addDropParticipants(ICanvasContext ctx) { + ctx.getDefaultHintContext().setHint(Hints.KEY_ALLOWED_DRAG_ACTIONS, DnDConstants.ACTION_COPY); + + ctx.add(new SysdynPopulateElementDropParticipant(synchronizer)); + ctx.add(new PopulateElementMonitorDropParticipant(synchronizer, 0.5, 0.5)); + } @Override protected void addOtherParticipants(CanvasContext ctx) { 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 index 00000000..6c0cbf61 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPopulateElementDropParticipant.java @@ -0,0 +1,94 @@ +package org.simantics.sysdyn.ui.editor.participant; + +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.dnd.DropTargetDragEvent; +import java.io.IOException; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.diagram.adapter.GraphToDiagramSynchronizer; +import org.simantics.diagram.ui.DiagramModelHints; +import org.simantics.g2d.dnd.IDnDContext; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.dnd.LocalObjectTransfer; +import org.simantics.ui.dnd.LocalObjectTransferable; + +public class SysdynPopulateElementDropParticipant extends PopulateElementDropParticipant { + + public SysdynPopulateElementDropParticipant(GraphToDiagramSynchronizer synchronizer) { + super(synchronizer); + } + + @Override + public void dragEnter(DropTargetDragEvent dtde, IDnDContext dp) { + Transferable tr = dtde.getTransferable(); + if (tr.isDataFlavorSupported(LocalObjectTransferable.FLAVOR)) { + // This must be done to have SWT transfer set the source data + Object obj = null; + try { + obj = tr.getTransferData(LocalObjectTransferable.FLAVOR); + } catch (UnsupportedFlavorException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + // Check SWT + if (!(obj instanceof IStructuredSelection)) { + obj = LocalObjectTransfer.getTransfer().getObject(); + } + + if (obj instanceof IStructuredSelection) { + final IStructuredSelection sel = (IStructuredSelection) obj; + if (!sel.isEmpty()) { + + boolean allowed = true; + try { + allowed = synchronizer.getSession().syncRequest(new Read(){ + + @Override + public Boolean perform(ReadGraph graph) + throws DatabaseException { + for (Object elm : sel.toList()) { + if (elm instanceof IAdaptable) { + Object r = ((IAdaptable) elm).getAdapter(Resource.class); + if(r != null && r instanceof Resource) { + Resource resource = (Resource)r; + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.isInheritedFrom(resource, sr.ModuleSymbol)) { + Resource module = graph.getSingleObject(resource, ModelingResources.getInstance(graph).SymbolToComponentType); + Resource configuration = graph.getSingleObject(module, StructuralResource2.getInstance(graph).IsDefinedBy); + Resource dia = graph.getSingleObject(configuration, ModelingResources.getInstance(graph).CompositeToDiagram); + Resource editorDia = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE); + if(dia.equals(editorDia)) + return false; + } + } + } + } + return true; + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + // Trying to populate an instance of self to diagram -> infinite recursion + // This limitation should be removed by allowing differentation (copies) from modules. i.e. detaching from class and creating a new module type + if(!allowed) + return; + } + } + } + super.dragEnter(dtde, dp); + } +}