1 | package com.framsticks.gui.components; |
---|
2 | |
---|
3 | import com.framsticks.params.Flags; |
---|
4 | import com.framsticks.params.types.DecimalParam; |
---|
5 | import com.framsticks.params.types.FloatParam; |
---|
6 | import com.framsticks.params.types.ValueParam; |
---|
7 | import com.framsticks.util.Layout; |
---|
8 | import com.framsticks.util.Numbers; |
---|
9 | |
---|
10 | import javax.swing.*; |
---|
11 | import javax.swing.event.ChangeEvent; |
---|
12 | import javax.swing.event.ChangeListener; |
---|
13 | import java.awt.*; |
---|
14 | import java.awt.event.FocusEvent; |
---|
15 | import java.awt.event.FocusListener; |
---|
16 | import java.awt.event.KeyEvent; |
---|
17 | import java.awt.event.KeyListener; |
---|
18 | import java.util.Hashtable; |
---|
19 | |
---|
20 | |
---|
21 | @SuppressWarnings("serial") |
---|
22 | public class SliderControl extends ValueControl { |
---|
23 | |
---|
24 | private JSlider slider; |
---|
25 | |
---|
26 | private JTextField sliderValue; |
---|
27 | |
---|
28 | /** |
---|
29 | * Division factor used to implement float value slider. |
---|
30 | */ |
---|
31 | private final int div = 10; |
---|
32 | |
---|
33 | /** |
---|
34 | * Old value of slider used when user press ESCAPE key. |
---|
35 | */ |
---|
36 | private int oldValue; |
---|
37 | |
---|
38 | public SliderControl(ValueParam valueParam) { |
---|
39 | super(valueParam); |
---|
40 | slider = new JSlider(); |
---|
41 | slider.setEnabled(!isReadOnly()); |
---|
42 | slider.addChangeListener(new ChangeListener() { |
---|
43 | @Override |
---|
44 | public void stateChanged(ChangeEvent changeEvent) { |
---|
45 | notifyOfChange(); |
---|
46 | } |
---|
47 | }); |
---|
48 | if (param instanceof DecimalParam) { |
---|
49 | int min = param.getMin(Integer.class); |
---|
50 | int max = param.getMax(Integer.class); |
---|
51 | slider.setMinimum(min); |
---|
52 | slider.setMaximum(max); |
---|
53 | if (param.getDef(Integer.class) != null) { |
---|
54 | slider.setValue(param.getDef(Integer.class)); |
---|
55 | } else { |
---|
56 | slider.setValue(min); |
---|
57 | } |
---|
58 | slider.setMajorTickSpacing((int) ((max - min) / 5)); |
---|
59 | slider.setMinorTickSpacing((int) ((max - min) / 10)); |
---|
60 | slider.setPaintLabels(true); |
---|
61 | slider.setPaintTicks(true); |
---|
62 | } |
---|
63 | if (param instanceof FloatParam) { |
---|
64 | double min = param.getMin(Double.class) * div; |
---|
65 | slider.setMinimum((int) min); |
---|
66 | double max = param.getMax(Double.class) * div; |
---|
67 | slider.setMaximum((int) max); |
---|
68 | |
---|
69 | Hashtable<Integer, java.awt.Component> labels = new Hashtable<Integer, java.awt.Component>(); |
---|
70 | for (int i = 0; i < 6; i++) { |
---|
71 | double val = 0; |
---|
72 | if (min < 0) { |
---|
73 | val = (((Math.abs(max) - min) / 5) * i) + min; |
---|
74 | } else { |
---|
75 | val = (((Math.abs(max) - Math.abs(min)) / 5) * i) + Math.abs(min); |
---|
76 | } |
---|
77 | String label = String.format("%1$.1f", (val / 10)).replace(",", "."); |
---|
78 | labels.put(new Integer((int) val), new JLabel(label, JLabel.CENTER)); |
---|
79 | } |
---|
80 | slider.setLabelTable(labels); |
---|
81 | slider.setPaintLabels(true); |
---|
82 | slider.setMajorTickSpacing((int) ((max - Math.abs(min)) / 5)); |
---|
83 | slider.setMinorTickSpacing((int) ((max - Math.abs(min)) / 10)); |
---|
84 | slider.setPaintTicks(true); |
---|
85 | if (param.getDef(Double.class) != null) { |
---|
86 | double defaultValue = param.getDef(Double.class) * div; |
---|
87 | slider.setValue((int) defaultValue); |
---|
88 | } |
---|
89 | } |
---|
90 | //TODO: actually set the value |
---|
91 | /* |
---|
92 | if (property.getValue() != null) { |
---|
93 | setValueImpl(property.getValue()); |
---|
94 | } |
---|
95 | */ |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | createPanel(); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Creates panel with description in label, slider value and slider next to description. |
---|
104 | */ |
---|
105 | private void createPanel() { |
---|
106 | this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); |
---|
107 | this.setAlignmentX(Box.CENTER_ALIGNMENT); |
---|
108 | this.setAlignmentY(Box.CENTER_ALIGNMENT); |
---|
109 | |
---|
110 | JPanel sliderPanel = new JPanel(); |
---|
111 | sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.LINE_AXIS)); |
---|
112 | |
---|
113 | createSliderValue(); |
---|
114 | |
---|
115 | JPanel sVPanel = new JPanel(); |
---|
116 | sVPanel.setLayout(new BoxLayout(sVPanel, BoxLayout.LINE_AXIS)); |
---|
117 | sVPanel.add(sliderValue); |
---|
118 | Layout.copyComponentDimensions(sVPanel, sliderValue); |
---|
119 | |
---|
120 | JPanel sPanel = new JPanel(); |
---|
121 | sPanel.setLayout(new BoxLayout(sPanel, BoxLayout.LINE_AXIS)); |
---|
122 | |
---|
123 | sliderPanel.setLayout(new BorderLayout()); |
---|
124 | sliderPanel.add(slider, BorderLayout.CENTER); |
---|
125 | sliderPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); |
---|
126 | sliderPanel.setMinimumSize(new Dimension(0, 60)); |
---|
127 | |
---|
128 | sPanel.add(sVPanel); |
---|
129 | sPanel.add(sliderPanel); |
---|
130 | |
---|
131 | this.add(sPanel); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Creates slider value text field |
---|
136 | */ |
---|
137 | private void createSliderValue() { |
---|
138 | if (param instanceof DecimalParam) { |
---|
139 | sliderValue = new JTextField(Integer.toString(slider.getValue())); |
---|
140 | sliderValue.setText(Integer.toString(slider.getValue())); |
---|
141 | } else { |
---|
142 | sliderValue = new JTextField(Double.toString(slider.getValue() / div)); |
---|
143 | sliderValue.setText(Double.toString(slider.getValue() / div)); |
---|
144 | } |
---|
145 | Layout.fixComponentSize(sliderValue, new Dimension(60, Control.LINE_HEIGHT)); |
---|
146 | sliderValue.setHorizontalAlignment(JSlider.CENTER); |
---|
147 | |
---|
148 | if (param.hasFlag(Flags.READONLY)) { |
---|
149 | sliderValue.setEnabled(false); |
---|
150 | } |
---|
151 | |
---|
152 | if (param.getDef(Object.class) != null) { |
---|
153 | sliderValue.setText(param.getDef(Object.class).toString()); |
---|
154 | } |
---|
155 | |
---|
156 | slider.addChangeListener(new ChangeListener() { |
---|
157 | |
---|
158 | public void stateChanged(ChangeEvent e) { |
---|
159 | if (param instanceof DecimalParam) { |
---|
160 | sliderValue.setText(Integer.toString(slider.getValue())); |
---|
161 | } |
---|
162 | if (param instanceof FloatParam) { |
---|
163 | float val = (float) slider.getValue() / (float) div; |
---|
164 | sliderValue.setText(Double.toString(val)); |
---|
165 | } |
---|
166 | } |
---|
167 | }); |
---|
168 | |
---|
169 | sliderValue.addFocusListener(new FocusListener() { |
---|
170 | |
---|
171 | public void focusGained(FocusEvent e) { |
---|
172 | oldValue = slider.getValue(); |
---|
173 | } |
---|
174 | |
---|
175 | public void focusLost(FocusEvent e) { |
---|
176 | applyNewValue(); |
---|
177 | } |
---|
178 | |
---|
179 | }); |
---|
180 | |
---|
181 | sliderValue.addKeyListener(new KeyListener() { |
---|
182 | |
---|
183 | public void keyPressed(KeyEvent e) { |
---|
184 | } |
---|
185 | |
---|
186 | public void keyReleased(KeyEvent e) { |
---|
187 | if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
---|
188 | applyNewValue(); |
---|
189 | } |
---|
190 | if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
---|
191 | if (param instanceof DecimalParam) { |
---|
192 | sliderValue.setText(Integer.toString(oldValue)); |
---|
193 | } |
---|
194 | if (param instanceof FloatParam) { |
---|
195 | sliderValue.setText(Double.toString((float) oldValue / (float) div)); |
---|
196 | } |
---|
197 | sliderValue.setBackground(Color.WHITE); |
---|
198 | sliderValue.invalidate(); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | public void keyTyped(KeyEvent e) { |
---|
203 | } |
---|
204 | |
---|
205 | }); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Applies new Value of slider if typed value is correct |
---|
210 | */ |
---|
211 | private void applyNewValue() { |
---|
212 | try { |
---|
213 | if (param instanceof DecimalParam) { |
---|
214 | int min = param.getMin(Integer.class); |
---|
215 | int max = param.getMax(Integer.class); |
---|
216 | |
---|
217 | int val = Integer.parseInt(sliderValue.getText()); |
---|
218 | if (val < min) { |
---|
219 | val = min; |
---|
220 | } |
---|
221 | if (val > max) { |
---|
222 | val = max; |
---|
223 | } |
---|
224 | slider.setValue(val); |
---|
225 | sliderValue.setBackground(Color.WHITE); |
---|
226 | } |
---|
227 | if (param instanceof FloatParam) { |
---|
228 | double val = Double.parseDouble(sliderValue.getText()); |
---|
229 | int min = param.getMin(Double.class).intValue(); |
---|
230 | int max = param.getMax(Double.class).intValue(); |
---|
231 | |
---|
232 | int intVal = (int) (val * div); |
---|
233 | min = min * div; |
---|
234 | max = max * div; |
---|
235 | if (intVal < min) { |
---|
236 | intVal = min; |
---|
237 | } |
---|
238 | if (intVal > max) { |
---|
239 | intVal = max; |
---|
240 | } |
---|
241 | slider.setValue(intVal); |
---|
242 | sliderValue.setBackground(Color.WHITE); |
---|
243 | } |
---|
244 | } catch (NumberFormatException e) { |
---|
245 | sliderValue.setText(getStringValue()); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | private String getStringValue() { |
---|
252 | if (param instanceof DecimalParam) { |
---|
253 | return Integer.toString((Integer)getValue()); |
---|
254 | } |
---|
255 | if (param instanceof FloatParam) { |
---|
256 | return Double.toString((Double)getValue()); |
---|
257 | } |
---|
258 | return null; |
---|
259 | } |
---|
260 | |
---|
261 | @Override |
---|
262 | public void setValueImpl(Object value) { |
---|
263 | if (value == null) { |
---|
264 | return; |
---|
265 | } |
---|
266 | if (param instanceof DecimalParam) { |
---|
267 | slider.setValue(Numbers.cast(value, Integer.class)); |
---|
268 | } else if (param instanceof FloatParam) { |
---|
269 | slider.setValue((int) ((Numbers.cast(value, Double.class)) * div)); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | @Override |
---|
274 | public Object getValue() { |
---|
275 | if (param instanceof DecimalParam) { |
---|
276 | return slider.getValue(); |
---|
277 | } |
---|
278 | if (param instanceof FloatParam) { |
---|
279 | return (double) slider.getValue() / (double) div; |
---|
280 | } |
---|
281 | return null; |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | } |
---|