]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ElementCopyAdvisor.java
Layer0Utils.addL0Identifier to prevent possible differentiation of code
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ElementCopyAdvisor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.mapping;
13
14 import java.util.Map;
15 import java.util.function.BiFunction;
16
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;
35
36 import gnu.trove.map.hash.THashMap;
37
38 /**
39  * @author Tuukka Lehtonen
40  */
41 public class ElementCopyAdvisor extends GraphCopyAdvisor {
42
43     G2DResource G2D;
44     DiagramResource DIA;
45
46     /**
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.
50      */
51     BiFunction<ReadGraph, Statement, StatementEvaluation> statementAdvisor =
52             new BiFunction<ReadGraph, Statement, StatementEvaluation>() {
53         @Override
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;
60         }
61     };
62
63     @Override
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;
67     }
68
69     @Override
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>());
72     }
73
74     @Override
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));
78         //if (model == null)
79         //    throw new IllegalArgumentException("no model found for copy target container " + NameUtils.getSafeName(graph, targetContainer, true));
80
81         Layer0 L0 = Layer0.getInstance(graph);
82         G2D = G2DResource.getInstance(graph);
83         DIA = DiagramResource.getInstance(graph);
84
85         Resource copy = (model != null) ?
86                 CopyAdvisorUtil.copy3(graph, source, model, statementAdvisor, map) :
87                     CopyAdvisorUtil.copy2(graph, source, statementAdvisor, map);
88
89         if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
90             graph.claim(targetContainer, L0.ConsistsOf, copy);
91
92         return copy;
93     }
94
95     @Override
96     public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
97             Resource targetContainer) throws DatabaseException {
98
99         Layer0 L0 = Layer0.getInstance(graph);
100         DiagramResource DIA = DiagramResource.getInstance(graph);
101
102         if (sourceContainer.equals(targetContainer))
103             return null;
104
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));
111
112         // connect to target diagram
113         boolean prepend = graph.isInstanceOf(source, DIA.Connection);
114         boolean add = true;
115         if (prepend)
116             add = OrderedSetUtils.addFirst(graph, targetContainer, source);
117         else
118             add = OrderedSetUtils.add(graph, targetContainer, source);
119         if (!add)
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));
124
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);
129         }
130
131         return source;
132     }
133
134 }