f4c9470bdaa6e6465b97543a2ae81469ccdbfc19
[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     private AffineTransform translateAndScaleIfNeeded(AffineTransform tr, Vec2d offset, boolean relativeTransform) {
23         if(relativeTransform) {
24                 if(!offset.isZero()) {
25                         tr = new AffineTransform(tr);
26                         tr.translate(offset.x, offset.y);
27                 }
28         } else {
29                 Vec2d translation = new Vec2d(tr.getTranslateX()+offset.x, tr.getTranslateY()+offset.y);
30                 tr = new AffineTransform();
31                 tr.translate(translation.x, translation.y);
32         }
33         return tr;
34     }
35     
36     private AffineTransform getSymbolTransform(INode node, Vec2d offset, boolean relativeTransform) {
37         if(node instanceof SingleElementNode) {
38                 SingleElementNode s = (SingleElementNode)node;
39                 INode symbol = NodeUtil.findChildByPrefix(s, "composite_image"); 
40                 return translateAndScaleIfNeeded(symbol != null ? ((IG2DNode)symbol).getTransform() : new AffineTransform(), offset, relativeTransform);
41         }
42         return null;
43     }
44     
45     @Override
46     public void applyStyleForNode(EvaluationContext observer, INode node, T result) {
47
48         if (result == null) {
49             ProfileVariables.denyChildren(node, getNodePrefix());
50             return;
51         }
52
53         IconButtonState state = result.getState();
54         IconButtonConfiguration config = state.getConfiguration(); 
55         
56         if (config.getOffset() == null) {
57                 ProfileVariables.denyChild(node, getNodePrefix() + config.getId());
58                 return;
59         }
60
61         @SuppressWarnings("unchecked")
62                 IconButtonNode iconNode = ProfileVariables.claimChild(node, "", getNodePrefix() + config.getId(), (Class<IconButtonNode>)getNodeClass(), observer);
63
64         iconNode.setZIndex( Integer.MAX_VALUE );
65         iconNode.setTransform( getSymbolTransform(node, config.getOffset(), config.getRelativeTransform()));
66
67         iconNode.setData(result);
68             
69     }
70
71     @Override
72     protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
73         ProfileVariables.denyChildren(node, getNodePrefix());
74     }
75
76 }