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.util;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
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;
39 public class ClipboardUtils {
41 public static String HINT_TARGET_RESOURCE = "HINT_TARGET_RESOURCE";
43 public static <T> T accept(Set<Representation> set, Key key) throws DatabaseException {
44 return accept(set, key, Collections.emptyMap());
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);
52 public static <T> T accept(RequestProcessor processor, Set<Representation> set, Key key) throws DatabaseException {
53 return accept(processor, set, key, Collections.emptyMap());
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);
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());
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);
70 public static <T> T acceptPossible(ReadGraph graph, Key key) throws DatabaseException {
71 return acceptPossible(graph, key, Collections.emptyMap());
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();
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));
94 public static SimanticsClipboard fileClipboard(String fileName) throws IOException {
96 DataContainer container = DataContainers.readFile(new File(fileName));
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;
110 public static Representation createTransferableGraph(Resource ... resources) {
111 return new TGRepresentation(resources);
114 public static Representation createTransferableGraph(TransferableGraphConfiguration2 configuration) {
115 return new TGRepresentation(configuration);
118 public static Representation createTransferableGraph(boolean ignoreVirtualResources, Resource ... resources) {
119 return new TGRepresentation(ignoreVirtualResources, resources);
122 public static Representation createVariable(String uri) {
123 return new VariableRepresentation(uri);
126 public static Representation createVariable(RequestProcessor processor, final Variable var) {
130 return new VariableRepresentation(processor.syncRequest(new Read<String>() {
133 public String perform(ReadGraph graph) throws DatabaseException {
134 return var.getURI(graph);
139 } catch (DatabaseException e) {
140 Logger.defaultLogError(e);
147 public static Representation createCopyResources(final Collection<Resource> resources) {
148 return new ResourceCopyRepresentation(resources);
151 public static Representation createCutResources(final Collection<Resource> resources) {
152 return new ResourceCutRepresentation(resources);
155 public static PasteHandler pasteHandler(Resource resource) throws DatabaseException {
156 return SimanticsInternal.sync(new ResourceRead<PasteHandler>(resource) {
159 public PasteHandler perform(ReadGraph graph) throws DatabaseException {
160 return graph.adapt(resource, PasteHandler.class);