1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.mapping;
15 import java.util.function.BiFunction;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.common.utils.OrderedSetUtils;
23 import org.simantics.db.exception.CancelTransactionException;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.request.PossibleModel;
26 import org.simantics.diagram.stubs.DiagramResource;
27 import org.simantics.diagram.stubs.G2DResource;
28 import org.simantics.diagram.synchronization.ISynchronizationContext;
29 import org.simantics.diagram.synchronization.StatementEvaluation;
30 import org.simantics.diagram.synchronization.graph.BasicResources;
31 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
32 import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
33 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
34 import org.simantics.layer0.Layer0;
36 import gnu.trove.map.hash.THashMap;
39 * @author Tuukka Lehtonen
41 public class ElementCopyAdvisor extends GraphCopyAdvisor {
47 * This is necessary to have DIA.Flag type copied over to the copied flag.
48 * Diagram mapping will have problems and potentially break the
49 * configuration if the type is not the same as in the source.
51 BiFunction<ReadGraph, Statement, StatementEvaluation> statementAdvisor =
52 new BiFunction<ReadGraph, Statement, StatementEvaluation>() {
54 public StatementEvaluation apply(ReadGraph graph, Statement stm) {
55 if (DIA.HasFlagType.equals(stm.getPredicate()))
56 return StatementEvaluation.INCLUDE;
57 if (G2D.HasFontStyle.equals(stm.getPredicate()))
58 return StatementEvaluation.INCLUDE;
59 return StatementEvaluation.USE_DEFAULT;
64 public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourcecontainer, Resource targetContainer) throws DatabaseException {
65 BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
66 return graph.isInstanceOf(source, br.DIA.Element) ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
70 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
71 return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
75 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
76 Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
77 Resource model = graph.syncRequest(new PossibleModel(targetContainer));
79 // throw new IllegalArgumentException("no model found for copy target container " + NameUtils.getSafeName(graph, targetContainer, true));
81 Layer0 L0 = Layer0.getInstance(graph);
82 G2D = G2DResource.getInstance(graph);
83 DIA = DiagramResource.getInstance(graph);
85 Resource copy = (model != null) ?
86 CopyAdvisorUtil.copy3(graph, source, model, statementAdvisor, map) :
87 CopyAdvisorUtil.copy2(graph, source, statementAdvisor, map);
89 if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
90 graph.claim(targetContainer, L0.ConsistsOf, copy);
96 public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
97 Resource targetContainer) throws DatabaseException {
99 Layer0 L0 = Layer0.getInstance(graph);
100 DiagramResource DIA = DiagramResource.getInstance(graph);
102 if (sourceContainer.equals(targetContainer))
105 // disconnect from source diagram
106 if (!OrderedSetUtils.remove(graph, sourceContainer, source))
107 // Unlink failed for some reason.
108 throw new CancelTransactionException("Failed to unlink element "
109 + NameUtils.getSafeName(graph, source) + " from source diagram "
110 + NameUtils.getSafeName(graph, sourceContainer));
112 // connect to target diagram
113 boolean prepend = graph.isInstanceOf(source, DIA.Connection);
116 add = OrderedSetUtils.addFirst(graph, targetContainer, source);
118 add = OrderedSetUtils.add(graph, targetContainer, source);
120 // Link failed for some reason.
121 throw new CancelTransactionException("Failed to link element "
122 + NameUtils.getSafeName(graph, source) + " to target diagram "
123 + NameUtils.getSafeName(graph, targetContainer));
125 // This handles e.g. connections, which are not part of container
126 if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
127 graph.deny(sourceContainer, L0.ConsistsOf, source);
128 graph.claim(targetContainer, L0.ConsistsOf, source);