--- /dev/null
+package org.simantics.diagram.adapter;
+
+import java.awt.Shape;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+
+import org.simantics.g2d.connection.handler.ConnectionHandler;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.SceneGraphNodeKey;
+import org.simantics.g2d.element.handler.InternalSize;
+import org.simantics.g2d.element.handler.Outline;
+import org.simantics.g2d.element.handler.Resize;
+import org.simantics.g2d.element.handler.SceneGraph;
+import org.simantics.g2d.image.Image;
+import org.simantics.scenegraph.Node;
+import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.IG2DNode;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+
+public class DefinedElementHandler implements SceneGraph, InternalSize, Resize, Outline {
+
+ private static final long serialVersionUID = -2074850877791708846L;
+
+ public static final DefinedElementHandler INSTANCE = new DefinedElementHandler();
+
+ public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "IMAGE_SG_NODE");
+
+ protected Image getImage(IElement e) {
+ Image i = e.getHint(ElementHints.KEY_IMAGE);
+ if (i == null)
+ throw new IllegalStateException("element " + e + " has no ElementHints.KEY_IMAGE hint");
+ return i;
+ }
+
+ protected Key getNodeKey() {
+ return KEY_SG_NODE;
+ }
+
+ @Override
+ public void init(final IElement e, final G2DParentNode parent) {
+ Image i = getImage(e);
+ Node node = i.init(parent);
+ if (node != null)
+ e.setHint(getNodeKey(), node);
+ if(node instanceof IG2DNode) {
+ IG2DNode n = (IG2DNode)node;
+ AffineTransform at = ElementUtils.getTransform(e);
+ if(at != null) {
+ n.setTransform(at); // FIXME: not tested..
+ }
+ }
+ }
+
+ public void cleanup(final IElement e) {
+ Node node = e.removeHint(getNodeKey());
+ if (node != null)
+ node.remove();
+ }
+
+ public Rectangle2D imageBounds(IElement e) {
+ Image i = getImage(e);
+ return i.getBounds();
+ }
+
+ @Override
+ public Rectangle2D getBounds(IElement e, Rectangle2D size) {
+
+ if (size == null)
+ size = new Rectangle2D.Double();
+
+ ElementClass ec = e.getElementClass();
+ if(ec.containsClass(ConnectionHandler.class)) {
+ size.setFrame(imageBounds(e));
+ } else {
+ IG2DNode node = e.getHint(getNodeKey());
+ if(node != null) {
+ size.setFrame(node.getBoundsInLocal());
+ } else {
+ size.setFrame(imageBounds(e));
+ }
+ }
+
+ return size;
+
+ }
+
+ @Override
+ public Double getFixedAspectRatio(IElement e) {
+ Image i = getImage(e);
+ Rectangle2D r = i.getBounds();
+ return r.getWidth() / r.getHeight();
+ }
+
+ @Override
+ public Rectangle2D getMaximumSize(IElement e) {
+ Image i = getImage(e);
+ return i.getBounds();
+ }
+
+ @Override
+ public Rectangle2D getMinimumSize(IElement e) {
+ Image i = getImage(e);
+ return i.getBounds();
+ }
+
+ @Override
+ public void resize(IElement e, Rectangle2D newSize) {
+ }
+
+ @Override
+ public Shape getElementShape(IElement e) {
+
+ IG2DNode node = e.getHint(getNodeKey());
+ if (node == null)
+ throw new IllegalStateException();
+ return node.getBoundsInLocal();
+
+ }
+
+}
\ No newline at end of file