]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementCopyHandler.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentAndElementCopyHandler.java
1 package org.simantics.modeling.mapping;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 \r
6 import org.simantics.databoard.Bindings;\r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.common.NamedResource;\r
10 import org.simantics.db.common.utils.OrderedSetUtils;\r
11 import org.simantics.db.exception.DatabaseException;\r
12 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;\r
13 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
14 import org.simantics.diagram.stubs.DiagramResource;\r
15 import org.simantics.layer0.Layer0;\r
16 import org.simantics.modeling.ModelingResources;\r
17 import org.simantics.structural.stubs.StructuralResource2;\r
18 \r
19 \r
20 /**\r
21  * Copy handler for Component and it's Element.\r
22  * \r
23  * FIXME: Requires patched ModelTransferableGraphSourceRequest\r
24  * \r
25  *  in classifyPredicates(), excluded resources must be filtered. Otherwise the code fails with :Diagram.hasInverse having no URI.\r
26  *  \r
27  *  Resource predicate = stm.getPredicate();\r
28  *      Resource object = stm.getObject();              \r
29  *      if (exclusions.contains(object) || exclusions.contains(predicate))\r
30  *              continue; \r
31  *\r
32  * \r
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
34  *\r
35  */\r
36 public class ComponentAndElementCopyHandler extends DefaultCopyHandler{\r
37 \r
38         protected boolean includeComponent = true;\r
39         protected boolean includeElement = true;\r
40         \r
41         public ComponentAndElementCopyHandler(Resource resource) {\r
42                 super(resource);\r
43         }\r
44         \r
45         /**\r
46          * \r
47          * @param resource Component, if component is included. Otherwise the Element.\r
48          * @param includeComponent\r
49          * @param includeElement\r
50          */\r
51         public ComponentAndElementCopyHandler(Resource resource, boolean includeComponent, boolean includeElement) {\r
52                 super(resource);\r
53                 if (!includeComponent && !includeElement)\r
54                         throw new IllegalArgumentException();\r
55                 this.includeComponent = includeComponent;\r
56                 this.includeElement = includeElement;\r
57         }\r
58         \r
59         \r
60 \r
61         \r
62         @Override\r
63         protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {\r
64         ModelingResources MR = ModelingResources.getInstance(graph);\r
65 \r
66         ArrayList<Resource> exclusions = new ArrayList<Resource>();\r
67 \r
68         ArrayList<NamedResource> roots = new ArrayList<NamedResource>();\r
69         \r
70         Resource component = null;\r
71         Resource element = null;\r
72         \r
73         if (includeComponent) {\r
74                 component = getResource();\r
75                 element = graph.getPossibleObject(component, MR.ComponentToElement);\r
76         } else {\r
77                 element = getResource();\r
78                 component = graph.getPossibleObject(element, MR.ElementToComponent);\r
79         }\r
80                 // Include component as root\r
81         if (component != null) {\r
82                 processComponent(graph, component, includeComponent, roots, exclusions);\r
83         }\r
84                 \r
85             if (element != null) {\r
86                 processElement(graph, element, includeElement, roots, exclusions);\r
87             }\r
88             \r
89         return TransferableGraphConfiguration2.createWithNames(graph, roots, exclusions, true, false);\r
90         }\r
91         \r
92         protected void processComponent(ReadGraph graph, Resource component, boolean includeComponent, ArrayList<NamedResource> roots, ArrayList<Resource> exclusions) throws DatabaseException {\r
93                 Layer0 L0 = Layer0.getInstance(graph);\r
94         StructuralResource2 SR = StructuralResource2.getInstance(graph);\r
95         Resource resource = getResource();\r
96                 if (includeComponent) {\r
97                         String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);\r
98                         roots.add(new NamedResource(rootName, resource));\r
99                 } else {\r
100                         exclusions.add(component);\r
101                 }\r
102 \r
103                 // filter connections\r
104                 for (Resource connection : graph.getObjects(resource, SR.IsConnectedTo))\r
105                         exclusions.add(connection);\r
106         }\r
107         \r
108         protected void processElement(ReadGraph graph, Resource element, boolean includeElement, ArrayList<NamedResource> roots, ArrayList<Resource> exclusions) throws DatabaseException {\r
109                 Layer0 L0 = Layer0.getInstance(graph);\r
110                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
111         StructuralResource2 SR = StructuralResource2.getInstance(graph);\r
112                 \r
113                 if (includeElement) {\r
114                         // Add element as a root.\r
115                         String name = graph.getRelatedValue(element, L0.HasName, Bindings.STRING);\r
116                         roots.add(new NamedResource(name, element));\r
117                 } else {\r
118                         exclusions.add(element);\r
119                 }\r
120                 \r
121                 // exclude diagram from copied data.\r
122                 Collection<Resource> diagram_ = OrderedSetUtils.getOwnerLists(graph, element, DIA.Diagram);\r
123                 for (Resource diagram : diagram_) {\r
124                         \r
125                         Resource inv = graph.getInverse(diagram);\r
126                         exclusions.add(diagram);\r
127                         exclusions.add(inv);\r
128                         \r
129                         // Exclude next and previous elements in the diagram\r
130                         Resource next = OrderedSetUtils.next(graph, diagram, element);\r
131                         Resource previous = OrderedSetUtils.prev(graph, diagram, element);\r
132                         if (next != null && !next.equals(element))\r
133                                 exclusions.add(next);\r
134                         if (previous != null && !previous.equals(element) && !previous.equals(next))\r
135                                 exclusions.add(previous);\r
136                 }\r
137                 // filter connections\r
138         for (Resource connection : graph.getObjects(element, SR.IsConnectedTo))\r
139                 exclusions.add(connection);\r
140         }\r
141                 \r
142 }\r