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: if (slider.getOrientation() == JSlider.HORIZONTAL)
196: horizThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y);
197: else
198: vertThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y);
199: }
200:
201:
206: public void paintTrack(Graphics g)
207: {
208: Color shadowColor = MetalLookAndFeel.getControlShadow();
209: if (slider.getOrientation() == JSlider.HORIZONTAL)
210: {
211: int trackX = trackRect.x;
212: int trackY = trackRect.y + (trackRect.height - getTrackWidth()) / 2;
213: int trackW = trackRect.width - 1;
214: int trackH = getTrackWidth();
215:
216:
217: if (slider.isEnabled())
218: BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
219: darkShadowColor, shadowColor, darkShadowColor, highlightColor);
220: else
221: {
222: g.setColor(MetalLookAndFeel.getControlShadow());
223: g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
224: }
225:
226:
227: if (filledSlider)
228: {
229: int xPos = xPositionForValue(slider.getValue());
230: int x = (slider.getInverted() ? xPos : trackRect.x);
231: int w = (slider.getInverted() ? trackX + trackW - xPos
232: : xPos - trackRect.x);
233: g.setColor(MetalLookAndFeel.getControlShadow());
234: g.fillRect(x + 1, trackY + 1, w - 3, getTrackWidth() - 3);
235: if (slider.isEnabled())
236: {
237: g.setColor(MetalLookAndFeel.getControl());
238: g.drawLine(x + 1, trackY + 1, x + w - 3, trackY + 1);
239: g.drawLine(x + 1, trackY + 1, x + 1,
240: trackY + getTrackWidth() - 3);
241: }
242: }
243: }
244: else
245: {
246: int trackX = trackRect.x + (trackRect.width - getTrackWidth()) / 2;
247: int trackY = trackRect.y;
248: int trackW = getTrackWidth();
249: int trackH = trackRect.height - 1;
250: if (slider.isEnabled())
251: BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
252: darkShadowColor, shadowColor, darkShadowColor, highlightColor);
253: else
254: {
255: g.setColor(MetalLookAndFeel.getControlShadow());
256: g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
257: }
258:
259: if (filledSlider)
260: {
261: int yPos = yPositionForValue(slider.getValue());
262: int y = (slider.getInverted() ? trackY : yPos);
263: int h = (slider.getInverted() ? yPos - trackY
264: : trackY + trackH - yPos);
265: g.setColor(MetalLookAndFeel.getControlShadow());
266: g.fillRect(trackX + 1, y + 1, getTrackWidth() - 3, h - 3);
267: if (slider.isEnabled())
268: {
269: g.setColor(MetalLookAndFeel.getControl());
270: g.drawLine(trackX + 1, y + 1, trackX + trackW - 3, y + 1);
271: g.drawLine(trackX + 1, y + 1, trackX + 1, y + h - 3);
272: }
273: }
274: }
275: }
276:
277:
286: public void paintFocus(Graphics g)
287: {
288:
289: }
290:
291:
296: protected Dimension getThumbSize()
297: {
298: if (slider.getOrientation() == JSlider.HORIZONTAL)
299: return new Dimension(horizThumbIcon.getIconWidth(),
300: horizThumbIcon.getIconHeight());
301: else
302: return new Dimension(vertThumbIcon.getIconWidth(),
303: vertThumbIcon.getIconHeight());
304: }
305:
306:
311: public int getTickLength()
312: {
313: return tickLength + TICK_BUFFER;
314: }
315:
316:
321: protected int getTrackWidth()
322: {
323: return trackWidth;
324: }
325:
326:
331: protected int getTrackLength()
332: {
333: return (slider.getOrientation() == JSlider.HORIZONTAL
334: ? tickRect.width : tickRect.height);
335: }
336:
337:
342: protected int getThumbOverhang()
343: {
344:
345: return 0;
346: }
347:
348: protected void scrollDueToClickInTrack(int dir)
349: {
350:
351: super.scrollDueToClickInTrack(dir);
352: }
353:
354:
361: protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds,
362: int x)
363: {
364:
365:
366: if (slider.isEnabled())
367: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
368: else
369: g.setColor(MetalLookAndFeel.getControlDisabled());
370: g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength / 2);
371: }
372:
373:
380: protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds,
381: int x)
382: {
383:
384:
385: if (slider.isEnabled())
386: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
387: else
388: g.setColor(MetalLookAndFeel.getControlDisabled());
389: g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength);
390: }
391:
392:
399: protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds,
400: int y)
401: {
402:
403:
404: if (slider.isEnabled())
405: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
406: else
407: g.setColor(MetalLookAndFeel.getControlDisabled());
408: g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength / 2, y);
409: }
410:
411:
418: protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds,
419: int y)
420: {
421:
422:
423: if (slider.isEnabled())
424: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
425: else
426: g.setColor(MetalLookAndFeel.getControlDisabled());
427: g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength, y);
428: }
429:
430: }