1 package org.simantics.modeling.mapping;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\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
21 * Copy handler for Component and it's Element.
\r
23 * FIXME: Requires patched ModelTransferableGraphSourceRequest
\r
25 * in classifyPredicates(), excluded resources must be filtered. Otherwise the code fails with :Diagram.hasInverse having no URI.
\r
27 * Resource predicate = stm.getPredicate();
\r
28 * Resource object = stm.getObject();
\r
29 * if (exclusions.contains(object) || exclusions.contains(predicate))
\r
33 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
36 public class ComponentAndElementCopyHandler extends DefaultCopyHandler{
\r
38 protected boolean includeComponent = true;
\r
39 protected boolean includeElement = true;
\r
41 public ComponentAndElementCopyHandler(Resource resource) {
\r
47 * @param resource Component, if component is included. Otherwise the Element.
\r
48 * @param includeComponent
\r
49 * @param includeElement
\r
51 public ComponentAndElementCopyHandler(Resource resource, boolean includeComponent, boolean includeElement) {
\r
53 if (!includeComponent && !includeElement)
\r
54 throw new IllegalArgumentException();
\r
55 this.includeComponent = includeComponent;
\r
56 this.includeElement = includeElement;
\r
63 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
\r
64 ModelingResources MR = ModelingResources.getInstance(graph);
\r
66 ArrayList<Resource> exclusions = new ArrayList<Resource>();
\r
68 ArrayList<NamedResource> roots = new ArrayList<NamedResource>();
\r
70 Resource component = null;
\r
71 Resource element = null;
\r
73 if (includeComponent) {
\r
74 component = getResource();
\r
75 element = graph.getPossibleObject(component, MR.ComponentToElement);
\r
77 element = getResource();
\r
78 component = graph.getPossibleObject(element, MR.ElementToComponent);
\r
80 // Include component as root
\r
81 if (component != null) {
\r
82 processComponent(graph, component, includeComponent, roots, exclusions);
\r
85 if (element != null) {
\r
86 processElement(graph, element, includeElement, roots, exclusions);
\r
89 return TransferableGraphConfiguration2.createWithNames(graph, roots, exclusions, true, false);
\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
100 exclusions.add(component);
\r
103 // filter connections
\r
104 for (Resource connection : graph.getObjects(resource, SR.IsConnectedTo))
\r
105 exclusions.add(connection);
\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
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
118 exclusions.add(element);
\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
125 Resource inv = graph.getInverse(diagram);
\r
126 exclusions.add(diagram);
\r
127 exclusions.add(inv);
\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
137 // filter connections
\r
138 for (Resource connection : graph.getObjects(element, SR.IsConnectedTo))
\r
139 exclusions.add(connection);
\r