]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/adapters/SystemResourcePasteHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / adapters / SystemResourcePasteHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2013 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.graphfile.adapters;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.simantics.Simantics;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.adapter.PasteHandlerAdapter;
24 import org.simantics.db.layer0.util.ClipboardUtils;
25 import org.simantics.db.layer0.util.PasteEventHandler;
26 import org.simantics.db.layer0.util.RemoverUtil;
27 import org.simantics.db.layer0.util.SimanticsClipboard;
28 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
29 import org.simantics.db.layer0.util.SimanticsKeys;
30 import org.simantics.graph.db.IImportAdvisor;
31 import org.simantics.graph.db.TransferableGraphException;
32 import org.simantics.graph.db.TransferableGraphs;
33 import org.simantics.graph.representation.TransferableGraph1;
34
35 public class SystemResourcePasteHandler  extends PasteHandlerAdapter  
36
37 {
38         private Resource resource;
39
40     public SystemResourcePasteHandler(Resource resource) {
41         this.resource = resource;
42     }
43
44     public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
45         TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor);
46     }
47
48     public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
49         TransferableGraphs.importGraph1(graph, tg, advisor);
50     }
51
52     public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
53         defaultExecute(graph, tg, resource, advisor);
54     }
55
56     /**
57      * Override this in your inherited class if post processing is done.
58      */
59     public void onPasteBegin(WriteGraph graph) throws DatabaseException {
60     }
61
62     /**
63      * Override this in your inherited class if post processing is done
64      * advisor.getTarget() returns an object under which data is copied
65      * advisor.getRoot() returns the object which have been pasted.
66      * @param representations TODO
67      */
68     public void onPaste(WriteGraph graph, SystemResourcePasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {
69         advisor.attachRoot(graph);
70     }
71
72     /**
73      * Override this in your inherited class if post processing is done.
74      */
75     public void onPasteEnd(WriteGraph graph) throws DatabaseException{
76     }
77
78     public Collection<Resource> pasteObject(WriteGraph graph, Set<Representation> object) throws DatabaseException {
79         Collection<Resource> result = new ArrayList<Resource>();
80         TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
81         if (tg != null) {
82            SystemResourcePasteImportAdvisor advisor = new SystemResourcePasteImportAdvisor(graph, resource); 
83             execute(graph, tg, resource, advisor);
84             result.add(advisor.getRoot());
85             try {
86                 onPaste(graph, advisor, object);
87             } catch (Throwable e) {
88             }
89         }
90         return result;
91     }
92
93     @Override
94     public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
95         
96         Collection<Resource> result = new ArrayList<Resource>();
97         
98         try {
99             onPasteBegin(graph);
100         } catch (Throwable e) {
101         }
102
103         final Set<Resource> cuts = new HashSet<Resource>();
104         for(Set<Representation> object : clipboard.getContents()) {
105             result.addAll(pasteObject(graph, object));
106             Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
107             if (cut != null)
108                 cuts.addAll(cut);
109         }
110
111         try {
112             onPasteEnd(graph);
113         } catch (Throwable e) {
114         }
115
116         if(!cuts.isEmpty()) {
117                 for (Resource cut : cuts)
118                         RemoverUtil.remove(graph, cut);
119         }
120         
121         return result;
122         
123     }
124
125     @SuppressWarnings("rawtypes")
126     @Override
127     public Object getAdapter(Class adapter) {
128         if(Resource.class == adapter) return resource;
129         return null;
130     }
131
132         
133
134 }