1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.mapping;
\r
14 import gnu.trove.map.hash.THashMap;
\r
16 import java.util.Map;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.exception.DatabaseException;
\r
21 import org.simantics.diagram.synchronization.CopyAdvisor;
\r
22 import org.simantics.diagram.synchronization.ISynchronizationContext;
\r
23 import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
\r
24 import org.simantics.modeling.ModelingResources;
\r
25 import org.simantics.utils.datastructures.Pair;
\r
28 * @author Tuukka Lehtonen
\r
30 public class MappedElementCopyAdvisor extends GraphCopyAdvisor {
\r
32 protected final CopyAdvisor elementAdvisor;
\r
33 protected final CopyAdvisor componentAdvisor;
\r
35 public MappedElementCopyAdvisor(CopyAdvisor elementAdvisor, CopyAdvisor componentAdvisor) {
\r
36 this.elementAdvisor = elementAdvisor;
\r
37 this.componentAdvisor = componentAdvisor;
\r
41 public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer)
\r
42 throws DatabaseException {
\r
43 if (CopyAdvisor.SUPPORTED.contains(elementAdvisor.canCopy(context, source, sourceContainer, targetContainer))) {
\r
44 ModelingResources MOD = context.get(ModelingSynchronizationHints.MODELING_RESOURCE);
\r
45 //Layer0 L0 = Layer0.getInstance(graph);
\r
47 Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);
\r
48 if (component != null) {
\r
49 Resource sourceComposite = sourceContainer != null ? graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite) : null;
\r
50 Resource targetComposite = targetContainer != null ? graph.getPossibleObject(targetContainer, MOD.DiagramToComposite) : null;
\r
51 //if(targetComposite == null) targetComposite = graph.getPossibleObject(component, L0.PartOf);
\r
53 Evaluation componentEval = componentAdvisor.canCopy(context, component, sourceComposite, targetComposite);
\r
54 if (componentEval != Evaluation.SUPPORTED)
\r
55 return componentEval;
\r
58 Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);
\r
59 if (connection != null) {
\r
60 Resource sourceComposite = graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite);
\r
61 Resource targetComposite = graph.getPossibleObject(targetContainer, MOD.DiagramToComposite);
\r
62 //if (targetComposite == null) targetComposite = graph.getPossibleObject(component, L0.PartOf);
\r
64 Evaluation connectionEval = componentAdvisor.canCopy(context, connection, sourceComposite, targetComposite);
\r
65 if (connectionEval != Evaluation.SUPPORTED)
\r
66 return connectionEval;
\r
68 Resource specialConnection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnectionSpecial);
\r
69 if (specialConnection != null) {
\r
70 connectionEval = componentAdvisor.canCopy(context, specialConnection, sourceComposite, targetComposite);
\r
73 return connectionEval;
\r
75 return Evaluation.SUPPORTED;
\r
77 return Evaluation.NOT_SUPPORTED;
\r
81 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
\r
82 return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
\r
86 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
\r
87 ModelingResources MOD = context.get(ModelingSynchronizationHints.MODELING_RESOURCE);
\r
89 throw new IllegalStateException("no ModelingSynchronizationHints.MODELING_RESOURCE hint");
\r
91 Resource sourceComposite = sourceContainer != null ? graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite) : null;
\r
92 Resource targetComposite = targetContainer != null ? graph.getPossibleObject(targetContainer, MOD.DiagramToComposite) : null;
\r
93 //if(targetComposite == null) targetComposite = graph.getPossibleObject(component, L0.PartOf);
\r
95 Resource elementCopy = (Resource) elementAdvisor.copy(context, source, sourceContainer, targetContainer, map);
\r
97 Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);
\r
98 Resource componentCopy = null;
\r
99 if (component != null) {
\r
100 componentCopy = (Resource) componentAdvisor.copy(context, component, sourceComposite, targetComposite, map);
\r
101 if (componentCopy != null) {
\r
102 graph.claim(elementCopy, MOD.ElementToComponent, componentCopy);
\r
106 Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);
\r
107 if (connection != null) {
\r
108 Resource connectionCopy = (Resource) componentAdvisor.copy(context, connection, sourceComposite, targetComposite, map);
\r
109 if (connectionCopy != null) {
\r
110 graph.claim(elementCopy, MOD.DiagramConnectionToConnection, connectionCopy);
\r
113 Resource specialConnection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnectionSpecial);
\r
114 if (specialConnection != null) {
\r
115 Resource specialConnectionCopy = (Resource) componentAdvisor.copy(context, specialConnection, sourceComposite, targetComposite, map);
\r
116 if (specialConnectionCopy != null) {
\r
117 graph.claim(elementCopy, MOD.DiagramConnectionToConnectionSpecial, specialConnectionCopy);
\r
121 // NOTICE: this does not copy MOD.ConnectorToComponent relations.
\r
122 // Those are unfortunately reconstructed in Paster since they would
\r
123 // be rather difficult to deterministically reconstruct here.
\r
126 return elementCopy;
\r
130 public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
\r
131 Resource targetContainer) throws DatabaseException {
\r
132 ModelingResources MOD = context.get(ModelingSynchronizationHints.MODELING_RESOURCE);
\r
134 Resource sourceComposite = graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite);
\r
135 Resource targetComposite = graph.getPossibleObject(targetContainer, MOD.DiagramToComposite);
\r
137 Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);
\r
138 Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);
\r
140 Object elementResult = elementAdvisor.cut(context, source, sourceContainer, targetContainer);
\r
141 Object componentResult = null;
\r
143 if (sourceComposite != null && targetComposite != null) {
\r
144 if (component != null)
\r
145 componentResult = componentAdvisor.cut(context, component, sourceComposite, targetComposite);
\r
146 if (connection != null)
\r
147 componentResult = componentAdvisor.cut(context, connection, sourceComposite, targetComposite);
\r
150 if (elementResult == null && componentResult == null)
\r
152 return Pair.make(elementResult, componentResult);
\r
156 public void onFinish(ISynchronizationContext context) {
\r
157 super.onFinish(context);
\r
158 elementAdvisor.onFinish(context);
\r
159 componentAdvisor.onFinish(context);
\r