]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/RadioButtonListNode.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / RadioButtonListNode.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.Color;
15 import java.awt.Font;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.swing.Box;
23 import javax.swing.BoxLayout;
24 import javax.swing.JRadioButton;
25
26 public class RadioButtonListNode extends ComponentNode<Box> implements ActionListener {
27     /**
28      * 
29      */
30     private static final long serialVersionUID = 7073028693751719102L;
31
32     protected boolean editable = true;
33     protected Object value = "";
34     protected String tooltip = "";
35     protected double borderWidth = 0;
36
37     protected transient ActionListener actionListener = null;
38
39     protected Font font = null;
40     protected Color color = null;
41     protected Map<String, JRadioButton> items = new HashMap<String, JRadioButton>();
42     
43     @Override
44     public String toString() {
45         return super.toString() + "[editable=" + editable + ", value=" + value + "]";
46     }
47     
48     @Override
49     public void init() {
50         component = new Box(BoxLayout.Y_AXIS);
51         component.setEnabled(editable);
52         component.setLocation(0, 0);
53         
54         super.init();
55     }
56
57     @SyncField("items")
58     public void setItems(List<String> items) {
59         this.items.clear();
60         component.removeAll();
61         for(String item : items) {
62                 JRadioButton btn = new JRadioButton();
63                 btn.setText(item);
64                 btn.addActionListener(this);
65                 component.add(btn);
66                 this.items.put(item, btn);
67         }
68         if(items.size() > 0)
69                 ((JRadioButton)component.getComponent(0)).setSelected(true); // First item selected
70
71         component.doLayout();
72     }
73
74     @SyncField("editable")
75     public void setEditable(boolean value) {
76         this.editable = value;
77
78         if(component != null) {
79             component.setEnabled(value);
80         }
81     }
82
83     @PropertySetter("Stroke Width")
84     @SyncField("borderWidth")
85     public void setBorderWidth(Float borderWidth) {
86         this.borderWidth = borderWidth;
87 //        if(component != null) {
88 //            ((TextField)component).setBorder(borderWidth);
89 //        }
90     }
91
92     @SyncField("value")
93     public void setValue(Object value) {
94         this.value = value;
95         // RemoteViewer does not have component initialized
96         if (component != null && value != null) {
97                 JRadioButton b = items.get(value);
98                 if(b != null) {
99                         for(JRadioButton i : items.values()) {
100                                 if(i == b) {
101                                         i.setSelected(true);
102                                 } else {
103                                         i.setSelected(false);
104                                 }
105                         }
106                 }
107         }
108     }
109
110     @SyncField("tooltip")
111     public void setToolTipText(String tooltip) {
112         this.tooltip = tooltip;
113         if (component != null) {
114             component.setToolTipText(tooltip);
115         }
116     }
117
118     @PropertySetter("Font")
119     @SyncField("font")
120     public void setFont(Font font) {
121         this.font = font;
122         if (component != null) {
123             setComponentFont(font);
124         }
125     }
126
127     @PropertySetter("Color")
128     @SyncField("color")
129     public void setColor(Color color) {
130         this.color = color;
131         if (component != null) {
132             component.setForeground(color);
133         }
134     }
135
136     public Object getValue() {
137         return value;
138     }
139
140     public Font getFont() {
141         return font;
142     }
143     
144     public void setActionListener(ActionListener actionListener) {
145         this.actionListener = actionListener;
146     }
147
148     /**
149      * Wrapper method to send event to server side
150      * 
151      * @param e
152      */
153     @ServerSide
154     public void performAction(ActionEvent e) {
155         if (actionListener != null) {
156             //System.out.println("MonitorNode.performAction(" + e + ")");
157             actionListener.actionPerformed(e);
158         }
159     }
160         @Override
161         public void actionPerformed(ActionEvent e) {
162                 JRadioButton b = items.get(e.getActionCommand());
163                 if(b != null) {
164                         for(JRadioButton i : items.values()) {
165                                 if(i == b) {
166                                         i.setSelected(true);
167                                 } else {
168                                         i.setSelected(false);
169                                 }
170                         }
171                 }
172                 performAction(e);
173         }
174
175 }