author | peterz |
Thu, 10 Dec 2009 12:08:58 +0300 | |
changeset 4394 | 92a8ec883f5d |
parent 2658 | 43e06bc950ec |
child 4848 | ffcc849b9351 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing.plaf.synth; |
|
27 |
||
28 |
import java.awt.event.*; |
|
29 |
import java.awt.Graphics; |
|
30 |
import java.awt.Dimension; |
|
31 |
import java.awt.FontMetrics; |
|
32 |
import java.awt.Rectangle; |
|
33 |
import java.awt.Point; |
|
34 |
import java.awt.Insets; |
|
35 |
import java.beans.*; |
|
36 |
import java.util.Dictionary; |
|
37 |
import java.util.Enumeration; |
|
38 |
import javax.swing.*; |
|
39 |
import javax.swing.plaf.*; |
|
40 |
import javax.swing.plaf.basic.BasicSliderUI; |
|
41 |
import sun.swing.SwingUtilities2; |
|
42 |
||
43 |
||
44 |
/** |
|
4394 | 45 |
* Provides the Synth L&F UI delegate for |
46 |
* {@link javax.swing.JSlider}. |
|
2 | 47 |
* |
48 |
* @author Joshua Outwater |
|
4394 | 49 |
* @since 1.7 |
2 | 50 |
*/ |
4394 | 51 |
public class SynthSliderUI extends BasicSliderUI |
52 |
implements PropertyChangeListener, SynthUI { |
|
53 |
private Rectangle valueRect = new Rectangle(); |
|
54 |
private boolean paintValue; |
|
2 | 55 |
|
2658 | 56 |
/** |
57 |
* When a JSlider is used as a renderer in a JTable, its layout is not |
|
58 |
* being recomputed even though the size is changing. Even though there |
|
59 |
* is a ComponentListener installed, it is not being notified. As such, |
|
60 |
* at times when being asked to paint the layout should first be redone. |
|
61 |
* At the end of the layout method we set this lastSize variable, which |
|
62 |
* represents the size of the slider the last time it was layed out. |
|
63 |
* |
|
64 |
* In the paint method we then check to see that this is accurate, that |
|
65 |
* the slider has not changed sizes since being last layed out. If necessary |
|
66 |
* we recompute the layout. |
|
67 |
*/ |
|
68 |
private Dimension lastSize = null; |
|
69 |
||
2 | 70 |
private int trackHeight; |
71 |
private int trackBorder; |
|
72 |
private int thumbWidth; |
|
73 |
private int thumbHeight; |
|
74 |
||
75 |
private SynthStyle style; |
|
76 |
private SynthStyle sliderTrackStyle; |
|
77 |
private SynthStyle sliderThumbStyle; |
|
78 |
||
79 |
/** Used to determine the color to paint the thumb. */ |
|
2658 | 80 |
private transient boolean thumbActive; //happens on rollover, and when pressed |
81 |
private transient boolean thumbPressed; //happens when mouse was depressed while over thumb |
|
2 | 82 |
|
83 |
/////////////////////////////////////////////////// |
|
84 |
// ComponentUI Interface Implementation methods |
|
85 |
/////////////////////////////////////////////////// |
|
4394 | 86 |
/** |
87 |
* Creates a new UI object for the given component. |
|
88 |
* |
|
89 |
* @param c component to create UI object for |
|
90 |
* @return the UI object |
|
91 |
*/ |
|
2 | 92 |
public static ComponentUI createUI(JComponent c) { |
93 |
return new SynthSliderUI((JSlider)c); |
|
94 |
} |
|
95 |
||
4394 | 96 |
protected SynthSliderUI(JSlider c) { |
2 | 97 |
super(c); |
98 |
} |
|
99 |
||
4394 | 100 |
/** |
101 |
* @inheritDoc |
|
102 |
*/ |
|
103 |
@Override |
|
2 | 104 |
protected void installDefaults(JSlider slider) { |
105 |
updateStyle(slider); |
|
106 |
} |
|
107 |
||
4394 | 108 |
/** |
109 |
* Uninstalls default setting. This method is called when a |
|
110 |
* {@code LookAndFeel} is uninstalled. |
|
111 |
*/ |
|
2 | 112 |
protected void uninstallDefaults() { |
113 |
SynthContext context = getContext(slider, ENABLED); |
|
114 |
style.uninstallDefaults(context); |
|
115 |
context.dispose(); |
|
116 |
style = null; |
|
117 |
||
118 |
context = getContext(slider, Region.SLIDER_TRACK, ENABLED); |
|
119 |
sliderTrackStyle.uninstallDefaults(context); |
|
120 |
context.dispose(); |
|
121 |
sliderTrackStyle = null; |
|
122 |
||
123 |
context = getContext(slider, Region.SLIDER_THUMB, ENABLED); |
|
124 |
sliderThumbStyle.uninstallDefaults(context); |
|
125 |
context.dispose(); |
|
126 |
sliderThumbStyle = null; |
|
127 |
} |
|
128 |
||
4394 | 129 |
/** |
130 |
* @inheritDoc |
|
131 |
*/ |
|
132 |
@Override |
|
2 | 133 |
protected void installListeners(JSlider slider) { |
134 |
super.installListeners(slider); |
|
135 |
slider.addPropertyChangeListener(this); |
|
136 |
} |
|
137 |
||
4394 | 138 |
/** |
139 |
* @inheritDoc |
|
140 |
*/ |
|
141 |
@Override |
|
2 | 142 |
protected void uninstallListeners(JSlider slider) { |
143 |
slider.removePropertyChangeListener(this); |
|
144 |
super.uninstallListeners(slider); |
|
145 |
} |
|
146 |
||
147 |
private void updateStyle(JSlider c) { |
|
148 |
SynthContext context = getContext(c, ENABLED); |
|
149 |
SynthStyle oldStyle = style; |
|
150 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
151 |
||
152 |
if (style != oldStyle) { |
|
153 |
thumbWidth = |
|
154 |
style.getInt(context, "Slider.thumbWidth", 30); |
|
155 |
||
156 |
thumbHeight = |
|
157 |
style.getInt(context, "Slider.thumbHeight", 14); |
|
158 |
||
2658 | 159 |
// handle scaling for sizeVarients for special case components. The |
160 |
// key "JComponent.sizeVariant" scales for large/small/mini |
|
161 |
// components are based on Apples LAF |
|
162 |
String scaleKey = (String)slider.getClientProperty( |
|
163 |
"JComponent.sizeVariant"); |
|
164 |
if (scaleKey != null){ |
|
165 |
if ("large".equals(scaleKey)){ |
|
166 |
thumbWidth *= 1.15; |
|
167 |
thumbHeight *= 1.15; |
|
168 |
} else if ("small".equals(scaleKey)){ |
|
169 |
thumbWidth *= 0.857; |
|
170 |
thumbHeight *= 0.857; |
|
171 |
} else if ("mini".equals(scaleKey)){ |
|
172 |
thumbWidth *= 0.784; |
|
173 |
thumbHeight *= 0.784; |
|
174 |
} |
|
175 |
} |
|
176 |
||
2 | 177 |
trackBorder = |
178 |
style.getInt(context, "Slider.trackBorder", 1); |
|
179 |
||
180 |
trackHeight = thumbHeight + trackBorder * 2; |
|
181 |
||
182 |
paintValue = style.getBoolean(context, |
|
183 |
"Slider.paintValue", true); |
|
184 |
if (oldStyle != null) { |
|
185 |
uninstallKeyboardActions(c); |
|
186 |
installKeyboardActions(c); |
|
187 |
} |
|
188 |
} |
|
189 |
context.dispose(); |
|
190 |
||
191 |
context = getContext(c, Region.SLIDER_TRACK, ENABLED); |
|
192 |
sliderTrackStyle = |
|
193 |
SynthLookAndFeel.updateStyle(context, this); |
|
194 |
context.dispose(); |
|
195 |
||
196 |
context = getContext(c, Region.SLIDER_THUMB, ENABLED); |
|
197 |
sliderThumbStyle = |
|
198 |
SynthLookAndFeel.updateStyle(context, this); |
|
199 |
context.dispose(); |
|
200 |
} |
|
201 |
||
4394 | 202 |
/** |
203 |
* @inheritDoc |
|
204 |
*/ |
|
205 |
@Override |
|
2 | 206 |
protected TrackListener createTrackListener(JSlider s) { |
207 |
return new SynthTrackListener(); |
|
208 |
} |
|
209 |
||
210 |
private void updateThumbState(int x, int y) { |
|
211 |
setThumbActive(thumbRect.contains(x, y)); |
|
212 |
} |
|
213 |
||
2658 | 214 |
private void updateThumbState(int x, int y, boolean pressed) { |
215 |
updateThumbState(x, y); |
|
216 |
setThumbPressed(pressed); |
|
217 |
} |
|
218 |
||
2 | 219 |
private void setThumbActive(boolean active) { |
220 |
if (thumbActive != active) { |
|
221 |
thumbActive = active; |
|
222 |
slider.repaint(thumbRect); |
|
223 |
} |
|
224 |
} |
|
225 |
||
2658 | 226 |
private void setThumbPressed(boolean pressed) { |
227 |
if (thumbPressed != pressed) { |
|
228 |
thumbPressed = pressed; |
|
229 |
slider.repaint(thumbRect); |
|
230 |
} |
|
231 |
} |
|
232 |
||
4394 | 233 |
/** |
234 |
* @inheritDoc |
|
235 |
*/ |
|
236 |
@Override |
|
2 | 237 |
public int getBaseline(JComponent c, int width, int height) { |
238 |
if (c == null) { |
|
239 |
throw new NullPointerException("Component must be non-null"); |
|
240 |
} |
|
241 |
if (width < 0 || height < 0) { |
|
242 |
throw new IllegalArgumentException( |
|
243 |
"Width and height must be >= 0"); |
|
244 |
} |
|
245 |
if (slider.getPaintLabels() && labelsHaveSameBaselines()) { |
|
246 |
// Get the insets for the track. |
|
247 |
Insets trackInsets = new Insets(0, 0, 0, 0); |
|
248 |
SynthContext trackContext = getContext(slider, |
|
249 |
Region.SLIDER_TRACK); |
|
250 |
style.getInsets(trackContext, trackInsets); |
|
251 |
trackContext.dispose(); |
|
252 |
if (slider.getOrientation() == JSlider.HORIZONTAL) { |
|
253 |
int valueHeight = 0; |
|
254 |
if (paintValue) { |
|
255 |
SynthContext context = getContext(slider); |
|
256 |
valueHeight = context.getStyle().getGraphicsUtils(context). |
|
257 |
getMaximumCharHeight(context); |
|
258 |
context.dispose(); |
|
259 |
} |
|
260 |
int tickHeight = 0; |
|
261 |
if (slider.getPaintTicks()) { |
|
262 |
tickHeight = getTickLength(); |
|
263 |
} |
|
264 |
int labelHeight = getHeightOfTallestLabel(); |
|
265 |
int contentHeight = valueHeight + trackHeight + |
|
266 |
trackInsets.top + trackInsets.bottom + |
|
267 |
tickHeight + labelHeight + 4; |
|
268 |
int centerY = height / 2 - contentHeight / 2; |
|
269 |
centerY += valueHeight + 2; |
|
270 |
centerY += trackHeight + trackInsets.top + trackInsets.bottom; |
|
271 |
centerY += tickHeight + 2; |
|
676
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
272 |
JComponent label = (JComponent) slider.getLabelTable().elements().nextElement(); |
2 | 273 |
Dimension pref = label.getPreferredSize(); |
274 |
return centerY + label.getBaseline(pref.width, pref.height); |
|
275 |
} |
|
276 |
else { // VERTICAL |
|
277 |
Integer value = slider.getInverted() ? getLowestValue() : |
|
278 |
getHighestValue(); |
|
279 |
if (value != null) { |
|
280 |
int valueY = insetCache.top; |
|
281 |
int valueHeight = 0; |
|
282 |
if (paintValue) { |
|
283 |
SynthContext context = getContext(slider); |
|
284 |
valueHeight = context.getStyle().getGraphicsUtils( |
|
285 |
context).getMaximumCharHeight(context); |
|
286 |
context.dispose(); |
|
287 |
} |
|
288 |
int contentHeight = height - insetCache.top - |
|
289 |
insetCache.bottom; |
|
290 |
int trackY = valueY + valueHeight; |
|
291 |
int trackHeight = contentHeight - valueHeight; |
|
292 |
int yPosition = yPositionForValue(value.intValue(), trackY, |
|
293 |
trackHeight); |
|
676
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
294 |
JComponent label = (JComponent) slider.getLabelTable().get(value); |
2 | 295 |
Dimension pref = label.getPreferredSize(); |
296 |
return yPosition - pref.height / 2 + |
|
297 |
label.getBaseline(pref.width, pref.height); |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
return -1; |
|
302 |
} |
|
303 |
||
4394 | 304 |
/** |
305 |
* @inheritDoc |
|
306 |
*/ |
|
307 |
@Override |
|
2 | 308 |
public Dimension getPreferredSize(JComponent c) { |
309 |
recalculateIfInsetsChanged(); |
|
4394 | 310 |
Dimension d = new Dimension(contentRect.width, contentRect.height); |
2 | 311 |
if (slider.getOrientation() == JSlider.VERTICAL) { |
312 |
d.height = 200; |
|
313 |
} else { |
|
314 |
d.width = 200; |
|
315 |
} |
|
316 |
Insets i = slider.getInsets(); |
|
317 |
d.width += i.left + i.right; |
|
318 |
d.height += i.top + i.bottom; |
|
319 |
return d; |
|
320 |
} |
|
321 |
||
4394 | 322 |
/** |
323 |
* @inheritDoc |
|
324 |
*/ |
|
325 |
@Override |
|
2 | 326 |
public Dimension getMinimumSize(JComponent c) { |
327 |
recalculateIfInsetsChanged(); |
|
4394 | 328 |
Dimension d = new Dimension(contentRect.width, contentRect.height); |
2 | 329 |
if (slider.getOrientation() == JSlider.VERTICAL) { |
330 |
d.height = thumbRect.height + insetCache.top + insetCache.bottom; |
|
331 |
} else { |
|
332 |
d.width = thumbRect.width + insetCache.left + insetCache.right; |
|
333 |
} |
|
334 |
return d; |
|
335 |
} |
|
336 |
||
4394 | 337 |
/** |
338 |
* @inheritDoc |
|
339 |
*/ |
|
340 |
@Override |
|
2 | 341 |
protected void calculateGeometry() { |
342 |
layout(); |
|
343 |
calculateThumbLocation(); |
|
344 |
} |
|
345 |
||
4394 | 346 |
/** |
347 |
* Lays out the slider. |
|
348 |
*/ |
|
2 | 349 |
protected void layout() { |
350 |
SynthContext context = getContext(slider); |
|
351 |
SynthGraphicsUtils synthGraphics = style.getGraphicsUtils(context); |
|
352 |
||
353 |
// Set the thumb size. |
|
354 |
Dimension size = getThumbSize(); |
|
355 |
thumbRect.setSize(size.width, size.height); |
|
356 |
||
357 |
// Get the insets for the track. |
|
358 |
Insets trackInsets = new Insets(0, 0, 0, 0); |
|
359 |
SynthContext trackContext = getContext(slider, Region.SLIDER_TRACK); |
|
360 |
style.getInsets(trackContext, trackInsets); |
|
361 |
trackContext.dispose(); |
|
362 |
||
363 |
if (slider.getOrientation() == JSlider.HORIZONTAL) { |
|
364 |
// Calculate the height of all the subcomponents so we can center |
|
365 |
// them. |
|
366 |
valueRect.height = 0; |
|
367 |
if (paintValue) { |
|
368 |
valueRect.height = |
|
369 |
synthGraphics.getMaximumCharHeight(context); |
|
370 |
} |
|
371 |
||
372 |
trackRect.height = trackHeight; |
|
373 |
||
374 |
tickRect.height = 0; |
|
375 |
if (slider.getPaintTicks()) { |
|
376 |
tickRect.height = getTickLength(); |
|
377 |
} |
|
378 |
||
379 |
labelRect.height = 0; |
|
380 |
if (slider.getPaintLabels()) { |
|
381 |
labelRect.height = getHeightOfTallestLabel(); |
|
382 |
} |
|
383 |
||
4394 | 384 |
contentRect.height = valueRect.height + trackRect.height |
2 | 385 |
+ trackInsets.top + trackInsets.bottom |
386 |
+ tickRect.height + labelRect.height + 4; |
|
4394 | 387 |
contentRect.width = slider.getWidth() - insetCache.left |
2 | 388 |
- insetCache.right; |
389 |
||
390 |
// Check if any of the labels will paint out of bounds. |
|
391 |
int pad = 0; |
|
392 |
if (slider.getPaintLabels()) { |
|
393 |
// Calculate the track rectangle. It is necessary for |
|
394 |
// xPositionForValue to return correct values. |
|
395 |
trackRect.x = insetCache.left; |
|
4394 | 396 |
trackRect.width = contentRect.width; |
2 | 397 |
|
398 |
Dictionary dictionary = slider.getLabelTable(); |
|
399 |
if (dictionary != null) { |
|
400 |
int minValue = slider.getMinimum(); |
|
401 |
int maxValue = slider.getMaximum(); |
|
402 |
||
403 |
// Iterate through the keys in the dictionary and find the |
|
404 |
// first and last labels indices that fall within the |
|
405 |
// slider range. |
|
406 |
int firstLblIdx = Integer.MAX_VALUE; |
|
407 |
int lastLblIdx = Integer.MIN_VALUE; |
|
408 |
for (Enumeration keys = dictionary.keys(); |
|
409 |
keys.hasMoreElements(); ) { |
|
410 |
int keyInt = ((Integer)keys.nextElement()).intValue(); |
|
411 |
if (keyInt >= minValue && keyInt < firstLblIdx) { |
|
412 |
firstLblIdx = keyInt; |
|
413 |
} |
|
414 |
if (keyInt <= maxValue && keyInt > lastLblIdx) { |
|
415 |
lastLblIdx = keyInt; |
|
416 |
} |
|
417 |
} |
|
418 |
// Calculate the pad necessary for the labels at the first |
|
419 |
// and last visible indices. |
|
420 |
pad = getPadForLabel(firstLblIdx); |
|
421 |
pad = Math.max(pad, getPadForLabel(lastLblIdx)); |
|
422 |
} |
|
423 |
} |
|
424 |
// Calculate the painting rectangles for each of the different |
|
425 |
// slider areas. |
|
426 |
valueRect.x = trackRect.x = tickRect.x = labelRect.x = |
|
427 |
(insetCache.left + pad); |
|
428 |
valueRect.width = trackRect.width = tickRect.width = |
|
4394 | 429 |
labelRect.width = (contentRect.width - (pad * 2)); |
2 | 430 |
|
4394 | 431 |
int centerY = slider.getHeight() / 2 - contentRect.height / 2; |
2 | 432 |
|
433 |
valueRect.y = centerY; |
|
434 |
centerY += valueRect.height + 2; |
|
435 |
||
436 |
trackRect.y = centerY + trackInsets.top; |
|
437 |
centerY += trackRect.height + trackInsets.top + trackInsets.bottom; |
|
438 |
||
439 |
tickRect.y = centerY; |
|
440 |
centerY += tickRect.height + 2; |
|
441 |
||
442 |
labelRect.y = centerY; |
|
443 |
centerY += labelRect.height; |
|
444 |
} else { |
|
445 |
// Calculate the width of all the subcomponents so we can center |
|
446 |
// them. |
|
447 |
trackRect.width = trackHeight; |
|
448 |
||
449 |
tickRect.width = 0; |
|
450 |
if (slider.getPaintTicks()) { |
|
451 |
tickRect.width = getTickLength(); |
|
452 |
} |
|
453 |
||
454 |
labelRect.width = 0; |
|
455 |
if (slider.getPaintLabels()) { |
|
456 |
labelRect.width = getWidthOfWidestLabel(); |
|
457 |
} |
|
458 |
||
459 |
valueRect.y = insetCache.top; |
|
460 |
valueRect.height = 0; |
|
461 |
if (paintValue) { |
|
462 |
valueRect.height = |
|
463 |
synthGraphics.getMaximumCharHeight(context); |
|
464 |
} |
|
465 |
||
466 |
// Get the max width of the min or max value of the slider. |
|
467 |
FontMetrics fm = slider.getFontMetrics(slider.getFont()); |
|
468 |
valueRect.width = Math.max( |
|
469 |
synthGraphics.computeStringWidth(context, slider.getFont(), |
|
470 |
fm, "" + slider.getMaximum()), |
|
471 |
synthGraphics.computeStringWidth(context, slider.getFont(), |
|
472 |
fm, "" + slider.getMinimum())); |
|
473 |
||
474 |
int l = valueRect.width / 2; |
|
475 |
int w1 = trackInsets.left + trackRect.width / 2; |
|
476 |
int w2 = trackRect.width / 2 + trackInsets.right + |
|
477 |
tickRect.width + labelRect.width; |
|
4394 | 478 |
contentRect.width = Math.max(w1, l) + Math.max(w2, l) + |
2 | 479 |
2 + insetCache.left + insetCache.right; |
4394 | 480 |
contentRect.height = slider.getHeight() - |
2 | 481 |
insetCache.top - insetCache.bottom; |
482 |
||
483 |
// Layout the components. |
|
484 |
trackRect.y = tickRect.y = labelRect.y = |
|
485 |
valueRect.y + valueRect.height; |
|
486 |
trackRect.height = tickRect.height = labelRect.height = |
|
4394 | 487 |
contentRect.height - valueRect.height; |
2 | 488 |
|
4394 | 489 |
int startX = slider.getWidth() / 2 - contentRect.width / 2; |
2 | 490 |
if (SynthLookAndFeel.isLeftToRight(slider)) { |
491 |
if (l > w1) { |
|
492 |
startX += (l - w1); |
|
493 |
} |
|
494 |
trackRect.x = startX + trackInsets.left; |
|
495 |
||
496 |
startX += trackInsets.left + trackRect.width + trackInsets.right; |
|
497 |
tickRect.x = startX; |
|
498 |
labelRect.x = startX + tickRect.width + 2; |
|
499 |
} else { |
|
500 |
if (l > w2) { |
|
501 |
startX += (l - w2); |
|
502 |
} |
|
503 |
labelRect.x = startX; |
|
504 |
||
505 |
startX += labelRect.width + 2; |
|
506 |
tickRect.x = startX; |
|
507 |
trackRect.x = startX + tickRect.width + trackInsets.left; |
|
508 |
} |
|
509 |
} |
|
510 |
context.dispose(); |
|
2658 | 511 |
lastSize = slider.getSize(); |
2 | 512 |
} |
513 |
||
514 |
/** |
|
515 |
* Calculates the pad for the label at the specified index. |
|
516 |
* |
|
676
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
517 |
* @param i index of the label to calculate pad for. |
2 | 518 |
* @return padding required to keep label visible. |
519 |
*/ |
|
520 |
private int getPadForLabel(int i) { |
|
521 |
int pad = 0; |
|
522 |
||
676
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
523 |
JComponent c = (JComponent) slider.getLabelTable().get(i); |
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
524 |
if (c != null) { |
2 | 525 |
int centerX = xPositionForValue(i); |
526 |
int cHalfWidth = c.getPreferredSize().width / 2; |
|
527 |
if (centerX - cHalfWidth < insetCache.left) { |
|
528 |
pad = Math.max(pad, insetCache.left - (centerX - cHalfWidth)); |
|
529 |
} |
|
530 |
||
531 |
if (centerX + cHalfWidth > slider.getWidth() - insetCache.right) { |
|
532 |
pad = Math.max(pad, (centerX + cHalfWidth) - |
|
533 |
(slider.getWidth() - insetCache.right)); |
|
534 |
} |
|
535 |
} |
|
536 |
return pad; |
|
537 |
} |
|
538 |
||
4394 | 539 |
/** |
540 |
* @inheritDoc |
|
541 |
*/ |
|
542 |
@Override |
|
2 | 543 |
protected void calculateThumbLocation() { |
544 |
super.calculateThumbLocation(); |
|
545 |
if (slider.getOrientation() == JSlider.HORIZONTAL) { |
|
546 |
thumbRect.y += trackBorder; |
|
547 |
} else { |
|
548 |
thumbRect.x += trackBorder; |
|
549 |
} |
|
550 |
Point mousePosition = slider.getMousePosition(); |
|
551 |
if(mousePosition != null) { |
|
552 |
updateThumbState(mousePosition.x, mousePosition.y); |
|
553 |
} |
|
554 |
} |
|
555 |
||
4394 | 556 |
/** |
557 |
* @inheritDoc |
|
558 |
*/ |
|
559 |
@Override |
|
2 | 560 |
protected void calculateTickRect() { |
561 |
if (slider.getOrientation() == JSlider.HORIZONTAL) { |
|
562 |
tickRect.x = trackRect.x; |
|
563 |
tickRect.y = trackRect.y + trackRect.height + 2 + getTickLength(); |
|
564 |
tickRect.width = trackRect.width; |
|
565 |
tickRect.height = getTickLength(); |
|
566 |
||
567 |
if (!slider.getPaintTicks()) { |
|
568 |
--tickRect.y; |
|
569 |
tickRect.height = 0; |
|
570 |
} |
|
571 |
} else { |
|
572 |
if (SynthLookAndFeel.isLeftToRight(slider)) { |
|
573 |
tickRect.x = trackRect.x + trackRect.width; |
|
574 |
tickRect.width = getTickLength(); |
|
575 |
} else { |
|
576 |
tickRect.width = getTickLength(); |
|
577 |
tickRect.x = trackRect.x - tickRect.width; |
|
578 |
} |
|
579 |
tickRect.y = trackRect.y; |
|
580 |
tickRect.height = trackRect.height; |
|
581 |
||
582 |
if (!slider.getPaintTicks()) { |
|
583 |
--tickRect.x; |
|
584 |
tickRect.width = 0; |
|
585 |
} |
|
586 |
} |
|
587 |
} |
|
588 |
||
4394 | 589 |
/** |
590 |
* @inheritDoc |
|
591 |
*/ |
|
592 |
@Override |
|
2 | 593 |
public void setThumbLocation(int x, int y) { |
594 |
super.setThumbLocation(x, y); |
|
595 |
// Value rect is tied to the thumb location. We need to repaint when |
|
596 |
// the thumb repaints. |
|
597 |
slider.repaint(valueRect.x, valueRect.y, |
|
598 |
valueRect.width, valueRect.height); |
|
599 |
setThumbActive(false); |
|
600 |
} |
|
601 |
||
4394 | 602 |
/** |
603 |
* @inheritDoc |
|
604 |
*/ |
|
605 |
@Override |
|
2 | 606 |
protected int xPositionForValue(int value) { |
607 |
int min = slider.getMinimum(); |
|
608 |
int max = slider.getMaximum(); |
|
609 |
int trackLeft = trackRect.x + thumbRect.width / 2 + trackBorder; |
|
610 |
int trackRight = trackRect.x + trackRect.width - thumbRect.width / 2 |
|
611 |
- trackBorder; |
|
612 |
int trackLength = trackRight - trackLeft; |
|
613 |
double valueRange = (double)max - (double)min; |
|
614 |
double pixelsPerValue = (double)trackLength / valueRange; |
|
615 |
int xPosition; |
|
616 |
||
617 |
if (!drawInverted()) { |
|
618 |
xPosition = trackLeft; |
|
619 |
xPosition += Math.round( pixelsPerValue * ((double)value - min)); |
|
620 |
} else { |
|
621 |
xPosition = trackRight; |
|
622 |
xPosition -= Math.round( pixelsPerValue * ((double)value - min)); |
|
623 |
} |
|
624 |
||
625 |
xPosition = Math.max(trackLeft, xPosition); |
|
626 |
xPosition = Math.min(trackRight, xPosition); |
|
627 |
||
628 |
return xPosition; |
|
629 |
} |
|
630 |
||
4394 | 631 |
/** |
632 |
* @inheritDoc |
|
633 |
*/ |
|
634 |
@Override |
|
2 | 635 |
protected int yPositionForValue(int value, int trackY, int trackHeight) { |
636 |
int min = slider.getMinimum(); |
|
637 |
int max = slider.getMaximum(); |
|
638 |
int trackTop = trackY + thumbRect.height / 2 + trackBorder; |
|
639 |
int trackBottom = trackY + trackHeight - thumbRect.height / 2 - |
|
640 |
trackBorder; |
|
641 |
int trackLength = trackBottom - trackTop; |
|
642 |
double valueRange = (double)max - (double)min; |
|
676
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
643 |
double pixelsPerValue = (double)trackLength / valueRange; |
2 | 644 |
int yPosition; |
645 |
||
646 |
if (!drawInverted()) { |
|
647 |
yPosition = trackTop; |
|
648 |
yPosition += Math.round(pixelsPerValue * ((double)max - value)); |
|
649 |
} else { |
|
650 |
yPosition = trackTop; |
|
651 |
yPosition += Math.round(pixelsPerValue * ((double)value - min)); |
|
652 |
} |
|
653 |
||
654 |
yPosition = Math.max(trackTop, yPosition); |
|
655 |
yPosition = Math.min(trackBottom, yPosition); |
|
656 |
||
657 |
return yPosition; |
|
658 |
} |
|
659 |
||
660 |
/** |
|
4394 | 661 |
* @inheritDoc |
2 | 662 |
*/ |
4394 | 663 |
@Override |
2 | 664 |
public int valueForYPosition(int yPos) { |
665 |
int value; |
|
666 |
int minValue = slider.getMinimum(); |
|
667 |
int maxValue = slider.getMaximum(); |
|
668 |
int trackTop = trackRect.y + thumbRect.height / 2 + trackBorder; |
|
669 |
int trackBottom = trackRect.y + trackRect.height |
|
670 |
- thumbRect.height / 2 - trackBorder; |
|
671 |
int trackLength = trackBottom - trackTop; |
|
672 |
||
673 |
if (yPos <= trackTop) { |
|
674 |
value = drawInverted() ? minValue : maxValue; |
|
675 |
} else if (yPos >= trackBottom) { |
|
676 |
value = drawInverted() ? maxValue : minValue; |
|
677 |
} else { |
|
678 |
int distanceFromTrackTop = yPos - trackTop; |
|
679 |
double valueRange = (double)maxValue - (double)minValue; |
|
680 |
double valuePerPixel = valueRange / (double)trackLength; |
|
681 |
int valueFromTrackTop = |
|
682 |
(int)Math.round(distanceFromTrackTop * valuePerPixel); |
|
683 |
value = drawInverted() ? |
|
684 |
minValue + valueFromTrackTop : maxValue - valueFromTrackTop; |
|
685 |
} |
|
686 |
return value; |
|
687 |
} |
|
688 |
||
689 |
/** |
|
4394 | 690 |
* @inheritDoc |
2 | 691 |
*/ |
4394 | 692 |
@Override |
2 | 693 |
public int valueForXPosition(int xPos) { |
694 |
int value; |
|
695 |
int minValue = slider.getMinimum(); |
|
696 |
int maxValue = slider.getMaximum(); |
|
697 |
int trackLeft = trackRect.x + thumbRect.width / 2 + trackBorder; |
|
698 |
int trackRight = trackRect.x + trackRect.width |
|
699 |
- thumbRect.width / 2 - trackBorder; |
|
700 |
int trackLength = trackRight - trackLeft; |
|
701 |
||
702 |
if (xPos <= trackLeft) { |
|
703 |
value = drawInverted() ? maxValue : minValue; |
|
704 |
} else if (xPos >= trackRight) { |
|
705 |
value = drawInverted() ? minValue : maxValue; |
|
706 |
} else { |
|
707 |
int distanceFromTrackLeft = xPos - trackLeft; |
|
708 |
double valueRange = (double)maxValue - (double)minValue; |
|
709 |
double valuePerPixel = valueRange / (double)trackLength; |
|
710 |
int valueFromTrackLeft = |
|
711 |
(int)Math.round(distanceFromTrackLeft * valuePerPixel); |
|
712 |
value = drawInverted() ? |
|
713 |
maxValue - valueFromTrackLeft : minValue + valueFromTrackLeft; |
|
714 |
} |
|
715 |
return value; |
|
716 |
} |
|
717 |
||
4394 | 718 |
/** |
719 |
* @inheritDoc |
|
720 |
*/ |
|
721 |
@Override |
|
2 | 722 |
protected Dimension getThumbSize() { |
723 |
Dimension size = new Dimension(); |
|
724 |
||
725 |
if (slider.getOrientation() == JSlider.VERTICAL) { |
|
726 |
size.width = thumbHeight; |
|
727 |
size.height = thumbWidth; |
|
728 |
} else { |
|
729 |
size.width = thumbWidth; |
|
730 |
size.height = thumbHeight; |
|
731 |
} |
|
732 |
return size; |
|
733 |
} |
|
734 |
||
4394 | 735 |
/** |
736 |
* @inheritDoc |
|
737 |
*/ |
|
738 |
@Override |
|
2 | 739 |
protected void recalculateIfInsetsChanged() { |
740 |
SynthContext context = getContext(slider); |
|
741 |
Insets newInsets = style.getInsets(context, null); |
|
742 |
Insets compInsets = slider.getInsets(); |
|
743 |
newInsets.left += compInsets.left; newInsets.right += compInsets.right; |
|
744 |
newInsets.top += compInsets.top; newInsets.bottom += compInsets.bottom; |
|
745 |
if (!newInsets.equals(insetCache)) { |
|
746 |
insetCache = newInsets; |
|
747 |
calculateGeometry(); |
|
748 |
} |
|
749 |
context.dispose(); |
|
750 |
} |
|
751 |
||
4394 | 752 |
/** |
753 |
* @inheritDoc |
|
754 |
*/ |
|
755 |
@Override |
|
756 |
public SynthContext getContext(JComponent c) { |
|
757 |
return getContext(c, SynthLookAndFeel.getComponentState(c)); |
|
2 | 758 |
} |
759 |
||
4394 | 760 |
private SynthContext getContext(JComponent c, int state) { |
2 | 761 |
return SynthContext.getContext(SynthContext.class, c, |
762 |
SynthLookAndFeel.getRegion(c), style, state); |
|
763 |
} |
|
764 |
||
4394 | 765 |
private SynthContext getContext(JComponent c, Region subregion) { |
2 | 766 |
return getContext(c, subregion, getComponentState(c, subregion)); |
767 |
} |
|
768 |
||
769 |
private SynthContext getContext(JComponent c, Region subregion, int state) { |
|
770 |
SynthStyle style = null; |
|
771 |
Class klass = SynthContext.class; |
|
772 |
||
773 |
if (subregion == Region.SLIDER_TRACK) { |
|
774 |
style = sliderTrackStyle; |
|
775 |
} else if (subregion == Region.SLIDER_THUMB) { |
|
776 |
style = sliderThumbStyle; |
|
777 |
} |
|
778 |
return SynthContext.getContext(klass, c, subregion, style, state); |
|
779 |
} |
|
780 |
||
781 |
private int getComponentState(JComponent c, Region region) { |
|
782 |
if (region == Region.SLIDER_THUMB && thumbActive &&c.isEnabled()) { |
|
2658 | 783 |
int state = thumbPressed ? PRESSED : MOUSE_OVER; |
784 |
if (c.isFocusOwner()) state |= FOCUSED; |
|
785 |
return state; |
|
2 | 786 |
} |
787 |
return SynthLookAndFeel.getComponentState(c); |
|
788 |
} |
|
789 |
||
4394 | 790 |
/** |
791 |
* @inheritDoc |
|
792 |
*/ |
|
793 |
@Override |
|
2 | 794 |
public void update(Graphics g, JComponent c) { |
795 |
SynthContext context = getContext(c); |
|
796 |
SynthLookAndFeel.update(context, g); |
|
797 |
context.getPainter().paintSliderBackground(context, |
|
798 |
g, 0, 0, c.getWidth(), c.getHeight(), |
|
799 |
slider.getOrientation()); |
|
800 |
paint(context, g); |
|
801 |
context.dispose(); |
|
802 |
} |
|
803 |
||
4394 | 804 |
/** |
805 |
* @inheritDoc |
|
806 |
*/ |
|
807 |
@Override |
|
2 | 808 |
public void paint(Graphics g, JComponent c) { |
809 |
SynthContext context = getContext(c); |
|
810 |
paint(context, g); |
|
811 |
context.dispose(); |
|
812 |
} |
|
813 |
||
4394 | 814 |
/** |
815 |
* Paints the specified component. |
|
816 |
* |
|
817 |
* @param context context for the component being painted |
|
818 |
* @param g {@code Graphics} object used for painting |
|
819 |
*/ |
|
820 |
protected void paint(SynthContext context, Graphics g) { |
|
2 | 821 |
recalculateIfInsetsChanged(); |
822 |
recalculateIfOrientationChanged(); |
|
823 |
Rectangle clip = g.getClipBounds(); |
|
824 |
||
2658 | 825 |
if (lastSize == null || !lastSize.equals(slider.getSize())) { |
826 |
calculateGeometry(); |
|
827 |
} |
|
828 |
||
2 | 829 |
if (paintValue) { |
830 |
FontMetrics fm = SwingUtilities2.getFontMetrics(slider, g); |
|
831 |
int labelWidth = context.getStyle().getGraphicsUtils(context). |
|
832 |
computeStringWidth(context, g.getFont(), fm, |
|
833 |
"" + slider.getValue()); |
|
834 |
valueRect.x = thumbRect.x + (thumbRect.width - labelWidth) / 2; |
|
835 |
||
836 |
// For horizontal sliders, make sure value is not painted |
|
837 |
// outside slider bounds. |
|
838 |
if (slider.getOrientation() == JSlider.HORIZONTAL) { |
|
4394 | 839 |
if (valueRect.x + labelWidth > insetCache.left + contentRect.width) { |
840 |
valueRect.x = (insetCache.left + contentRect.width) - labelWidth; |
|
2 | 841 |
} |
842 |
valueRect.x = Math.max(valueRect.x, 0); |
|
843 |
} |
|
844 |
||
845 |
g.setColor(context.getStyle().getColor( |
|
846 |
context, ColorType.TEXT_FOREGROUND)); |
|
847 |
context.getStyle().getGraphicsUtils(context).paintText( |
|
848 |
context, g, "" + slider.getValue(), valueRect.x, |
|
849 |
valueRect.y, -1); |
|
850 |
} |
|
851 |
||
852 |
SynthContext subcontext = getContext(slider, Region.SLIDER_TRACK); |
|
853 |
paintTrack(subcontext, g, trackRect); |
|
854 |
subcontext.dispose(); |
|
855 |
||
856 |
subcontext = getContext(slider, Region.SLIDER_THUMB); |
|
857 |
paintThumb(subcontext, g, thumbRect); |
|
858 |
subcontext.dispose(); |
|
859 |
||
860 |
if (slider.getPaintTicks() && clip.intersects(tickRect)) { |
|
861 |
paintTicks(g); |
|
862 |
} |
|
863 |
||
864 |
if (slider.getPaintLabels() && clip.intersects(labelRect)) { |
|
865 |
paintLabels(g); |
|
866 |
} |
|
867 |
} |
|
868 |
||
4394 | 869 |
/** |
870 |
* @inheritDoc |
|
871 |
*/ |
|
872 |
@Override |
|
2 | 873 |
public void paintBorder(SynthContext context, Graphics g, int x, |
874 |
int y, int w, int h) { |
|
875 |
context.getPainter().paintSliderBorder(context, g, x, y, w, h, |
|
876 |
slider.getOrientation()); |
|
877 |
} |
|
878 |
||
4394 | 879 |
/** |
880 |
* Paints the slider thumb. |
|
881 |
* |
|
882 |
* @param context context for the component being painted |
|
883 |
* @param g {@code Graphics} object used for painting |
|
884 |
* @param thumbBounds bounding box for the thumb |
|
885 |
*/ |
|
886 |
protected void paintThumb(SynthContext context, Graphics g, |
|
2 | 887 |
Rectangle thumbBounds) { |
888 |
int orientation = slider.getOrientation(); |
|
889 |
SynthLookAndFeel.updateSubregion(context, g, thumbBounds); |
|
890 |
context.getPainter().paintSliderThumbBackground(context, g, |
|
891 |
thumbBounds.x, thumbBounds.y, thumbBounds.width, |
|
892 |
thumbBounds.height, orientation); |
|
893 |
context.getPainter().paintSliderThumbBorder(context, g, |
|
894 |
thumbBounds.x, thumbBounds.y, thumbBounds.width, |
|
895 |
thumbBounds.height, orientation); |
|
896 |
} |
|
897 |
||
4394 | 898 |
/** |
899 |
* Paints the slider track. |
|
900 |
* |
|
901 |
* @param context context for the component being painted |
|
902 |
* @param g {@code Graphics} object used for painting |
|
903 |
* @param trackBounds bounding box for the track |
|
904 |
*/ |
|
905 |
protected void paintTrack(SynthContext context, Graphics g, |
|
2 | 906 |
Rectangle trackBounds) { |
907 |
int orientation = slider.getOrientation(); |
|
908 |
SynthLookAndFeel.updateSubregion(context, g, trackBounds); |
|
909 |
context.getPainter().paintSliderTrackBackground(context, g, |
|
910 |
trackBounds.x, trackBounds.y, trackBounds.width, |
|
911 |
trackBounds.height, orientation); |
|
912 |
context.getPainter().paintSliderTrackBorder(context, g, |
|
913 |
trackBounds.x, trackBounds.y, trackBounds.width, |
|
914 |
trackBounds.height, orientation); |
|
915 |
} |
|
916 |
||
4394 | 917 |
/** |
918 |
* @inheritDoc |
|
919 |
*/ |
|
920 |
@Override |
|
2 | 921 |
public void propertyChange(PropertyChangeEvent e) { |
922 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
923 |
updateStyle((JSlider)e.getSource()); |
|
924 |
} |
|
925 |
} |
|
926 |
||
927 |
////////////////////////////////////////////////// |
|
928 |
/// Track Listener Class |
|
929 |
////////////////////////////////////////////////// |
|
930 |
/** |
|
931 |
* Track mouse movements. |
|
932 |
*/ |
|
4394 | 933 |
private class SynthTrackListener extends TrackListener { |
2 | 934 |
|
4394 | 935 |
@Override public void mouseExited(MouseEvent e) { |
2 | 936 |
setThumbActive(false); |
937 |
} |
|
938 |
||
4394 | 939 |
@Override public void mousePressed(MouseEvent e) { |
2658 | 940 |
super.mousePressed(e); |
941 |
setThumbPressed(thumbRect.contains(e.getX(), e.getY())); |
|
942 |
} |
|
943 |
||
4394 | 944 |
@Override public void mouseReleased(MouseEvent e) { |
2 | 945 |
super.mouseReleased(e); |
2658 | 946 |
updateThumbState(e.getX(), e.getY(), false); |
2 | 947 |
} |
948 |
||
4394 | 949 |
@Override public void mouseDragged(MouseEvent e) { |
676
8cf833d60e87
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents:
2
diff
changeset
|
950 |
int thumbMiddle; |
2 | 951 |
|
952 |
if (!slider.isEnabled()) { |
|
953 |
return; |
|
954 |
} |
|
955 |
||
956 |
currentMouseX = e.getX(); |
|
957 |
currentMouseY = e.getY(); |
|
958 |
||
959 |
if (!isDragging()) { |
|
960 |
return; |
|
961 |
} |
|
962 |
||
963 |
slider.setValueIsAdjusting(true); |
|
964 |
||
965 |
switch (slider.getOrientation()) { |
|
966 |
case JSlider.VERTICAL: |
|
967 |
int halfThumbHeight = thumbRect.height / 2; |
|
968 |
int thumbTop = e.getY() - offset; |
|
969 |
int trackTop = trackRect.y; |
|
970 |
int trackBottom = trackRect.y + trackRect.height |
|
971 |
- halfThumbHeight - trackBorder; |
|
972 |
int vMax = yPositionForValue(slider.getMaximum() - |
|
973 |
slider.getExtent()); |
|
974 |
||
975 |
if (drawInverted()) { |
|
976 |
trackBottom = vMax; |
|
2658 | 977 |
trackTop = trackTop + halfThumbHeight; |
2 | 978 |
} else { |
979 |
trackTop = vMax; |
|
980 |
} |
|
981 |
thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); |
|
982 |
thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); |
|
983 |
||
984 |
setThumbLocation(thumbRect.x, thumbTop); |
|
985 |
||
986 |
thumbMiddle = thumbTop + halfThumbHeight; |
|
987 |
slider.setValue(valueForYPosition(thumbMiddle)); |
|
988 |
break; |
|
989 |
case JSlider.HORIZONTAL: |
|
990 |
int halfThumbWidth = thumbRect.width / 2; |
|
991 |
int thumbLeft = e.getX() - offset; |
|
992 |
int trackLeft = trackRect.x + halfThumbWidth + trackBorder; |
|
993 |
int trackRight = trackRect.x + trackRect.width |
|
994 |
- halfThumbWidth - trackBorder; |
|
995 |
int hMax = xPositionForValue(slider.getMaximum() - |
|
996 |
slider.getExtent()); |
|
997 |
||
998 |
if (drawInverted()) { |
|
999 |
trackLeft = hMax; |
|
1000 |
} else { |
|
1001 |
trackRight = hMax; |
|
1002 |
} |
|
1003 |
thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); |
|
1004 |
thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); |
|
1005 |
||
1006 |
setThumbLocation(thumbLeft, thumbRect.y); |
|
1007 |
||
1008 |
thumbMiddle = thumbLeft + halfThumbWidth; |
|
1009 |
slider.setValue(valueForXPosition(thumbMiddle)); |
|
1010 |
break; |
|
1011 |
default: |
|
1012 |
return; |
|
1013 |
} |
|
1014 |
||
1015 |
if (slider.getValueIsAdjusting()) { |
|
1016 |
setThumbActive(true); |
|
1017 |
} |
|
1018 |
} |
|
1019 |
||
4394 | 1020 |
@Override public void mouseMoved(MouseEvent e) { |
2 | 1021 |
updateThumbState(e.getX(), e.getY()); |
1022 |
} |
|
1023 |
} |
|
1024 |
} |