]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/ModelPasteHandler.java
Prevent paste to resources that are `L0.Entity_published`
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / ModelPasteHandler.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.HashSet;
17 import java.util.Set;
18
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;
36
37 @Deprecated
38 public class ModelPasteHandler implements PasteHandler {
39
40         private Resource resource;
41         
42         public ModelPasteHandler(Resource resource) {
43                 this.resource = resource;
44         }
45         
46         @Override
47         public Collection<Resource> pasteFromClipboard(WriteGraph graph, SimanticsClipboard clipboard, PasteEventHandler handler) throws DatabaseException {
48                 throw new UnsupportedOperationException();
49         }
50
51         @Override
52         public Collection<Resource> pasteFromClipboard(SimanticsClipboard clipboard) throws DatabaseException {
53
54                 final Collection<Resource> result = new ArrayList<Resource>();
55                 final Set<Resource> cuts = new HashSet<Resource>();
56                 for(Set<Representation> object : clipboard.getContents()) {
57                         
58                         TransferableGraph1 tg = ClipboardUtils.accept(object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
59                         if(tg != null) {
60
61                                 try {
62                                         
63                                         DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(resource) {
64                                                 
65                                                 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
66
67                                                         SimulationResource SIMU = SimulationResource.getInstance(graph);
68                                                         Resource typeResource = graph.getResource(root.type);
69                                                         if(graph.isInheritedFrom(typeResource, SIMU.Model)) {
70                                                                 library = SimanticsInternal.getProject();
71                                                         }
72                                                         
73                                                 }
74
75                                         };
76                                         
77                                         TransferableGraphs.importGraph1(SimanticsInternal.getSession(), tg, advisor);
78                                         result.addAll(advisor.getRoots());
79                                         
80                                 } catch (TransferableGraphException e) {
81                                         throw new DatabaseException(e);
82                                 }
83                                 
84                         }
85                         
86                         final Collection<Resource> cut = ClipboardUtils.accept(object, SimanticsKeys.KEY_CUT_RESOURCES);
87                         if(cut != null) cuts.addAll(cut);
88                         
89                 }
90                 
91                 if(!cuts.isEmpty()) {
92                         SimanticsInternal.getSession().syncRequest(new WriteRequest() {
93
94                                 @Override
95                                 public void perform(WriteGraph graph) throws DatabaseException {
96                                         for(Resource cut : cuts) graph.deny(cut);
97                                 }
98                                 
99                         });
100                 }
101                 
102                 return result;
103
104         }
105         
106         @SuppressWarnings("unchecked")
107         @Override
108         public <T> T getAdapter(Class<T> adapter) {
109                 if(Resource.class == adapter) return (T) resource;
110                 return null;
111         }
112         
113 }