1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
59: public class MetalSliderUI extends BasicSliderUI
60: {
61:
67: protected class MetalPropertyListener
68: extends BasicSliderUI.PropertyChangeHandler
69: {
70:
73: protected MetalPropertyListener()
74: {
75:
76: }
77:
78:
84: public void propertyChange(PropertyChangeEvent e)
85: {
86: if (e.getPropertyName().equals(SLIDER_FILL))
87: {
88: Boolean b = (Boolean) e.getNewValue();
89: if (b == null)
90: filledSlider = false;
91: else
92: filledSlider = b.booleanValue();
93: }
94: else
95: super.propertyChange(e);
96: }
97: }
98:
99:
100: protected static Color thumbColor;
101:
102:
106: protected static Color highlightColor;
107:
108:
112: protected static Color darkShadowColor;
113:
114:
115: protected static int trackWidth = UIManager.getInt("Slider.trackWidth");
116:
117:
118: protected static int tickLength = UIManager.getInt("Slider.majorTickLength");
119:
120:
121: protected static Icon horizThumbIcon = UIManager.getIcon(
122: "Slider.horizontalThumbIcon");
123:
124:
125: protected static Icon vertThumbIcon = UIManager.getIcon(
126: "Slider.verticalThumbIcon");
127:
128:
129: protected final int TICK_BUFFER = 4;
130:
131:
132: protected final String SLIDER_FILL = "JSlider.isFilled";
133:
134:
138: protected boolean filledSlider;
139:
140:
143: public MetalSliderUI()
144: {
145: super(null);
146: filledSlider = UIManager.getBoolean(SLIDER_FILL);
147: darkShadowColor = MetalLookAndFeel.getControlDarkShadow();
148: highlightColor = MetalLookAndFeel.getControlHighlight();
149: }
150:
151:
158: public static ComponentUI createUI(JComponent component)
159: {
160: return new MetalSliderUI();
161: }
162:
163:
168: public void installUI(JComponent c)
169: {
170: super.installUI(c);
171: Boolean b = (Boolean) c.getClientProperty(SLIDER_FILL);
172: if (b != null)
173: filledSlider = b.booleanValue();
174: }
175:
176:
183: protected PropertyChangeListener createPropertyChangeListener(JSlider slider)
184: {
185: return new MetalPropertyListener();
186: }
187:
188:
193: public void paintThumb(Graphics g)
194: {
195: Color save = g.getColor();
196: g.setColor(thumbColor);
197: if (slider.getOrientation() == JSlider.HORIZONTAL)
198: horizThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y);
199: else
200: vertThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y);
201: g.setColor(save);
202: }
203:
204:
209: public void paintTrack(Graphics g)
210: {
211: Color shadowColor = MetalLookAndFeel.getControlShadow();
212: if (slider.getOrientation() == JSlider.HORIZONTAL)
213: {
214: int trackX = trackRect.x;
215: int trackY = trackRect.y + (trackRect.height - getTrackWidth()) / 2;
216: int trackW = trackRect.width;
217: int trackH = getTrackWidth();
218:
219:
220: if (slider.isEnabled())
221: BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
222: darkShadowColor, shadowColor, darkShadowColor, highlightColor);
223: else
224: {
225: g.setColor(MetalLookAndFeel.getControlShadow());
226: g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
227: }
228:
229:
230: if (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme)
231: {
232: if (slider.isEnabled())
233: {
234: int xPos = xPositionForValue(slider.getValue());
235: int x = slider.getInverted() ? xPos : trackRect.x;
236: int w = slider.getInverted() ? trackX + trackW - xPos
237: : xPos - trackRect.x;
238: g.setColor(MetalLookAndFeel.getWhite());
239: g.drawLine(x + 1, trackY + 1, x + w - 3, trackY + 1);
240: g.setColor(UIManager.getColor("Slider.altTrackColor"));
241: g.drawLine(x + 1, trackY + 2, x + w - 3, trackY + 2);
242: g.setColor(MetalLookAndFeel.getControlShadow());
243: g.drawLine(x + 1, trackY + 3, x + w - 3, trackY + 3);
244: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
245: g.drawLine(x + 1, trackY + 4, x + w - 3, trackY + 4);
246: }
247: }
248: else if (filledSlider)
249: {
250: int xPos = xPositionForValue(slider.getValue());
251: int x = slider.getInverted() ? xPos : trackRect.x;
252: int w = slider.getInverted() ? trackX + trackW - xPos
253: : xPos - trackRect.x;
254: g.setColor(MetalLookAndFeel.getControlShadow());
255: g.fillRect(x + 1, trackY + 1, w - 3, getTrackWidth() - 3);
256: if (slider.isEnabled())
257: {
258: g.setColor(MetalLookAndFeel.getControl());
259: g.drawLine(x + 1, trackY + 1, x + w - 3, trackY + 1);
260: g.drawLine(x + 1, trackY + 1, x + 1,
261: trackY + getTrackWidth() - 3);
262: }
263: }
264: }
265: else
266: {
267: int trackX = trackRect.x + (trackRect.width - getTrackWidth()) / 2;
268: int trackY = trackRect.y;
269: int trackW = getTrackWidth();
270: int trackH = trackRect.height;
271: if (slider.isEnabled())
272: BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
273: darkShadowColor, shadowColor, darkShadowColor, highlightColor);
274: else
275: {
276: g.setColor(MetalLookAndFeel.getControlShadow());
277: g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
278: }
279:
280:
281: if (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme)
282: {
283: if (slider.isEnabled())
284: {
285: int yPos = yPositionForValue(slider.getValue());
286: int y = slider.getInverted() ? trackY : yPos;
287: int h = slider.getInverted() ? yPos - trackY
288: : trackY + trackH - yPos;
289:
290: g.setColor(MetalLookAndFeel.getWhite());
291: g.drawLine(trackX + 1, y + 1, trackX + 1, y + h - 3);
292: g.setColor(UIManager.getColor("Slider.altTrackColor"));
293: g.drawLine(trackX + 2, y + 1, trackX + 2, y + h - 3);
294: g.setColor(MetalLookAndFeel.getControlShadow());
295: g.drawLine(trackX + 3, y + 1, trackX + 3, y + h - 3);
296: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
297: g.drawLine(trackX + 4, y + 1, trackX + 4, y + h - 3);
298: }
299: }
300: else if (filledSlider)
301: {
302: int yPos = yPositionForValue(slider.getValue());
303: int y = slider.getInverted() ? trackY : yPos;
304: int h = slider.getInverted() ? yPos - trackY
305: : trackY + trackH - yPos;
306: g.setColor(MetalLookAndFeel.getControlShadow());
307: g.fillRect(trackX + 1, y + 1, getTrackWidth() - 3, h - 3);
308: if (slider.isEnabled())
309: {
310: g.setColor(MetalLookAndFeel.getControl());
311: g.drawLine(trackX + 1, y + 1, trackX + trackW - 3, y + 1);
312: g.drawLine(trackX + 1, y + 1, trackX + 1, y + h - 3);
313: }
314: }
315: }
316: }
317:
318:
327: public void paintFocus(Graphics g)
328: {
329: thumbColor = getFocusColor();
330: paintThumb(g);
331: }
332:
333:
338: protected Dimension getThumbSize()
339: {
340: if (slider.getOrientation() == JSlider.HORIZONTAL)
341: return new Dimension(horizThumbIcon.getIconWidth(),
342: horizThumbIcon.getIconHeight());
343: else
344: return new Dimension(vertThumbIcon.getIconWidth(),
345: vertThumbIcon.getIconHeight());
346: }
347:
348:
353: public int getTickLength()
354: {
355: int len = tickLength + TICK_BUFFER + 1;
356: if (slider.getOrientation() == JSlider.VERTICAL)
357: len += 2;
358: return len;
359: }
360:
361:
366: protected int getTrackWidth()
367: {
368: return trackWidth;
369: }
370:
371:
376: protected int getTrackLength()
377: {
378: return slider.getOrientation() == JSlider.HORIZONTAL
379: ? tickRect.width : tickRect.height;
380: }
381:
382:
387: protected int getThumbOverhang()
388: {
389:
390: return 0;
391: }
392:
393: protected void scrollDueToClickInTrack(int dir)
394: {
395:
396: super.scrollDueToClickInTrack(dir);
397: }
398:
399:
406: protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds,
407: int x)
408: {
409:
410:
411: if (slider.isEnabled())
412: g.setColor(slider.getForeground());
413: else
414: g.setColor(MetalLookAndFeel.getControlShadow());
415: g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength / 2);
416: }
417:
418:
425: protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds,
426: int x)
427: {
428:
429:
430: if (slider.isEnabled())
431: g.setColor(slider.getForeground());
432: else
433: g.setColor(MetalLookAndFeel.getControlShadow());
434: g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength - 1);
435: }
436:
437:
444: protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds,
445: int y)
446: {
447:
448:
449: if (slider.isEnabled())
450: g.setColor(slider.getForeground());
451: else
452: g.setColor(MetalLookAndFeel.getControlShadow());
453: g.drawLine(TICK_BUFFER, y, TICK_BUFFER + tickLength / 2, y);
454: }
455:
456:
463: protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds,
464: int y)
465: {
466:
467:
468: if (slider.isEnabled())
469: g.setColor(slider.getForeground());
470: else
471: g.setColor(MetalLookAndFeel.getControlShadow());
472: g.drawLine(TICK_BUFFER, y, TICK_BUFFER + tickLength, y);
473: }
474:
475: }