1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphfile.adapters;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
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;
35 public class SystemResourcePasteHandler extends PasteHandlerAdapter
38 private Resource resource;
40 public SystemResourcePasteHandler(Resource resource) {
41 this.resource = resource;
44 public static void defaultExecute(TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
45 TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor);
48 public static void defaultExecute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
49 TransferableGraphs.importGraph1(graph, tg, advisor);
52 public void execute(WriteGraph graph, TransferableGraph1 tg, Resource resource, IImportAdvisor advisor) throws DatabaseException {
53 defaultExecute(graph, tg, resource, advisor);
57 * Override this in your inherited class if post processing is done.
59 public void onPasteBegin(WriteGraph graph) throws DatabaseException {
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
68 public void onPaste(WriteGraph graph, SystemResourcePasteImportAdvisor advisor, Set<Representation> representations) throws DatabaseException {
69 advisor.attachRoot(graph);
73 * Override this in your inherited class if post processing is done.
75 public void onPasteEnd(WriteGraph graph) throws DatabaseException{
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);
82 SystemResourcePasteImportAdvisor advisor = new SystemResourcePasteImportAdvisor(graph, resource);
83 execute(graph, tg, resource, advisor);
84 result.add(advisor.getRoot());
86 onPaste(graph, advisor, object);
87 } catch (Throwable e) {
94 public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
96 Collection<Resource> result = new ArrayList<Resource>();
100 } catch (Throwable e) {
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);
113 } catch (Throwable e) {
116 if(!cuts.isEmpty()) {
117 for (Resource cut : cuts)
118 RemoverUtil.remove(graph, cut);
125 @SuppressWarnings("rawtypes")
127 public Object getAdapter(Class adapter) {
128 if(Resource.class == adapter) return resource;