]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ClipboardUtils.java
Speeding up platform startup time
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ClipboardUtils.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.util;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.simantics.databoard.adapter.AdaptException;
23 import org.simantics.databoard.container.DataContainer;
24 import org.simantics.databoard.container.DataContainers;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.RequestProcessor;
27 import org.simantics.db.Resource;
28 import org.simantics.db.common.request.ResourceRead;
29 import org.simantics.db.common.utils.Logger;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.adapter.PasteHandler;
32 import org.simantics.db.layer0.internal.SimanticsInternal;
33 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
34 import org.simantics.db.layer0.variable.Variable;
35 import org.simantics.db.request.Read;
36 import org.simantics.graph.representation.TransferableGraph1;
37 import org.simantics.utils.datastructures.hints.IHintContext.Key;
38
39 public class ClipboardUtils {
40         
41         public static String HINT_TARGET_RESOURCE = "HINT_TARGET_RESOURCE";
42
43         public static <T> T accept(Set<Representation> set, Key key) throws DatabaseException {
44                 return accept(set, key, Collections.emptyMap());
45         }
46
47         public static <T> T accept(Set<Representation> set, Key key, Map<String,Object> hints) throws DatabaseException {
48                 for(Representation r : set) if(r.getKey().equals(key)) return r.getValue(SimanticsInternal.getSession(), hints);
49                 return null;
50         }
51         
52         public static <T> T accept(RequestProcessor processor, Set<Representation> set, Key key) throws DatabaseException {
53                 return accept(processor, set, key, Collections.emptyMap());
54         }
55
56         public static <T> T accept(RequestProcessor processor, Set<Representation> set, Key key, Map<String,Object> hints) throws DatabaseException {
57                 for(Representation r : set) if(r.getKey().equals(key)) return r.getValue(processor, hints);
58                 return null;
59         }
60
61         public static <T> T accept(ReadGraph graph, Set<Representation> set, Key key) throws DatabaseException {
62                 return accept(graph, set, key, Collections.<String, Object>emptyMap());
63         }
64
65         public static <T> T accept(ReadGraph graph, Set<Representation> set, Key key, Map<String,Object> hints) throws DatabaseException {
66                 for(Representation r : set) if(r.getKey().equals(key)) return r.getValue(graph, hints);
67                 return null;
68         }
69         
70         public static <T> T acceptPossible(ReadGraph graph, Key key) throws DatabaseException {
71                 return acceptPossible(graph, key, Collections.emptyMap());
72         }
73
74         public static <T> T acceptPossible(ReadGraph graph, Key key, Map<String,Object> hints) throws DatabaseException {
75             Collection<T> results = accept(graph, key, hints);
76             if(results.size() == 1) return results.iterator().next();
77             else return null;
78         }
79
80     @SuppressWarnings("unchecked")
81     public static <T> Collection<T> accept(ReadGraph graph, Key key, Map<String,Object> hints) throws DatabaseException {
82         ArrayList<T> result = new ArrayList<T>();
83         for(Set<Representation> rs : SimanticsInternal.getClipboard().getContents()) {
84             for(Representation r : rs) {
85                 if(r.getKey().equals(key)) {
86                     result.add((T)r.getValue(graph, hints));
87                     continue;
88                 }
89             }
90         }
91         return result;
92     }
93
94         public static SimanticsClipboard fileClipboard(String fileName) throws IOException {
95                 
96                 DataContainer container = DataContainers.readFile(new File(fileName));
97                 
98                 try {
99                         Representation tgRep = new TGRepresentation((TransferableGraph1)container.content.getValue(TransferableGraph1.BINDING));
100                         Representation dcRep = new DataContainerRepresentation(container);
101                         return SimanticsClipboardImpl.make(dcRep, tgRep);
102                 } catch (AdaptException e) {
103                         Logger.defaultLogError(e);
104                         return SimanticsClipboardImpl.EMPTY;
105                 }
106                 
107                 
108         }
109         
110         public static Representation createTransferableGraph(Resource ... resources) {
111                 return new TGRepresentation(resources);
112         }
113
114         public static Representation createTransferableGraph(TransferableGraphConfiguration2 configuration) {
115                 return new TGRepresentation(configuration);
116         }
117
118         public static Representation createTransferableGraph(boolean ignoreVirtualResources, Resource ... resources) {
119                 return new TGRepresentation(ignoreVirtualResources, resources);
120         }
121
122         public static Representation createVariable(String uri) {
123                 return new VariableRepresentation(uri);
124         }
125
126         public static Representation createVariable(RequestProcessor processor, final Variable var) {
127                 
128                 try {
129                         
130                         return new VariableRepresentation(processor.syncRequest(new Read<String>() {
131
132                                 @Override
133                                 public String perform(ReadGraph graph) throws DatabaseException {
134                                         return var.getURI(graph);
135                                 }
136                                 
137                         }));
138                         
139                 } catch (DatabaseException e) {
140                         Logger.defaultLogError(e);
141                 }
142                 
143                 return null;
144                 
145         }
146
147     public static Representation createCopyResources(final Collection<Resource> resources) {
148         return new ResourceCopyRepresentation(resources);
149     }
150
151     public static Representation createCutResources(final Collection<Resource> resources) {
152         return new ResourceCutRepresentation(resources);
153     }
154     
155     public static PasteHandler pasteHandler(Resource resource) throws DatabaseException {
156         return SimanticsInternal.sync(new ResourceRead<PasteHandler>(resource) {
157
158                         @Override
159                         public PasteHandler perform(ReadGraph graph) throws DatabaseException {
160                                 return graph.adapt(resource, PasteHandler.class);
161                         }
162                 
163         });
164     }
165
166 }