Added identity-providing constructor for IconButtonStyleBase
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / IconButtonStyleBase.java
1 package org.simantics.diagram.profile;
2
3 import java.awt.geom.AffineTransform;
4
5 import org.simantics.datatypes.literal.Vec2d;
6 import org.simantics.scenegraph.INode;
7 import org.simantics.scenegraph.g2d.IG2DNode;
8 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
9 import org.simantics.scenegraph.profile.EvaluationContext;
10 import org.simantics.scenegraph.profile.common.ProfileVariables;
11 import org.simantics.scenegraph.utils.NodeUtil;
12
13 /**
14  * @author Antti Villberg
15  * @author Tuukka Lehtonen
16  */
17 public abstract class IconButtonStyleBase<T extends IconButtonResult> extends StyleBase<T> {
18
19         abstract protected String getNodePrefix();
20         abstract protected Class<?> getNodeClass();
21
22     public IconButtonStyleBase(Object identity) {
23         super(identity);
24     }
25
26     public IconButtonStyleBase() {
27         super();
28     }
29
30     private AffineTransform translateAndScaleIfNeeded(AffineTransform tr, Vec2d offset, boolean relativeTransform) {
31         if(relativeTransform) {
32                 if(!offset.isZero()) {
33                         tr = new AffineTransform(tr);
34                         tr.translate(offset.x, offset.y);
35                 }
36         } else {
37                 Vec2d translation = new Vec2d(tr.getTranslateX()+offset.x, tr.getTranslateY()+offset.y);
38                 tr = new AffineTransform();
39                 tr.translate(translation.x, translation.y);
40         }
41         return tr;
42     }
43     
44     private AffineTransform getSymbolTransform(INode node, Vec2d offset, boolean relativeTransform) {
45         if(node instanceof SingleElementNode) {
46                 SingleElementNode s = (SingleElementNode)node;
47                 INode symbol = NodeUtil.findChildByPrefix(s, "composite_image"); 
48                 return translateAndScaleIfNeeded(symbol != null ? ((IG2DNode)symbol).getTransform() : new AffineTransform(), offset, relativeTransform);
49         }
50         return null;
51     }
52     
53     @Override
54     public void applyStyleForNode(EvaluationContext observer, INode node, T result) {
55
56         if (result == null) {
57             ProfileVariables.denyChildren(node, getNodePrefix());
58             return;
59         }
60
61         IconButtonState state = result.getState();
62         IconButtonConfiguration config = state.getConfiguration(); 
63         
64         if (config.getOffset() == null) {
65                 ProfileVariables.denyChild(node, getNodePrefix() + config.getId());
66                 return;
67         }
68
69         @SuppressWarnings("unchecked")
70                 IconButtonNode iconNode = ProfileVariables.claimChild(node, "", getNodePrefix() + config.getId(), (Class<IconButtonNode>)getNodeClass(), observer);
71
72         iconNode.setZIndex( Integer.MAX_VALUE );
73         iconNode.setTransform( getSymbolTransform(node, config.getOffset(), config.getRelativeTransform()));
74
75         iconNode.setData(result);
76             
77     }
78
79     @Override
80     protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
81         ProfileVariables.denyChildren(node, getNodePrefix());
82     }
83
84 }