]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementPasteHandler.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentAndElementPasteHandler.java
1 package org.simantics.modeling.mapping;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Set;
6
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;
17
18 /**
19  * Paste handler for Component and it's Element.
20  * 
21  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
22  *
23  */
24 public abstract class ComponentAndElementPasteHandler extends DefaultPasteHandler{
25
26         protected boolean includeComponent = true;
27         protected boolean includeElement = true;
28         
29         /**
30          * Creates Paste Handler for target composite (Composite where the data is copied).
31          * @param composite
32          */
33         public ComponentAndElementPasteHandler(Resource composite) {
34                 super(composite);
35         }
36         
37         /**
38          * Creates Paste Handler for target composite (Composite where the data is copied).
39          * @param composite
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. 
42          */
43         public ComponentAndElementPasteHandler(Resource composite, boolean includeComponent, boolean includeElement) {
44                 super(composite);
45                 if (!includeComponent && !includeElement)
46                         throw new IllegalArgumentException();
47                 this.includeElement = includeElement;
48                 this.includeComponent = includeComponent;
49                 
50         }
51
52         /**
53          * Returns configuration composite
54          * 
55          * TODO: Current Modeling, Structural, or Diagram Ontologies do not have specific relation/type for configuration, so it is impossible to create generic implementation.
56          * 
57          * @param graph
58          * @return
59          * @throws DatabaseException
60          */
61         abstract protected  Resource getConfiguration(ReadGraph graph) throws DatabaseException;
62         
63         public void onPaste(WriteGraph graph, ComponentAndElementPasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {
64                 
65         }
66         
67         /**
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.
70          * 
71          * Override this method if your element requires custom data. 
72          *    
73          * @param graph
74          * @param element
75          * @throws DatabaseException
76          */
77         protected void createdElementInAdvisor(WriteGraph graph, Resource element) throws DatabaseException {
78                 
79         }
80         
81         @Override
82         public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {
83                 Collection<Resource> result = new ArrayList<Resource>();
84                 
85         TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
86         if (tg != null) {
87
88                 doPaste(graph, object, tg, result);
89         }
90         return result;
91     }
92         
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) {
97                 @Override
98                 public void createElement(WriteGraph graph, Resource element)
99                                 throws DatabaseException {
100                         createdElementInAdvisor(graph, element);
101                 }
102         }; 
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());
110         }
111         
112         
113         
114         
115 }