--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.handler;\r
+\r
+import java.awt.geom.Point2D;\r
+import java.lang.ref.WeakReference;\r
+import java.util.Collections;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.utils.strings.EString;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DiagramSelection {\r
+\r
+ public static final DiagramSelection EMPTY = new DiagramSelection((Resource) null);\r
+\r
+ // Reference may be null\r
+ private final WeakReference<ICanvasContext> sourceCanvas;\r
+ private final Resource source;\r
+\r
+ /**\r
+ * May be empty or at least invalid after the source canvas no longer\r
+ * exists. Should not be used for anything besides visualisation.\r
+ */\r
+ private transient final Set<IElement> elements;\r
+\r
+ private final ElementObjectAssortment assortment;\r
+ private final boolean cut;\r
+ private final Point2D copyPos;\r
+\r
+ private DiagramSelection(Resource source) {\r
+ this.sourceCanvas = null;\r
+ this.source = source;\r
+ this.elements = Collections.emptySet();\r
+ this.assortment = ElementObjectAssortment.fromElements(elements);\r
+ this.cut = false;\r
+ this.copyPos = new Point2D.Double();\r
+ }\r
+\r
+ public DiagramSelection(ICanvasContext sourceCanvas, Resource source, Set<IElement> elements, boolean cut, Point2D copyPos) {\r
+ assert source != null;\r
+ assert elements != null;\r
+\r
+ this.sourceCanvas = new WeakReference<ICanvasContext>(sourceCanvas);\r
+ this.source = source;\r
+ this.elements = Collections.unmodifiableSet(elements);\r
+ this.assortment = ElementObjectAssortment.fromElements(elements);\r
+ this.cut = cut;\r
+ this.copyPos = copyPos;\r
+ }\r
+\r
+ /**\r
+ * @return <code>null</code> if the source canvas has already been disposed.\r
+ */\r
+ public ICanvasContext getSourceCanvas() {\r
+ ICanvasContext ctx = sourceCanvas == null ? null : sourceCanvas.get();\r
+ return ctx == null ? null : ctx.isDisposed() ? null : ctx;\r
+ }\r
+\r
+ public boolean isFromCanvasContext(ICanvasContext ctx) {\r
+ ICanvasContext srcCtx = getSourceCanvas();\r
+ return ctx != null && ctx == srcCtx;\r
+ }\r
+\r
+ public Resource getSourceDiagram() {\r
+ return source;\r
+ }\r
+\r
+ public boolean isCut() {\r
+ return cut;\r
+ }\r
+\r
+ public Point2D getCopyPos() {\r
+ return copyPos;\r
+ }\r
+\r
+ public Set<IElement> getOriginalElements() {\r
+ return elements;\r
+ }\r
+\r
+ public ElementObjectAssortment getAssortment() {\r
+ return assortment;\r
+ }\r
+\r
+ public boolean isEmpty() {\r
+ return getAssortment().isEmpty();\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("Diagram selection of ");\r
+ sb.append(elements.size());\r
+ sb.append(" element(s) ");\r
+ if (cut)\r
+ sb.append("cut");\r
+ else\r
+ sb.append("copied");\r
+ sb.append(" from ");\r
+ sb.append(source);\r
+ sb.append(" with reference position ");\r
+ sb.append(copyPos);\r
+ if (elements != null && !elements.isEmpty()) {\r
+ sb.append(":\n");\r
+ String elems = EString.implode(elements, "\n");\r
+ if (elems != null)\r
+ sb.append(EString.addPrefix(elems, "\t"));\r
+ }\r
+ if (assortment != null && !assortment.isEmpty()) {\r
+ sb.append(assortment.toString());\r
+ }\r
+ return sb.toString();\r
+ }\r
+}\r
+\r