]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.ResourceRead;
25 import org.simantics.db.common.utils.Logger;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.adapter.CopyHandler;
28 import org.simantics.db.layer0.adapter.CopyHandler2;
29 import org.simantics.db.layer0.util.ClipboardUtils;
30 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
31 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
32 import org.simantics.db.layer0.util.SimanticsClipboardBuilder;
33 import org.simantics.db.layer0.util.TGRepresentation;
34 import org.simantics.db.layer0.util.TGRepresentationUtils;
35 import org.simantics.db.layer0.util.TGSourceRepresentation;
36 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
37 import org.simantics.graph.db.TransferableGraphSource;
38 import org.simantics.graph.db.TransferableGraphs;
39 import org.simantics.graph.representation.TransferableGraph1;
40 import org.simantics.operation.Layer0X;
41 import org.slf4j.LoggerFactory;
42
43 public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
44
45     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DefaultCopyHandler.class);
46     
47     protected Collection<Resource> resources;
48
49     public DefaultCopyHandler(Resource resource) {
50         this.resources = Collections.singletonList(resource);
51     }
52
53     public DefaultCopyHandler(Collection<Resource> resources) {
54         this.resources = resources;
55     }
56
57     public Resource getResource() {
58         if(resources.size() != 1) throw new IllegalStateException();
59         return resources.iterator().next();
60     }
61
62     protected CopyHandler2 create(Collection<Resource> resources) {
63         return new DefaultCopyHandler(resources);
64     }
65
66     protected boolean ignoreVirtualResources() {
67         return true;
68     }
69
70     @Override
71     public CopyHandler2 combine(CopyHandler2 other_) {
72         if(other_ instanceof DefaultCopyHandler) {
73             DefaultCopyHandler other = (DefaultCopyHandler)other_;
74             ArrayList<Resource> allResources = new ArrayList<Resource>();
75             allResources.addAll(resources);
76             allResources.addAll(other.resources);
77             return create(allResources);
78         }
79         return null;
80     }
81
82     protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
83                 if(cut) return null;
84         return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false);
85     }
86
87     protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
88         return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
89     }
90
91     protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
92         return TransferableGraphs.create(graph, computeSource(graph, conf));
93     }
94
95     protected Representation createTransferableGraph(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
96         final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
97         if(conf == null) return null;
98         return new TGRepresentation(resources.toArray(new Resource[resources.size()])) {
99             @Override
100             public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
101                 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
102                 return DefaultCopyHandler.this.compute(graph, conf);
103             }
104         };
105     }
106
107     protected Representation createTransferableGraphSource(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
108         final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
109         if(conf == null) return null;
110         return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) {
111             @Override
112             public TransferableGraphSource compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
113                 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
114                 return DefaultCopyHandler.this.computeSource(graph, conf);
115             }
116         };
117     }
118
119     protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut, IProgressMonitor monitor) {
120         SubMonitor subMonitor = SubMonitor.convert(monitor, 10);
121         subMonitor.subTask("Fill tg");
122         try {
123
124             Representation tgRepresentation = createTransferableGraph(graph, resources, cut);
125             if (tgRepresentation != null)
126                 items.add(tgRepresentation);
127             subMonitor.worked(2);
128             Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut);
129             if (tgSourceRepresentation != null)
130                 items.add(tgSourceRepresentation);
131             subMonitor.worked(2);
132
133             if (resources.size() == 1) {
134                 SubMonitor splittedChild = subMonitor.split(6);
135                 Collection<Representation> representations = graph
136                         .syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {
137                             @Override
138                             public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {
139                                 ArrayList<Representation> result = new ArrayList<Representation>();
140                                 Layer0X L0X = Layer0X.getInstance(graph);
141                                 for (Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {
142                                     result.add(graph.adapt(adapter, Representation.class));
143                                     splittedChild.worked(1);
144                                 }
145                                 return result;
146                             }
147                         });
148                 for (Representation r : representations)
149                     items.add(r);
150             } else {
151                 LOGGER.warn("Invalid amount of resources {} for filling tg", resources);
152             }
153
154         } catch (DatabaseException e) {
155             Logger.defaultLogError(e);
156         }
157
158     }
159
160     protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items, IProgressMonitor monitor) {
161         SubMonitor subMonitor = SubMonitor.convert(monitor, 1);
162         subMonitor.subTask("Fill copy resources");
163         items.add(ClipboardUtils.createCopyResources(resources));
164         subMonitor.worked(1);
165     }
166
167     protected void fillCutResource(ReadGraph graph, HashSet<Representation> items, IProgressMonitor monitor) {
168         SubMonitor subMonitor = SubMonitor.convert(monitor, 1);
169         subMonitor.subTask("Fill cut resources");
170         items.add(ClipboardUtils.createCutResources(resources));
171         subMonitor.worked(1);
172     }
173
174     @Override
175     public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard, IProgressMonitor monitor) throws DatabaseException {
176         SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
177         HashSet<Representation> items = new HashSet<Representation>();
178         fillTG(graph, items, false, subMonitor.split(80));
179         fillCopyResource(graph, items, subMonitor.split(10));
180         clipboard.addContent(items, subMonitor.split(10));
181     }
182
183     @Override
184     public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard, IProgressMonitor monitor) throws DatabaseException {
185         SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
186         HashSet<Representation> items = new HashSet<Representation>();
187         fillTG(graph, items, true, subMonitor.split(80));
188         fillCutResource(graph, items, subMonitor.split(10));
189         clipboard.addContent(items, subMonitor.split(10));
190     }
191
192 }