1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.layer0.adapter.impl;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.HashSet;
\r
18 import java.util.Map;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.common.request.ResourceRead;
\r
23 import org.simantics.db.common.utils.Logger;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.adapter.CopyHandler;
\r
26 import org.simantics.db.layer0.adapter.CopyHandler2;
\r
27 import org.simantics.db.layer0.util.ClipboardUtils;
\r
28 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
\r
29 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
\r
30 import org.simantics.db.layer0.util.SimanticsClipboardBuilder;
\r
31 import org.simantics.db.layer0.util.TGRepresentation;
\r
32 import org.simantics.db.layer0.util.TGRepresentationUtils;
\r
33 import org.simantics.db.layer0.util.TGSourceRepresentation;
\r
34 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
\r
35 import org.simantics.graph.db.TransferableGraphSource;
\r
36 import org.simantics.graph.db.TransferableGraphs;
\r
37 import org.simantics.graph.representation.TransferableGraph1;
\r
38 import org.simantics.operation.Layer0X;
\r
40 public class DefaultCopyHandler implements CopyHandler, CopyHandler2 {
\r
42 protected Collection<Resource> resources;
\r
44 public DefaultCopyHandler(Resource resource) {
\r
45 this.resources = Collections.singletonList(resource);
\r
48 public DefaultCopyHandler(Collection<Resource> resources) {
\r
49 this.resources = resources;
\r
52 public Resource getResource() {
\r
53 if(resources.size() != 1) throw new IllegalStateException();
\r
54 return resources.iterator().next();
\r
57 protected CopyHandler2 create(Collection<Resource> resources) {
\r
58 return new DefaultCopyHandler(resources);
\r
61 protected boolean ignoreVirtualResources() {
\r
66 public CopyHandler2 combine(CopyHandler2 other_) {
\r
67 if(other_ instanceof DefaultCopyHandler) {
\r
68 DefaultCopyHandler other = (DefaultCopyHandler)other_;
\r
69 ArrayList<Resource> allResources = new ArrayList<Resource>();
\r
70 allResources.addAll(resources);
\r
71 allResources.addAll(other.resources);
\r
72 return create(allResources);
\r
77 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
\r
78 if(cut) return null;
\r
79 return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false);
\r
82 protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
\r
83 return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
\r
86 protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
\r
87 return TransferableGraphs.create(graph, computeSource(graph, conf));
\r
90 protected Representation createTransferableGraph(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
\r
91 final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
\r
92 if(conf == null) return null;
\r
93 return new TGRepresentation(resources.toArray(new Resource[resources.size()])) {
\r
95 public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
\r
96 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
\r
97 return DefaultCopyHandler.this.compute(graph, conf);
\r
102 protected Representation createTransferableGraphSource(ReadGraph graph, Collection<Resource> resources, boolean cut) throws DatabaseException {
\r
103 final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut);
\r
104 if(conf == null) return null;
\r
105 return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) {
\r
107 public TransferableGraphSource compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
\r
108 conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
\r
109 return DefaultCopyHandler.this.computeSource(graph, conf);
\r
114 protected void fillTG(ReadGraph graph, HashSet<Representation> items, boolean cut) {
\r
118 Representation tgRepresentation = createTransferableGraph(graph, resources, cut);
\r
119 if(tgRepresentation != null) items.add(tgRepresentation);
\r
120 Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut);
\r
121 if(tgSourceRepresentation != null) items.add(tgSourceRepresentation);
\r
123 if(resources.size() == 1) {
\r
124 Collection<Representation> representations = graph.syncRequest(new ResourceRead<Collection<Representation>>(resources.iterator().next()) {
\r
126 public Collection<Representation> perform(ReadGraph graph) throws DatabaseException {
\r
127 ArrayList<Representation> result = new ArrayList<Representation>();
\r
128 Layer0X L0X = Layer0X.getInstance(graph);
\r
129 for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) {
\r
130 result.add(graph.adapt(adapter, Representation.class));
\r
135 for (Representation r : representations)
\r
139 } catch (DatabaseException e) {
\r
140 Logger.defaultLogError(e);
\r
145 protected void fillCopyResource(ReadGraph graph, HashSet<Representation> items) {
\r
146 items.add(ClipboardUtils.createCopyResources(resources));
\r
149 protected void fillCutResource(ReadGraph graph, HashSet<Representation> items) {
\r
150 items.add(ClipboardUtils.createCutResources(resources));
\r
154 public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
\r
155 HashSet<Representation> items = new HashSet<Representation>();
\r
156 fillTG(graph, items, false);
\r
157 fillCopyResource(graph, items);
\r
158 clipboard.addContent(items);
\r
162 public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException {
\r
163 HashSet<Representation> items = new HashSet<Representation>();
\r
164 fillTG(graph, items, true);
\r
165 fillCutResource(graph, items);
\r
166 clipboard.addContent(items);
\r