]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ImageClass.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / ImageClass.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;
13
14 import java.awt.Shape;
15 import java.awt.geom.AffineTransform;
16 import java.awt.geom.Rectangle2D;
17
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;
33
34 /**
35  * See {@link ElementHints}.KEY_IMAGE
36  *
37  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
38  * @author Tuukka Lehtonen
39  */
40 public class ImageClass {
41
42     public static final ElementClass INSTANCE =
43         ElementClass.compile(
44                 ImageElementHandler.INSTANCE,
45                 DefaultTransform.INSTANCE
46         );
47
48     public static class ImageElementHandler implements SceneGraph, InternalSize, Resize, Outline {
49
50         private static final long serialVersionUID = 7980198654721917029L;
51
52         public static final ImageElementHandler INSTANCE = new ImageElementHandler();
53
54         public static final Key  KEY_SG_NODE             = new SceneGraphNodeKey(Node.class, "IMAGE_SG_NODE");
55
56         protected Image getImage(IElement e) {
57             Image i = e.getHint(ElementHints.KEY_IMAGE);
58             if (i == null)
59                 throw new IllegalStateException("element " + e + " has no ElementHints.KEY_IMAGE hint");
60             return i;
61         }
62
63         protected Key getNodeKey() {
64             return KEY_SG_NODE;
65         }
66
67         @Override
68         public void init(final IElement e, final G2DParentNode parent) {
69             Image i = getImage(e);
70             Node node = i.init(parent);
71             if (node != null)
72                 e.setHint(getNodeKey(), node);
73             if(node instanceof IG2DNode) {
74                 IG2DNode n = (IG2DNode)node;
75                 AffineTransform at = ElementUtils.getTransform(e);
76                 if(at != null) {
77                         n.setTransform(at); // FIXME: not tested..
78                 }
79             }
80         }
81
82         public void cleanup(final IElement e) {
83             Node node = e.removeHint(getNodeKey());
84             if (node != null)
85                 node.remove();
86         }
87
88         @Override
89         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
90             Image i = getImage(e);
91             Rectangle2D r = i.getBounds();
92             if (size == null)
93                 size = new Rectangle2D.Double();
94             size.setFrame(r);
95             return size;
96         }
97
98         @Override
99         public Double getFixedAspectRatio(IElement e) {
100             Image i = getImage(e);
101             Rectangle2D r = i.getBounds();
102             return r.getWidth() / r.getHeight();
103         }
104
105         @Override
106         public Rectangle2D getMaximumSize(IElement e) {
107             Image i = getImage(e);
108             return i.getBounds();
109         }
110
111         @Override
112         public Rectangle2D getMinimumSize(IElement e) {
113             Image i = getImage(e);
114             return i.getBounds();
115         }
116
117         @Override
118         public void resize(IElement e, Rectangle2D newSize) {
119         }
120
121         @Override
122         public Shape getElementShape(IElement e) {
123             Image i = getImage(e);
124             return i.getOutline();
125         }
126     }
127
128
129     public static class StaticImageElementHandler extends ImageElementHandler {
130         private static final long serialVersionUID = 7691329107125520049L;
131
132         public static final Key   KEY_SG_NODE      = new SceneGraphNodeKey(Node.class, "STATIC_IMAGE_SG_NODE");
133
134         private final Image i;
135
136         public StaticImageElementHandler(Image i) {
137             this.i = i;
138         }
139
140         @Override
141         protected Image getImage(IElement e) {
142             return i;
143         }
144
145         @Override
146         protected Key getNodeKey() {
147             return KEY_SG_NODE;
148         }
149     }
150
151 }