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.diagram.handler;
14 import java.awt.geom.Point2D;
15 import java.lang.ref.WeakReference;
16 import java.util.Collections;
19 import org.simantics.db.Resource;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.utils.strings.EString;
25 * @author Tuukka Lehtonen
27 public class DiagramSelection {
29 public static final DiagramSelection EMPTY = new DiagramSelection((Resource) null);
31 // Reference may be null
32 private final WeakReference<ICanvasContext> sourceCanvas;
33 private final Resource source;
36 * May be empty or at least invalid after the source canvas no longer
37 * exists. Should not be used for anything besides visualisation.
39 private transient final Set<IElement> elements;
41 private final ElementObjectAssortment assortment;
42 private final boolean cut;
43 private final Point2D copyPos;
45 private DiagramSelection(Resource source) {
46 this.sourceCanvas = null;
48 this.elements = Collections.emptySet();
49 this.assortment = ElementObjectAssortment.fromElements(elements);
51 this.copyPos = new Point2D.Double();
54 public DiagramSelection(ICanvasContext sourceCanvas, Resource source, Set<IElement> elements, boolean cut, Point2D copyPos) {
55 assert source != null;
56 assert elements != null;
58 this.sourceCanvas = new WeakReference<ICanvasContext>(sourceCanvas);
60 this.elements = Collections.unmodifiableSet(elements);
61 this.assortment = ElementObjectAssortment.fromElements(elements);
63 this.copyPos = copyPos;
67 * @return <code>null</code> if the source canvas has already been disposed.
69 public ICanvasContext getSourceCanvas() {
70 ICanvasContext ctx = sourceCanvas == null ? null : sourceCanvas.get();
71 return ctx == null ? null : ctx.isDisposed() ? null : ctx;
74 public boolean isFromCanvasContext(ICanvasContext ctx) {
75 ICanvasContext srcCtx = getSourceCanvas();
76 return ctx != null && ctx == srcCtx;
79 public Resource getSourceDiagram() {
83 public boolean isCut() {
87 public Point2D getCopyPos() {
91 public Set<IElement> getOriginalElements() {
95 public ElementObjectAssortment getAssortment() {
99 public boolean isEmpty() {
100 return getAssortment().isEmpty();
104 public String toString() {
105 StringBuilder sb = new StringBuilder();
106 sb.append("Diagram selection of ");
107 sb.append(elements.size());
108 sb.append(" element(s) ");
115 sb.append(" with reference position ");
117 if (elements != null && !elements.isEmpty()) {
119 String elems = EString.implode(elements, "\n");
121 sb.append(EString.addPrefix(elems, "\t"));
123 if (assortment != null && !assortment.isEmpty()) {
124 sb.append(assortment.toString());
126 return sb.toString();