]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/MappedElementCopyAdvisor.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / MappedElementCopyAdvisor.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.mapping;\r
13 \r
14 import gnu.trove.map.hash.THashMap;\r
15 \r
16 import java.util.Map;\r
17 \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
26 \r
27 /**\r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public class MappedElementCopyAdvisor extends GraphCopyAdvisor {\r
31 \r
32     protected final CopyAdvisor elementAdvisor;\r
33     protected final CopyAdvisor componentAdvisor;\r
34 \r
35     public MappedElementCopyAdvisor(CopyAdvisor elementAdvisor, CopyAdvisor componentAdvisor) {\r
36         this.elementAdvisor = elementAdvisor;\r
37         this.componentAdvisor = componentAdvisor;\r
38     }\r
39 \r
40     @Override\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
46 \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
52 \r
53                 Evaluation componentEval = componentAdvisor.canCopy(context, component, sourceComposite, targetComposite);\r
54                 if (componentEval != Evaluation.SUPPORTED)\r
55                     return componentEval;\r
56             }\r
57 \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
63 \r
64                 Evaluation connectionEval = componentAdvisor.canCopy(context, connection, sourceComposite, targetComposite);\r
65                 if (connectionEval != Evaluation.SUPPORTED)\r
66                     return connectionEval;\r
67 \r
68                 Resource specialConnection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnectionSpecial);\r
69                 if (specialConnection != null) {\r
70                     connectionEval = componentAdvisor.canCopy(context, specialConnection, sourceComposite, targetComposite);\r
71                 }\r
72 \r
73                 return connectionEval;\r
74             }\r
75             return Evaluation.SUPPORTED;\r
76         }\r
77         return Evaluation.NOT_SUPPORTED;\r
78     }\r
79 \r
80     @Override\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
83     }\r
84 \r
85     @Override\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
88         if (MOD == null)\r
89             throw new IllegalStateException("no ModelingSynchronizationHints.MODELING_RESOURCE hint");\r
90 \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
94 \r
95         Resource elementCopy = (Resource) elementAdvisor.copy(context, source, sourceContainer, targetContainer, map);\r
96 \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
103             }\r
104         }\r
105 \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
111             }\r
112 \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
118                 }\r
119             }\r
120 \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
124         }\r
125 \r
126         return elementCopy;\r
127     }\r
128 \r
129     @Override\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
133 \r
134         Resource sourceComposite = graph.getPossibleObject(sourceContainer, MOD.DiagramToComposite);\r
135         Resource targetComposite = graph.getPossibleObject(targetContainer, MOD.DiagramToComposite);\r
136 \r
137         Resource component = graph.getPossibleObject(source, MOD.ElementToComponent);\r
138         Resource connection = graph.getPossibleObject(source, MOD.DiagramConnectionToConnection);\r
139 \r
140         Object elementResult = elementAdvisor.cut(context, source, sourceContainer, targetContainer);\r
141         Object componentResult = null;\r
142 \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
148         }\r
149 \r
150         if (elementResult == null && componentResult == null)\r
151             return null;\r
152         return Pair.make(elementResult, componentResult);\r
153     }\r
154     \r
155     @Override\r
156     public void onFinish(ISynchronizationContext context) {\r
157         super.onFinish(context);\r
158         elementAdvisor.onFinish(context);\r
159         componentAdvisor.onFinish(context);\r
160     }\r
161     \r
162 \r
163 }\r