1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.db.layer0.adapter.impl;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.adapter.PasteHandler;
25 import org.simantics.db.layer0.internal.SimanticsInternal;
26 import org.simantics.db.layer0.util.ClipboardUtils;
27 import org.simantics.db.layer0.util.PasteEventHandler;
28 import org.simantics.db.layer0.util.SimanticsClipboard;
29 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
30 import org.simantics.db.layer0.util.SimanticsKeys;
31 import org.simantics.graph.db.TransferableGraphException;
32 import org.simantics.graph.db.TransferableGraphs;
33 import org.simantics.graph.representation.Root;
34 import org.simantics.graph.representation.TransferableGraph1;
35 import org.simantics.simulation.ontology.SimulationResource;
38 public class ModelPasteHandler implements PasteHandler {
40 private Resource resource;
42 public ModelPasteHandler(Resource resource) {
43 this.resource = resource;
47 public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
48 throw new UnsupportedOperationException();
52 public Collection<Resource> pasteFromClipboard(SimanticsClipboard clipboard) throws DatabaseException {
54 final Collection<Resource> result = new ArrayList<Resource>();
55 final Set<Resource> cuts = new HashSet<Resource>();
56 for(Set<Representation> object : clipboard.getContents()) {
58 TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
63 DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(resource) {
65 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
67 SimulationResource SIMU = SimulationResource.getInstance(graph);
68 Resource typeResource = graph.getResource(root.type);
69 if(graph.isInheritedFrom(typeResource, SIMU.Model)) {
70 library = SimanticsInternal.getProject();
77 TransferableGraphs.importGraph1(SimanticsInternal.getSession(), tg, advisor);
78 result.addAll(advisor.getRoots());
80 } catch (TransferableGraphException e) {
81 throw new DatabaseException(e);
86 final Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
87 if(cut != null) cuts.addAll(cut);
92 SimanticsInternal.getSession().syncRequest(new WriteRequest() {
95 public void perform(WriteGraph graph) throws DatabaseException {
96 for(Resource cut : cuts) graph.deny(cut);
106 @SuppressWarnings("unchecked")
108 public <T> T getAdapter(Class<T> adapter) {
109 if(Resource.class == adapter) return (T) resource;