]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DiagramSelection.java
Merge "Add missing javax.servlet-api bundle requirement for jersey bundles"
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / DiagramSelection.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.handler;
13
14 import java.awt.geom.Point2D;
15 import java.lang.ref.WeakReference;
16 import java.util.Collections;
17 import java.util.Set;
18
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;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class DiagramSelection {
28
29     public static final DiagramSelection EMPTY = new DiagramSelection((Resource) null);
30
31     // Reference may be null
32     private final WeakReference<ICanvasContext> sourceCanvas;
33     private final Resource source;
34
35     /**
36      * May be empty or at least invalid after the source canvas no longer
37      * exists. Should not be used for anything besides visualisation.
38      */
39     private transient final Set<IElement> elements;
40
41     private final ElementObjectAssortment assortment;
42     private final boolean cut;
43     private final Point2D copyPos;
44
45     private DiagramSelection(Resource source) {
46         this.sourceCanvas = null;
47         this.source = source;
48         this.elements = Collections.emptySet();
49         this.assortment = ElementObjectAssortment.fromElements(elements);
50         this.cut = false;
51         this.copyPos = new Point2D.Double();
52     }
53
54     public DiagramSelection(ICanvasContext sourceCanvas, Resource source, Set<IElement> elements, boolean cut, Point2D copyPos) {
55         assert source != null;
56         assert elements != null;
57
58         this.sourceCanvas = new WeakReference<ICanvasContext>(sourceCanvas);
59         this.source = source;
60         this.elements = Collections.unmodifiableSet(elements);
61         this.assortment = ElementObjectAssortment.fromElements(elements);
62         this.cut = cut;
63         this.copyPos = copyPos;
64     }
65
66     /**
67      * @return <code>null</code> if the source canvas has already been disposed.
68      */
69     public ICanvasContext getSourceCanvas() {
70         ICanvasContext ctx = sourceCanvas == null ? null : sourceCanvas.get();
71         return ctx == null ? null : ctx.isDisposed() ? null : ctx;
72     }
73
74     public boolean isFromCanvasContext(ICanvasContext ctx) {
75         ICanvasContext srcCtx = getSourceCanvas();
76         return ctx != null && ctx == srcCtx;
77     }
78
79     public Resource getSourceDiagram() {
80         return source;
81     }
82
83     public boolean isCut() {
84         return cut;
85     }
86
87     public Point2D getCopyPos() {
88         return copyPos;
89     }
90
91     public Set<IElement> getOriginalElements() {
92         return elements;
93     }
94
95     public ElementObjectAssortment getAssortment() {
96         return assortment;
97     }
98
99     public boolean isEmpty() {
100         return getAssortment().isEmpty();
101     }
102
103     @Override
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) ");
109         if (cut)
110             sb.append("cut");
111         else
112             sb.append("copied");
113         sb.append(" from ");
114         sb.append(source);
115         sb.append(" with reference position ");
116         sb.append(copyPos);
117         if (elements != null && !elements.isEmpty()) {
118             sb.append(":\n");
119             String elems = EString.implode(elements, "\n");
120             if (elems != null)
121                 sb.append(EString.addPrefix(elems, "\t"));
122         }
123         if (assortment != null && !assortment.isEmpty()) {
124             sb.append(assortment.toString());
125         }
126         return sb.toString();
127     }
128 }
129