]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/JBooleanToggleValueNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / JBooleanToggleValueNode.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.swing;\r
13 \r
14 import java.awt.BasicStroke;\r
15 import java.awt.Color;\r
16 import java.awt.Cursor;\r
17 import java.awt.Graphics2D;\r
18 import java.awt.Shape;\r
19 import java.awt.event.ActionEvent;\r
20 import java.awt.event.ActionListener;\r
21 import java.awt.geom.AffineTransform;\r
22 import java.awt.geom.Rectangle2D;\r
23 import java.util.HashSet;\r
24 import java.util.Set;\r
25 \r
26 import javax.swing.JToggleButton;\r
27 \r
28 import org.simantics.scenegraph.utils.DummyComponent;\r
29 \r
30 public class JBooleanToggleValueNode extends ComponentNode<JToggleButton> implements ActionListener {\r
31     /**\r
32          * \r
33          */\r
34         private static final long serialVersionUID = 3255791584573492072L;\r
35 \r
36         protected static final BasicStroke STROKE = new BasicStroke(1.0f);\r
37     protected static final Rectangle2D BOUNDS = new Rectangle2D.Double(-10, -10, 20, 20);\r
38 \r
39     protected Boolean value = null;\r
40     protected transient Set<ActionListener> actionListeners = new HashSet<ActionListener>();\r
41 \r
42     @SyncField("value")\r
43     public void setValue(Boolean value) {\r
44         this.value = value;\r
45         if(component instanceof JToggleButton) {\r
46                 ((JToggleButton)component).setSelected(value == null ? false : value);\r
47         }\r
48     }\r
49     \r
50     @SyncField("transform")\r
51     public void setTransform(AffineTransform transform) {\r
52         assert(transform != null);\r
53         this.transform = transform;\r
54         this.transform.translate(-10, -10);\r
55     }\r
56 \r
57     @Override\r
58     public void render(Graphics2D g2d) {\r
59         if (component != null) {\r
60                 int width = (int)BOUNDS.getWidth();\r
61                 int height = (int)BOUNDS.getHeight();\r
62                 \r
63                 AffineTransform ot = g2d.getTransform();\r
64                 double sx = 1;\r
65                 double sy = 1;\r
66             if(transform != null) {\r
67                 g2d.transform(transform);\r
68                 sx = g2d.getTransform().getScaleX();\r
69                 sy = g2d.getTransform().getScaleY();\r
70                 g2d.scale(1/sx, 1/sy);\r
71             }\r
72             component.setSize((int)(width*sx), (int)(height*sy));\r
73                 component.paint(g2d);\r
74                 \r
75                 g2d.setTransform(ot);\r
76         }\r
77     }\r
78     \r
79     /**\r
80      * Helper method to be used inside MonitorClass\r
81      * @return\r
82      */\r
83     public static Shape getOutline() {\r
84         return BOUNDS;\r
85     }\r
86     \r
87         @Override\r
88         public Rectangle2D getBoundsInLocal() {\r
89                 return BOUNDS;\r
90         }\r
91 \r
92         /**\r
93          * ClientSide method for handling event and transforming it\r
94          * NOTE: This is just a wrapper method\r
95          */\r
96         @Override\r
97         public void actionPerformed(ActionEvent e) {\r
98                 Boolean s = component.isSelected();\r
99                 performAction(new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, ""+s));\r
100         }\r
101         \r
102         /**\r
103          * ServerSide method for calling the actionlisteners\r
104          * @param e\r
105          */\r
106     @ServerSide\r
107     protected void performAction(ActionEvent e) {\r
108                 for(ActionListener listener : actionListeners)\r
109                         listener.actionPerformed(e);\r
110     }\r
111         \r
112     public void addActionListener(ActionListener l) {\r
113         actionListeners.add(l);\r
114     }\r
115     \r
116     public void removeActionListener(ActionListener l) {\r
117         actionListeners.remove(l);\r
118     }\r
119     \r
120     @Override\r
121     public void init() {\r
122         Boolean s = value == null ? false : value;\r
123         component = new JToggleButton(null, null, s);\r
124         component.setCursor(new Cursor(Cursor.HAND_CURSOR));\r
125         component.addActionListener(this);\r
126         component.setBackground(Color.RED);\r
127         super.init();\r
128     }\r
129 }\r