]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultPasteHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultPasteHandler.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 import java.util.Set;\r
20 \r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.adapter.PasteHandlerAdapter;\r
26 import org.simantics.db.layer0.internal.SimanticsInternal;\r
27 import org.simantics.db.layer0.util.ClipboardUtils;\r
28 import org.simantics.db.layer0.util.ModelTransferableGraphSource;\r
29 import org.simantics.db.layer0.util.PasteEventHandler;\r
30 import org.simantics.db.layer0.util.RemoverUtil;\r
31 import org.simantics.db.layer0.util.SimanticsClipboard;\r
32 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
33 import org.simantics.db.layer0.util.SimanticsKeys;\r
34 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.RootSpec;\r
35 import org.simantics.graph.db.IImportAdvisor;\r
36 import org.simantics.graph.db.IImportAdvisor2;\r
37 import org.simantics.graph.db.IImportAdvisor2.RootInfo;\r
38 import org.simantics.graph.db.TransferableGraphException;\r
39 import org.simantics.graph.db.TransferableGraphSource;\r
40 import org.simantics.graph.db.TransferableGraphs;\r
41 import org.simantics.graph.representation.Identity;\r
42 import org.simantics.graph.representation.Root;\r
43 import org.simantics.graph.representation.TransferableGraph1;\r
44 import org.simantics.graph.representation.TransferableGraphUtils;\r
45 import org.simantics.layer0.Layer0;\r
46 \r
47 public class DefaultPasteHandler extends PasteHandlerAdapter {\r
48 \r
49     protected Resource resource;\r
50 \r
51     public DefaultPasteHandler(Resource resource) {\r
52         this.resource = resource;\r
53     }\r
54 \r
55     public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {\r
56         TransferableGraphs.importGraph1(SimanticsInternal.getSession(), tg, advisor);\r
57     }\r
58 \r
59     public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {\r
60         TransferableGraphs.importGraph1(graph, tg, advisor);\r
61     }\r
62 \r
63     public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {\r
64         defaultExecute(graph, tg, resource, advisor);\r
65     }\r
66 \r
67     /**\r
68      * Override this in your inherited class if post processing is done.\r
69      */\r
70     public void onPasteBegin(WriteGraph graph) throws DatabaseException {\r
71     }\r
72 \r
73     /**\r
74      * Override this in your inherited class if post processing is done\r
75      * advisor.getTarget() returns an object under which data is copied\r
76      * advisor.getRoot() returns the object which have been pasted.\r
77      * @param representations TODO\r
78      */\r
79     public void onPaste(WriteGraph graph, IImportAdvisor2 advisor, Set<Representation> representations) throws DatabaseException {\r
80     }\r
81 \r
82     /**\r
83      * Override this in your inherited class if post processing is done.\r
84      */\r
85     public void onPasteEnd(WriteGraph graph) throws DatabaseException {\r
86     }\r
87     \r
88     public IImportAdvisor2 getAdvisor(ReadGraph graph, TransferableGraph1 tg, Resource target, PasteEventHandler handler) throws DatabaseException {\r
89         \r
90         Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);\r
91         if(roots.size() == 1) {\r
92                 Root root = (Root)roots.iterator().next().definition;\r
93                 Resource type = graph.getPossibleResource(root.type);\r
94                 ImportAdvisorFactory factory = graph.getPossibleAdapter(type, ImportAdvisorFactory.class);\r
95                 if(factory != null) {\r
96                         if(handler != null)\r
97                                 return handler.createAdvisor(graph, factory, target);\r
98                         else\r
99                                 return factory.create(graph, target, Collections.<String,Object>emptyMap());\r
100                 }\r
101         }\r
102 \r
103         return new DefaultPasteImportAdvisor(target);\r
104     }\r
105 \r
106     public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object, PasteEventHandler handler) throws DatabaseException {\r
107         Collection<Resource> result = new ArrayList<>();\r
108         TransferableGraphSource tgs = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE);\r
109         if (tgs != null) {\r
110                 TransferableGraph1 tg = TransferableGraphs.create(graph, tgs);\r
111             IImportAdvisor2 advisor = getAdvisor(graph, tg, resource, handler); \r
112             execute(graph, tg, resource, advisor);\r
113             Collection<RootInfo> roots = advisor.getRootInfo();\r
114             if(handler != null) {\r
115                 for(RootInfo r : roots) handler.postProcess(graph, r.resource);\r
116             }\r
117             onPaste(graph, advisor, object);\r
118             if(tgs instanceof ModelTransferableGraphSource) {\r
119                 \r
120                 ModelTransferableGraphSource mtgs = (ModelTransferableGraphSource)tgs;\r
121                 \r
122                 loop: for(RootSpec spec : mtgs.getConfiguration().roots) {\r
123                         if(!spec.internal) continue;\r
124                         for(RootInfo info : roots) {\r
125                                 if(spec.name.equals(info.root.name)) {\r
126                                         result.add(info.resource);\r
127                                         continue loop;\r
128                                 }\r
129                         }\r
130                                 // Fallback\r
131                                 result.clear();\r
132                                 result.addAll(advisor.getRoots());\r
133                                 break;\r
134                 }\r
135                 \r
136             } else {\r
137                 result.addAll(advisor.getRoots());\r
138             }\r
139         }\r
140         return result;\r
141     }\r
142 \r
143     @Override\r
144     public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {\r
145         \r
146         Collection<Resource> result = new ArrayList<>();\r
147         \r
148         Map<String,Object> hints = Collections.singletonMap(ClipboardUtils.HINT_TARGET_RESOURCE, resource);\r
149         \r
150         onPasteBegin(graph);\r
151 \r
152         final Set<Resource> cuts = new HashSet<>();\r
153         for(Set<Representation> object : clipboard.getContents()) {\r
154             Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);\r
155             TransferableGraphSource tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE, hints);\r
156             if(tg != null) {\r
157                 result.addAll(pasteObject(graph, object, handler));\r
158                 if (cut != null)\r
159                     cuts.addAll(cut);\r
160             } else {\r
161                 Layer0 L0 = Layer0.getInstance(graph);\r
162                 for(Resource r : cut) {\r
163                         graph.deny(r, L0.PartOf);\r
164                         graph.claim(resource, L0.ConsistsOf, L0.PartOf, r);\r
165                         result.add(r);\r
166                 }\r
167             }\r
168         }\r
169 \r
170         onPasteEnd(graph);\r
171 \r
172         if(!cuts.isEmpty()) {\r
173                 for (Resource cut : cuts)\r
174                         RemoverUtil.remove(graph, cut);\r
175         }\r
176         \r
177         return result;\r
178         \r
179     }\r
180 \r
181     @SuppressWarnings("rawtypes")\r
182     @Override\r
183     public Object getAdapter(Class adapter) {\r
184         if(Resource.class == adapter) return resource;\r
185         return null;\r
186     }\r
187 \r
188 }\r