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