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