--- /dev/null
+/*******************************************************************************\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