1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
60: public class MetalScrollBarUI extends BasicScrollBarUI
61: {
62:
63:
68: class MetalScrollBarPropertyChangeHandler
69: extends BasicScrollBarUI.PropertyChangeHandler
70: {
71:
76: public MetalScrollBarPropertyChangeHandler()
77: {
78:
79: }
80:
81:
88: public void propertyChange(PropertyChangeEvent e)
89: {
90: if (e.getPropertyName().equals(FREE_STANDING_PROP))
91: {
92: Boolean prop = (Boolean) e.getNewValue();
93: isFreeStanding = prop == null ? true : prop.booleanValue();
94: if (increaseButton != null)
95: increaseButton.setFreeStanding(isFreeStanding);
96: if (decreaseButton != null)
97: decreaseButton.setFreeStanding(isFreeStanding);
98: }
99: else
100: super.propertyChange(e);
101: }
102: }
103:
104:
105: public static final String FREE_STANDING_PROP = "JScrollBar.isFreeStanding";
106:
107:
108: private static final Dimension MIN_THUMB_SIZE = new Dimension(15, 15);
109:
110:
111: private static final Dimension MIN_THUMB_SIZE_FREE_STANDING
112: = new Dimension(17, 17);
113:
114:
115: protected MetalScrollButton increaseButton;
116:
117:
118: protected MetalScrollButton decreaseButton;
119:
120:
123: protected int scrollBarWidth;
124:
125:
131: protected boolean isFreeStanding = true;
132:
133:
137: Color scrollBarShadowColor;
138:
139:
143: public MetalScrollBarUI()
144: {
145: super();
146: }
147:
148:
155: public static ComponentUI createUI(JComponent component)
156: {
157: return new MetalScrollBarUI();
158: }
159:
160:
163: protected void installDefaults()
164: {
165:
166:
167:
168:
169: Boolean prop = (Boolean) scrollbar.getClientProperty(FREE_STANDING_PROP);
170: isFreeStanding = prop == null ? true : prop.booleanValue();
171: scrollBarShadowColor = UIManager.getColor("ScrollBar.shadow");
172: super.installDefaults();
173: }
174:
175:
183: protected PropertyChangeListener createPropertyChangeListener()
184: {
185: return new MetalScrollBarPropertyChangeHandler();
186: }
187:
188:
197: protected JButton createDecreaseButton(int orientation)
198: {
199: scrollBarWidth = UIManager.getInt("ScrollBar.width");
200: decreaseButton = new MetalScrollButton(orientation, scrollBarWidth,
201: isFreeStanding);
202: return decreaseButton;
203: }
204:
205:
214: protected JButton createIncreaseButton(int orientation)
215: {
216: scrollBarWidth = UIManager.getInt("ScrollBar.width");
217: increaseButton = new MetalScrollButton(orientation, scrollBarWidth,
218: isFreeStanding);
219: return increaseButton;
220: }
221:
222:
229: protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
230: {
231: g.setColor(MetalLookAndFeel.getControl());
232: g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width,
233: trackBounds.height);
234: if (scrollbar.getOrientation() == HORIZONTAL)
235: paintTrackHorizontal(g, c, trackBounds.x, trackBounds.y,
236: trackBounds.width, trackBounds.height);
237: else
238: paintTrackVertical(g, c, trackBounds.x, trackBounds.y,
239: trackBounds.width, trackBounds.height);
240:
241: }
242:
243:
253: private void paintTrackHorizontal(Graphics g, JComponent c,
254: int x, int y, int w, int h)
255: {
256: if (c.isEnabled())
257: {
258: g.setColor(MetalLookAndFeel.getControlDarkShadow());
259: g.drawLine(x, y, x, y + h - 1);
260: g.drawLine(x, y, x + w - 1, y);
261: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
262:
263: g.setColor(scrollBarShadowColor);
264: g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
265: g.drawLine(x + 1, y + 1, x + w - 2, y + 1);
266:
267: if (isFreeStanding)
268: {
269: g.setColor(MetalLookAndFeel.getControlDarkShadow());
270: g.drawLine(x, y + h - 2, x + w - 1, y + h - 2);
271: g.setColor(scrollBarShadowColor);
272: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
273: }
274: }
275: else
276: {
277: g.setColor(MetalLookAndFeel.getControlDisabled());
278: if (isFreeStanding)
279: g.drawRect(x, y, w - 1, h - 1);
280: else
281: {
282: g.drawLine(x, y, x + w - 1, y);
283: g.drawLine(x, y, x, y + h - 1);
284: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
285: }
286: }
287: }
288:
289:
299: private void paintTrackVertical(Graphics g, JComponent c,
300: int x, int y, int w, int h)
301: {
302: if (c.isEnabled())
303: {
304: g.setColor(MetalLookAndFeel.getControlDarkShadow());
305: g.drawLine(x, y, x, y + h - 1);
306: g.drawLine(x, y, x + w - 1, y);
307: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
308:
309: g.setColor(scrollBarShadowColor);
310: g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
311: g.drawLine(x + 1, y + 1, x + 1, y + h - 2);
312:
313: if (isFreeStanding)
314: {
315: g.setColor(MetalLookAndFeel.getControlDarkShadow());
316: g.drawLine(x + w - 2, y, x + w - 2, y + h - 1);
317: g.setColor(MetalLookAndFeel.getControlHighlight());
318: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
319: }
320: }
321: else
322: {
323: g.setColor(MetalLookAndFeel.getControlDisabled());
324: if (isFreeStanding)
325: g.drawRect(x, y, w - 1, h - 1);
326: else
327: {
328: g.drawLine(x, y, x + w - 1, y);
329: g.drawLine(x, y, x, y + h - 1);
330: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
331: }
332: }
333: }
334:
335:
342: protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
343: {
344:
345: if (!c.isEnabled())
346: return;
347: if (scrollbar.getOrientation() == HORIZONTAL)
348: paintThumbHorizontal(g, c, thumbBounds);
349: else
350: paintThumbVertical(g, c, thumbBounds);
351:
352:
353: if (! (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme))
354: {
355: MetalUtils.fillMetalPattern(c, g, thumbBounds.x + 3, thumbBounds.y + 3,
356: thumbBounds.width - 6,
357: thumbBounds.height - 6,
358: thumbHighlightColor,
359: thumbLightShadowColor);
360: }
361: }
362:
363:
370: private void paintThumbHorizontal(Graphics g, JComponent c,
371: Rectangle thumbBounds)
372: {
373: int x = thumbBounds.x;
374: int y = thumbBounds.y;
375: int w = thumbBounds.width;
376: int h = thumbBounds.height;
377:
378:
379: MetalTheme theme = MetalLookAndFeel.getCurrentTheme();
380: if (theme instanceof OceanTheme
381: && UIManager.get("ScrollBar.gradient") != null)
382: {
383: MetalUtils.paintGradient(g, x + 2, y + 2, w - 4, h - 2,
384: SwingConstants.VERTICAL,
385: "ScrollBar.gradient");
386: }
387: else
388: {
389: g.setColor(thumbColor);
390: if (isFreeStanding)
391: g.fillRect(x, y, w, h - 1);
392: else
393: g.fillRect(x, y, w, h);
394: }
395:
396:
397: g.setColor(thumbLightShadowColor);
398: if (isFreeStanding)
399: g.drawRect(x, y, w - 1, h - 2);
400: else
401: {
402: g.drawLine(x, y, x + w - 1, y);
403: g.drawLine(x, y, x, y + h - 1);
404: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
405: }
406:
407:
408: g.setColor(thumbHighlightColor);
409: if (isFreeStanding)
410: {
411: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
412: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
413: }
414: else
415: {
416: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
417: g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
418: }
419:
420:
421: g.setColor(UIManager.getColor("ScrollBar.shadow"));
422: g.drawLine(x + w, y + 1, x + w, y + h - 1);
423:
424:
425: if (theme instanceof OceanTheme)
426: {
427: g.setColor(thumbLightShadowColor);
428: int middle = x + w / 2;
429: g.drawLine(middle - 2, y + 4, middle - 2, y + h - 5);
430: g.drawLine(middle, y + 4, middle, y + h - 5);
431: g.drawLine(middle + 2, y + 4, middle + 2, y + h - 5);
432: g.setColor(UIManager.getColor("ScrollBar.highlight"));
433: g.drawLine(middle - 1, y + 5, middle - 1, y + h - 4);
434: g.drawLine(middle + 1, y + 5, middle + 1, y + h - 4);
435: g.drawLine(middle + 3, y + 5, middle + 3, y + h - 4);
436: }
437: }
438:
439:
446: private void paintThumbVertical(Graphics g, JComponent c,
447: Rectangle thumbBounds)
448: {
449: int x = thumbBounds.x;
450: int y = thumbBounds.y;
451: int w = thumbBounds.width;
452: int h = thumbBounds.height;
453:
454:
455: MetalTheme theme = MetalLookAndFeel.getCurrentTheme();
456: if (theme instanceof OceanTheme
457: && UIManager.get("ScrollBar.gradient") != null)
458: {
459: MetalUtils.paintGradient(g, x + 2, y + 2, w - 2, h - 4,
460: SwingConstants.HORIZONTAL,
461: "ScrollBar.gradient");
462: }
463: else
464: {
465: g.setColor(thumbColor);
466: if (isFreeStanding)
467: g.fillRect(x, y, w - 1, h);
468: else
469: g.fillRect(x, y, w, h);
470: }
471:
472:
473: g.setColor(thumbLightShadowColor);
474: if (isFreeStanding)
475: g.drawRect(x, y, w - 2, h - 1);
476: else
477: {
478: g.drawLine(x, y, x + w - 1, y);
479: g.drawLine(x, y, x, y + h - 1);
480: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
481: }
482:
483:
484: g.setColor(thumbHighlightColor);
485: if (isFreeStanding)
486: {
487: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
488: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
489: }
490: else
491: {
492: g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
493: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
494: }
495:
496:
497: g.setColor(UIManager.getColor("ScrollBar.shadow"));
498: g.drawLine(x + 1, y + h, x + w - 2, y + h);
499:
500:
501: if (theme instanceof OceanTheme)
502: {
503: g.setColor(thumbLightShadowColor);
504: int middle = y + h / 2;
505: g.drawLine(x + 4, middle - 2, x + w - 5, middle - 2);
506: g.drawLine(x + 4, middle, x + w - 5, middle);
507: g.drawLine(x + 4, middle + 2, x + w - 5, middle + 2);
508: g.setColor(UIManager.getColor("ScrollBar.highlight"));
509: g.drawLine(x + 5, middle - 1, x + w - 4, middle - 1);
510: g.drawLine(x + 5, middle + 1, x + w - 4, middle + 1);
511: g.drawLine(x + 5, middle + 3, x + w - 4, middle + 3);
512: }
513: }
514:
515:
522: protected Dimension getMinimumThumbSize()
523: {
524: Dimension retVal;
525: if (scrollbar != null)
526: {
527: if (isFreeStanding)
528: retVal = MIN_THUMB_SIZE_FREE_STANDING;
529: else
530: retVal = MIN_THUMB_SIZE;
531: }
532: else
533: retVal = new Dimension(0, 0);
534: return retVal;
535: }
536:
537:
551: public Dimension getPreferredSize(JComponent c)
552: {
553: int height;
554: int width;
555: height = width = 0;
556:
557: if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)
558: {
559: width += incrButton.getPreferredSize().getWidth();
560: width += decrButton.getPreferredSize().getWidth();
561: width += 30;
562: height = UIManager.getInt("ScrollBar.width");
563: }
564: else
565: {
566: height += incrButton.getPreferredSize().getHeight();
567: height += decrButton.getPreferredSize().getHeight();
568: height += 30;
569: width = UIManager.getInt("ScrollBar.width");
570: }
571:
572: Insets insets = scrollbar.getInsets();
573:
574: height += insets.top + insets.bottom;
575: width += insets.left + insets.right;
576:
577: return new Dimension(width, height);
578: }
579: }