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: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
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: import ;
76:
77:
137: public class BasicSliderUI extends SliderUI
138: {
139:
146: public class ChangeHandler implements ChangeListener
147: {
148:
155: public void stateChanged(ChangeEvent e)
156: {
157:
158:
159:
160: calculateThumbLocation();
161: slider.repaint();
162: }
163: }
164:
165:
172: public class ComponentHandler extends ComponentAdapter
173: {
174:
181: public void componentResized(ComponentEvent e)
182: {
183: calculateGeometry();
184:
185: slider.revalidate();
186: slider.repaint();
187: }
188: }
189:
190:
197: public class FocusHandler implements FocusListener
198: {
199:
205: public void focusGained(FocusEvent e)
206: {
207:
208: }
209:
210:
216: public void focusLost(FocusEvent e)
217: {
218:
219: }
220: }
221:
222:
226: public class PropertyChangeHandler implements PropertyChangeListener
227: {
228:
234: public void propertyChange(PropertyChangeEvent e)
235: {
236:
237: if (e.getPropertyName().equals("orientation"))
238: recalculateIfOrientationChanged();
239: else if (e.getPropertyName().equals("model"))
240: {
241: BoundedRangeModel oldModel = (BoundedRangeModel) e.getOldValue();
242: oldModel.removeChangeListener(changeListener);
243: slider.getModel().addChangeListener(changeListener);
244: calculateThumbLocation();
245: }
246:
247:
248:
249:
250:
251:
252:
253: slider.repaint();
254: }
255: }
256:
257:
266: public class ScrollListener implements ActionListener
267: {
268:
269: private transient int direction;
270:
271:
272: private transient boolean block;
273:
274:
277: public ScrollListener()
278: {
279: direction = POSITIVE_SCROLL;
280: block = false;
281: }
282:
283:
289: public ScrollListener(int dir, boolean block)
290: {
291: direction = dir;
292: this.block = block;
293: }
294:
295:
302: public void actionPerformed(ActionEvent e)
303: {
304: if (! trackListener.shouldScroll(direction))
305: {
306: scrollTimer.stop();
307: return;
308: }
309:
310: if (block)
311: scrollByBlock(direction);
312: else
313: scrollByUnit(direction);
314: }
315:
316:
321: public void setDirection(int direction)
322: {
323: this.direction = direction;
324: }
325:
326:
331: public void setScrollByBlock(boolean block)
332: {
333: this.block = block;
334: }
335: }
336:
337:
344: public class TrackListener extends MouseInputAdapter
345: {
346:
347: protected int currentMouseX;
348:
349:
350: protected int currentMouseY;
351:
352:
355: protected int offset;
356:
357:
364: public void mouseDragged(MouseEvent e)
365: {
366: currentMouseX = e.getX();
367: currentMouseY = e.getY();
368: if (slider.getValueIsAdjusting())
369: {
370: int value;
371: if (slider.getOrientation() == JSlider.HORIZONTAL)
372: value = valueForXPosition(currentMouseX) - offset;
373: else
374: value = valueForYPosition(currentMouseY) - offset;
375:
376: slider.setValue(value);
377: }
378: }
379:
380:
386: public void mouseMoved(MouseEvent e)
387: {
388:
389: }
390:
391:
399: public void mousePressed(MouseEvent e)
400: {
401: currentMouseX = e.getX();
402: currentMouseY = e.getY();
403:
404: int value;
405: if (slider.getOrientation() == JSlider.HORIZONTAL)
406: value = valueForXPosition(currentMouseX);
407: else
408: value = valueForYPosition(currentMouseY);
409:
410: if (slider.getSnapToTicks())
411: value = findClosestTick(value);
412:
413:
414: if (! thumbRect.contains(e.getPoint()))
415: {
416:
417:
418: if (value > slider.getValue())
419: scrollDueToClickInTrack(POSITIVE_SCROLL);
420: else
421: scrollDueToClickInTrack(NEGATIVE_SCROLL);
422: }
423: else
424: {
425: slider.setValueIsAdjusting(true);
426: offset = value - slider.getValue();
427: }
428: }
429:
430:
436: public void mouseReleased(MouseEvent e)
437: {
438: currentMouseX = e.getX();
439: currentMouseY = e.getY();
440:
441: if (slider.getValueIsAdjusting())
442: {
443: slider.setValueIsAdjusting(false);
444: if (slider.getSnapToTicks())
445: slider.setValue(findClosestTick(slider.getValue()));
446: }
447: if (scrollTimer != null)
448: scrollTimer.stop();
449: }
450:
451:
458: public boolean shouldScroll(int direction)
459: {
460: int value;
461: if (slider.getOrientation() == JSlider.HORIZONTAL)
462: value = valueForXPosition(currentMouseX);
463: else
464: value = valueForYPosition(currentMouseY);
465:
466: if (direction == POSITIVE_SCROLL)
467: return (value > slider.getValue());
468: else
469: return (value < slider.getValue());
470: }
471: }
472:
473:
474: private transient int thumbHeight;
475:
476:
477: private transient int thumbWidth;
478:
479:
480: private transient int tickHeight;
481:
482:
483: protected ChangeListener changeListener;
484:
485:
486: protected PropertyChangeListener propertyChangeListener;
487:
488:
489: protected ScrollListener scrollListener;
490:
491:
492: protected ComponentListener componentListener;
493:
494:
495: protected FocusListener focusListener;
496:
497:
498: protected TrackListener trackListener;
499:
500:
501: protected Insets focusInsets;
502:
503:
504: protected Insets insetCache;
505:
506:
507: protected Rectangle contentRect;
508:
509:
510: protected Rectangle focusRect;
511:
512:
513: protected Rectangle thumbRect;
514:
515:
516: protected Rectangle tickRect;
517:
518:
519: protected Rectangle labelRect;
520:
521:
522: protected Rectangle trackRect;
523:
524:
525: public static final int MAX_SCROLL = 2;
526:
527:
528: public static final int MIN_SCROLL = -2;
529:
530:
531: public static final int NEGATIVE_SCROLL = -1;
532:
533:
534: public static final int POSITIVE_SCROLL = 1;
535:
536:
537: protected int trackBuffer;
538:
539:
540: protected boolean leftToRightCache;
541:
542:
543: protected Timer scrollTimer;
544:
545:
546: protected JSlider slider;
547:
548:
549: private transient Color shadowColor;
550:
551:
552: private transient Color highlightColor;
553:
554:
555: private transient Color focusColor;
556:
557:
562: public BasicSliderUI(JSlider b)
563: {
564: super();
565: }
566:
567:
573: protected Color getShadowColor()
574: {
575: return shadowColor;
576: }
577:
578:
584: protected Color getHighlightColor()
585: {
586: return highlightColor;
587: }
588:
589:
596: protected Color getFocusColor()
597: {
598: return focusColor;
599: }
600:
601:
609: public static ComponentUI createUI(JComponent b)
610: {
611: return new BasicSliderUI((JSlider) b);
612: }
613:
614:
621: public void installUI(JComponent c)
622: {
623: super.installUI(c);
624: if (c instanceof JSlider)
625: {
626: slider = (JSlider) c;
627:
628: focusRect = new Rectangle();
629: contentRect = new Rectangle();
630: thumbRect = new Rectangle();
631: trackRect = new Rectangle();
632: tickRect = new Rectangle();
633: labelRect = new Rectangle();
634:
635: insetCache = slider.getInsets();
636: leftToRightCache = ! slider.getInverted();
637:
638: scrollTimer = new Timer(200, null);
639: scrollTimer.setRepeats(true);
640:
641: installDefaults(slider);
642: installListeners(slider);
643: installKeyboardActions(slider);
644:
645: calculateFocusRect();
646:
647: calculateContentRect();
648: calculateThumbSize();
649: calculateTrackBuffer();
650: calculateTrackRect();
651: calculateThumbLocation();
652:
653: calculateTickRect();
654: calculateLabelRect();
655: }
656: }
657:
658:
665: public void uninstallUI(JComponent c)
666: {
667: super.uninstallUI(c);
668:
669: uninstallKeyboardActions(slider);
670: uninstallListeners(slider);
671:
672: scrollTimer = null;
673:
674: focusRect = null;
675: contentRect = null;
676: thumbRect = null;
677: trackRect = null;
678: tickRect = null;
679: labelRect = null;
680:
681: focusInsets = null;
682: }
683:
684:
690: protected void installDefaults(JSlider slider)
691: {
692: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
693:
694: slider.setForeground(defaults.getColor("Slider.foreground"));
695: slider.setBackground(defaults.getColor("Slider.background"));
696: shadowColor = defaults.getColor("Slider.shadow");
697: highlightColor = defaults.getColor("Slider.highlight");
698: focusColor = defaults.getColor("Slider.focus");
699: slider.setBorder(defaults.getBorder("Slider.border"));
700: slider.setOpaque(true);
701:
702: thumbHeight = defaults.getInt("Slider.thumbHeight");
703: thumbWidth = defaults.getInt("Slider.thumbWidth");
704: tickHeight = defaults.getInt("Slider.tickHeight");
705:
706: focusInsets = defaults.getInsets("Slider.focusInsets");
707: }
708:
709:
717: protected TrackListener createTrackListener(JSlider slider)
718: {
719: return new TrackListener();
720: }
721:
722:
730: protected ChangeListener createChangeListener(JSlider slider)
731: {
732: return new ChangeHandler();
733: }
734:
735:
743: protected ComponentListener createComponentListener(JSlider slider)
744: {
745: return new ComponentHandler();
746: }
747:
748:
756: protected FocusListener createFocusListener(JSlider slider)
757: {
758: return new FocusHandler();
759: }
760:
761:
769: protected ScrollListener createScrollListener(JSlider slider)
770: {
771: return new ScrollListener();
772: }
773:
774:
782: protected PropertyChangeListener createPropertyChangeListener(JSlider slider)
783: {
784: return new PropertyChangeHandler();
785: }
786:
787:
793: protected void installListeners(JSlider slider)
794: {
795: propertyChangeListener = createPropertyChangeListener(slider);
796: componentListener = createComponentListener(slider);
797: trackListener = createTrackListener(slider);
798: focusListener = createFocusListener(slider);
799: changeListener = createChangeListener(slider);
800: scrollListener = createScrollListener(slider);
801:
802: slider.addPropertyChangeListener(propertyChangeListener);
803: slider.addComponentListener(componentListener);
804: slider.addMouseListener(trackListener);
805: slider.addMouseMotionListener(trackListener);
806: slider.addFocusListener(focusListener);
807: slider.getModel().addChangeListener(changeListener);
808:
809: scrollTimer.addActionListener(scrollListener);
810: }
811:
812:
818: protected void uninstallListeners(JSlider slider)
819: {
820: slider.removePropertyChangeListener(propertyChangeListener);
821: slider.removeComponentListener(componentListener);
822: slider.removeMouseListener(trackListener);
823: slider.removeMouseMotionListener(trackListener);
824: slider.removeFocusListener(focusListener);
825: slider.getModel().removeChangeListener(changeListener);
826:
827: scrollTimer.removeActionListener(scrollListener);
828:
829: propertyChangeListener = null;
830: componentListener = null;
831: trackListener = null;
832: focusListener = null;
833: changeListener = null;
834: scrollListener = null;
835: }
836:
837:
844: protected void installKeyboardActions(JSlider slider)
845: {
846:
847: }
848:
849:
856: protected void uninstallKeyboardActions(JSlider slider)
857: {
858:
859: }
860:
861:
875:
876:
882: public Dimension getPreferredHorizontalSize()
883: {
884: Insets insets = slider.getInsets();
885:
886:
887:
888: int width = getWidthOfWidestLabel() * (slider.getLabelTable() == null ? 0
889: : slider.getLabelTable()
890: .size());
891:
892:
893:
894: if (width < 200)
895: width = 200;
896:
897:
898:
899: width += insets.left + insets.right + focusInsets.left + focusInsets.right;
900:
901:
902: int height = thumbHeight;
903:
904: if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0
905: || slider.getMinorTickSpacing() > 0)
906: height += tickHeight;
907:
908: if (slider.getPaintLabels())
909: height += getHeightOfTallestLabel();
910:
911: height += insets.top + insets.bottom + focusInsets.top
912: + focusInsets.bottom;
913:
914: return new Dimension(width, height);
915: }
916:
917:
923: public Dimension getPreferredVerticalSize()
924: {
925: Insets insets = slider.getInsets();
926:
927: int height = getHeightOfTallestLabel() * (slider.getLabelTable() == null
928: ? 0 : slider.getLabelTable()
929: .size());
930:
931: if (height < 200)
932: height = 200;
933:
934: height += insets.top + insets.bottom + focusInsets.top
935: + focusInsets.bottom;
936:
937: int width = thumbHeight;
938:
939: if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0
940: || slider.getMinorTickSpacing() > 0)
941: width += tickHeight;
942:
943: if (slider.getPaintLabels())
944: width += getWidthOfWidestLabel();
945:
946: width += insets.left + insets.right + focusInsets.left + focusInsets.right;
947:
948: return new Dimension(width, height);
949: }
950:
951:
957: public Dimension getMinimumHorizontalSize()
958: {
959: return getPreferredHorizontalSize();
960: }
961:
962:
968: public Dimension getMinimumVerticalSize()
969: {
970: return getPreferredVerticalSize();
971: }
972:
973:
982: public Dimension getPreferredSize(JComponent c)
983: {
984: if (slider.getOrientation() == JSlider.HORIZONTAL)
985: return getPreferredHorizontalSize();
986: else
987: return getPreferredVerticalSize();
988: }
989:
990:
999: public Dimension getMinimumSize(JComponent c)
1000: {
1001: if (slider.getOrientation() == JSlider.HORIZONTAL)
1002: return getPreferredHorizontalSize();
1003: else
1004: return getPreferredVerticalSize();
1005: }
1006:
1007:
1016: public Dimension getMaximumSize(JComponent c)
1017: {
1018: if (slider.getOrientation() == JSlider.HORIZONTAL)
1019: return getPreferredHorizontalSize();
1020: else
1021: return getPreferredVerticalSize();
1022: }
1023:
1024:
1028: protected void calculateGeometry()
1029: {
1030: calculateFocusRect();
1031: calculateContentRect();
1032: calculateThumbSize();
1033: calculateTrackBuffer();
1034: calculateTrackRect();
1035: calculateTickRect();
1036: calculateLabelRect();
1037: calculateThumbLocation();
1038: }
1039:
1040:
1044: protected void calculateFocusRect()
1045: {
1046: insetCache = slider.getInsets();
1047: focusRect = SwingUtilities.calculateInnerArea(slider, focusRect);
1048:
1049: if (focusRect.width < 0)
1050: focusRect.width = 0;
1051: if (focusRect.height < 0)
1052: focusRect.height = 0;
1053: }
1054:
1055:
1059: protected void calculateThumbSize()
1060: {
1061: if (slider.getOrientation() == JSlider.HORIZONTAL)
1062: {
1063: if (thumbWidth > contentRect.width)
1064: thumbRect.width = contentRect.width / 4;
1065: else
1066: thumbRect.width = thumbWidth;
1067: if (thumbHeight > contentRect.height)
1068: thumbRect.height = contentRect.height;
1069: else
1070: thumbRect.height = thumbHeight;
1071: }
1072: else
1073: {
1074:
1075:
1076: if (thumbWidth > contentRect.height)
1077: thumbRect.height = contentRect.height / 4;
1078: else
1079: thumbRect.height = thumbWidth;
1080: if (thumbHeight > contentRect.width)
1081: thumbRect.width = contentRect.width;
1082: else
1083: thumbRect.width = thumbHeight;
1084: }
1085: }
1086:
1087:
1091: protected void calculateContentRect()
1092: {
1093: contentRect.x = focusRect.x + focusInsets.left;
1094: contentRect.y = focusRect.y + focusInsets.top;
1095: contentRect.width = focusRect.width - focusInsets.left - focusInsets.right;
1096: contentRect.height = focusRect.height - focusInsets.top
1097: - focusInsets.bottom;
1098:
1099: if (contentRect.width < 0)
1100: contentRect.width = 0;
1101: if (contentRect.height < 0)
1102: contentRect.height = 0;
1103: }
1104:
1105:
1109: protected void calculateThumbLocation()
1110: {
1111: int value = slider.getValue();
1112:
1113: if (slider.getOrientation() == JSlider.HORIZONTAL)
1114: {
1115: thumbRect.x = xPositionForValue(value) - thumbRect.width / 2;
1116: thumbRect.y = contentRect.y;
1117: }
1118: else
1119: {
1120: thumbRect.x = contentRect.x;
1121: thumbRect.y = yPositionForValue(value) - thumbRect.height / 2;
1122: }
1123: }
1124:
1125:
1129: protected void calculateTrackBuffer()
1130: {
1131: if (slider.getOrientation() == JSlider.HORIZONTAL)
1132: trackBuffer = thumbRect.width;
1133: else
1134: trackBuffer = thumbRect.height;
1135: }
1136:
1137:
1142: protected Dimension getThumbSize()
1143: {
1144:
1145:
1146: return thumbRect.getSize();
1147: }
1148:
1149:
1153: protected void calculateTrackRect()
1154: {
1155: if (slider.getOrientation() == JSlider.HORIZONTAL)
1156: {
1157: trackRect.x = contentRect.x + trackBuffer;
1158: trackRect.y = contentRect.y;
1159: trackRect.width = contentRect.width - 2 * trackBuffer;
1160: trackRect.height = thumbRect.height;
1161: }
1162: else
1163: {
1164: trackRect.x = contentRect.x;
1165: trackRect.y = contentRect.y + trackBuffer;
1166: trackRect.width = thumbRect.width;
1167: trackRect.height = contentRect.height - 2 * trackBuffer;
1168: }
1169: }
1170:
1171:
1181: protected int getTickLength()
1182: {
1183: return tickHeight;
1184: }
1185:
1186:
1190: protected void calculateTickRect()
1191: {
1192: if (slider.getOrientation() == JSlider.HORIZONTAL)
1193: {
1194: tickRect.x = trackRect.x;
1195: tickRect.y = trackRect.y + trackRect.height;
1196: tickRect.width = trackRect.width;
1197: tickRect.height = getTickLength();
1198:
1199: if (tickRect.y + tickRect.height > contentRect.y + contentRect.height)
1200: tickRect.height = contentRect.y + contentRect.height - tickRect.y;
1201: }
1202: else
1203: {
1204: tickRect.x = trackRect.x + trackRect.width;
1205: tickRect.y = trackRect.y;
1206: tickRect.width = getTickLength();
1207: tickRect.height = trackRect.height;
1208:
1209: if (tickRect.x + tickRect.width > contentRect.x + contentRect.width)
1210: tickRect.width = contentRect.x + contentRect.width - tickRect.x;
1211: }
1212: }
1213:
1214:
1218: protected void calculateLabelRect()
1219: {
1220: if (slider.getOrientation() == JSlider.HORIZONTAL)
1221: {
1222: labelRect.x = contentRect.x;
1223: labelRect.y = tickRect.y + tickRect.height;
1224: labelRect.width = contentRect.width;
1225: labelRect.height = contentRect.height - labelRect.y;
1226: }
1227: else
1228: {
1229: labelRect.x = tickRect.x + tickRect.width;
1230: labelRect.y = contentRect.y;
1231: labelRect.width = contentRect.width - labelRect.x;
1232: labelRect.height = contentRect.height;
1233: }
1234: }
1235:
1236:
1242: protected int getWidthOfWidestLabel()
1243: {
1244: int widest = 0;
1245: Component label;
1246:
1247: if (slider.getLabelTable() == null)
1248: return 0;
1249:
1250: Dimension pref;
1251: for (Enumeration list = slider.getLabelTable().elements();
1252: list.hasMoreElements();)
1253: {
1254: Object comp = list.nextElement();
1255: if (! (comp instanceof Component))
1256: continue;
1257: label = (Component) comp;
1258: pref = label.getPreferredSize();
1259: if (pref != null && pref.width > widest)
1260: widest = pref.width;
1261: }
1262: return widest;
1263: }
1264:
1265:
1271: protected int getHeightOfTallestLabel()
1272: {
1273: int tallest = 0;
1274: Component label;
1275:
1276: if (slider.getLabelTable() == null)
1277: return 0;
1278: Dimension pref;
1279: for (Enumeration list = slider.getLabelTable().elements();
1280: list.hasMoreElements();)
1281: {
1282: Object comp = list.nextElement();
1283: if (! (comp instanceof Component))
1284: continue;
1285: label = (Component) comp;
1286: pref = label.getPreferredSize();
1287: if (pref != null && pref.height > tallest)
1288: tallest = pref.height;
1289: }
1290: return tallest;
1291: }
1292:
1293:
1299: protected int getWidthOfHighValueLabel()
1300: {
1301: Component highValueLabel = getHighestValueLabel();
1302: if (highValueLabel != null)
1303: return highValueLabel.getWidth();
1304: else
1305: return 0;
1306: }
1307:
1308:
1314: protected int getWidthOfLowValueLabel()
1315: {
1316: Component lowValueLabel = getLowestValueLabel();
1317: if (lowValueLabel != null)
1318: return lowValueLabel.getWidth();
1319: else
1320: return 0;
1321: }
1322:
1323:
1329: protected int getHeightOfHighValueLabel()
1330: {
1331: Component highValueLabel = getHighestValueLabel();
1332: if (highValueLabel != null)
1333: return highValueLabel.getHeight();
1334: else
1335: return 0;
1336: }
1337:
1338:
1344: protected int getHeightOfLowValueLabel()
1345: {
1346: Component lowValueLabel = getLowestValueLabel();
1347: if (lowValueLabel != null)
1348: return lowValueLabel.getHeight();
1349: else
1350: return 0;
1351: }
1352:
1353:
1358: protected boolean drawInverted()
1359: {
1360: return ! (slider.getInverted() ^ leftToRightCache);
1361: }
1362:
1363:
1368: protected Component getLowestValueLabel()
1369: {
1370: Integer key = new Integer(Integer.MAX_VALUE);
1371: Integer tmpKey;
1372: Dictionary labelTable = slider.getLabelTable();
1373:
1374: if (labelTable == null)
1375: return null;
1376:
1377: for (Enumeration list = labelTable.keys(); list.hasMoreElements();)
1378: {
1379: Object value = list.nextElement();
1380: if (! (value instanceof Integer))
1381: continue;
1382: tmpKey = (Integer) value;
1383: if (tmpKey.intValue() < key.intValue())
1384: key = tmpKey;
1385: }
1386: Object comp = labelTable.get(key);
1387: if (! (comp instanceof Component))
1388: return null;
1389: return (Component) comp;
1390: }
1391:
1392:
1397: protected Component getHighestValueLabel()
1398: {
1399: Integer key = new Integer(Integer.MIN_VALUE);
1400: Integer tmpKey;
1401: Dictionary labelTable = slider.getLabelTable();
1402:
1403: if (labelTable == null)
1404: return null;
1405:
1406: for (Enumeration list = labelTable.keys(); list.hasMoreElements();)
1407: {
1408: Object value = list.nextElement();
1409: if (! (value instanceof Integer))
1410: continue;
1411: tmpKey = (Integer) value;
1412: if (tmpKey.intValue() > key.intValue())
1413: key = tmpKey;
1414: }
1415: Object comp = labelTable.get(key);
1416: if (! (comp instanceof Component))
1417: return null;
1418: return (Component) comp;
1419: }
1420:
1421:
1429: public void paint(Graphics g, JComponent c)
1430: {
1431:
1432: leftToRightCache = slider.getComponentOrientation() != ComponentOrientation.RIGHT_TO_LEFT;
1433:
1434: calculateThumbLocation();
1435:
1436: if (slider.getPaintTrack())
1437: paintTrack(g);
1438: if (slider.getPaintTicks())
1439: paintTicks(g);
1440: if (slider.getPaintLabels())
1441: paintLabels(g);
1442:
1443:
1444: paintThumb(g);
1445: }
1446:
1447:
1451: protected void recalculateIfInsetsChanged()
1452: {
1453:
1454:
1455: calculateFocusRect();
1456:
1457: calculateContentRect();
1458: calculateThumbSize();
1459: calculateTrackBuffer();
1460: calculateTrackRect();
1461: calculateThumbLocation();
1462:
1463: calculateTickRect();
1464: calculateLabelRect();
1465: }
1466:
1467:
1471: protected void recalculateIfOrientationChanged()
1472: {
1473:
1474:
1475: calculateThumbSize();
1476: calculateTrackBuffer();
1477: calculateTrackRect();
1478: calculateThumbLocation();
1479:
1480: calculateTickRect();
1481: calculateLabelRect();
1482: }
1483:
1484:
1491: public void paintFocus(Graphics g)
1492: {
1493: Color saved_color = g.getColor();
1494:
1495: g.setColor(getFocusColor());
1496:
1497: g.drawRect(focusRect.x, focusRect.y, focusRect.width, focusRect.height);
1498:
1499: g.setColor(saved_color);
1500: }
1501:
1502:
1528: public void paintTrack(Graphics g)
1529: {
1530: Color saved_color = g.getColor();
1531: int width;
1532: int height;
1533:
1534: Point a = new Point(trackRect.x, trackRect.y);
1535: Point b = new Point(a);
1536: Point c = new Point(a);
1537: Point d = new Point(a);
1538:
1539: Polygon high;
1540: Polygon shadow;
1541:
1542: if (slider.getOrientation() == JSlider.HORIZONTAL)
1543: {
1544: width = trackRect.width;
1545: height = (thumbRect.height / 4 == 0) ? 1 : thumbRect.height / 4;
1546:
1547: a.translate(0, (trackRect.height / 2) - (height / 2));
1548: b.translate(0, (trackRect.height / 2) + (height / 2));
1549: c.translate(trackRect.width, (trackRect.height / 2) + (height / 2));
1550: d.translate(trackRect.width, (trackRect.height / 2) - (height / 2));
1551: }
1552: else
1553: {
1554: width = (thumbRect.width / 4 == 0) ? 1 : thumbRect.width / 4;
1555: height = trackRect.height;
1556:
1557: a.translate((trackRect.width / 2) - (width / 2), 0);
1558: b.translate((trackRect.width / 2) - (width / 2), trackRect.height);
1559: c.translate((trackRect.width / 2) + (width / 2), trackRect.height);
1560: d.translate((trackRect.width / 2) + (width / 2), 0);
1561: }
1562: g.setColor(Color.GRAY);
1563: g.fillRect(a.x, a.y, width, height);
1564:
1565: g.setColor(getHighlightColor());
1566: g.drawLine(b.x, b.y, c.x, c.y);
1567: g.drawLine(c.x, c.y, d.x, d.y);
1568:
1569: g.setColor(getShadowColor());
1570: g.drawLine(b.x, b.y, a.x, a.y);
1571: g.drawLine(a.x, a.y, d.x, d.y);
1572:
1573: g.setColor(saved_color);
1574: }
1575:
1576:
1583: public void paintTicks(Graphics g)
1584: {
1585: int max = slider.getMaximum();
1586: int min = slider.getMinimum();
1587: int majorSpace = slider.getMajorTickSpacing();
1588: int minorSpace = slider.getMinorTickSpacing();
1589:
1590: if (majorSpace > 0)
1591: {
1592: if (slider.getOrientation() == JSlider.HORIZONTAL)
1593: {
1594: double loc = tickRect.x;
1595: double increment = (max == min) ? 0
1596: : majorSpace * (double) tickRect.width / (max
1597: - min);
1598: if (drawInverted())
1599: {
1600: loc += tickRect.width;
1601: increment *= -1;
1602: }
1603: for (int i = min; i <= max; i += majorSpace)
1604: {
1605: paintMajorTickForHorizSlider(g, tickRect, (int) loc);
1606: loc += increment;
1607: }
1608: }
1609: else
1610: {
1611: double loc = tickRect.height + tickRect.y;
1612: double increment = (max == min) ? 0
1613: : -majorSpace * (double) tickRect.height / (max
1614: - min);
1615: if (drawInverted())
1616: {
1617: loc = tickRect.y;
1618: increment *= -1;
1619: }
1620: for (int i = min; i <= max; i += majorSpace)
1621: {
1622: paintMajorTickForVertSlider(g, tickRect, (int) loc);
1623: loc += increment;
1624: }
1625: }
1626: }
1627: if (minorSpace > 0)
1628: {
1629: if (slider.getOrientation() == JSlider.HORIZONTAL)
1630: {
1631: double loc = tickRect.x;
1632: double increment = (max == min) ? 0
1633: : minorSpace * (double) tickRect.width / (max
1634: - min);
1635: if (drawInverted())
1636: {
1637: loc += tickRect.width;
1638: increment *= -1;
1639: }
1640: for (int i = min; i <= max; i += minorSpace)
1641: {
1642: paintMinorTickForHorizSlider(g, tickRect, (int) loc);
1643: loc += increment;
1644: }
1645: }
1646: else
1647: {
1648: double loc = tickRect.height + tickRect.y;
1649: double increment = (max == min) ? 0
1650: : -minorSpace * (double) tickRect.height / (max
1651: - min);
1652: if (drawInverted())
1653: {
1654: loc = tickRect.y;
1655: increment *= -1;
1656: }
1657: for (int i = min; i <= max; i += minorSpace)
1658: {
1659: paintMinorTickForVertSlider(g, tickRect, (int) loc);
1660: loc += increment;
1661: }
1662: }
1663: }
1664: }
1665:
1666:
1671:
1672:
1680: protected void paintMinorTickForHorizSlider(Graphics g,
1681: Rectangle tickBounds, int x)
1682: {
1683: int y = tickRect.y + tickRect.height / 4;
1684: Color saved = g.getColor();
1685: g.setColor(Color.BLACK);
1686:
1687: g.drawLine(x, y, x, y + tickRect.height / 4);
1688: g.setColor(saved);
1689: }
1690:
1691:
1699: protected void paintMajorTickForHorizSlider(Graphics g,
1700: Rectangle tickBounds, int x)
1701: {
1702: int y = tickRect.y + tickRect.height / 4;
1703: Color saved = g.getColor();
1704: g.setColor(Color.BLACK);
1705:
1706: g.drawLine(x, y, x, y + tickRect.height / 2);
1707: g.setColor(saved);
1708: }
1709:
1710:
1718: protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds,
1719: int y)
1720: {
1721: int x = tickRect.x + tickRect.width / 4;
1722: Color saved = g.getColor();
1723: g.setColor(Color.BLACK);
1724:
1725: g.drawLine(x, y, x + tickRect.width / 4, y);
1726: g.setColor(saved);
1727: }
1728:
1729:
1737: protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds,
1738: int y)
1739: {
1740: int x = tickRect.x + tickRect.width / 4;
1741: Color saved = g.getColor();
1742: g.setColor(Color.BLACK);
1743:
1744: g.drawLine(x, y, x + tickRect.width / 2, y);
1745: g.setColor(saved);
1746: }
1747:
1748:
1756: public void paintLabels(Graphics g)
1757: {
1758: if (slider.getLabelTable() != null)
1759: {
1760: Dictionary table = slider.getLabelTable();
1761: Integer tmpKey;
1762: Object key;
1763: Object element;
1764: Component label;
1765: if (slider.getOrientation() == JSlider.HORIZONTAL)
1766: {
1767: for (Enumeration list = table.keys(); list.hasMoreElements();)
1768: {
1769: key = list.nextElement();
1770: if (! (key instanceof Integer))
1771: continue;
1772: tmpKey = (Integer) key;
1773: element = table.get(tmpKey);
1774:
1775:
1776: if (! (element instanceof JLabel))
1777: continue;
1778: label = (Component) element;
1779: paintHorizontalLabel(g, tmpKey.intValue(), label);
1780: }
1781: }
1782: else
1783: {
1784: for (Enumeration list = table.keys(); list.hasMoreElements();)
1785: {
1786: key = list.nextElement();
1787: if (! (key instanceof Integer))
1788: continue;
1789: tmpKey = (Integer) key;
1790: element = table.get(tmpKey);
1791:
1792:
1793: if (! (element instanceof JLabel))
1794: continue;
1795: label = (Component) element;
1796: paintVerticalLabel(g, tmpKey.intValue(), label);
1797: }
1798: }
1799: }
1800: }
1801:
1802:
1813: protected void paintHorizontalLabel(Graphics g, int value, Component label)
1814: {
1815:
1816:
1817:
1818:
1819:
1820: Dimension dim = label.getPreferredSize();
1821: int w = (int) dim.getWidth();
1822: int h = (int) dim.getHeight();
1823:
1824: int max = slider.getMaximum();
1825: int min = slider.getMinimum();
1826:
1827: if (value > max || value < min)
1828: return;
1829:
1830:
1831:
1832:
1833:
1834:
1835:
1836:
1837: int xpos = xPositionForValue(value) - w / 2;
1838: int ypos = labelRect.y;
1839:
1840:
1841:
1842:
1843:
1844: if (xpos < 0)
1845: xpos = 0;
1846:
1847:
1848:
1849:
1850: if (xpos + w > labelRect.x + labelRect.width)
1851: w = labelRect.x + labelRect.width - xpos;
1852:
1853:
1854:
1855: if (h > labelRect.height)
1856: h = labelRect.height;
1857:
1858: label.setBounds(xpos, ypos, w, h);
1859: javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds());
1860: }
1861:
1862:
1873: protected void paintVerticalLabel(Graphics g, int value, Component label)
1874: {
1875: Dimension dim = label.getPreferredSize();
1876: int w = (int) dim.getWidth();
1877: int h = (int) dim.getHeight();
1878:
1879: int max = slider.getMaximum();
1880: int min = slider.getMinimum();
1881:
1882: if (value > max || value < min)
1883: return;
1884:
1885: int xpos = labelRect.x;
1886: int ypos = yPositionForValue(value) - h / 2;
1887:
1888: if (ypos < 0)
1889: ypos = 0;
1890:
1891: if (ypos + h > labelRect.y + labelRect.height)
1892: h = labelRect.y + labelRect.height - ypos;
1893:
1894: if (w > labelRect.width)
1895: w = labelRect.width;
1896:
1897: label.setBounds(xpos, ypos, w, h);
1898: javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds());
1899: }
1900:
1901:
1923: public void paintThumb(Graphics g)
1924: {
1925: Color saved_color = g.getColor();
1926:
1927: Polygon thumb = new Polygon();
1928:
1929: Point a = new Point(thumbRect.x, thumbRect.y);
1930: Point b = new Point(a);
1931: Point c = new Point(a);
1932: Point d = new Point(a);
1933: Point e = new Point(a);
1934:
1935: Polygon bright;
1936: Polygon dark;
1937: Polygon all;
1938:
1939:
1940: int turnPoint;
1941:
1942: if (slider.getOrientation() == JSlider.HORIZONTAL)
1943: {
1944: turnPoint = thumbRect.height * 3 / 4;
1945:
1946: b.translate(thumbRect.width, 0);
1947: c.translate(thumbRect.width, turnPoint);
1948: d.translate(thumbRect.width / 2, thumbRect.height);
1949: e.translate(0, turnPoint);
1950:
1951: bright = new Polygon(new int[] { b.x, a.x, e.x, d.x },
1952: new int[] { b.y, a.y, e.y, d.y }, 4);
1953:
1954: dark = new Polygon(new int[] { b.x, c.x, d.x },
1955: new int[] { b.y, c.y, d.y }, 3);
1956: all = new Polygon(new int[] { a.x + 1, b.x, c.x, d.x, e.x + 1 },
1957: new int[] { a.y + 1, b.y + 1, c.y, d.y + 1, e.y }, 5);
1958: }
1959: else
1960: {
1961: turnPoint = thumbRect.width * 3 / 4;
1962:
1963: b.translate(turnPoint, 0);
1964: c.translate(thumbRect.width, thumbRect.height / 2);
1965: d.translate(turnPoint, thumbRect.height);
1966: e.translate(0, thumbRect.height);
1967:
1968: bright = new Polygon(new int[] { c.x, b.x, a.x, e.x },
1969: new int[] { c.y, b.y, a.y, e.y }, 4);
1970:
1971: dark = new Polygon(new int[] { c.x, d.x, e.x + 1 },
1972: new int[] { c.y, d.y, e.y }, 3);
1973:
1974: all = new Polygon(new int[] { a.x + 1, b.x, c.x - 1, d.x, e.x + 1 },
1975: new int[] { a.y + 1, b.y + 1, c.y, d.y, e.y }, 5);
1976: }
1977:
1978: g.setColor(Color.WHITE);
1979: g.drawPolyline(bright.xpoints, bright.ypoints, bright.npoints);
1980:
1981: g.setColor(Color.BLACK);
1982: g.drawPolyline(dark.xpoints, dark.ypoints, dark.npoints);
1983:
1984: g.setColor(Color.GRAY);
1985: g.fillPolygon(all);
1986:
1987: g.setColor(saved_color);
1988: }
1989:
1990:
1996: public void setThumbLocation(int x, int y)
1997: {
1998: thumbRect.x = x;
1999: thumbRect.y = y;
2000: }
2001:
2002:
2009: public void scrollByBlock(int direction)
2010: {
2011:
2012: int unit = direction * (slider.getMaximum() - slider.getMinimum()) / 10;
2013:
2014: int moveTo = slider.getValue() + unit;
2015:
2016: if (slider.getSnapToTicks())
2017: moveTo = findClosestTick(moveTo);
2018:
2019: slider.setValue(moveTo);
2020: }
2021:
2022:
2029: public void scrollByUnit(int direction)
2030: {
2031:
2032: int moveTo = slider.getValue() + direction;
2033:
2034: if (slider.getSnapToTicks())
2035: moveTo = findClosestTick(moveTo);
2036:
2037: slider.setValue(moveTo);
2038: }
2039:
2040:
2047: protected void scrollDueToClickInTrack(int dir)
2048: {
2049: scrollTimer.stop();
2050:
2051: scrollListener.setDirection(dir);
2052: scrollListener.setScrollByBlock(true);
2053:
2054: scrollTimer.start();
2055: }
2056:
2057:
2064: protected int xPositionForValue(int value)
2065: {
2066: int min = slider.getMinimum();
2067: int max = slider.getMaximum();
2068: int extent = slider.getExtent();
2069: int len = trackRect.width;
2070:
2071: int xPos = (max == min) ? 0 : (value - min) * len / (max - min);
2072:
2073: if (! drawInverted())
2074: xPos += trackRect.x;
2075: else
2076: {
2077: xPos = trackRect.width - xPos;
2078: xPos += trackRect.x;
2079: }
2080: return xPos;
2081: }
2082:
2083:
2090: protected int yPositionForValue(int value)
2091: {
2092: int min = slider.getMinimum();
2093: int max = slider.getMaximum();
2094: int extent = slider.getExtent();
2095: int len = trackRect.height;
2096:
2097: int yPos = (max == min) ? 0 : (value - min) * len / (max - min);
2098:
2099: if (! drawInverted())
2100: {
2101: yPos = trackRect.height - yPos;
2102: yPos += trackRect.y;
2103: }
2104: else
2105: yPos += trackRect.y;
2106: return yPos;
2107: }
2108:
2109:
2118: public int valueForYPosition(int yPos)
2119: {
2120: int min = slider.getMinimum();
2121: int max = slider.getMaximum();
2122: int len = trackRect.height;
2123:
2124: int value;
2125:
2126:
2127:
2128: if (len == 0)
2129: return ((max - min) / 2);
2130:
2131: if (! drawInverted())
2132: value = ((len - (yPos - trackRect.y)) * (max - min) / len + min);
2133: else
2134: value = ((yPos - trackRect.y) * (max - min) / len + min);
2135:
2136:
2137: if (value > max)
2138: value = max;
2139: else if (value < min)
2140: value = min;
2141: return value;
2142: }
2143:
2144:
2153: public int valueForXPosition(int xPos)
2154: {
2155: int min = slider.getMinimum();
2156: int max = slider.getMaximum();
2157: int len = trackRect.width;
2158:
2159: int value;
2160:
2161:
2162:
2163: if (len == 0)
2164: return ((max - min) / 2);
2165:
2166: if (! drawInverted())
2167: value = ((xPos - trackRect.x) * (max - min) / len + min);
2168: else
2169: value = ((len - (xPos - trackRect.x)) * (max - min) / len + min);
2170:
2171:
2172: if (value > max)
2173: value = max;
2174: else if (value < min)
2175: value = min;
2176: return value;
2177: }
2178:
2179:
2187: int findClosestTick(int value)
2188: {
2189: int min = slider.getMinimum();
2190: int max = slider.getMaximum();
2191: int majorSpace = slider.getMajorTickSpacing();
2192: int minorSpace = slider.getMinorTickSpacing();
2193:
2194:
2195:
2196:
2197:
2198:
2199: int minor = min - value;
2200: int major = min - value;
2201:
2202:
2203:
2204:
2205: if (majorSpace <= 0 && minorSpace <= 0)
2206: return value;
2207:
2208:
2209: if (majorSpace > 0)
2210: {
2211: int lowerBound = (value - min) / majorSpace;
2212: int majLower = majorSpace * lowerBound + min;
2213: int majHigher = majorSpace * (lowerBound + 1) + min;
2214:
2215: if (majHigher <= max && majHigher - value <= value - majLower)
2216: major = majHigher - value;
2217: else
2218: major = majLower - value;
2219: }
2220:
2221: if (minorSpace > 0)
2222: {
2223: int lowerBound = value / minorSpace;
2224: int minLower = minorSpace * lowerBound;
2225: int minHigher = minorSpace * (lowerBound + 1);
2226:
2227: if (minHigher <= max && minHigher - value <= value - minLower)
2228: minor = minHigher - value;
2229: else
2230: minor = minLower - value;
2231: }
2232:
2233:
2234: if (Math.abs(minor) > Math.abs(major))
2235: return value + major;
2236: else
2237: return value + minor;
2238: }
2239: }