From: lempinen Date: Tue, 12 Oct 2010 10:15:36 +0000 (+0000) Subject: A better solution to prevent directly instantiating a module to itself X-Git-Tag: simantics-1.2.1~29 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2bf3b2de5a37411cd66f4943596c0ff9a289d0f3;p=simantics%2Fsysdyn.git A better solution to prevent directly instantiating a module to itself git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18385 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 index 6c0cbf61..5c434c2c 100644 --- 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 @@ -1,25 +1,22 @@ 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 java.util.ArrayList; +import java.util.Collection; -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.ElementClassDragItem; import org.simantics.g2d.dnd.IDnDContext; +import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; 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 { @@ -29,66 +26,46 @@ public class SysdynPopulateElementDropParticipant extends PopulateElementDropPar @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(){ + super.dragEnter(dtde, dp); + + // Check that user is not trying to populate a module to itself. + // This doesn't prevent infinite recursion if there is one module between two exactly the same modules. + // e.g. WorkModule -> WokrforceModule -> WorkModule creates an infinite recursion + final Collection items = dp.getItemsByClass(ElementClassDragItem.class); + if(!items.isEmpty()) { + Collection unvalidModules = null; + try { + unvalidModules = 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(); + @Override + public Collection perform(ReadGraph graph) + throws DatabaseException { + Collection unvalidModules = new ArrayList(); + for(ElementClassDragItem item : items) { + StaticObjectAdapter soa = item.getElementClass().getSingleItem(StaticObjectAdapter.class); + Resource type = soa.adapt(Resource.class); + + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.isInheritedFrom(type, sr.ModuleSymbol)) { + Resource module = graph.getSingleObject(type, 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)) + unvalidModules.add(item); + } + } + return unvalidModules; } - - // 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; - } - } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + if(!unvalidModules.isEmpty()) { + for(ElementClassDragItem item : unvalidModules) { + dp.remove(item); + } + } } - super.dragEnter(dtde, dp); } }