1 package org.simantics.modeling.mapping;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
12 import org.simantics.db.layer0.util.ClipboardUtils;
13 import org.simantics.db.layer0.util.PasteEventHandler;
14 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
15 import org.simantics.db.layer0.util.SimanticsKeys;
16 import org.simantics.graph.representation.TransferableGraph1;
19 * Paste handler for Component and it's Element.
21 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
24 public abstract class ComponentAndElementPasteHandler extends DefaultPasteHandler{
26 protected boolean includeComponent = true;
27 protected boolean includeElement = true;
30 * Creates Paste Handler for target composite (Composite where the data is copied).
33 public ComponentAndElementPasteHandler(Resource composite) {
38 * Creates Paste Handler for target composite (Composite where the data is copied).
40 * @param includeComponent true if component is copied. (component must exist in the source data).
41 * @param includeElement true if element is copied. If the element is not included in the source data and this flag is true, the element is created.
43 public ComponentAndElementPasteHandler(Resource composite, boolean includeComponent, boolean includeElement) {
45 if (!includeComponent && !includeElement)
46 throw new IllegalArgumentException();
47 this.includeElement = includeElement;
48 this.includeComponent = includeComponent;
53 * Returns configuration composite
55 * TODO: Current Modeling, Structural, or Diagram Ontologies do not have specific relation/type for configuration, so it is impossible to create generic implementation.
59 * @throws DatabaseException
61 abstract protected Resource getConfiguration(ReadGraph graph) throws DatabaseException;
63 public void onPaste(WriteGraph graph, ComponentAndElementPasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {
68 * Called if the PasteImportAdviosr had to create the element.
69 * This happens if an element was required, but the copied data did not include the element.
71 * Override this method if your element requires custom data.
75 * @throws DatabaseException
77 protected void createdElementInAdvisor(WriteGraph graph, Resource element) throws DatabaseException {
82 public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {
83 Collection<Resource> result = new ArrayList<Resource>();
85 TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
88 doPaste(graph, object, tg, result);
93 public void doPaste(WriteGraph graph, Set<Representation> object, TransferableGraph1 tg, Collection<Resource> result) throws DatabaseException{
94 Resource resource = (Resource)getAdapter(Resource.class);
95 Resource configuration = getConfiguration(graph);
96 ComponentAndElementPasteImportAdvisor advisor = new ComponentAndElementPasteImportAdvisor(graph, configuration, includeComponent, includeElement) {
98 public void createElement(WriteGraph graph, Resource element)
99 throws DatabaseException {
100 createdElementInAdvisor(graph, element);
103 execute(graph, tg, resource, advisor);
104 advisor.attach(graph);
105 onPaste(graph, advisor, object);
106 if (advisor.getComponent() != null)
107 result.add(advisor.getComponent());
108 else if (advisor.getElement() != null)
109 result.add(advisor.getElement());