]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ButtonNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / ButtonNode.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;
13
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 import java.awt.Graphics2D;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;\r
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import javax.swing.JButton;\r
23
24 public class ButtonNode extends ComponentNode<JButton> implements ActionListener {
25
26     private static final long serialVersionUID = 3161843367263793336L;
27
28     protected String text;
29     protected Font font = null;\r
30     protected Color color = null;\r
31
32     protected transient Set<ActionListener> actionListeners = new HashSet<ActionListener>();
33
34     @Override
35     public void render(Graphics2D g2d) {\r
36         if (component != null)\r
37                 component.setText(text);\r
38         super.render(g2d);
39     }\r
40
41     @SyncField("text")
42     public void setText(String text) {
43         this.text = text;
44     }\r
45     \r
46     @PropertySetter("Font")
47     @SyncField("font")\r
48     public void setFont(Font font) {\r
49         this.font = font;\r
50         if (component != null) {\r
51             setComponentFont(font);\r
52         }\r
53     }\r
54 \r
55     @PropertySetter("Color")\r
56     @SyncField("color")\r
57     public void setColor(Color color) {\r
58         this.color = color;\r
59         if (component != null) {\r
60             component.setForeground(color);\r
61         }\r
62     }\r
63     \r
64     public Font getFont() {\r
65         return font;\r
66     }\r
67
68     @ServerSide
69         @Override
70         public void actionPerformed(ActionEvent e) {
71                 for(ActionListener listener : actionListeners)
72                         listener.actionPerformed(e);
73         }
74         
75     public void addActionListener(ActionListener l) {
76         actionListeners.add(l);
77     }
78     
79     public void removeActionListener(ActionListener l) {
80         actionListeners.remove(l);
81     }\r
82
83     @Override\r
84     public void init() {\r
85         component = new JButton();\r
86                 component.addActionListener(this);\r
87 //      scale = true;\r
88         super.init();\r
89     }
90 }