]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/canvas/ElementViewport.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / canvas / ElementViewport.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.g2d.elementclass.canvas;
13
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
16
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;
26
27 /**
28  * Lock viewport (KEY_VIEWPORT) of DiagramPainter to an element.
29  * 
30  * e.setHint(DiagramHints.KEY_DIAGRAM, ElementUtils.getDiagram(focusE)); Note!
31  * Add this handler _before_ diagram painter in element class
32  * 
33  * Keys: ElementViewport.KEY_ELEMENT
34  * 
35  * @See {@link DiagramPainter}
36  * @author Toni Kalajainen
37  */
38 public class ElementViewport implements SceneGraph {
39
40     public static final Key             KEY_ELEMENT      = new KeyOf(IElement.class);
41
42     private static final long           serialVersionUID = -849879952227051310L;
43
44     public static final ElementViewport INSTANCE         = new ElementViewport();
45
46     private final Margins               margins          = MarginUtils.MARGINS5;
47
48     @Override
49     public void cleanup(IElement e) {
50     }
51
52     @Override
53     public void init(IElement e, G2DParentNode parent) {
54         // Initializes viewport before DiagramPainter initializes the
55         // scenegraph.
56         IElement focusE = e.getHint(KEY_ELEMENT);
57         Rectangle2D focusBounds = ElementUtils.getElementBounds(focusE);
58         Rectangle2D bounds = ElementUtils.getElementBounds(e);
59
60         AffineTransform at = GeometryUtils.fitArea(bounds, focusBounds, margins);
61         e.setHint(DiagramPainter.KEY_VIEWPORT, at);
62     }
63
64 }