]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultCopyHandler.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java
new file mode 100644 (file)
index 0000000..2b9d20d
--- /dev/null
@@ -0,0 +1,169 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.adapter.impl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.CopyHandler;\r
+import org.simantics.db.layer0.adapter.CopyHandler2;\r
+import org.simantics.db.layer0.util.ClipboardUtils;\r
+import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
+import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
+import org.simantics.db.layer0.util.SimanticsClipboardBuilder;\r
+import org.simantics.db.layer0.util.TGRepresentation;\r
+import org.simantics.db.layer0.util.TGRepresentationUtils;\r
+import org.simantics.db.layer0.util.TGSourceRepresentation;\r
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
+import org.simantics.graph.db.TransferableGraphSource;\r
+import org.simantics.graph.db.TransferableGraphs;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.operation.Layer0X;\r
+\r
+public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {\r
+\r
+    protected Collection<Resource> resources;\r
+\r
+    public DefaultCopyHandler(Resource resource) {\r
+        this.resources = Collections.singletonList(resource);\r
+    }\r
+\r
+    public DefaultCopyHandler(Collection<Resource> resources) {\r
+        this.resources = resources;\r
+    }\r
+\r
+    public Resource getResource() {\r
+        if(resources.size() != 1) throw new IllegalStateException();\r
+        return resources.iterator().next();\r
+    }\r
+\r
+    protected CopyHandler2 create(Collection<Resource> resources) {\r
+        return new DefaultCopyHandler(resources);\r
+    }\r
+\r
+    protected boolean ignoreVirtualResources() {\r
+        return true;\r
+    }\r
+\r
+    @Override\r
+    public CopyHandler2 combine(CopyHandler2 other_) {\r
+        if(other_ instanceof DefaultCopyHandler) {\r
+            DefaultCopyHandler other = (DefaultCopyHandler)other_;\r
+            ArrayList<Resource> allResources = new ArrayList<Resource>();\r
+            allResources.addAll(resources);\r
+            allResources.addAll(other.resources);\r
+            return create(allResources);\r
+        }\r
+        return null;\r
+    }\r
+\r
+    protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {\r
+               if(cut) return null;\r
+        return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false);\r
+    }\r
+\r
+    protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {\r
+        return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));\r
+    }\r
+\r
+    protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {\r
+        return TransferableGraphs.create(graph, computeSource(graph, conf));\r
+    }\r
+\r
+    protected Representation createTransferableGraph(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {\r
+        final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);\r
+        if(conf == null) return null;\r
+        return new TGRepresentation(resources.toArray(new Resource[resources.size()])) {\r
+            @Override\r
+            public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {\r
+                conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);\r
+                return DefaultCopyHandler.this.compute(graph, conf);\r
+            }\r
+        };\r
+    }\r
+\r
+    protected Representation createTransferableGraphSource(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {\r
+        final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);\r
+        if(conf == null) return null;\r
+        return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) {\r
+            @Override\r
+            public TransferableGraphSource compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {\r
+                conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);\r
+                return DefaultCopyHandler.this.computeSource(graph, conf);\r
+            }\r
+        };\r
+    }\r
+\r
+    protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut) {\r
+\r
+        try {\r
+\r
+            Representation tgRepresentation = createTransferableGraph(graph, resources, cut);\r
+            if(tgRepresentation != null) items.add(tgRepresentation);\r
+            Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut);\r
+            if(tgSourceRepresentation != null) items.add(tgSourceRepresentation);\r
+\r
+            if(resources.size() == 1) {\r
+                Collection<Representation> representations = graph.syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {\r
+                    @Override\r
+                    public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {\r
+                        ArrayList<Representation> result = new ArrayList<Representation>();\r
+                        Layer0X L0X = Layer0X.getInstance(graph);\r
+                        for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {\r
+                            result.add(graph.adapt(adapter, Representation.class));\r
+                        }\r
+                        return result;\r
+                    }\r
+                });\r
+                for (Representation r : representations)\r
+                    items.add(r);\r
+            }\r
+\r
+        } catch (DatabaseException e) {\r
+            Logger.defaultLogError(e);\r
+        }\r
+\r
+    }\r
+\r
+    protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items) {\r
+        items.add(ClipboardUtils.createCopyResources(resources));\r
+    }\r
+\r
+    protected void fillCutResource(ReadGraph graph, HashSet<Representation> items) {\r
+        items.add(ClipboardUtils.createCutResources(resources));\r
+    }\r
+\r
+    @Override\r
+    public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {\r
+        HashSet<Representation> items = new HashSet<Representation>();\r
+        fillTG(graph, items, false);\r
+        fillCopyResource(graph, items);\r
+        clipboard.addContent(items);\r
+    }\r
+\r
+    @Override\r
+    public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {\r
+        HashSet<Representation> items = new HashSet<Representation>();\r
+        fillTG(graph, items, true);\r
+        fillCutResource(graph, items);\r
+        clipboard.addContent(items);\r
+    }\r
+\r
+}\r