--- /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.g2d.participant;\r
+\r
+import java.awt.Rectangle;\r
+import java.awt.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.SGDesignation;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.HintReflection.HintListener;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
+import org.simantics.g2d.utils.GeometryUtils;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.BoundsNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext;\r
+import org.simantics.utils.datastructures.hints.IHintObservable;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * A canvas participant that stores the current control and canvas boundaries\r
+ * and detects their changes.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class CanvasBoundsParticipant extends AbstractCanvasParticipant {\r
+\r
+ BoundsNode.ResizeListener resizeListener = new BoundsNode.ResizeListener() {\r
+ @Override\r
+ public void controlResized(Rectangle2D bounds) {\r
+ controlBoundsUpdated(bounds);\r
+ }\r
+ };\r
+\r
+ @Dependency\r
+ private TransformUtil util;\r
+\r
+ private BoundsNode boundsNode = null;\r
+\r
+ private Rectangle2D controlBounds = new Rectangle2D.Double();\r
+\r
+ private Rectangle2D canvasBounds = new Rectangle2D.Double();\r
+\r
+ @HintListener(Class=Hints.class, Field="KEY_CANVAS_TRANSFORM")\r
+ public void selectionChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
+ // Update canvas bounds whenever canvas transform changes.\r
+ AffineTransform inv = util.getInverseTransform((AffineTransform) newValue);\r
+ if (inv != null) {\r
+ canvasTransformUpdated(controlBounds, inv);\r
+ } else {\r
+ // TODO: what should be done here? Leave canvas bounds as they are or mark them invalid somehow ??\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void addedToContext(ICanvasContext ctx) {\r
+ super.addedToContext(ctx);\r
+ IHintContext hctx = ctx.getDefaultHintContext();\r
+ hctx.setHint(Hints.KEY_CONTROL_BOUNDS, controlBounds);\r
+ hctx.setHint(Hints.KEY_CANVAS_BOUNDS, canvasBounds);\r
+ }\r
+\r
+ @SGInit(designation = SGDesignation.CONTROL)\r
+ public void initSG(G2DParentNode parent) {\r
+ boundsNode = parent.addNode("canvasBounds", BoundsNode.class);\r
+ boundsNode.setZIndex(Integer.MIN_VALUE);\r
+ boundsNode.setResizeListener(resizeListener);\r
+ }\r
+\r
+ @SGCleanup\r
+ public void cleanupSG() {\r
+ if (boundsNode != null) {\r
+ boundsNode.setResizeListener(null);\r
+ boundsNode.remove();\r
+ boundsNode = null;\r
+ }\r
+ }\r
+\r
+ private void controlBoundsUpdated(Rectangle2D bounds) {\r
+ if (!controlBounds.equals(bounds)) {\r
+ setControlBounds(bounds.getBounds());\r
+ }\r
+ AffineTransform invTr = util.getInverseTransform();\r
+ if (invTr != null)\r
+ canvasTransformUpdated(controlBounds, invTr);\r
+ }\r
+\r
+ private void canvasTransformUpdated(Rectangle2D controlBounds, AffineTransform inverseViewTr) {\r
+ Shape shape = GeometryUtils.transformShape(controlBounds, inverseViewTr);\r
+ Rectangle2D visibleCanvasBounds = shape.getBounds2D();\r
+ if (!canvasBounds.equals(visibleCanvasBounds)) {\r
+ setCanvasBounds(visibleCanvasBounds);\r
+ }\r
+ }\r
+\r
+ private void setControlBounds(Rectangle bounds) {\r
+ //System.out.println("Set control bounds: " + bounds);\r
+ this.controlBounds = bounds;\r
+ IHintContext ctx = getContext().getDefaultHintContext();\r
+ ctx.setHint(Hints.KEY_CONTROL_BOUNDS, controlBounds);\r
+ }\r
+\r
+ private void setCanvasBounds(Rectangle2D bounds) {\r
+ //System.out.println("Set canvas bounds: " + bounds);\r
+ this.canvasBounds = bounds;\r
+ IHintContext ctx = getContext().getDefaultHintContext();\r
+ ctx.setHint(Hints.KEY_CANVAS_BOUNDS, canvasBounds);\r
+ }\r
+\r
+ /**\r
+ * @return control bounds. Returns internal state, do not modify.\r
+ * If the current rendering surface is pixel based, the returned\r
+ * {@link Rectangle2D} may also be a {@link Rectangle}.\r
+ */\r
+ public Rectangle2D getControlBounds() {\r
+ return controlBounds;\r
+ }\r
+\r
+ /**\r
+ * @return visible canvas area bounds in canvas space coordinates. Returns\r
+ * internal state, do not modify.\r
+ */\r
+ public Rectangle2D getCanvasBounds() {\r
+ return canvasBounds;\r
+ }\r
+\r
+}\r