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: scrollBarWidth = UIManager.getInt("ScrollBar.width");
173: super.installDefaults();
174: }
175:
176:
184: protected PropertyChangeListener createPropertyChangeListener()
185: {
186: return new MetalScrollBarPropertyChangeHandler();
187: }
188:
189:
201: protected JButton createDecreaseButton(int orientation)
202: {
203: decreaseButton = new MetalScrollButton(orientation, scrollBarWidth,
204: isFreeStanding);
205: return decreaseButton;
206: }
207:
208:
220: protected JButton createIncreaseButton(int orientation)
221: {
222: increaseButton = new MetalScrollButton(orientation, scrollBarWidth,
223: isFreeStanding);
224: return increaseButton;
225: }
226:
227:
234: protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
235: {
236: g.setColor(MetalLookAndFeel.getControl());
237: g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width,
238: trackBounds.height);
239: if (scrollbar.getOrientation() == HORIZONTAL)
240: paintTrackHorizontal(g, c, trackBounds.x, trackBounds.y,
241: trackBounds.width, trackBounds.height);
242: else
243: paintTrackVertical(g, c, trackBounds.x, trackBounds.y,
244: trackBounds.width, trackBounds.height);
245:
246: }
247:
248:
258: private void paintTrackHorizontal(Graphics g, JComponent c,
259: int x, int y, int w, int h)
260: {
261: if (c.isEnabled())
262: {
263: g.setColor(MetalLookAndFeel.getControlDarkShadow());
264: g.drawLine(x, y, x, y + h - 1);
265: g.drawLine(x, y, x + w - 1, y);
266: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
267:
268: g.setColor(scrollBarShadowColor);
269: g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
270: g.drawLine(x + 1, y + 1, x + w - 2, y + 1);
271:
272: if (isFreeStanding)
273: {
274: g.setColor(MetalLookAndFeel.getControlDarkShadow());
275: g.drawLine(x, y + h - 2, x + w - 1, y + h - 2);
276: g.setColor(scrollBarShadowColor);
277: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
278: }
279: }
280: else
281: {
282: g.setColor(MetalLookAndFeel.getControlDisabled());
283: if (isFreeStanding)
284: g.drawRect(x, y, w - 1, h - 1);
285: else
286: {
287: g.drawLine(x, y, x + w - 1, y);
288: g.drawLine(x, y, x, y + h - 1);
289: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
290: }
291: }
292: }
293:
294:
304: private void paintTrackVertical(Graphics g, JComponent c,
305: int x, int y, int w, int h)
306: {
307: if (c.isEnabled())
308: {
309: g.setColor(MetalLookAndFeel.getControlDarkShadow());
310: g.drawLine(x, y, x, y + h - 1);
311: g.drawLine(x, y, x + w - 1, y);
312: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
313:
314: g.setColor(scrollBarShadowColor);
315: g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
316: g.drawLine(x + 1, y + 1, x + 1, y + h - 2);
317:
318: if (isFreeStanding)
319: {
320: g.setColor(MetalLookAndFeel.getControlDarkShadow());
321: g.drawLine(x + w - 2, y, x + w - 2, y + h - 1);
322: g.setColor(MetalLookAndFeel.getControlHighlight());
323: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
324: }
325: }
326: else
327: {
328: g.setColor(MetalLookAndFeel.getControlDisabled());
329: if (isFreeStanding)
330: g.drawRect(x, y, w - 1, h - 1);
331: else
332: {
333: g.drawLine(x, y, x + w - 1, y);
334: g.drawLine(x, y, x, y + h - 1);
335: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
336: }
337: }
338: }
339:
340:
347: protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
348: {
349:
350: if (!c.isEnabled())
351: return;
352: if (scrollbar.getOrientation() == HORIZONTAL)
353: paintThumbHorizontal(g, c, thumbBounds);
354: else
355: paintThumbVertical(g, c, thumbBounds);
356:
357:
358: if (! (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme))
359: {
360: MetalUtils.fillMetalPattern(c, g, thumbBounds.x + 3, thumbBounds.y + 3,
361: thumbBounds.width - 6,
362: thumbBounds.height - 6,
363: thumbHighlightColor,
364: thumbLightShadowColor);
365: }
366: }
367:
368:
375: private void paintThumbHorizontal(Graphics g, JComponent c,
376: Rectangle thumbBounds)
377: {
378: int x = thumbBounds.x;
379: int y = thumbBounds.y;
380: int w = thumbBounds.width;
381: int h = thumbBounds.height;
382:
383:
384: MetalTheme theme = MetalLookAndFeel.getCurrentTheme();
385: if (theme instanceof OceanTheme
386: && UIManager.get("ScrollBar.gradient") != null)
387: {
388: MetalUtils.paintGradient(g, x + 2, y + 2, w - 4, h - 2,
389: SwingConstants.VERTICAL,
390: "ScrollBar.gradient");
391: }
392: else
393: {
394: g.setColor(thumbColor);
395: if (isFreeStanding)
396: g.fillRect(x, y, w, h - 1);
397: else
398: g.fillRect(x, y, w, h);
399: }
400:
401:
402: g.setColor(thumbLightShadowColor);
403: if (isFreeStanding)
404: g.drawRect(x, y, w - 1, h - 2);
405: else
406: {
407: g.drawLine(x, y, x + w - 1, y);
408: g.drawLine(x, y, x, y + h - 1);
409: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
410: }
411:
412:
413: g.setColor(thumbHighlightColor);
414: if (isFreeStanding)
415: {
416: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
417: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
418: }
419: else
420: {
421: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
422: g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
423: }
424:
425:
426: g.setColor(UIManager.getColor("ScrollBar.shadow"));
427: g.drawLine(x + w, y + 1, x + w, y + h - 1);
428:
429:
430: if (theme instanceof OceanTheme)
431: {
432: g.setColor(thumbLightShadowColor);
433: int middle = x + w / 2;
434: g.drawLine(middle - 2, y + 4, middle - 2, y + h - 5);
435: g.drawLine(middle, y + 4, middle, y + h - 5);
436: g.drawLine(middle + 2, y + 4, middle + 2, y + h - 5);
437: g.setColor(UIManager.getColor("ScrollBar.highlight"));
438: g.drawLine(middle - 1, y + 5, middle - 1, y + h - 4);
439: g.drawLine(middle + 1, y + 5, middle + 1, y + h - 4);
440: g.drawLine(middle + 3, y + 5, middle + 3, y + h - 4);
441: }
442: }
443:
444:
451: private void paintThumbVertical(Graphics g, JComponent c,
452: Rectangle thumbBounds)
453: {
454: int x = thumbBounds.x;
455: int y = thumbBounds.y;
456: int w = thumbBounds.width;
457: int h = thumbBounds.height;
458:
459:
460: MetalTheme theme = MetalLookAndFeel.getCurrentTheme();
461: if (theme instanceof OceanTheme
462: && UIManager.get("ScrollBar.gradient") != null)
463: {
464: MetalUtils.paintGradient(g, x + 2, y + 2, w - 2, h - 4,
465: SwingConstants.HORIZONTAL,
466: "ScrollBar.gradient");
467: }
468: else
469: {
470: g.setColor(thumbColor);
471: if (isFreeStanding)
472: g.fillRect(x, y, w - 1, h);
473: else
474: g.fillRect(x, y, w, h);
475: }
476:
477:
478: g.setColor(thumbLightShadowColor);
479: if (isFreeStanding)
480: g.drawRect(x, y, w - 2, h - 1);
481: else
482: {
483: g.drawLine(x, y, x + w - 1, y);
484: g.drawLine(x, y, x, y + h - 1);
485: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
486: }
487:
488:
489: g.setColor(thumbHighlightColor);
490: if (isFreeStanding)
491: {
492: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
493: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
494: }
495: else
496: {
497: g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
498: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
499: }
500:
501:
502: g.setColor(UIManager.getColor("ScrollBar.shadow"));
503: g.drawLine(x + 1, y + h, x + w - 2, y + h);
504:
505:
506: if (theme instanceof OceanTheme)
507: {
508: g.setColor(thumbLightShadowColor);
509: int middle = y + h / 2;
510: g.drawLine(x + 4, middle - 2, x + w - 5, middle - 2);
511: g.drawLine(x + 4, middle, x + w - 5, middle);
512: g.drawLine(x + 4, middle + 2, x + w - 5, middle + 2);
513: g.setColor(UIManager.getColor("ScrollBar.highlight"));
514: g.drawLine(x + 5, middle - 1, x + w - 4, middle - 1);
515: g.drawLine(x + 5, middle + 1, x + w - 4, middle + 1);
516: g.drawLine(x + 5, middle + 3, x + w - 4, middle + 3);
517: }
518: }
519:
520:
527: protected Dimension getMinimumThumbSize()
528: {
529: Dimension retVal;
530: if (scrollbar != null)
531: {
532: if (isFreeStanding)
533: retVal = MIN_THUMB_SIZE_FREE_STANDING;
534: else
535: retVal = MIN_THUMB_SIZE;
536: }
537: else
538: retVal = new Dimension(0, 0);
539: return retVal;
540: }
541:
542:
556: public Dimension getPreferredSize(JComponent c)
557: {
558: int height;
559: int width;
560: height = width = 0;
561:
562: if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)
563: {
564: width += incrButton.getPreferredSize().getWidth();
565: width += decrButton.getPreferredSize().getWidth();
566: width += 30;
567: height = UIManager.getInt("ScrollBar.width");
568: }
569: else
570: {
571: height += incrButton.getPreferredSize().getHeight();
572: height += decrButton.getPreferredSize().getHeight();
573: height += 30;
574: width = UIManager.getInt("ScrollBar.width");
575: }
576:
577: Insets insets = scrollbar.getInsets();
578:
579: height += insets.top + insets.bottom;
580: width += insets.left + insets.right;
581:
582: return new Dimension(width, height);
583: }
584: }