X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fadapter%2Fimpl%2FDefaultCopyHandler.java;h=312d4a10c2890cd14f2425a9a4a7657beacd3f98;hb=60ef4f8b863d892019a61978a63119dc1ebd852f;hp=2b9d20d23278cba9a8ac6ef983c6cc5192b413dc;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java index 2b9d20d23..312d4a10c 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultCopyHandler.java @@ -1,169 +1,169 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.db.layer0.adapter.impl; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Map; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ResourceRead; -import org.simantics.db.common.utils.Logger; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.CopyHandler; -import org.simantics.db.layer0.adapter.CopyHandler2; -import org.simantics.db.layer0.util.ClipboardUtils; -import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest; -import org.simantics.db.layer0.util.SimanticsClipboard.Representation; -import org.simantics.db.layer0.util.SimanticsClipboardBuilder; -import org.simantics.db.layer0.util.TGRepresentation; -import org.simantics.db.layer0.util.TGRepresentationUtils; -import org.simantics.db.layer0.util.TGSourceRepresentation; -import org.simantics.db.layer0.util.TransferableGraphConfiguration2; -import org.simantics.graph.db.TransferableGraphSource; -import org.simantics.graph.db.TransferableGraphs; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.operation.Layer0X; - -public class DefaultCopyHandler implements CopyHandler, CopyHandler2 { - - protected Collection resources; - - public DefaultCopyHandler(Resource resource) { - this.resources = Collections.singletonList(resource); - } - - public DefaultCopyHandler(Collection resources) { - this.resources = resources; - } - - public Resource getResource() { - if(resources.size() != 1) throw new IllegalStateException(); - return resources.iterator().next(); - } - - protected CopyHandler2 create(Collection resources) { - return new DefaultCopyHandler(resources); - } - - protected boolean ignoreVirtualResources() { - return true; - } - - @Override - public CopyHandler2 combine(CopyHandler2 other_) { - if(other_ instanceof DefaultCopyHandler) { - DefaultCopyHandler other = (DefaultCopyHandler)other_; - ArrayList allResources = new ArrayList(); - allResources.addAll(resources); - allResources.addAll(other.resources); - return create(allResources); - } - return null; - } - - protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException { - if(cut) return null; - return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false); - } - - protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException { - return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf)); - } - - protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException { - return TransferableGraphs.create(graph, computeSource(graph, conf)); - } - - protected Representation createTransferableGraph(ReadGraph graph, Collection resources, boolean cut) throws DatabaseException { - final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut); - if(conf == null) return null; - return new TGRepresentation(resources.toArray(new Resource[resources.size()])) { - @Override - public TransferableGraph1 compute(ReadGraph graph, Map hints) throws DatabaseException { - conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints); - return DefaultCopyHandler.this.compute(graph, conf); - } - }; - } - - protected Representation createTransferableGraphSource(ReadGraph graph, Collection resources, boolean cut) throws DatabaseException { - final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut); - if(conf == null) return null; - return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) { - @Override - public TransferableGraphSource compute(ReadGraph graph, Map hints) throws DatabaseException { - conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints); - return DefaultCopyHandler.this.computeSource(graph, conf); - } - }; - } - - protected void fillTG(ReadGraph graph, HashSet items, boolean cut) { - - try { - - Representation tgRepresentation = createTransferableGraph(graph, resources, cut); - if(tgRepresentation != null) items.add(tgRepresentation); - Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut); - if(tgSourceRepresentation != null) items.add(tgSourceRepresentation); - - if(resources.size() == 1) { - Collection representations = graph.syncRequest(new ResourceRead>(resources.iterator().next()) { - @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - ArrayList result = new ArrayList(); - Layer0X L0X = Layer0X.getInstance(graph); - for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) { - result.add(graph.adapt(adapter, Representation.class)); - } - return result; - } - }); - for (Representation r : representations) - items.add(r); - } - - } catch (DatabaseException e) { - Logger.defaultLogError(e); - } - - } - - protected void fillCopyResource(ReadGraph graph, HashSet items) { - items.add(ClipboardUtils.createCopyResources(resources)); - } - - protected void fillCutResource(ReadGraph graph, HashSet items) { - items.add(ClipboardUtils.createCutResources(resources)); - } - - @Override - public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException { - HashSet items = new HashSet(); - fillTG(graph, items, false); - fillCopyResource(graph, items); - clipboard.addContent(items); - } - - @Override - public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException { - HashSet items = new HashSet(); - fillTG(graph, items, true); - fillCutResource(graph, items); - clipboard.addContent(items); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.layer0.adapter.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.CopyHandler; +import org.simantics.db.layer0.adapter.CopyHandler2; +import org.simantics.db.layer0.util.ClipboardUtils; +import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest; +import org.simantics.db.layer0.util.SimanticsClipboard.Representation; +import org.simantics.db.layer0.util.SimanticsClipboardBuilder; +import org.simantics.db.layer0.util.TGRepresentation; +import org.simantics.db.layer0.util.TGRepresentationUtils; +import org.simantics.db.layer0.util.TGSourceRepresentation; +import org.simantics.db.layer0.util.TransferableGraphConfiguration2; +import org.simantics.graph.db.TransferableGraphSource; +import org.simantics.graph.db.TransferableGraphs; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.operation.Layer0X; + +public class DefaultCopyHandler implements CopyHandler, CopyHandler2 { + + protected Collection resources; + + public DefaultCopyHandler(Resource resource) { + this.resources = Collections.singletonList(resource); + } + + public DefaultCopyHandler(Collection resources) { + this.resources = resources; + } + + public Resource getResource() { + if(resources.size() != 1) throw new IllegalStateException(); + return resources.iterator().next(); + } + + protected CopyHandler2 create(Collection resources) { + return new DefaultCopyHandler(resources); + } + + protected boolean ignoreVirtualResources() { + return true; + } + + @Override + public CopyHandler2 combine(CopyHandler2 other_) { + if(other_ instanceof DefaultCopyHandler) { + DefaultCopyHandler other = (DefaultCopyHandler)other_; + ArrayList allResources = new ArrayList(); + allResources.addAll(resources); + allResources.addAll(other.resources); + return create(allResources); + } + return null; + } + + protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException { + if(cut) return null; + return new TransferableGraphConfiguration2(graph, resources, ignoreVirtualResources(), false); + } + + protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException { + return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf)); + } + + protected TransferableGraph1 compute(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException { + return TransferableGraphs.create(graph, computeSource(graph, conf)); + } + + protected Representation createTransferableGraph(ReadGraph graph, Collection resources, boolean cut) throws DatabaseException { + final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut); + if(conf == null) return null; + return new TGRepresentation(resources.toArray(new Resource[resources.size()])) { + @Override + public TransferableGraph1 compute(ReadGraph graph, Map hints) throws DatabaseException { + conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints); + return DefaultCopyHandler.this.compute(graph, conf); + } + }; + } + + protected Representation createTransferableGraphSource(ReadGraph graph, Collection resources, boolean cut) throws DatabaseException { + final TransferableGraphConfiguration2 conf = createConfiguration(graph, cut); + if(conf == null) return null; + return new TGSourceRepresentation(resources.toArray(new Resource[resources.size()])) { + @Override + public TransferableGraphSource compute(ReadGraph graph, Map hints) throws DatabaseException { + conf.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints); + return DefaultCopyHandler.this.computeSource(graph, conf); + } + }; + } + + protected void fillTG(ReadGraph graph, HashSet items, boolean cut) { + + try { + + Representation tgRepresentation = createTransferableGraph(graph, resources, cut); + if(tgRepresentation != null) items.add(tgRepresentation); + Representation tgSourceRepresentation = createTransferableGraphSource(graph, resources, cut); + if(tgSourceRepresentation != null) items.add(tgSourceRepresentation); + + if(resources.size() == 1) { + Collection representations = graph.syncRequest(new ResourceRead>(resources.iterator().next()) { + @Override + public Collection perform(ReadGraph graph) throws DatabaseException { + ArrayList result = new ArrayList(); + Layer0X L0X = Layer0X.getInstance(graph); + for(Resource adapter : graph.getObjects(resource, L0X.HasRepresentation)) { + result.add(graph.adapt(adapter, Representation.class)); + } + return result; + } + }); + for (Representation r : representations) + items.add(r); + } + + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + + } + + protected void fillCopyResource(ReadGraph graph, HashSet items) { + items.add(ClipboardUtils.createCopyResources(resources)); + } + + protected void fillCutResource(ReadGraph graph, HashSet items) { + items.add(ClipboardUtils.createCutResources(resources)); + } + + @Override + public void copyToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException { + HashSet items = new HashSet(); + fillTG(graph, items, false); + fillCopyResource(graph, items); + clipboard.addContent(items); + } + + @Override + public void cutToClipboard(ReadGraph graph, SimanticsClipboardBuilder clipboard) throws DatabaseException { + HashSet items = new HashSet(); + fillTG(graph, items, true); + fillCutResource(graph, items); + clipboard.addContent(items); + } + +}