1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.element.handler.impl;
\r
14 import java.awt.geom.AffineTransform;
\r
15 import java.awt.geom.Rectangle2D;
\r
17 import org.simantics.g2d.element.ElementHints;
\r
18 import org.simantics.g2d.element.IElement;
\r
19 import org.simantics.g2d.element.SceneGraphNodeKey;
\r
20 import org.simantics.g2d.element.handler.Clickable;
\r
21 import org.simantics.g2d.element.handler.Clickable.PressStatus;
\r
22 import org.simantics.g2d.element.handler.SceneGraph;
\r
23 import org.simantics.g2d.element.handler.Togglable;
\r
24 import org.simantics.g2d.image.Image;
\r
25 import org.simantics.scenegraph.Node;
\r
26 import org.simantics.scenegraph.g2d.G2DNode;
\r
27 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
28 import org.simantics.utils.datastructures.hints.IHintContext.Key;
\r
29 import org.simantics.utils.datastructures.hints.IHintListener;
\r
30 import org.simantics.utils.datastructures.hints.IHintObservable;
\r
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
37 public abstract class ResizeableImagePaint implements SceneGraph {
\r
39 private static final long serialVersionUID = 5955540004168283683L;
\r
40 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "RESIZABLE_IMAGE_SG_NODE");
\r
43 public ResizeableImagePaint() {
\r
48 public void init(final IElement e, final G2DParentNode parent) {
\r
49 System.out.println("ResizeableImagePaint.init");
\r
50 Togglable togglable = e.getElementClass().getAtMostOneItemOfClass(Togglable.class);
\r
51 boolean toggled = false;
\r
52 if (togglable != null)
\r
53 toggled = togglable.isChecked(e);
\r
55 Clickable.PressStatus status = null;
\r
56 Clickable clickable = e.getElementClass().getAtMostOneItemOfClass(Clickable.class);
\r
57 status = PressStatus.NORMAL;//clickable.getPressStatus(e, ctx);
\r
59 Image image = getImage(status, toggled);
\r
62 Node node = e.getHint(KEY_SG_NODE);
\r
66 node = image.init(parent);
\r
67 e.setHint(KEY_SG_NODE, node);
\r
68 e.addHintListener(new IHintListener() {
\r
71 public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
\r
72 // TODO Auto-generated method stub
\r
77 public void hintChanged(IHintObservable sender, Key key, Object oldValue,
\r
79 if (key == ElementHints.KEY_TRANSFORM) {
\r
80 G2DNode node = e.getHint(KEY_SG_NODE);
\r
81 Rectangle2D size = e.getHint(ElementHints.KEY_BOUNDS);
\r
82 AffineTransform at = (AffineTransform)newValue;
\r
83 AffineTransform at2 = node.getTransform();
\r
84 node.setTransform(new AffineTransform(at2.getScaleX(),at2.getShearX(),at2.getShearY(),at2.getScaleY(),at.getTranslateX()+size.getWidth()*0.5,at.getTranslateY()+size.getHeight()*0.5));
\r
86 if (key == ElementHints.KEY_BOUNDS) {
\r
87 G2DNode node = e.getHint(KEY_SG_NODE);
\r
88 Rectangle2D size = (Rectangle2D)newValue;
\r
89 Rectangle2D bounds = node.getBoundsInLocal();
\r
90 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
\r
92 node.setTransform(new AffineTransform(size.getWidth()/bounds.getWidth(),0,0,size.getHeight()/bounds.getHeight(),at.getTranslateX()+size.getWidth()*0.5,at.getTranslateY()+size.getHeight()*0.5));
\r
101 public void cleanup(final IElement e) {
\r
102 Node node = e.getHint(KEY_SG_NODE);
\r
108 public abstract Image getImage(Clickable.PressStatus status, boolean toggled);
\r