]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementPasteHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentAndElementPasteHandler.java
1 package org.simantics.modeling.mapping;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Set;\r
6 \r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.WriteGraph;\r
10 import org.simantics.db.exception.DatabaseException;\r
11 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
12 import org.simantics.db.layer0.util.ClipboardUtils;\r
13 import org.simantics.db.layer0.util.PasteEventHandler;\r
14 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
15 import org.simantics.db.layer0.util.SimanticsKeys;\r
16 import org.simantics.graph.representation.TransferableGraph1;\r
17 \r
18 /**\r
19  * Paste handler for Component and it's Element.\r
20  * \r
21  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
22  *\r
23  */\r
24 public abstract class ComponentAndElementPasteHandler extends DefaultPasteHandler{\r
25 \r
26         protected boolean includeComponent = true;\r
27         protected boolean includeElement = true;\r
28         \r
29         /**\r
30          * Creates Paste Handler for target composite (Composite where the data is copied).\r
31          * @param composite\r
32          */\r
33         public ComponentAndElementPasteHandler(Resource composite) {\r
34                 super(composite);\r
35         }\r
36         \r
37         /**\r
38          * Creates Paste Handler for target composite (Composite where the data is copied).\r
39          * @param composite\r
40          * @param includeComponent true if component is copied. (component must exist in the source data).\r
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. \r
42          */\r
43         public ComponentAndElementPasteHandler(Resource composite, boolean includeComponent, boolean includeElement) {\r
44                 super(composite);\r
45                 if (!includeComponent && !includeElement)\r
46                         throw new IllegalArgumentException();\r
47                 this.includeElement = includeElement;\r
48                 this.includeComponent = includeComponent;\r
49                 \r
50         }\r
51 \r
52         /**\r
53          * Returns configuration composite\r
54          * \r
55          * TODO: Current Modeling, Structural, or Diagram Ontologies do not have specific relation/type for configuration, so it is impossible to create generic implementation.\r
56          * \r
57          * @param graph\r
58          * @return\r
59          * @throws DatabaseException\r
60          */\r
61         abstract protected  Resource getConfiguration(ReadGraph graph) throws DatabaseException;\r
62         \r
63         public void onPaste(WriteGraph graph, ComponentAndElementPasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {\r
64                 \r
65         }\r
66         \r
67         /**\r
68          * Called if the PasteImportAdviosr had to create the element.\r
69          * This happens if an element was required, but the copied data did not include the element.\r
70          * \r
71          * Override this method if your element requires custom data. \r
72          *    \r
73          * @param graph\r
74          * @param element\r
75          * @throws DatabaseException\r
76          */\r
77         protected void createdElementInAdvisor(WriteGraph graph, Resource element) throws DatabaseException {\r
78                 \r
79         }\r
80         \r
81         @Override\r
82         public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {\r
83                 Collection<Resource> result = new ArrayList<Resource>();\r
84                 \r
85         TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);\r
86         if (tg != null) {\r
87 \r
88                 doPaste(graph, object, tg, result);\r
89         }\r
90         return result;\r
91     }\r
92         \r
93         public void doPaste(WriteGraph graph, Set<Representation> object, TransferableGraph1 tg, Collection<Resource> result) throws DatabaseException{\r
94                 Resource resource = (Resource)getAdapter(Resource.class);\r
95                 Resource configuration = getConfiguration(graph);\r
96         ComponentAndElementPasteImportAdvisor advisor = new ComponentAndElementPasteImportAdvisor(graph, configuration, includeComponent, includeElement) {\r
97                 @Override\r
98                 public void createElement(WriteGraph graph, Resource element)\r
99                                 throws DatabaseException {\r
100                         createdElementInAdvisor(graph, element);\r
101                 }\r
102         }; \r
103         execute(graph, tg, resource, advisor);\r
104         advisor.attach(graph);\r
105         onPaste(graph, advisor, object);\r
106         if (advisor.getComponent() != null)\r
107                 result.add(advisor.getComponent());\r
108         else if (advisor.getElement() != null)\r
109                 result.add(advisor.getElement());\r
110         }\r
111         \r
112         \r
113         \r
114         \r
115 }\r