]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ElementCopyAdvisor.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[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.db.layer0.util.Layer0Utils;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.diagram.stubs.G2DResource;
29 import org.simantics.diagram.synchronization.ISynchronizationContext;
30 import org.simantics.diagram.synchronization.StatementEvaluation;
31 import org.simantics.diagram.synchronization.graph.BasicResources;
32 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
33 import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
34 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
35 import org.simantics.layer0.Layer0;
36
37 import gnu.trove.map.hash.THashMap;
38
39 /**
40  * @author Tuukka Lehtonen
41  */
42 public class ElementCopyAdvisor extends GraphCopyAdvisor {
43
44     G2DResource G2D;
45     DiagramResource DIA;
46
47     /**
48      * This is necessary to have DIA.Flag type copied over to the copied flag.
49      * Diagram mapping will have problems and potentially break the
50      * configuration if the type is not the same as in the source.
51      */
52     BiFunction<ReadGraph, Statement, StatementEvaluation> statementAdvisor =
53             new BiFunction<ReadGraph, Statement, StatementEvaluation>() {
54         @Override
55         public StatementEvaluation apply(ReadGraph graph, Statement stm) {
56             if (DIA.HasFlagType.equals(stm.getPredicate()))
57                 return StatementEvaluation.INCLUDE;
58             if (G2D.HasFontStyle.equals(stm.getPredicate()))
59                 return StatementEvaluation.INCLUDE;
60             return StatementEvaluation.USE_DEFAULT;
61         }
62     };
63
64     @Override
65     public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourcecontainer, Resource targetContainer) throws DatabaseException {
66         BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
67         return graph.isInstanceOf(source, br.DIA.Element) ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
68     }
69
70     @Override
71     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
72         return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
73     }
74
75     @Override
76     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
77             Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
78         Resource model = graph.syncRequest(new PossibleModel(targetContainer));
79         //if (model == null)
80         //    throw new IllegalArgumentException("no model found for copy target container " + NameUtils.getSafeName(graph, targetContainer, true));
81
82         Layer0 L0 = Layer0.getInstance(graph);
83         G2D = G2DResource.getInstance(graph);
84         DIA = DiagramResource.getInstance(graph);
85
86         Resource copy = (model != null) ?
87                 CopyAdvisorUtil.copy3(graph, source, model, statementAdvisor, map) :
88                     CopyAdvisorUtil.copy2(graph, source, statementAdvisor, map);
89
90         if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
91             graph.claim(targetContainer, L0.ConsistsOf, copy);
92
93         Layer0Utils.claimNewIdentifier(graph, copy, false);
94
95         return copy;
96     }
97
98     @Override
99     public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
100             Resource targetContainer) throws DatabaseException {
101
102         Layer0 L0 = Layer0.getInstance(graph);
103         DiagramResource DIA = DiagramResource.getInstance(graph);
104
105         if (sourceContainer.equals(targetContainer))
106             return null;
107
108         // disconnect from source diagram
109         if (!OrderedSetUtils.remove(graph, sourceContainer, source))
110             // Unlink failed for some reason.
111             throw new CancelTransactionException("Failed to unlink element "
112                     + NameUtils.getSafeName(graph, source) + " from source diagram "
113                     + NameUtils.getSafeName(graph, sourceContainer));
114
115         // connect to target diagram
116         boolean prepend = graph.isInstanceOf(source, DIA.Connection);
117         boolean add = true;
118         if (prepend)
119             add = OrderedSetUtils.addFirst(graph, targetContainer, source);
120         else
121             add = OrderedSetUtils.add(graph, targetContainer, source);
122         if (!add)
123             // Link failed for some reason.
124             throw new CancelTransactionException("Failed to link element "
125                     + NameUtils.getSafeName(graph, source) + " to target diagram "
126                     + NameUtils.getSafeName(graph, targetContainer));
127
128         // This handles e.g. connections, which are not part of container
129         if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
130             graph.deny(sourceContainer, L0.ConsistsOf, source);
131             graph.claim(targetContainer, L0.ConsistsOf, source);
132         }
133
134         return source;
135     }
136
137 }