]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/SliderNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / SliderNode.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 \r
14 import java.awt.Color;\r
15 import java.awt.event.ActionEvent;\r
16 import java.awt.event.ActionListener;\r
17 import java.awt.event.MouseEvent;\r
18 import java.awt.event.MouseListener;\r
19 \r
20 import javax.swing.BorderFactory;\r
21 import javax.swing.JSlider;\r
22 \r
23 import org.simantics.scenegraph.ExportableWidget.InputWidget;\r
24 import org.simantics.scenegraph.ExportableWidget.OutputWidget;\r
25 \r
26 @OutputWidget({"value", "min", "max"})\r
27 @InputWidget("value")
28 public class SliderNode extends ComponentNode<JSlider> {
29     private static final long serialVersionUID = 3161843367263793336L;\r
30         protected transient ActionListener actionListener = null;
31 \r
32         // Necessary for widget ui:\r
33     protected Integer value = 0;\r
34     protected Integer min = 0;\r
35     protected Integer max = 0;\r
36     \r
37     @ClientSide
38     public void setValue(Integer value) {\r
39         this.value = value;\r
40         if(component != null) {\r
41                 if(!component.getValueIsAdjusting()) { // FIXME: propably does not work\r
42 //                      System.out.println("valueIsAdjusting == false");\r
43                         component.setValue(value);\r
44                 } else {\r
45                         // TODO: cache value?\r
46 //                      System.out.println("valueIsAdjusting == true");                         \r
47                 }\r
48         }\r
49     }\r
50
51     @Override\r
52     public void init() {\r
53         component = new JSlider();\r
54         component.setPaintTicks(true);\r
55                 component.addMouseListener(new MouseListener() {\r
56 \r
57                         @Override\r
58                         public void mouseClicked(MouseEvent e) {\r
59                                 // TODO Auto-generated method stub\r
60                                 \r
61                         }\r
62 \r
63                         @Override\r
64                         public void mousePressed(MouseEvent e) {\r
65                                 // TODO Auto-generated method stub\r
66                                 \r
67                         }\r
68 \r
69                         @Override\r
70                         public void mouseReleased(MouseEvent e) {\r
71                                 if(component.getValueIsAdjusting())\r
72                                         valueChanged(component.getValue());\r
73                         }\r
74 \r
75                         @Override\r
76                         public void mouseEntered(MouseEvent e) {\r
77                                 // TODO Auto-generated method stub\r
78                                 \r
79                         }\r
80 \r
81                         @Override\r
82                         public void mouseExited(MouseEvent e) {\r
83                                 // TODO Auto-generated method stub\r
84                                 \r
85                         }});\r
86         super.init();\r
87     }\r
88     \r
89         @ServerSide\r
90         protected void valueChanged(Integer value) {\r
91                 if(actionListener != null) { // FIXME: there might not be a component on the server side\r
92                 ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, ""+value);\r
93                         actionListener.actionPerformed(e);\r
94                 }\r
95         }\r
96         \r
97     public void setActionListener(ActionListener actionListener) {\r
98         this.actionListener = actionListener;\r
99     }\r
100 \r
101     @PropertySetter("Value Extent")\r
102     @ClientSide\r
103     public void setExtent(Integer extent) {\r
104         if(component != null)\r
105                 component.setExtent(extent);\r
106     }\r
107     \r
108     @PropertySetter("Min Value")\r
109     @ClientSide\r
110     public void setMinimum(Integer min) {\r
111         this.min = min;\r
112         if(component != null)\r
113                 component.setMinimum(min);\r
114     }\r
115     \r
116     @PropertySetter("Max Value")\r
117     @ClientSide\r
118     public void setMaximum(Integer max) {\r
119         this.max = max;\r
120         if(component != null)\r
121                 component.setMaximum(max);\r
122     }\r
123     \r
124     @ClientSide\r
125     public void setOrientation(Integer orientation) {\r
126         if(component != null) {\r
127                 component.setOrientation(orientation);\r
128         }\r
129     }\r
130 \r
131     @ClientSide\r
132         public void setTitle(String string) {\r
133                 if(component != null)\r
134                         component.setBorder(BorderFactory.createTitledBorder(string));\r
135         }\r
136 \r
137     @PropertySetter("Major Tick Spacing")\r
138     @ClientSide\r
139         public void setMajorTickSpacing(Integer i) {\r
140                 if(component != null) {\r
141                         component.setLabelTable(component.createStandardLabels(i, component.getMinimum()));\r
142                         component.setMajorTickSpacing(i);\r
143                 }\r
144         }\r
145 \r
146     @PropertySetter("Minor Tick Spacing")\r
147     @ClientSide\r
148         public void setMinorTickSpacing(Integer i) {\r
149                 if(component != null) {\r
150                         component.setMinorTickSpacing(i);\r
151                 }\r
152         }\r
153 \r
154     @PropertySetter("Paint Ticks")\r
155     @ClientSide\r
156         public void setPaintTicks(Boolean b) {\r
157                 if(component != null) {\r
158                         component.setPaintTicks(b);\r
159                         component.updateUI();\r
160                 }\r
161         }\r
162     \r
163     @PropertySetter("Paint Labels")\r
164     @ClientSide\r
165         public void setPaintLabels(Boolean b) {\r
166                 if(component != null)\r
167                         component.setPaintLabels(b);\r
168         }\r
169     \r
170     @PropertySetter("Paint Track")\r
171     @ClientSide\r
172         public void setPaintTrack(Boolean b) {\r
173                 if(component != null)\r
174                         component.setPaintTrack(b);\r
175         }\r
176     \r
177     @PropertySetter("Snap To Ticks")\r
178     @ClientSide\r
179         public void setSnapToTicks(Boolean b) {\r
180                 if(component != null)\r
181                         component.setSnapToTicks(b);\r
182         }\r
183     \r
184     @PropertySetter("Inverted")\r
185     @ClientSide\r
186         public void setInverted(Boolean b) {\r
187                 if(component != null)\r
188                         component.setInverted(b);\r
189         }\r
190     \r
191     @PropertySetter("Background Color")\r
192     @ClientSide\r
193         public void setBackgroundColor(Color color) {\r
194         // Not supported (For some reason this hides the slider)\r
195         }\r
196     \r
197     public String widgetGet(String name) {\r
198         if("value".equals(name)) {\r
199             return ""+value;\r
200         } else if("min".equals(name)) {\r
201             return ""+min;\r
202         } else if("max".equals(name)) {\r
203             return ""+max;\r
204         }\r
205         return null;\r
206     }\r
207     \r
208     public void widgetSet(String name, String value) {\r
209         if("value".equals(name)) {\r
210             this.value = Integer.parseInt(value);\r
211             valueChanged(this.value);\r
212         }\r
213     }\r
214 }