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