]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/MappedElementCopyAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / MappedElementCopyAdvisor.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/MappedElementCopyAdvisor.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/MappedElementCopyAdvisor.java
new file mode 100644 (file)
index 0000000..31d52b9
--- /dev/null
@@ -0,0 +1,163 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.mapping;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.synchronization.CopyAdvisor;\r
+import org.simantics.diagram.synchronization.ISynchronizationContext;\r
+import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class MappedElementCopyAdvisor extends GraphCopyAdvisor {\r
+\r
+    protected final CopyAdvisor elementAdvisor;\r
+    protected final CopyAdvisor componentAdvisor;\r
+\r
+    public MappedElementCopyAdvisor(CopyAdvisor elementAdvisor, CopyAdvisor componentAdvisor) {\r
+        this.elementAdvisor = elementAdvisor;\r
+        this.componentAdvisor = componentAdvisor;\r
+    }\r
+\r
+    @Override\r
+    public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer)\r
+    throws DatabaseException {\r
+        if (CopyAdvisor.SUPPORTED.contains(elementAdvisor.canCopy(context, source, sourceContainer, targetContainer))) {\r
+            ModelingResources MOD = context.get(ModelingSynchronizationHints.MODELING_RESOURCE);\r
+            //Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+            Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);\r
+            if (component != null) {\r
+                Resource sourceComposite = sourceContainer != null ? graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite) : null;\r
+                Resource targetComposite = targetContainer != null ? graph.getPossibleObject(targetContainer, MOD.DiagramToComposite) : null;\r
+                //if(targetComposite == null) targetComposite = graph.getPossibleObject(component, L0.PartOf);\r
+\r
+                Evaluation componentEval = componentAdvisor.canCopy(context, component, sourceComposite, targetComposite);\r
+                if (componentEval != Evaluation.SUPPORTED)\r
+                    return componentEval;\r
+            }\r
+\r
+            Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);\r
+            if (connection != null) {\r
+                Resource sourceComposite = graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite);\r
+                Resource targetComposite = graph.getPossibleObject(targetContainer, MOD.DiagramToComposite);\r
+                //if (targetComposite == null) targetComposite = graph.getPossibleObject(component, L0.PartOf);\r
+\r
+                Evaluation connectionEval = componentAdvisor.canCopy(context, connection, sourceComposite, targetComposite);\r
+                if (connectionEval != Evaluation.SUPPORTED)\r
+                    return connectionEval;\r
+\r
+                Resource specialConnection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnectionSpecial);\r
+                if (specialConnection != null) {\r
+                    connectionEval = componentAdvisor.canCopy(context, specialConnection, sourceComposite, targetComposite);\r
+                }\r
+\r
+                return connectionEval;\r
+            }\r
+            return Evaluation.SUPPORTED;\r
+        }\r
+        return Evaluation.NOT_SUPPORTED;\r
+    }\r
+\r
+    @Override\r
+    public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
+        return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());\r
+    }\r
+\r
+    @Override\r
+    public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer, Map<Object, Object> map) throws DatabaseException {\r
+        ModelingResources MOD = context.get(ModelingSynchronizationHints.MODELING_RESOURCE);\r
+        if (MOD == null)\r
+            throw new IllegalStateException("no ModelingSynchronizationHints.MODELING_RESOURCE hint");\r
+\r
+        Resource sourceComposite = sourceContainer != null ? graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite) : null;\r
+        Resource targetComposite = targetContainer != null ? graph.getPossibleObject(targetContainer, MOD.DiagramToComposite) : null;\r
+        //if(targetComposite == null) targetComposite = graph.getPossibleObject(component, L0.PartOf);\r
+\r
+        Resource elementCopy = (Resource) elementAdvisor.copy(context, source, sourceContainer, targetContainer, map);\r
+\r
+        Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);\r
+        Resource componentCopy = null;\r
+        if (component != null) {\r
+            componentCopy = (Resource) componentAdvisor.copy(context, component, sourceComposite, targetComposite, map);\r
+            if (componentCopy != null) {\r
+                graph.claim(elementCopy, MOD.ElementToComponent, componentCopy);\r
+            }\r
+        }\r
+\r
+        Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);\r
+        if (connection != null) {\r
+            Resource connectionCopy = (Resource) componentAdvisor.copy(context, connection, sourceComposite, targetComposite, map);\r
+            if (connectionCopy != null) {\r
+                graph.claim(elementCopy, MOD.DiagramConnectionToConnection, connectionCopy);\r
+            }\r
+\r
+            Resource specialConnection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnectionSpecial);\r
+            if (specialConnection != null) {\r
+                Resource specialConnectionCopy = (Resource) componentAdvisor.copy(context, specialConnection, sourceComposite, targetComposite, map);\r
+                if (specialConnectionCopy != null) {\r
+                    graph.claim(elementCopy, MOD.DiagramConnectionToConnectionSpecial, specialConnectionCopy);\r
+                }\r
+            }\r
+\r
+            // NOTICE: this does not copy MOD.ConnectorToComponent relations.\r
+            // Those are unfortunately reconstructed in Paster since they would\r
+            // be rather difficult to deterministically reconstruct here.\r
+        }\r
+\r
+        return elementCopy;\r
+    }\r
+\r
+    @Override\r
+    public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
+            Resource targetContainer) throws DatabaseException {\r
+        ModelingResources MOD = context.get(ModelingSynchronizationHints.MODELING_RESOURCE);\r
+\r
+        Resource sourceComposite = graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite);\r
+        Resource targetComposite = graph.getPossibleObject(targetContainer, MOD.DiagramToComposite);\r
+\r
+        Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);\r
+        Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);\r
+\r
+        Object elementResult = elementAdvisor.cut(context, source, sourceContainer, targetContainer);\r
+        Object componentResult = null;\r
+\r
+        if (sourceComposite != null && targetComposite != null) {\r
+            if (component != null)\r
+                componentResult = componentAdvisor.cut(context, component, sourceComposite, targetComposite);\r
+            if (connection != null)\r
+                componentResult = componentAdvisor.cut(context, connection, sourceComposite, targetComposite);\r
+        }\r
+\r
+        if (elementResult == null && componentResult == null)\r
+            return null;\r
+        return Pair.make(elementResult, componentResult);\r
+    }\r
+    \r
+    @Override\r
+    public void onFinish(ISynchronizationContext context) {\r
+       super.onFinish(context);\r
+       elementAdvisor.onFinish(context);\r
+       componentAdvisor.onFinish(context);\r
+    }\r
+    \r
+\r
+}\r