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