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.g2d.elementclass.canvas;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
17 import org.simantics.g2d.element.ElementUtils;
18 import org.simantics.g2d.element.IElement;
19 import org.simantics.g2d.element.handler.SceneGraph;
20 import org.simantics.scenegraph.g2d.G2DParentNode;
21 import org.simantics.scenegraph.utils.GeometryUtils;
22 import org.simantics.utils.datastructures.hints.IHintContext.Key;
23 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
24 import org.simantics.utils.page.MarginUtils;
25 import org.simantics.utils.page.MarginUtils.Margins;
28 * Lock viewport (KEY_VIEWPORT) of DiagramPainter to an element.
30 * e.setHint(DiagramHints.KEY_DIAGRAM, ElementUtils.getDiagram(focusE)); Note!
31 * Add this handler _before_ diagram painter in element class
33 * Keys: ElementViewport.KEY_ELEMENT
35 * @See {@link DiagramPainter}
36 * @author Toni Kalajainen
38 public class ElementViewport implements SceneGraph {
40 public static final Key KEY_ELEMENT = new KeyOf(IElement.class);
42 private static final long serialVersionUID = -849879952227051310L;
44 public static final ElementViewport INSTANCE = new ElementViewport();
46 private final Margins margins = MarginUtils.MARGINS5;
49 public void cleanup(IElement e) {
53 public void init(IElement e, G2DParentNode parent) {
54 // Initializes viewport before DiagramPainter initializes the
56 IElement focusE = e.getHint(KEY_ELEMENT);
57 Rectangle2D focusBounds = ElementUtils.getElementBounds(focusE);
58 Rectangle2D bounds = ElementUtils.getElementBounds(e);
60 AffineTransform at = GeometryUtils.fitArea(bounds, focusBounds, margins);
61 e.setHint(DiagramPainter.KEY_VIEWPORT, at);