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;
14 import java.awt.Shape;
15 import java.awt.geom.AffineTransform;
16 import java.awt.geom.Rectangle2D;
18 import org.simantics.g2d.element.ElementClass;
19 import org.simantics.g2d.element.ElementHints;
20 import org.simantics.g2d.element.ElementUtils;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.g2d.element.SceneGraphNodeKey;
23 import org.simantics.g2d.element.handler.InternalSize;
24 import org.simantics.g2d.element.handler.Outline;
25 import org.simantics.g2d.element.handler.Resize;
26 import org.simantics.g2d.element.handler.SceneGraph;
27 import org.simantics.g2d.element.handler.impl.DefaultTransform;
28 import org.simantics.g2d.image.Image;
29 import org.simantics.scenegraph.Node;
30 import org.simantics.scenegraph.g2d.G2DParentNode;
31 import org.simantics.scenegraph.g2d.IG2DNode;
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;
35 * See {@link ElementHints}.KEY_IMAGE
37 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
38 * @author Tuukka Lehtonen
40 public class ImageClass {
42 public static final ElementClass INSTANCE =
44 ImageElementHandler.INSTANCE,
45 DefaultTransform.INSTANCE
48 public static class ImageElementHandler implements SceneGraph, InternalSize, Resize, Outline {
50 private static final long serialVersionUID = 7980198654721917029L;
52 public static final ImageElementHandler INSTANCE = new ImageElementHandler();
54 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "IMAGE_SG_NODE");
56 protected Image getImage(IElement e) {
57 Image i = e.getHint(ElementHints.KEY_IMAGE);
59 throw new IllegalStateException("element " + e + " has no ElementHints.KEY_IMAGE hint");
63 protected Key getNodeKey() {
68 public void init(final IElement e, final G2DParentNode parent) {
69 Image i = getImage(e);
70 Node node = i.init(parent);
72 e.setHint(getNodeKey(), node);
73 if(node instanceof IG2DNode) {
74 IG2DNode n = (IG2DNode)node;
75 AffineTransform at = ElementUtils.getTransform(e);
77 n.setTransform(at); // FIXME: not tested..
82 public void cleanup(final IElement e) {
83 Node node = e.removeHint(getNodeKey());
89 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
90 Image i = getImage(e);
91 Rectangle2D r = i.getBounds();
93 size = new Rectangle2D.Double();
99 public Double getFixedAspectRatio(IElement e) {
100 Image i = getImage(e);
101 Rectangle2D r = i.getBounds();
102 return r.getWidth() / r.getHeight();
106 public Rectangle2D getMaximumSize(IElement e) {
107 Image i = getImage(e);
108 return i.getBounds();
112 public Rectangle2D getMinimumSize(IElement e) {
113 Image i = getImage(e);
114 return i.getBounds();
118 public void resize(IElement e, Rectangle2D newSize) {
122 public Shape getElementShape(IElement e) {
123 Image i = getImage(e);
124 return i.getOutline();
129 public static class StaticImageElementHandler extends ImageElementHandler {
130 private static final long serialVersionUID = 7691329107125520049L;
132 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "STATIC_IMAGE_SG_NODE");
134 private final Image i;
136 public StaticImageElementHandler(Image i) {
141 protected Image getImage(IElement e) {
146 protected Key getNodeKey() {