]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java
312d4a10c2890cd14f2425a9a4a7657beacd3f98
[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.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.common.utils.Logger;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.CopyHandler;
26 import org.simantics.db.layer0.adapter.CopyHandler2;
27 import org.simantics.db.layer0.util.ClipboardUtils;
28 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
29 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
30 import org.simantics.db.layer0.util.SimanticsClipboardBuilder;
31 import org.simantics.db.layer0.util.TGRepresentation;
32 import org.simantics.db.layer0.util.TGRepresentationUtils;
33 import org.simantics.db.layer0.util.TGSourceRepresentation;
34 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
35 import org.simantics.graph.db.TransferableGraphSource;
36 import org.simantics.graph.db.TransferableGraphs;
37 import org.simantics.graph.representation.TransferableGraph1;
38 import org.simantics.operation.Layer0X;
39
40 public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
41
42     protected Collection<Resource> resources;
43
44     public DefaultCopyHandler(Resource resource) {
45         this.resources = Collections.singletonList(resource);
46     }
47
48     public DefaultCopyHandler(Collection<Resource> resources) {
49         this.resources = resources;
50     }
51
52     public Resource getResource() {
53         if(resources.size() != 1) throw new IllegalStateException();
54         return resources.iterator().next();
55     }
56
57     protected CopyHandler2 create(Collection<Resource> resources) {
58         return new DefaultCopyHandler(resources);
59     }
60
61     protected boolean ignoreVirtualResources() {
62         return true;
63     }
64
65     @Override
66     public CopyHandler2 combine(CopyHandler2 other_) {
67         if(other_ instanceof DefaultCopyHandler) {
68             DefaultCopyHandler other = (DefaultCopyHandler)other_;
69             ArrayList<Resource> allResources = new ArrayList<Resource>();
70             allResources.addAll(resources);
71             allResources.addAll(other.resources);
72             return create(allResources);
73         }
74         return null;
75     }
76
77     protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
78                 if(cut) return null;
79         return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false);
80     }
81
82     protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
83         return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
84     }
85
86     protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
87         return TransferableGraphs.create(graph, computeSource(graph, conf));
88     }
89
90     protected Representation createTransferableGraph(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
91         final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
92         if(conf == null) return null;
93         return new TGRepresentation(resources.toArray(new Resource[resources.size()])) {
94             @Override
95             public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
96                 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
97                 return DefaultCopyHandler.this.compute(graph, conf);
98             }
99         };
100     }
101
102     protected Representation createTransferableGraphSource(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
103         final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
104         if(conf == null) return null;
105         return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) {
106             @Override
107             public TransferableGraphSource compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
108                 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
109                 return DefaultCopyHandler.this.computeSource(graph, conf);
110             }
111         };
112     }
113
114     protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut) {
115
116         try {
117
118             Representation tgRepresentation = createTransferableGraph(graph, resources, cut);
119             if(tgRepresentation != null) items.add(tgRepresentation);
120             Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut);
121             if(tgSourceRepresentation != null) items.add(tgSourceRepresentation);
122
123             if(resources.size() == 1) {
124                 Collection<Representation> representations = graph.syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {
125                     @Override
126                     public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {
127                         ArrayList<Representation> result = new ArrayList<Representation>();
128                         Layer0X L0X = Layer0X.getInstance(graph);
129                         for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {
130                             result.add(graph.adapt(adapter, Representation.class));
131                         }
132                         return result;
133                     }
134                 });
135                 for (Representation r : representations)
136                     items.add(r);
137             }
138
139         } catch (DatabaseException e) {
140             Logger.defaultLogError(e);
141         }
142
143     }
144
145     protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items) {
146         items.add(ClipboardUtils.createCopyResources(resources));
147     }
148
149     protected void fillCutResource(ReadGraph graph, HashSet<Representation> items) {
150         items.add(ClipboardUtils.createCutResources(resources));
151     }
152
153     @Override
154     public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
155         HashSet<Representation> items = new HashSet<Representation>();
156         fillTG(graph, items, false);
157         fillCopyResource(graph, items);
158         clipboard.addContent(items);
159     }
160
161     @Override
162     public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
163         HashSet<Representation> items = new HashSet<Representation>();
164         fillTG(graph, items, true);
165         fillCutResource(graph, items);
166         clipboard.addContent(items);
167     }
168
169 }