]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementPasteHandler.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentAndElementPasteHandler.java
new file mode 100644 (file)
index 0000000..f24454b
--- /dev/null
@@ -0,0 +1,115 @@
+package org.simantics.modeling.mapping;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.util.ClipboardUtils;\r
+import org.simantics.db.layer0.util.PasteEventHandler;\r
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
+import org.simantics.db.layer0.util.SimanticsKeys;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+\r
+/**\r
+ * Paste handler for Component and it's Element.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public abstract class ComponentAndElementPasteHandler extends DefaultPasteHandler{\r
+\r
+       protected boolean includeComponent = true;\r
+       protected boolean includeElement = true;\r
+       \r
+       /**\r
+        * Creates Paste Handler for target composite (Composite where the data is copied).\r
+        * @param composite\r
+        */\r
+       public ComponentAndElementPasteHandler(Resource composite) {\r
+               super(composite);\r
+       }\r
+       \r
+       /**\r
+        * Creates Paste Handler for target composite (Composite where the data is copied).\r
+        * @param composite\r
+        * @param includeComponent true if component is copied. (component must exist in the source data).\r
+        * @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
+        */\r
+       public ComponentAndElementPasteHandler(Resource composite, boolean includeComponent, boolean includeElement) {\r
+               super(composite);\r
+               if (!includeComponent && !includeElement)\r
+                       throw new IllegalArgumentException();\r
+               this.includeElement = includeElement;\r
+               this.includeComponent = includeComponent;\r
+               \r
+       }\r
+\r
+       /**\r
+        * Returns configuration composite\r
+        * \r
+        * TODO: Current Modeling, Structural, or Diagram Ontologies do not have specific relation/type for configuration, so it is impossible to create generic implementation.\r
+        * \r
+        * @param graph\r
+        * @return\r
+        * @throws DatabaseException\r
+        */\r
+       abstract protected  Resource getConfiguration(ReadGraph graph) throws DatabaseException;\r
+       \r
+       public void onPaste(WriteGraph graph, ComponentAndElementPasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {\r
+               \r
+       }\r
+       \r
+       /**\r
+        * Called if the PasteImportAdviosr had to create the element.\r
+        * This happens if an element was required, but the copied data did not include the element.\r
+        * \r
+        * Override this method if your element requires custom data. \r
+        *    \r
+        * @param graph\r
+        * @param element\r
+        * @throws DatabaseException\r
+        */\r
+       protected void createdElementInAdvisor(WriteGraph graph, Resource element) throws DatabaseException {\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {\r
+               Collection<Resource> result = new ArrayList<Resource>();\r
+               \r
+        TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);\r
+        if (tg != null) {\r
+\r
+               doPaste(graph, object, tg, result);\r
+        }\r
+        return result;\r
+    }\r
+       \r
+       public void doPaste(WriteGraph graph, Set<Representation> object, TransferableGraph1 tg, Collection<Resource> result) throws DatabaseException{\r
+               Resource resource = (Resource)getAdapter(Resource.class);\r
+               Resource configuration = getConfiguration(graph);\r
+        ComponentAndElementPasteImportAdvisor advisor = new ComponentAndElementPasteImportAdvisor(graph, configuration, includeComponent, includeElement) {\r
+               @Override\r
+               public void createElement(WriteGraph graph, Resource element)\r
+                               throws DatabaseException {\r
+                       createdElementInAdvisor(graph, element);\r
+               }\r
+        }; \r
+        execute(graph, tg, resource, advisor);\r
+        advisor.attach(graph);\r
+        onPaste(graph, advisor, object);\r
+        if (advisor.getComponent() != null)\r
+               result.add(advisor.getComponent());\r
+        else if (advisor.getElement() != null)\r
+               result.add(advisor.getElement());\r
+       }\r
+       \r
+       \r
+       \r
+       \r
+}\r