X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fhandler%2FDiagramSelection.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fhandler%2FDiagramSelection.java;h=d304dc279e132f53f824ad00a3e095636be0228f;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DiagramSelection.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DiagramSelection.java new file mode 100644 index 000000000..d304dc279 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DiagramSelection.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * 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.diagram.handler; + +import java.awt.geom.Point2D; +import java.lang.ref.WeakReference; +import java.util.Collections; +import java.util.Set; + +import org.simantics.db.Resource; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.element.IElement; +import org.simantics.utils.strings.EString; + +/** + * @author Tuukka Lehtonen + */ +public class DiagramSelection { + + public static final DiagramSelection EMPTY = new DiagramSelection((Resource) null); + + // Reference may be null + private final WeakReference sourceCanvas; + private final Resource source; + + /** + * May be empty or at least invalid after the source canvas no longer + * exists. Should not be used for anything besides visualisation. + */ + private transient final Set elements; + + private final ElementObjectAssortment assortment; + private final boolean cut; + private final Point2D copyPos; + + private DiagramSelection(Resource source) { + this.sourceCanvas = null; + this.source = source; + this.elements = Collections.emptySet(); + this.assortment = ElementObjectAssortment.fromElements(elements); + this.cut = false; + this.copyPos = new Point2D.Double(); + } + + public DiagramSelection(ICanvasContext sourceCanvas, Resource source, Set elements, boolean cut, Point2D copyPos) { + assert source != null; + assert elements != null; + + this.sourceCanvas = new WeakReference(sourceCanvas); + this.source = source; + this.elements = Collections.unmodifiableSet(elements); + this.assortment = ElementObjectAssortment.fromElements(elements); + this.cut = cut; + this.copyPos = copyPos; + } + + /** + * @return null if the source canvas has already been disposed. + */ + public ICanvasContext getSourceCanvas() { + ICanvasContext ctx = sourceCanvas == null ? null : sourceCanvas.get(); + return ctx == null ? null : ctx.isDisposed() ? null : ctx; + } + + public boolean isFromCanvasContext(ICanvasContext ctx) { + ICanvasContext srcCtx = getSourceCanvas(); + return ctx != null && ctx == srcCtx; + } + + public Resource getSourceDiagram() { + return source; + } + + public boolean isCut() { + return cut; + } + + public Point2D getCopyPos() { + return copyPos; + } + + public Set getOriginalElements() { + return elements; + } + + public ElementObjectAssortment getAssortment() { + return assortment; + } + + public boolean isEmpty() { + return getAssortment().isEmpty(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Diagram selection of "); + sb.append(elements.size()); + sb.append(" element(s) "); + if (cut) + sb.append("cut"); + else + sb.append("copied"); + sb.append(" from "); + sb.append(source); + sb.append(" with reference position "); + sb.append(copyPos); + if (elements != null && !elements.isEmpty()) { + sb.append(":\n"); + String elems = EString.implode(elements, "\n"); + if (elems != null) + sb.append(EString.addPrefix(elems, "\t")); + } + if (assortment != null && !assortment.isEmpty()) { + sb.append(assortment.toString()); + } + return sb.toString(); + } +} +