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