]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementPasteImportAdvisor.java
Find SCL references in SCLModuleEditor with Ctrl+Shift+G
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentAndElementPasteImportAdvisor.java
1 package org.simantics.modeling.mapping;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.WriteOnlyGraph;
11 import org.simantics.db.common.request.FreshEscapedName;
12 import org.simantics.db.common.utils.OrderedSetUtils;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.request.PossibleModel;
15 import org.simantics.diagram.stubs.DiagramResource;
16 import org.simantics.diagram.stubs.G2DResource;
17 import org.simantics.graph.db.IImportAdvisor;
18 import org.simantics.graph.representation.Root;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ModelingResources;
21
22 /**
23  * ImportAdvisor for handling Component and Element copies.
24  * 
25  * The advisor can be configured to copy just component or element.
26  * 
27  * When copying a component, handling the element many vary:
28  * 1. If copied data does not contain element, but the target composite is attached to a diagram, the advisor creates the element.
29  * 2. If copied data contains element, but target composite is not attached to a diagram, the advisor deletes the element.  
30  * 
31  * 
32  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
33  *
34  */
35 public class ComponentAndElementPasteImportAdvisor implements IImportAdvisor {
36         
37         private static boolean DEBUG = false;
38         
39         protected Resource element;
40         protected Resource component;
41         
42         protected Root elementRoot;
43         protected Root componentRoot;
44         
45         protected Resource composite;
46         protected Resource diagram;
47         protected Resource model;
48         
49         protected Map<String, String> nameMappings = new HashMap<String, String>();
50         
51         protected boolean includeComponent = true;
52         protected boolean includeElement = true;
53         
54         public ComponentAndElementPasteImportAdvisor(ReadGraph graph, Resource composite) throws DatabaseException{
55                 this.composite = composite;
56                 this.diagram = graph.getPossibleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
57                 this.includeElement = diagram != null;
58                 this.model = graph.sync(new PossibleModel(composite));
59         }
60         
61         public ComponentAndElementPasteImportAdvisor(ReadGraph graph, Resource resource, boolean includeComponent, boolean includeElement) throws DatabaseException{
62                 if (!includeComponent && !includeElement)
63                         throw new IllegalArgumentException();
64                 
65                 this.includeComponent = includeComponent;
66                 this.includeElement = includeElement;
67                 if (includeComponent) {
68                         this.composite = resource;
69                         this.diagram = graph.getPossibleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
70                         if (this.diagram == null)
71                                 this.includeElement = false;
72                 } else {
73                         this.diagram = resource;
74                         this.composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);
75                 }
76                 
77         }
78         
79         @Override
80         public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
81
82                 if("%model".equals(root.name)) return model;
83                 
84                 DiagramResource DIA = DiagramResource.getInstance(graph);
85                 String type = root.type;
86                 
87                 Resource typeRes = graph.getResource(type);
88                 if (graph.isInheritedFrom(typeRes, DIA.Element)) {
89                         elementRoot = root;
90                         String newName = root.name;
91                         if (diagram != null)
92                                 newName = newName(graph, diagram, root.name);
93                         nameMappings.put(root.name, newName);
94                 } else {
95                         componentRoot = root;
96                         String newName = newName(graph, composite, root.name);
97                         nameMappings.put(root.name, newName);
98                 }
99                 
100                 return null;
101                 
102         }
103         
104         public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {
105                 return graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, name));
106         }
107         
108         public Resource getComponent() {
109                 return component;
110         }
111         
112         public Resource getElement() {
113                 return element;
114         }
115         
116         @Override
117         public Resource createRoot(WriteOnlyGraph graph, Root root)
118                         throws DatabaseException {
119                 Layer0 l0 = graph.getService(Layer0.class);
120                 if (root == elementRoot) {
121                         element = graph.newResource();
122                         String name = root.name;
123                         String newName = nameMappings.get(name);
124                         graph.addLiteral(element, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
125                         if (DEBUG) System.out.println("CaECopyAdvisor created element " + element + " " + newName);
126                         return element;
127                 } else if (root == componentRoot) {
128                         component = graph.newResource();
129                         String name = root.name;
130                         String newName = nameMappings.get(name);
131                         graph.addLiteral(component, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
132                         if (DEBUG) System.out.println("CaECopyAdvisor created component " + component + " " + newName);
133                         return component;
134                 }
135                 throw new DatabaseException("Unknown root " + root);
136         }
137         
138         /**
139          * Attaches pasted component / element to composite / diagram
140          * @param graph
141          * @throws DatabaseException
142          */
143         public void attach(WriteGraph graph) throws DatabaseException{
144                 Layer0 l0 = Layer0.getInstance(graph);
145                 if (includeComponent) {
146                         if (component != null)
147                                 graph.claim(composite, l0.ConsistsOf, l0.PartOf, component);
148                 } else {
149                         if (component != null)
150                                 graph.deny(component);
151                 }
152                 if (!includeElement) {
153                         // paste target does not need the  element.
154                         if (element != null) {
155                                 graph.deny(element);
156                         }
157                 } else {
158                         // paste target needs the element.
159                         boolean created = false;
160                         if (element == null) {
161                                 // element was not in the copied data, we must create a new one.
162                                 
163                                 ModelingResources MR = ModelingResources.getInstance(graph);
164                                 DiagramResource DIA = DiagramResource.getInstance(graph);
165                                 G2DResource G2D = G2DResource.getInstance(graph);
166                                 
167                                 Resource componentType = graph.getSingleType(component);
168                                 Resource elementType = graph.getSingleObject(componentType, MR.ComponentTypeToSymbol);
169                                 
170                                 element = graph.newResource();
171                                 
172                                 String newName = newName(graph, diagram, "element");
173                                 graph.claimLiteral(element, l0.HasName, newName);
174                                 
175                                 graph.claim(element, l0.InstanceOf, elementType);
176                                 graph.claim(component, MR.ComponentToElement, element);
177                                 
178                                 graph.claimLiteral(element, DIA.HasTransform, G2D.Transform, new double[]{1,0,0,1,0,0});
179                                 
180                                 if (DEBUG) System.out.println("CaECopyAdvisor created deferred element " + element + " " + newName);
181                                 
182                                 created = true;
183                         }
184                         graph.claim(diagram, l0.ConsistsOf, l0.PartOf, element);
185                         OrderedSetUtils.add(graph, diagram, element);
186                         if (created) {
187                                 createElement(graph, element);
188                         }
189                 }
190
191         }
192         
193         /**
194          * Callback called, if copied data did not contain element, and the advisor had to create the element by itself.
195          * 
196          * Note: default implementation does nothing.
197          * 
198          * @param graph
199          * @param element
200          * @throws DatabaseException
201          */
202         public void createElement(WriteGraph graph, Resource element) throws DatabaseException{
203                 
204         }
205         
206
207 }