]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ElementCopyAdvisor.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ElementCopyAdvisor.java
index 66cd34e945a342b0000719152ee848f5e243d45d..994aa1ab13feb132f6a9ce5ebaa5270c01bf6582 100644 (file)
-/*******************************************************************************\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.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
-import org.simantics.db.exception.CancelTransactionException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.stubs.G2DResource;\r
-import org.simantics.diagram.synchronization.ISynchronizationContext;\r
-import org.simantics.diagram.synchronization.StatementEvaluation;\r
-import org.simantics.diagram.synchronization.graph.BasicResources;\r
-import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;\r
-import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;\r
-import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.utils.datastructures.BinaryFunction;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ElementCopyAdvisor extends GraphCopyAdvisor {\r
-\r
-    G2DResource G2D;\r
-    DiagramResource DIA;\r
-\r
-    /**\r
-     * This is necessary to have DIA.Flag type copied over to the copied flag.\r
-     * Diagram mapping will have problems and potentially break the\r
-     * configuration if the type is not the same as in the source.\r
-     */\r
-    BinaryFunction<StatementEvaluation, ReadGraph, Statement> statementAdvisor =\r
-            new BinaryFunction<StatementEvaluation, ReadGraph, Statement>() {\r
-        @Override\r
-        public StatementEvaluation call(ReadGraph graph, Statement stm) {\r
-            if (DIA.HasFlagType.equals(stm.getPredicate()))\r
-                return StatementEvaluation.INCLUDE;\r
-            if (G2D.HasFontStyle.equals(stm.getPredicate()))\r
-                return StatementEvaluation.INCLUDE;\r
-            return StatementEvaluation.USE_DEFAULT;\r
-        }\r
-    };\r
-\r
-    @Override\r
-    public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourcecontainer, Resource targetContainer) throws DatabaseException {\r
-        BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);\r
-        return graph.isInstanceOf(source, br.DIA.Element) ? Evaluation.SUPPORTED : 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,\r
-            Resource targetContainer, Map<Object, Object> map) throws DatabaseException {\r
-        Resource model = graph.syncRequest(new PossibleModel(targetContainer));\r
-        //if (model == null)\r
-        //    throw new IllegalArgumentException("no model found for copy target container " + NameUtils.getSafeName(graph, targetContainer, true));\r
-\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        G2D = G2DResource.getInstance(graph);\r
-        DIA = DiagramResource.getInstance(graph);\r
-\r
-        Resource copy = (model != null) ?\r
-                CopyAdvisorUtil.copy3(graph, source, model, statementAdvisor, map) :\r
-                    CopyAdvisorUtil.copy2(graph, source, statementAdvisor, map);\r
-\r
-        if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))\r
-            graph.claim(targetContainer, L0.ConsistsOf, copy);\r
-\r
-        return copy;\r
-    }\r
-\r
-    @Override\r
-    public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
-            Resource targetContainer) throws DatabaseException {\r
-\r
-       Layer0 L0 = Layer0.getInstance(graph);\r
-       DiagramResource DIA = DiagramResource.getInstance(graph);\r
-\r
-        if (sourceContainer.equals(targetContainer))\r
-            return null;\r
-\r
-        // disconnect from source diagram\r
-        if (!OrderedSetUtils.remove(graph, sourceContainer, source))\r
-            // Unlink failed for some reason.\r
-            throw new CancelTransactionException("Failed to unlink element "\r
-                    + NameUtils.getSafeName(graph, source) + " from source diagram "\r
-                    + NameUtils.getSafeName(graph, sourceContainer));\r
-\r
-        // connect to target diagram\r
-        boolean prepend = graph.isInstanceOf(source, DIA.Connection);\r
-        boolean add = true;\r
-        if (prepend)\r
-            add = OrderedSetUtils.addFirst(graph, targetContainer, source);\r
-        else\r
-            add = OrderedSetUtils.add(graph, targetContainer, source);\r
-        if (!add)\r
-            // Link failed for some reason.\r
-            throw new CancelTransactionException("Failed to link element "\r
-                    + NameUtils.getSafeName(graph, source) + " to target diagram "\r
-                    + NameUtils.getSafeName(graph, targetContainer));\r
-\r
-        // This handles e.g. connections, which are not part of container\r
-        if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {\r
-            graph.deny(sourceContainer, L0.ConsistsOf, source);\r
-            graph.claim(targetContainer, L0.ConsistsOf, source);\r
-        }\r
-\r
-        return source;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.mapping;
+
+import java.util.Map;
+import java.util.function.BiFunction;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.common.utils.OrderedSetUtils;
+import org.simantics.db.exception.CancelTransactionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.stubs.G2DResource;
+import org.simantics.diagram.synchronization.ISynchronizationContext;
+import org.simantics.diagram.synchronization.StatementEvaluation;
+import org.simantics.diagram.synchronization.graph.BasicResources;
+import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
+import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
+import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
+import org.simantics.layer0.Layer0;
+
+import gnu.trove.map.hash.THashMap;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ElementCopyAdvisor extends GraphCopyAdvisor {
+
+    G2DResource G2D;
+    DiagramResource DIA;
+
+    /**
+     * This is necessary to have DIA.Flag type copied over to the copied flag.
+     * Diagram mapping will have problems and potentially break the
+     * configuration if the type is not the same as in the source.
+     */
+    BiFunction<ReadGraph, Statement, StatementEvaluation> statementAdvisor =
+            new BiFunction<ReadGraph, Statement, StatementEvaluation>() {
+        @Override
+        public StatementEvaluation apply(ReadGraph graph, Statement stm) {
+            if (DIA.HasFlagType.equals(stm.getPredicate()))
+                return StatementEvaluation.INCLUDE;
+            if (G2D.HasFontStyle.equals(stm.getPredicate()))
+                return StatementEvaluation.INCLUDE;
+            return StatementEvaluation.USE_DEFAULT;
+        }
+    };
+
+    @Override
+    public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourcecontainer, Resource targetContainer) throws DatabaseException {
+        BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
+        return graph.isInstanceOf(source, br.DIA.Element) ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
+    }
+
+    @Override
+    public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
+        return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
+    }
+
+    @Override
+    public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
+            Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
+        Resource model = graph.syncRequest(new PossibleModel(targetContainer));
+        //if (model == null)
+        //    throw new IllegalArgumentException("no model found for copy target container " + NameUtils.getSafeName(graph, targetContainer, true));
+
+        Layer0 L0 = Layer0.getInstance(graph);
+        G2D = G2DResource.getInstance(graph);
+        DIA = DiagramResource.getInstance(graph);
+
+        Resource copy = (model != null) ?
+                CopyAdvisorUtil.copy3(graph, source, model, statementAdvisor, map) :
+                    CopyAdvisorUtil.copy2(graph, source, statementAdvisor, map);
+
+        if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
+            graph.claim(targetContainer, L0.ConsistsOf, copy);
+
+        return copy;
+    }
+
+    @Override
+    public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
+            Resource targetContainer) throws DatabaseException {
+
+       Layer0 L0 = Layer0.getInstance(graph);
+       DiagramResource DIA = DiagramResource.getInstance(graph);
+
+        if (sourceContainer.equals(targetContainer))
+            return null;
+
+        // disconnect from source diagram
+        if (!OrderedSetUtils.remove(graph, sourceContainer, source))
+            // Unlink failed for some reason.
+            throw new CancelTransactionException("Failed to unlink element "
+                    + NameUtils.getSafeName(graph, source) + " from source diagram "
+                    + NameUtils.getSafeName(graph, sourceContainer));
+
+        // connect to target diagram
+        boolean prepend = graph.isInstanceOf(source, DIA.Connection);
+        boolean add = true;
+        if (prepend)
+            add = OrderedSetUtils.addFirst(graph, targetContainer, source);
+        else
+            add = OrderedSetUtils.add(graph, targetContainer, source);
+        if (!add)
+            // Link failed for some reason.
+            throw new CancelTransactionException("Failed to link element "
+                    + NameUtils.getSafeName(graph, source) + " to target diagram "
+                    + NameUtils.getSafeName(graph, targetContainer));
+
+        // This handles e.g. connections, which are not part of container
+        if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
+            graph.deny(sourceContainer, L0.ConsistsOf, source);
+            graph.claim(targetContainer, L0.ConsistsOf, source);
+        }
+
+        return source;
+    }
+
+}