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.Collections;
17 import java.util.HashSet;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.common.utils.Logger;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.CopyHandler;
26 import org.simantics.db.layer0.adapter.CopyHandler2;
27 import org.simantics.db.layer0.util.ClipboardUtils;
28 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
29 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
30 import org.simantics.db.layer0.util.SimanticsClipboardBuilder;
31 import org.simantics.db.layer0.util.TGRepresentation;
32 import org.simantics.db.layer0.util.TGRepresentationUtils;
33 import org.simantics.db.layer0.util.TGSourceRepresentation;
34 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
35 import org.simantics.graph.db.TransferableGraphSource;
36 import org.simantics.graph.db.TransferableGraphs;
37 import org.simantics.graph.representation.TransferableGraph1;
38 import org.simantics.operation.Layer0X;
40 public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
42 protected Collection<Resource> resources;
44 public DefaultCopyHandler(Resource resource) {
45 this.resources = Collections.singletonList(resource);
48 public DefaultCopyHandler(Collection<Resource> resources) {
49 this.resources = resources;
52 public Resource getResource() {
53 if(resources.size() != 1) throw new IllegalStateException();
54 return resources.iterator().next();
57 protected CopyHandler2 create(Collection<Resource> resources) {
58 return new DefaultCopyHandler(resources);
61 protected boolean ignoreVirtualResources() {
66 public CopyHandler2 combine(CopyHandler2 other_) {
67 if(other_ instanceof DefaultCopyHandler) {
68 DefaultCopyHandler other = (DefaultCopyHandler)other_;
69 ArrayList<Resource> allResources = new ArrayList<Resource>();
70 allResources.addAll(resources);
71 allResources.addAll(other.resources);
72 return create(allResources);
77 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
79 return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false);
82 protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
83 return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
86 protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
87 return TransferableGraphs.create(graph, computeSource(graph, conf));
90 protected Representation createTransferableGraph(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
91 final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
92 if(conf == null) return null;
93 return new TGRepresentation(resources.toArray(new Resource[resources.size()])) {
95 public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
96 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
97 return DefaultCopyHandler.this.compute(graph, conf);
102 protected Representation createTransferableGraphSource(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
103 final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
104 if(conf == null) return null;
105 return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) {
107 public TransferableGraphSource compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
108 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
109 return DefaultCopyHandler.this.computeSource(graph, conf);
114 protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut) {
118 Representation tgRepresentation = createTransferableGraph(graph, resources, cut);
119 if(tgRepresentation != null) items.add(tgRepresentation);
120 Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut);
121 if(tgSourceRepresentation != null) items.add(tgSourceRepresentation);
123 if(resources.size() == 1) {
124 Collection<Representation> representations = graph.syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {
126 public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {
127 ArrayList<Representation> result = new ArrayList<Representation>();
128 Layer0X L0X = Layer0X.getInstance(graph);
129 for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {
130 result.add(graph.adapt(adapter, Representation.class));
135 for (Representation r : representations)
139 } catch (DatabaseException e) {
140 Logger.defaultLogError(e);
145 protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items) {
146 items.add(ClipboardUtils.createCopyResources(resources));
149 protected void fillCutResource(ReadGraph graph, HashSet<Representation> items) {
150 items.add(ClipboardUtils.createCutResources(resources));
154 public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
155 HashSet<Representation> items = new HashSet<Representation>();
156 fillTG(graph, items, false);
157 fillCopyResource(graph, items);
158 clipboard.addContent(items);
162 public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
163 HashSet<Representation> items = new HashSet<Representation>();
164 fillTG(graph, items, true);
165 fillCutResource(graph, items);
166 clipboard.addContent(items);