]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java
Add progress monitoring for copying resources on desktop
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultCopyHandler.java
index 312d4a10c2890cd14f2425a9a4a7657beacd3f98..dfbaf2678e438f671b0508f37886d103195ce8e2 100644 (file)
@@ -17,6 +17,8 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Map;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ResourceRead;
@@ -36,9 +38,12 @@ import org.simantics.graph.db.TransferableGraphSource;
 import org.simantics.graph.db.TransferableGraphs;
 import org.simantics.graph.representation.TransferableGraph1;
 import org.simantics.operation.Layer0X;
+import org.slf4j.LoggerFactory;
 
 public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
 
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DefaultCopyHandler.class);
+    
     protected Collection<Resource> resources;
 
     public DefaultCopyHandler(Resource resource) {
@@ -111,29 +116,39 @@ public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
         };
     }
 
-    protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut) {
-
+    protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut, IProgressMonitor monitor) {
+        SubMonitor subMonitor = SubMonitor.convert(monitor, 10);
+        subMonitor.subTask("Fill tg");
         try {
 
             Representation tgRepresentation = createTransferableGraph(graph, resources, cut);
-            if(tgRepresentation != null) items.add(tgRepresentation);
+            if (tgRepresentation != null)
+                items.add(tgRepresentation);
+            subMonitor.worked(2);
             Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut);
-            if(tgSourceRepresentation != null) items.add(tgSourceRepresentation);
-
-            if(resources.size() == 1) {
-                Collection<Representation> representations = graph.syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {
-                    @Override
-                    public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {
-                        ArrayList<Representation> result = new ArrayList<Representation>();
-                        Layer0X L0X = Layer0X.getInstance(graph);
-                        for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {
-                            result.add(graph.adapt(adapter, Representation.class));
-                        }
-                        return result;
-                    }
-                });
+            if (tgSourceRepresentation != null)
+                items.add(tgSourceRepresentation);
+            subMonitor.worked(2);
+
+            if (resources.size() == 1) {
+                SubMonitor splittedChild = subMonitor.split(6);
+                Collection<Representation> representations = graph
+                        .syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {
+                            @Override
+                            public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {
+                                ArrayList<Representation> result = new ArrayList<Representation>();
+                                Layer0X L0X = Layer0X.getInstance(graph);
+                                for (Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {
+                                    result.add(graph.adapt(adapter, Representation.class));
+                                    splittedChild.worked(1);
+                                }
+                                return result;
+                            }
+                        });
                 for (Representation r : representations)
                     items.add(r);
+            } else {
+                LOGGER.warn("Invalid amount of resources {} for filling tg", resources);
             }
 
         } catch (DatabaseException e) {
@@ -142,28 +157,36 @@ public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
 
     }
 
-    protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items) {
+    protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items, IProgressMonitor monitor) {
+        SubMonitor subMonitor = SubMonitor.convert(monitor, 1);
+        subMonitor.subTask("Fill copy resources");
         items.add(ClipboardUtils.createCopyResources(resources));
+        subMonitor.worked(1);
     }
 
-    protected void fillCutResource(ReadGraph graph, HashSet<Representation> items) {
+    protected void fillCutResource(ReadGraph graph, HashSet<Representation> items, IProgressMonitor monitor) {
+        SubMonitor subMonitor = SubMonitor.convert(monitor, 1);
+        subMonitor.subTask("Fill cut resources");
         items.add(ClipboardUtils.createCutResources(resources));
+        subMonitor.worked(1);
     }
 
     @Override
-    public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
+    public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard, IProgressMonitor monitor) throws DatabaseException {
+        SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
         HashSet<Representation> items = new HashSet<Representation>();
-        fillTG(graph, items, false);
-        fillCopyResource(graph, items);
-        clipboard.addContent(items);
+        fillTG(graph, items, false, subMonitor.split(80));
+        fillCopyResource(graph, items, subMonitor.split(10));
+        clipboard.addContent(items, subMonitor.split(10));
     }
 
     @Override
-    public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
+    public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard, IProgressMonitor monitor) throws DatabaseException {
+        SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
         HashSet<Representation> items = new HashSet<Representation>();
-        fillTG(graph, items, true);
-        fillCutResource(graph, items);
-        clipboard.addContent(items);
+        fillTG(graph, items, true, subMonitor.split(80));
+        fillCutResource(graph, items, subMonitor.split(10));
+        clipboard.addContent(items, subMonitor.split(10));
     }
 
 }