-package org.simantics.diagram.adapter;\r
-\r
-import java.util.Collection;\r
-import java.util.Set;\r
-\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.CommentMetadata;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.utils.ListUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
-import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
-import org.simantics.diagram.internal.Activator;\r
-import org.simantics.graph.db.IImportAdvisor2;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.scenegraph.ontology.ScenegraphResources;\r
-\r
-/**\r
- * @author Teemu Mätäsniemi\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ScenegraphCompositePasteHandler extends DefaultPasteHandler {\r
-\r
- int copied;\r
-\r
- public ScenegraphCompositePasteHandler(Resource resource) {\r
- super(resource);\r
- }\r
-\r
- @Override\r
- public void onPasteBegin(WriteGraph graph) {\r
- copied = 0;\r
- }\r
-\r
- @Override\r
- public void onPaste(WriteGraph graph, IImportAdvisor2 advisor, Set<Representation> representations) {\r
- final Layer0 L0 = Layer0.getInstance(graph);\r
- final ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
-\r
- try {\r
- final Resource parent = advisor.getTarget();\r
- if (parent == null)\r
- return;\r
-\r
- final Collection<Resource> roots = advisor.getRoots();\r
- if (roots == null || roots.isEmpty())\r
- return;\r
-\r
- // Validate that the pasted roots are scene graph nodes.\r
- for (Resource root : roots) {\r
- if (!graph.isInstanceOf(root, SG.Node))\r
- return;\r
- }\r
-\r
- final Resource list = graph.syncRequest(new UniqueRead<Resource>() {\r
- @Override\r
- public Resource perform(ReadGraph g) throws DatabaseException {\r
- Resource res = g.getPossibleObject(parent, SG.Node_children);\r
- return (res == null || !g.isInstanceOf(res, L0.List)) ? null : res;\r
- }\r
- });\r
-\r
- if (list == null)\r
- return;\r
-\r
- ListUtils.insertBack(graph, list, roots);\r
- copied += roots.size();\r
-\r
- } catch (DatabaseException e) {\r
- Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to attach copied scene graph nodes to target composite node, see exception for details.", e));\r
- }\r
- }\r
-\r
- @Override\r
- public void onPasteEnd(WriteGraph graph) {\r
- if (copied > 0) {\r
- try {\r
- CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
- graph.addMetadata(cm.add("Copy " + copied + " scene graph nodes."));\r
- } catch (DatabaseException e) {\r
- Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to attach metadata to commit, see exception for details.", e));\r
- }\r
- }\r
- }\r
-\r
-}\r
+package org.simantics.diagram.adapter;
+
+import java.util.Collection;
+import java.util.Set;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.ListUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
+import org.simantics.diagram.internal.Activator;
+import org.simantics.graph.db.IImportAdvisor2;
+import org.simantics.layer0.Layer0;
+import org.simantics.scenegraph.ontology.ScenegraphResources;
+
+/**
+ * @author Teemu Mätäsniemi
+ * @author Tuukka Lehtonen
+ */
+public class ScenegraphCompositePasteHandler extends DefaultPasteHandler {
+
+ int copied;
+
+ public ScenegraphCompositePasteHandler(Resource resource) {
+ super(resource);
+ }
+
+ @Override
+ public void onPasteBegin(WriteGraph graph) {
+ copied = 0;
+ }
+
+ @Override
+ public void onPaste(WriteGraph graph, IImportAdvisor2 advisor, Set<Representation> representations) {
+ final Layer0 L0 = Layer0.getInstance(graph);
+ final ScenegraphResources SG = ScenegraphResources.getInstance(graph);
+
+ try {
+ final Resource parent = advisor.getTarget();
+ if (parent == null)
+ return;
+
+ final Collection<Resource> roots = advisor.getRoots();
+ if (roots == null || roots.isEmpty())
+ return;
+
+ // Validate that the pasted roots are scene graph nodes.
+ for (Resource root : roots) {
+ if (!graph.isInstanceOf(root, SG.Node))
+ return;
+ }
+
+ final Resource list = graph.syncRequest(new UniqueRead<Resource>() {
+ @Override
+ public Resource perform(ReadGraph g) throws DatabaseException {
+ Resource res = g.getPossibleObject(parent, SG.Node_children);
+ return (res == null || !g.isInstanceOf(res, L0.List)) ? null : res;
+ }
+ });
+
+ if (list == null)
+ return;
+
+ ListUtils.insertBack(graph, list, roots);
+ copied += roots.size();
+
+ } catch (DatabaseException e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to attach copied scene graph nodes to target composite node, see exception for details.", e));
+ }
+ }
+
+ @Override
+ public void onPasteEnd(WriteGraph graph) {
+ if (copied > 0) {
+ try {
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+ graph.addMetadata(cm.add("Copy " + copied + " scene graph nodes."));
+ } catch (DatabaseException e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to attach metadata to commit, see exception for details.", e));
+ }
+ }
+ }
+
+}