1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75:
76:
79: public class BasicScrollBarUI extends ScrollBarUI implements LayoutManager,
80: SwingConstants
81: {
82:
86: protected class ArrowButtonListener extends MouseAdapter
87: {
88:
89:
95: public void mousePressed(MouseEvent e)
96: {
97: scrollTimer.stop();
98: scrollListener.setScrollByBlock(false);
99: if (e.getSource() == incrButton)
100: scrollListener.setDirection(POSITIVE_SCROLL);
101: else if (e.getSource() == decrButton)
102: scrollListener.setDirection(NEGATIVE_SCROLL);
103: scrollTimer.setDelay(100);
104: scrollTimer.start();
105: }
106:
107:
112: public void mouseReleased(MouseEvent e)
113: {
114: scrollTimer.stop();
115: scrollTimer.setDelay(300);
116: if (e.getSource() == incrButton)
117: scrollByUnit(POSITIVE_SCROLL);
118: else if (e.getSource() == decrButton)
119: scrollByUnit(NEGATIVE_SCROLL);
120: }
121: }
122:
123:
126: protected class ModelListener implements ChangeListener
127: {
128:
133: public void stateChanged(ChangeEvent e)
134: {
135: calculatePreferredSize();
136: updateThumbRect();
137: scrollbar.repaint();
138: }
139: }
140:
141:
144: public class PropertyChangeHandler implements PropertyChangeListener
145: {
146:
151: public void propertyChange(PropertyChangeEvent e)
152: {
153: if (e.getPropertyName().equals("model"))
154: {
155: ((BoundedRangeModel) e.getOldValue()).removeChangeListener(modelListener);
156: scrollbar.getModel().addChangeListener(modelListener);
157: updateThumbRect();
158: }
159: else if (e.getPropertyName().equals("orientation"))
160: {
161: uninstallListeners();
162: uninstallComponents();
163: uninstallDefaults();
164: installDefaults();
165: installComponents();
166: installListeners();
167: }
168: else if (e.getPropertyName().equals("enabled"))
169: {
170: Boolean b = (Boolean) e.getNewValue();
171: if (incrButton != null)
172: incrButton.setEnabled(b.booleanValue());
173: if (decrButton != null)
174: decrButton.setEnabled(b.booleanValue());
175: }
176: }
177: }
178:
179:
183: protected class ScrollListener implements ActionListener
184: {
185:
186: private transient int direction;
187:
188:
189: private transient boolean block;
190:
191:
195: public ScrollListener()
196: {
197: direction = POSITIVE_SCROLL;
198: block = true;
199: }
200:
201:
208: public ScrollListener(int dir, boolean block)
209: {
210: direction = dir;
211: this.block = block;
212: }
213:
214:
219: public void setDirection(int direction)
220: {
221: this.direction = direction;
222: }
223:
224:
229: public void setScrollByBlock(boolean block)
230: {
231: this.block = block;
232: }
233:
234:
239: public void actionPerformed(ActionEvent e)
240: {
241: if (block)
242: {
243:
244:
245:
246: if (!trackListener.shouldScroll(direction))
247: {
248: trackHighlight = NO_HIGHLIGHT;
249: scrollbar.repaint();
250: return;
251: }
252: scrollByBlock(direction);
253: }
254: else
255: scrollByUnit(direction);
256: }
257: }
258:
259:
262: protected class TrackListener extends MouseAdapter
263: implements MouseMotionListener
264: {
265:
266: protected int currentMouseX;
267:
268:
269: protected int currentMouseY;
270:
271:
275: protected int offset;
276:
277:
282: public void mouseDragged(MouseEvent e)
283: {
284: currentMouseX = e.getX();
285: currentMouseY = e.getY();
286: if (scrollbar.getValueIsAdjusting())
287: {
288: int value;
289: if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)
290: value = valueForXPosition(currentMouseX) - offset;
291: else
292: value = valueForYPosition(currentMouseY) - offset;
293:
294: scrollbar.setValue(value);
295: }
296: }
297:
298:
303: public void mouseMoved(MouseEvent e)
304: {
305: if (thumbRect.contains(e.getPoint()))
306: thumbRollover = true;
307: else
308: thumbRollover = false;
309: }
310:
311:
317: public void mousePressed(MouseEvent e)
318: {
319: currentMouseX = e.getX();
320: currentMouseY = e.getY();
321:
322: int value;
323: if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)
324: value = valueForXPosition(currentMouseX);
325: else
326: value = valueForYPosition(currentMouseY);
327:
328: if (! thumbRect.contains(e.getPoint()))
329: {
330: scrollTimer.stop();
331: scrollListener.setScrollByBlock(true);
332: if (value > scrollbar.getValue())
333: {
334: trackHighlight = INCREASE_HIGHLIGHT;
335: scrollListener.setDirection(POSITIVE_SCROLL);
336: }
337: else
338: {
339: trackHighlight = DECREASE_HIGHLIGHT;
340: scrollListener.setDirection(NEGATIVE_SCROLL);
341: }
342: scrollTimer.setDelay(100);
343: scrollTimer.start();
344: }
345: else
346: {
347:
348:
349:
350:
351:
352:
353:
354: scrollListener.setScrollByBlock(false);
355: scrollbar.setValueIsAdjusting(true);
356: offset = value - scrollbar.getValue();
357: }
358: scrollbar.repaint();
359: }
360:
361:
367: public void mouseReleased(MouseEvent e)
368: {
369: scrollTimer.stop();
370: scrollTimer.setDelay(300);
371: currentMouseX = e.getX();
372: currentMouseY = e.getY();
373:
374: if (shouldScroll(POSITIVE_SCROLL))
375: scrollByBlock(POSITIVE_SCROLL);
376: else if (shouldScroll(NEGATIVE_SCROLL))
377: scrollByBlock(NEGATIVE_SCROLL);
378:
379: trackHighlight = NO_HIGHLIGHT;
380: scrollListener.setScrollByBlock(false);
381: scrollbar.setValueIsAdjusting(true);
382: scrollbar.repaint();
383: }
384:
385:
393: boolean shouldScroll(int direction)
394: {
395: int value;
396: if (scrollbar.getOrientation() == HORIZONTAL)
397: value = valueForXPosition(currentMouseX);
398: else
399: value = valueForYPosition(currentMouseY);
400:
401: if (thumbRect.contains(currentMouseX, currentMouseY))
402: return false;
403:
404: if (direction == POSITIVE_SCROLL)
405: return value > scrollbar.getValue();
406: else
407: return value < scrollbar.getValue();
408: }
409: }
410:
411:
412: protected ArrowButtonListener buttonListener;
413:
414:
415: protected ModelListener modelListener;
416:
417:
418: protected PropertyChangeListener propertyChangeListener;
419:
420:
421: protected ScrollListener scrollListener;
422:
423:
424: protected TrackListener trackListener;
425:
426:
427: protected JButton decrButton;
428:
429:
430: protected JButton incrButton;
431:
432:
433: protected Dimension maximumThumbSize;
434:
435:
436: protected Dimension minimumThumbSize;
437:
438:
439: protected Color thumbColor;
440:
441:
442: protected Color thumbDarkShadowColor;
443:
444:
445: protected Color thumbHighlightColor;
446:
447:
448: protected Color thumbLightShadowColor;
449:
450:
451: protected Color trackHighlightColor;
452:
453:
454: protected Color trackColor;
455:
456:
457: protected Rectangle trackRect;
458:
459:
460: protected Rectangle thumbRect;
461:
462:
463: protected static final int DECREASE_HIGHLIGHT = 1;
464:
465:
466: protected static final int INCREASE_HIGHLIGHT = 2;
467:
468:
469: protected static final int NO_HIGHLIGHT = 0;
470:
471:
472: private static final int POSITIVE_SCROLL = 1;
473:
474:
475: private static final int NEGATIVE_SCROLL = -1;
476:
477:
478: private transient Dimension preferredSize;
479:
480:
481: protected int trackHighlight;
482:
483:
484: protected boolean isDragging;
485:
486:
487: protected Timer scrollTimer;
488:
489:
490: protected JScrollBar scrollbar;
491:
492:
493: boolean thumbRollover;
494:
495:
501: public void addLayoutComponent(String name, Component child)
502: {
503:
504:
505: }
506:
507:
511: protected void configureScrollBarColors()
512: {
513: trackColor = UIManager.getColor("ScrollBar.track");
514: trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
515: thumbColor = UIManager.getColor("ScrollBar.thumb");
516: thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
517: thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow");
518: thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow");
519: }
520:
521:
526: protected ArrowButtonListener createArrowButtonListener()
527: {
528: return new ArrowButtonListener();
529: }
530:
531:
539: protected JButton createIncreaseButton(int orientation)
540: {
541: return new BasicArrowButton(orientation);
542: }
543:
544:
552: protected JButton createDecreaseButton(int orientation)
553: {
554: return new BasicArrowButton(orientation);
555: }
556:
557:
562: protected ModelListener createModelListener()
563: {
564: return new ModelListener();
565: }
566:
567:
572: protected PropertyChangeListener createPropertyChangeListener()
573: {
574: return new PropertyChangeHandler();
575: }
576:
577:
582: protected ScrollListener createScrollListener()
583: {
584: return new ScrollListener();
585: }
586:
587:
592: protected TrackListener createTrackListener()
593: {
594: return new TrackListener();
595: }
596:
597:
604: public static ComponentUI createUI(JComponent c)
605: {
606: return new BasicScrollBarUI();
607: }
608:
609:
616: public Dimension getMaximumSize(JComponent c)
617: {
618: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
619: }
620:
621:
626: protected Dimension getMaximumThumbSize()
627: {
628: return maximumThumbSize;
629: }
630:
631:
638: public Dimension getMinimumSize(JComponent c)
639: {
640: return getPreferredSize(c);
641: }
642:
643:
648: protected Dimension getMinimumThumbSize()
649: {
650: return minimumThumbSize;
651: }
652:
653:
658: void calculatePreferredSize()
659: {
660: int height;
661: int width;
662: height = width = 0;
663:
664: if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)
665: {
666: width += incrButton.getPreferredSize().getWidth();
667: width += decrButton.getPreferredSize().getWidth();
668: width += 16;
669: height = UIManager.getInt("ScrollBar.width");
670: }
671: else
672: {
673: height += incrButton.getPreferredSize().getHeight();
674: height += decrButton.getPreferredSize().getHeight();
675: height += 16;
676: width = UIManager.getInt("ScrollBar.width");
677: }
678:
679: Insets insets = scrollbar.getInsets();
680:
681: height += insets.top + insets.bottom;
682: width += insets.left + insets.right;
683:
684: preferredSize = new Dimension(width, height);
685: }
686:
687:
698: public Dimension getPreferredSize(JComponent c)
699: {
700: calculatePreferredSize();
701: return preferredSize;
702: }
703:
704:
710: protected Rectangle getThumbBounds()
711: {
712: return thumbRect;
713: }
714:
715:
721: protected Rectangle getTrackBounds()
722: {
723: return trackRect;
724: }
725:
726:
730: protected void installComponents()
731: {
732: int orientation = scrollbar.getOrientation();
733: switch (orientation)
734: {
735: case JScrollBar.HORIZONTAL:
736: incrButton = createIncreaseButton(EAST);
737: decrButton = createDecreaseButton(WEST);
738: break;
739: default:
740: incrButton = createIncreaseButton(SOUTH);
741: decrButton = createDecreaseButton(NORTH);
742: break;
743: }
744:
745: if (incrButton != null)
746: scrollbar.add(incrButton);
747: if (decrButton != null)
748: scrollbar.add(decrButton);
749: }
750:
751:
755: protected void installDefaults()
756: {
757: LookAndFeel.installColors(scrollbar, "ScrollBar.background",
758: "ScrollBar.foreground");
759: LookAndFeel.installBorder(scrollbar, "ScrollBar.border");
760: scrollbar.setOpaque(true);
761: scrollbar.setLayout(this);
762:
763: configureScrollBarColors();
764:
765: maximumThumbSize = UIManager.getDimension("ScrollBar.maximumThumbSize");
766: minimumThumbSize = UIManager.getDimension("ScrollBar.minimumThumbSize");
767: }
768:
769:
774: protected void installKeyboardActions()
775: {
776: InputMap keyMap = getInputMap(
777: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
778: SwingUtilities.replaceUIInputMap(scrollbar,
779: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyMap);
780: ActionMap map = getActionMap();
781: SwingUtilities.replaceUIActionMap(scrollbar, map);
782: }
783:
784:
788: protected void uninstallKeyboardActions()
789: {
790: SwingUtilities.replaceUIActionMap(scrollbar, null);
791: SwingUtilities.replaceUIInputMap(scrollbar,
792: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
793: }
794:
795: InputMap getInputMap(int condition)
796: {
797: if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
798: return (InputMap) UIManager.get("ScrollBar.focusInputMap");
799: return null;
800: }
801:
802:
809: ActionMap getActionMap()
810: {
811: ActionMap map = (ActionMap) UIManager.get("ScrollBar.actionMap");
812:
813: if (map == null)
814: {
815: map = createActionMap();
816: if (map != null)
817: UIManager.put("ScrollBar.actionMap", map);
818: }
819: return map;
820: }
821:
822:
832: ActionMap createActionMap()
833: {
834: ActionMap map = new ActionMapUIResource();
835: map.put("positiveUnitIncrement",
836: new AbstractAction("positiveUnitIncrement") {
837: public void actionPerformed(ActionEvent event)
838: {
839: JScrollBar sb = (JScrollBar) event.getSource();
840: if (sb.isVisible())
841: {
842: int delta = sb.getUnitIncrement(1);
843: sb.setValue(sb.getValue() + delta);
844: }
845: }
846: }
847: );
848: map.put("positiveBlockIncrement",
849: new AbstractAction("positiveBlockIncrement") {
850: public void actionPerformed(ActionEvent event)
851: {
852: JScrollBar sb = (JScrollBar) event.getSource();
853: if (sb.isVisible())
854: {
855: int delta = sb.getBlockIncrement(1);
856: sb.setValue(sb.getValue() + delta);
857: }
858: }
859: }
860: );
861: map.put("negativeUnitIncrement",
862: new AbstractAction("negativeUnitIncrement") {
863: public void actionPerformed(ActionEvent event)
864: {
865: JScrollBar sb = (JScrollBar) event.getSource();
866: if (sb.isVisible())
867: {
868: int delta = sb.getUnitIncrement(-1);
869: sb.setValue(sb.getValue() + delta);
870: }
871: }
872: }
873: );
874: map.put("negativeBlockIncrement",
875: new AbstractAction("negativeBlockIncrement") {
876: public void actionPerformed(ActionEvent event)
877: {
878: JScrollBar sb = (JScrollBar) event.getSource();
879: if (sb.isVisible())
880: {
881: int delta = sb.getBlockIncrement(-1);
882: sb.setValue(sb.getValue() + delta);
883: }
884: }
885: }
886: );
887: map.put("minScroll",
888: new AbstractAction("minScroll") {
889: public void actionPerformed(ActionEvent event)
890: {
891: JScrollBar sb = (JScrollBar) event.getSource();
892: if (sb.isVisible())
893: {
894: sb.setValue(sb.getMinimum());
895: }
896: }
897: }
898: );
899: map.put("maxScroll",
900: new AbstractAction("maxScroll") {
901: public void actionPerformed(ActionEvent event)
902: {
903: JScrollBar sb = (JScrollBar) event.getSource();
904: if (sb.isVisible())
905: {
906: sb.setValue(sb.getMaximum());
907: }
908: }
909: }
910: );
911: return map;
912: }
913:
914:
918: protected void installListeners()
919: {
920: scrollListener = createScrollListener();
921: trackListener = createTrackListener();
922: buttonListener = createArrowButtonListener();
923: modelListener = createModelListener();
924: propertyChangeListener = createPropertyChangeListener();
925:
926: scrollbar.addMouseMotionListener(trackListener);
927: scrollbar.addMouseListener(trackListener);
928:
929: incrButton.addMouseListener(buttonListener);
930: decrButton.addMouseListener(buttonListener);
931:
932: scrollbar.addPropertyChangeListener(propertyChangeListener);
933: scrollbar.getModel().addChangeListener(modelListener);
934:
935: scrollTimer.addActionListener(scrollListener);
936: }
937:
938:
945: public void installUI(JComponent c)
946: {
947: super.installUI(c);
948: if (c instanceof JScrollBar)
949: {
950: scrollbar = (JScrollBar) c;
951:
952: trackRect = new Rectangle();
953: thumbRect = new Rectangle();
954:
955: scrollTimer = new Timer(300, null);
956:
957: installDefaults();
958: installComponents();
959: configureScrollBarColors();
960: installListeners();
961: installKeyboardActions();
962:
963: calculatePreferredSize();
964: }
965: }
966:
967:
972: public void layoutContainer(Container scrollbarContainer)
973: {
974: if (scrollbarContainer instanceof JScrollBar)
975: {
976: if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)
977: layoutHScrollbar((JScrollBar) scrollbarContainer);
978: else
979: layoutVScrollbar((JScrollBar) scrollbarContainer);
980: }
981: }
982:
983:
988: protected void layoutHScrollbar(JScrollBar sb)
989: {
990: Rectangle vr = new Rectangle();
991: SwingUtilities.calculateInnerArea(scrollbar, vr);
992:
993: Dimension incrDims = incrButton.getPreferredSize();
994: Dimension decrDims = decrButton.getPreferredSize();
995:
996:
997: SwingUtilities.calculateInnerArea(scrollbar, trackRect);
998: trackRect.width -= incrDims.getWidth();
999: trackRect.width -= decrDims.getWidth();
1000: trackRect.x += decrDims.getWidth();
1001:
1002: updateThumbRect();
1003:
1004: decrButton.setBounds(vr.x, vr.y, decrDims.width, trackRect.height);
1005: incrButton.setBounds(trackRect.x + trackRect.width, vr.y, incrDims.width,
1006: trackRect.height);
1007: }
1008:
1009:
1014: protected void layoutVScrollbar(JScrollBar sb)
1015: {
1016: Rectangle vr = new Rectangle();
1017: SwingUtilities.calculateInnerArea(scrollbar, vr);
1018:
1019: Dimension incrDims = incrButton.getPreferredSize();
1020: Dimension decrDims = decrButton.getPreferredSize();
1021:
1022:
1023: SwingUtilities.calculateInnerArea(scrollbar, trackRect);
1024: trackRect.height -= incrDims.getHeight();
1025: trackRect.height -= decrDims.getHeight();
1026: trackRect.y += decrDims.getHeight();
1027:
1028: updateThumbRect();
1029:
1030: decrButton.setBounds(vr.x, vr.y, trackRect.width, decrDims.height);
1031: incrButton.setBounds(vr.x, trackRect.y + trackRect.height,
1032: trackRect.width, incrDims.height);
1033: }
1034:
1035:
1038: void updateThumbRect()
1039: {
1040: int max = scrollbar.getMaximum();
1041: int min = scrollbar.getMinimum();
1042: int value = scrollbar.getValue();
1043: int extent = scrollbar.getVisibleAmount();
1044: if (max - extent <= min)
1045: {
1046: if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
1047: {
1048: thumbRect.x = trackRect.x;
1049: thumbRect.y = trackRect.y;
1050: thumbRect.width = getMinimumThumbSize().width;
1051: thumbRect.height = trackRect.height;
1052: }
1053: else
1054: {
1055: thumbRect.x = trackRect.x;
1056: thumbRect.y = trackRect.y;
1057: thumbRect.width = trackRect.width;
1058: thumbRect.height = getMinimumThumbSize().height;
1059: }
1060: }
1061: else
1062: {
1063: if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
1064: {
1065: thumbRect.x = trackRect.x;
1066: thumbRect.width = Math.max(extent * trackRect.width / (max - min),
1067: getMinimumThumbSize().width);
1068: int availableWidth = trackRect.width - thumbRect.width;
1069: thumbRect.x += (value - min) * availableWidth / (max - min - extent);
1070: thumbRect.y = trackRect.y;
1071: thumbRect.height = trackRect.height;
1072: }
1073: else
1074: {
1075: thumbRect.x = trackRect.x;
1076: thumbRect.height = Math.max(extent * trackRect.height / (max - min),
1077: getMinimumThumbSize().height);
1078: int availableHeight = trackRect.height - thumbRect.height;
1079: thumbRect.y = trackRect.y
1080: + (value - min) * availableHeight / (max - min - extent);
1081: thumbRect.width = trackRect.width;
1082: }
1083: }
1084:
1085: }
1086:
1087:
1094: public Dimension minimumLayoutSize(Container scrollbarContainer)
1095: {
1096: return preferredLayoutSize(scrollbarContainer);
1097: }
1098:
1099:
1105: public void paint(Graphics g, JComponent c)
1106: {
1107: paintTrack(g, c, getTrackBounds());
1108: paintThumb(g, c, getThumbBounds());
1109:
1110: if (trackHighlight == INCREASE_HIGHLIGHT)
1111: paintIncreaseHighlight(g);
1112: else if (trackHighlight == DECREASE_HIGHLIGHT)
1113: paintDecreaseHighlight(g);
1114: }
1115:
1116:
1123: protected void paintDecreaseHighlight(Graphics g)
1124: {
1125: Color saved = g.getColor();
1126:
1127: g.setColor(trackHighlightColor);
1128: if (scrollbar.getOrientation() == HORIZONTAL)
1129: g.fillRect(trackRect.x, trackRect.y, thumbRect.x - trackRect.x,
1130: trackRect.height);
1131: else
1132: g.fillRect(trackRect.x, trackRect.y, trackRect.width,
1133: thumbRect.y - trackRect.y);
1134: g.setColor(saved);
1135: }
1136:
1137:
1144: protected void paintIncreaseHighlight(Graphics g)
1145: {
1146: Color saved = g.getColor();
1147:
1148: g.setColor(trackHighlightColor);
1149: if (scrollbar.getOrientation() == HORIZONTAL)
1150: g.fillRect(thumbRect.x + thumbRect.width, trackRect.y,
1151: trackRect.x + trackRect.width - thumbRect.x - thumbRect.width,
1152: trackRect.height);
1153: else
1154: g.fillRect(trackRect.x, thumbRect.y + thumbRect.height, trackRect.width,
1155: trackRect.y + trackRect.height - thumbRect.y
1156: - thumbRect.height);
1157: g.setColor(saved);
1158: }
1159:
1160:
1167: protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
1168: {
1169: g.setColor(thumbColor);
1170: g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width,
1171: thumbBounds.height);
1172:
1173: BasicGraphicsUtils.drawBezel(g, thumbBounds.x, thumbBounds.y,
1174: thumbBounds.width, thumbBounds.height,
1175: false, false, thumbDarkShadowColor,
1176: thumbDarkShadowColor, thumbHighlightColor,
1177: thumbHighlightColor);
1178: }
1179:
1180:
1187: protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
1188: {
1189: Color saved = g.getColor();
1190: g.setColor(trackColor);
1191: g.fill3DRect(trackBounds.x, trackBounds.y, trackBounds.width,
1192: trackBounds.height, false);
1193: g.setColor(saved);
1194: }
1195:
1196:
1203: public Dimension preferredLayoutSize(Container scrollbarContainer)
1204: {
1205: if (scrollbarContainer instanceof JComponent)
1206: return getPreferredSize((JComponent) scrollbarContainer);
1207: else
1208: return null;
1209: }
1210:
1211:
1216: public void removeLayoutComponent(Component child)
1217: {
1218:
1219: }
1220:
1221:
1226: protected void scrollByBlock(int direction)
1227: {
1228: scrollByBlock(scrollbar, direction);
1229: }
1230:
1231:
1241: static final void scrollByBlock(JScrollBar scrollBar, int direction)
1242: {
1243: int delta;
1244: if (direction > 0)
1245: delta = scrollBar.getBlockIncrement(direction);
1246: else
1247: delta = - scrollBar.getBlockIncrement(direction);
1248: int oldValue = scrollBar.getValue();
1249: int newValue = oldValue + delta;
1250:
1251:
1252: if (delta > 0 && newValue < oldValue)
1253: newValue = scrollBar.getMaximum();
1254: else if (delta < 0 && newValue > oldValue)
1255: newValue = scrollBar.getMinimum();
1256:
1257: scrollBar.setValue(newValue);
1258: }
1259:
1260:
1265: protected void scrollByUnit(int direction)
1266: {
1267: scrollByUnits(scrollbar, direction, 1);
1268: }
1269:
1270:
1281: static final void scrollByUnits(JScrollBar scrollBar, int direction,
1282: int units)
1283: {
1284:
1285:
1286:
1287:
1288:
1289: for (int i = 0; i < units; i++)
1290: {
1291: int delta;
1292: if (direction > 0)
1293: delta = scrollBar.getUnitIncrement(direction);
1294: else
1295: delta = - scrollBar.getUnitIncrement(direction);
1296: int oldValue = scrollBar.getValue();
1297: int newValue = oldValue + delta;
1298:
1299:
1300: if (delta > 0 && newValue < oldValue)
1301: newValue = scrollBar.getMaximum();
1302: else if (delta < 0 && newValue > oldValue)
1303: newValue = scrollBar.getMinimum();
1304:
1305: scrollBar.setValue(newValue);
1306: }
1307: }
1308:
1309:
1317: protected void setThumbBounds(int x, int y, int width, int height)
1318: {
1319: thumbRect.x = x;
1320: thumbRect.y = y;
1321: thumbRect.width = width;
1322: thumbRect.height = height;
1323: }
1324:
1325:
1329: protected void uninstallComponents()
1330: {
1331: if (incrButton != null)
1332: scrollbar.remove(incrButton);
1333: if (decrButton != null)
1334: scrollbar.remove(decrButton);
1335: }
1336:
1337:
1341: protected void uninstallDefaults()
1342: {
1343: scrollbar.setForeground(null);
1344: scrollbar.setBackground(null);
1345: LookAndFeel.uninstallBorder(scrollbar);
1346: incrButton = null;
1347: decrButton = null;
1348: }
1349:
1350:
1353: protected void uninstallListeners()
1354: {
1355: if (scrollTimer != null)
1356: scrollTimer.removeActionListener(scrollListener);
1357:
1358: if (scrollbar != null)
1359: {
1360: scrollbar.getModel().removeChangeListener(modelListener);
1361: scrollbar.removePropertyChangeListener(propertyChangeListener);
1362: scrollbar.removeMouseListener(trackListener);
1363: scrollbar.removeMouseMotionListener(trackListener);
1364: }
1365:
1366: if (decrButton != null)
1367: decrButton.removeMouseListener(buttonListener);
1368: if (incrButton != null)
1369: incrButton.removeMouseListener(buttonListener);
1370:
1371: propertyChangeListener = null;
1372: modelListener = null;
1373: buttonListener = null;
1374: trackListener = null;
1375: scrollListener = null;
1376: }
1377:
1378:
1385: public void uninstallUI(JComponent c)
1386: {
1387: uninstallKeyboardActions();
1388: uninstallListeners();
1389: uninstallDefaults();
1390: uninstallComponents();
1391:
1392: scrollTimer = null;
1393:
1394: thumbRect = null;
1395: trackRect = null;
1396:
1397: trackColor = null;
1398: trackHighlightColor = null;
1399: thumbColor = null;
1400: thumbHighlightColor = null;
1401: thumbDarkShadowColor = null;
1402: thumbLightShadowColor = null;
1403:
1404: scrollbar = null;
1405: }
1406:
1407:
1417: int valueForYPosition(int yPos)
1418: {
1419: int min = scrollbar.getMinimum();
1420: int max = scrollbar.getMaximum();
1421: int len = trackRect.height;
1422:
1423: int value;
1424:
1425:
1426:
1427: if (len == 0)
1428: return (max - min) / 2;
1429:
1430: value = (yPos - trackRect.y) * (max - min) / len + min;
1431:
1432:
1433: if (value > max)
1434: value = max;
1435: else if (value < min)
1436: value = min;
1437: return value;
1438: }
1439:
1440:
1450: int valueForXPosition(int xPos)
1451: {
1452: int min = scrollbar.getMinimum();
1453: int max = scrollbar.getMaximum();
1454: int len = trackRect.width;
1455:
1456: int value;
1457:
1458:
1459:
1460: if (len == 0)
1461: return (max - min) / 2;
1462:
1463: value = (xPos - trackRect.x) * (max - min) / len + min;
1464:
1465:
1466: if (value > max)
1467: value = max;
1468: else if (value < min)
1469: value = min;
1470: return value;
1471: }
1472:
1473:
1480: public boolean isThumbRollover()
1481: {
1482: return thumbRollover;
1483: }
1484:
1485:
1493: protected void setThumbRollover(boolean active)
1494: {
1495: thumbRollover = active;
1496: }
1497:
1498:
1507: public boolean getSupportsAbsolutePositioning()
1508: {
1509:
1510:
1511: return false;
1512: }
1513: }