001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Sergey Burlak
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.Adjustable;
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Container;
027: import java.awt.Dimension;
028: import java.awt.Graphics;
029: import java.awt.LayoutManager;
030: import java.awt.Point;
031: import java.awt.Rectangle;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.MouseAdapter;
035: import java.awt.event.MouseEvent;
036: import java.awt.event.MouseMotionListener;
037: import java.beans.PropertyChangeEvent;
038: import java.beans.PropertyChangeListener;
039:
040: import javax.swing.AbstractAction;
041: import javax.swing.Action;
042: import javax.swing.BoundedRangeModel;
043: import javax.swing.JButton;
044: import javax.swing.JComponent;
045: import javax.swing.JScrollBar;
046: import javax.swing.LookAndFeel;
047: import javax.swing.SwingConstants;
048: import javax.swing.SwingUtilities;
049: import javax.swing.Timer;
050: import javax.swing.UIManager;
051: import javax.swing.event.ChangeEvent;
052: import javax.swing.event.ChangeListener;
053: import javax.swing.plaf.ComponentUI;
054: import javax.swing.plaf.ScrollBarUI;
055: import javax.swing.plaf.UIResource;
056:
057: import org.apache.harmony.x.swing.StringConstants;
058: import org.apache.harmony.x.swing.Utilities;
059:
060: public class BasicScrollBarUI extends ScrollBarUI implements
061: LayoutManager, SwingConstants {
062:
063: protected class TrackListener extends MouseAdapter implements
064: MouseMotionListener {
065: protected transient int offset;
066: protected transient int currentMouseX;
067: protected transient int currentMouseY;
068:
069: private Timer trackTimer;
070: private boolean inThumb;
071: private int currentModelValue;
072:
073: protected TrackListener() {
074: trackTimer = new Timer(150, new ActionListener() {
075: public void actionPerformed(final ActionEvent e) {
076: Point current = new Point(getThumbBounds().x,
077: getThumbBounds().y);
078: Point next = new Point(currentMouseX, currentMouseY);
079: int dir = orientationStrategy.calculateDirection(
080: current, next);
081: if (dir > 0) {
082: trackHighlight = INCREASE_HIGHLIGHT;
083: } else {
084: trackHighlight = DECREASE_HIGHLIGHT;
085: }
086:
087: if (!getThumbBounds().contains(currentMouseX,
088: currentMouseY)) {
089: scrollByBlock(dir);
090: }
091: }
092: });
093:
094: trackTimer.setInitialDelay(350);
095: }
096:
097: public void mouseReleased(final MouseEvent e) {
098: if (trackTimer.isRunning()) {
099: trackTimer.stop();
100: }
101: trackHighlight = NO_HIGHLIGHT;
102: scrollbar.getModel().setValueIsAdjusting(false);
103: scrollbar.repaint();
104: }
105:
106: public void mousePressed(final MouseEvent e) {
107: currentMouseX = e.getX();
108: currentMouseY = e.getY();
109:
110: currentModelValue = scrollbar.getValue();
111:
112: inThumb = getThumbBounds().contains(currentMouseX,
113: currentMouseY);
114: if (!inThumb
115: && trackRect.contains(currentMouseX, currentMouseY)
116: && SwingUtilities.isLeftMouseButton(e)) {
117:
118: Point currentPoint = new Point(getThumbBounds().x,
119: getThumbBounds().y);
120: int dir = orientationStrategy.calculateDirection(
121: currentPoint, e.getPoint());
122:
123: if (dir > 0) {
124: trackHighlight = INCREASE_HIGHLIGHT;
125: } else {
126: trackHighlight = DECREASE_HIGHLIGHT;
127: }
128: scrollByBlock(dir);
129: trackTimer.start();
130: }
131: }
132:
133: public void mouseDragged(final MouseEvent e) {
134: if (inThumb && SwingUtilities.isLeftMouseButton(e)) {
135: scrollbar.getModel().setValueIsAdjusting(true);
136: orientationStrategy
137: .setValueOnDragging(e, currentMouseX,
138: currentMouseY, currentModelValue);
139: scrollbar.repaint();
140: }
141:
142: if (trackTimer.isRunning()
143: && getTrackBounds().contains(e.getX(), e.getY())) {
144: currentMouseX = e.getX();
145: currentMouseY = e.getY();
146: }
147: }
148:
149: public void mouseMoved(final MouseEvent e) {
150: }
151:
152: public void mouseExited(final MouseEvent e) {
153: }
154: }
155:
156: protected class ScrollListener implements ActionListener {
157: private int dir;
158: private boolean block;
159:
160: public ScrollListener() {
161: this (1, false);
162: }
163:
164: public ScrollListener(final int dir, final boolean block) {
165: this .dir = dir;
166: this .block = block;
167: }
168:
169: public void setDirection(final int dir) {
170: this .dir = dir;
171: }
172:
173: public void setScrollByBlock(final boolean block) {
174: this .block = block;
175: }
176:
177: public void actionPerformed(final ActionEvent e) {
178: if (block) {
179: scrollByBlock(dir);
180: } else {
181: scrollByUnit(dir);
182: }
183: scrollbar.repaint();
184: }
185: }
186:
187: protected class ModelListener implements ChangeListener {
188: public void stateChanged(final ChangeEvent e) {
189: scrollbar.revalidate();
190: scrollbar.repaint();
191: }
192: }
193:
194: protected class ArrowButtonListener extends MouseAdapter {
195: private int direction;
196:
197: public void mousePressed(final MouseEvent e) {
198: if (scrollbar.getOrientation() == Adjustable.HORIZONTAL
199: && !scrollbar.getComponentOrientation()
200: .isLeftToRight()) {
201: direction = (e.getSource() == incrButton) ? -1 : 1;
202: } else {
203: direction = (e.getSource() == incrButton) ? 1 : -1;
204: }
205: scrollListener.setDirection(direction);
206: scrollByUnit(direction);
207: scrollTimer.start();
208: }
209:
210: public void mouseReleased(final MouseEvent e) {
211: scrollTimer.stop();
212: }
213: }
214:
215: public class PropertyChangeHandler implements
216: PropertyChangeListener {
217: public void propertyChange(final PropertyChangeEvent e) {
218: String propertyName = e.getPropertyName();
219: if (StringConstants.COMPONENT_ORIENTATION
220: .equals(propertyName)) {
221: uninstallKeyboardActions();
222: installKeyboardActions();
223: } else if (StringConstants.MODEL_PROPERTY_CHANGED
224: .equals(propertyName)) {
225: BoundedRangeModel oldValue = (BoundedRangeModel) e
226: .getOldValue();
227: if (oldValue != null) {
228: oldValue.removeChangeListener(modelListener);
229: }
230:
231: BoundedRangeModel newValue = (BoundedRangeModel) e
232: .getNewValue();
233: if (newValue != null) {
234: newValue.addChangeListener(modelListener);
235: }
236: }
237:
238: if (scrollbar != null) {
239: scrollbar.revalidate();
240: scrollbar.repaint();
241: }
242: }
243: }
244:
245: protected static final int DECREASE_HIGHLIGHT = 1;
246: protected static final int INCREASE_HIGHLIGHT = 2;
247: protected static final int NO_HIGHLIGHT = 0;
248:
249: protected PropertyChangeListener propertyChangeListener;
250: protected ArrowButtonListener buttonListener;
251: protected ModelListener modelListener;
252: protected ScrollListener scrollListener;
253: protected TrackListener trackListener;
254:
255: protected JButton decrButton;
256: protected JButton incrButton;
257:
258: protected boolean isDragging;
259:
260: protected Dimension maximumThumbSize;
261: protected Dimension minimumThumbSize;
262:
263: protected JScrollBar scrollbar;
264: protected Timer scrollTimer;
265: protected Color trackHighlightColor;
266: protected Color trackColor;
267: protected Color thumbColor;
268: protected Color thumbDarkShadowColor;
269: protected Color thumbHighlightColor;
270: protected Color thumbLightShadowColor;
271: protected Rectangle thumbRect;
272: protected int trackHighlight = NO_HIGHLIGHT;
273: protected Rectangle trackRect;
274:
275: private int defaultButtonSize;
276: private OrientationStrategy orientationStrategy;
277:
278: public static ComponentUI createUI(final JComponent c) {
279: return new BasicScrollBarUI();
280: }
281:
282: public void addLayoutComponent(final String s, final Component c) {
283: }
284:
285: public void removeLayoutComponent(final Component c) {
286: }
287:
288: public Dimension minimumLayoutSize(final Container c) {
289: return getMinimumSize(scrollbar);
290: }
291:
292: protected void configureScrollBarColors() {
293: if (scrollbar == null)
294: throw new NullPointerException();
295:
296: if ((thumbColor == null) || (thumbColor instanceof UIResource)) {
297: thumbColor = UIManager.getColor("ScrollBar.thumb");
298: }
299: if ((thumbDarkShadowColor == null)
300: || (thumbDarkShadowColor instanceof UIResource)) {
301: thumbDarkShadowColor = UIManager
302: .getColor("ScrollBar.thumbDarkShadow");
303: }
304: if ((thumbHighlightColor == null)
305: || (thumbHighlightColor instanceof UIResource)) {
306: thumbHighlightColor = UIManager
307: .getColor("ScrollBar.thumbHighlight");
308: }
309: if ((thumbLightShadowColor == null)
310: || (thumbLightShadowColor instanceof UIResource)) {
311: thumbLightShadowColor = UIManager
312: .getColor("ScrollBar.thumbShadow");
313: }
314: if ((trackColor == null) || (trackColor instanceof UIResource)) {
315: trackColor = UIManager.getColor("ScrollBar.track");
316: }
317: if ((trackHighlightColor == null)
318: || (trackHighlightColor instanceof UIResource)) {
319: trackHighlightColor = UIManager
320: .getColor("ScrollBar.trackHighlight");
321: }
322: }
323:
324: protected ArrowButtonListener createArrowButtonListener() {
325: return new ArrowButtonListener();
326: }
327:
328: protected JButton createDecreaseButton(final int orient) {
329: return new BasicArrowButton(orient);
330: }
331:
332: protected JButton createIncreaseButton(final int orient) {
333: return new BasicArrowButton(orient);
334: }
335:
336: protected ModelListener createModelListener() {
337: return new ModelListener();
338: }
339:
340: protected PropertyChangeListener createPropertyChangeListener() {
341: return new PropertyChangeHandler();
342: }
343:
344: protected BasicScrollBarUI.ScrollListener createScrollListener() {
345: return new ScrollListener();
346: }
347:
348: protected BasicScrollBarUI.TrackListener createTrackListener() {
349: return new TrackListener();
350: }
351:
352: public Dimension getMaximumSize(final JComponent c) {
353: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
354: }
355:
356: protected Dimension getMaximumThumbSize() {
357: return maximumThumbSize;
358: }
359:
360: public Dimension getMinimumSize(final JComponent c) {
361: return orientationStrategy.getPreferredSize(c);
362: }
363:
364: protected Dimension getMinimumThumbSize() {
365: return minimumThumbSize;
366: }
367:
368: public Dimension getPreferredSize(final JComponent c) {
369: return orientationStrategy.getPreferredSize(c);
370: }
371:
372: protected Rectangle getThumbBounds() {
373: return thumbRect;
374: }
375:
376: protected Rectangle getTrackBounds() {
377: return trackRect;
378: }
379:
380: protected void installComponents() {
381: incrButton = orientationStrategy.createIncreaseButton();
382: scrollbar.add(incrButton);
383:
384: decrButton = orientationStrategy.createDecreaseButton();
385: scrollbar.add(decrButton);
386: }
387:
388: protected void installDefaults() {
389: thumbRect = new Rectangle();
390: trackRect = new Rectangle();
391: LookAndFeel.installColors(scrollbar, "ScrollBar.background",
392: "ScrollBar.foreground");
393: LookAndFeel.installProperty(scrollbar, "opaque", Boolean.TRUE);
394: configureScrollBarColors();
395:
396: defaultButtonSize = UIManager.getInt("ScrollBar.width");
397: if ((maximumThumbSize == null)
398: || (maximumThumbSize instanceof UIResource)) {
399: maximumThumbSize = UIManager
400: .getDimension("ScrollBar.maximumThumbSize");
401: }
402: if ((minimumThumbSize == null)
403: || (minimumThumbSize instanceof UIResource)) {
404: minimumThumbSize = UIManager
405: .getDimension("ScrollBar.minimumThumbSize");
406: }
407: scrollbar.setLayout(this );
408: }
409:
410: protected void installKeyboardActions() {
411: Utilities.installKeyboardActions(scrollbar,
412: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
413: "ScrollBar.ancestorInputMap",
414: "ScrollBar.ancestorInputMap.RightToLeft");
415:
416: scrollbar.getActionMap().put("positiveUnitIncrement",
417: newPositiveUnitIncrementAction());
418: scrollbar.getActionMap().put("positiveBlockIncrement",
419: newPositiveBlockIncrementAction());
420: scrollbar.getActionMap().put("negativeUnitIncrement",
421: newNegativeUnitIncrementAction());
422: scrollbar.getActionMap().put("negativeBlockIncrement",
423: newNegativeBlockIncrementAction());
424: scrollbar.getActionMap().put("minScroll", newMinScrollAction());
425: scrollbar.getActionMap().put("maxScroll", newMaxScrollAction());
426: }
427:
428: protected void installListeners() {
429: propertyChangeListener = createPropertyChangeListener();
430: scrollbar.addPropertyChangeListener(propertyChangeListener);
431:
432: modelListener = createModelListener();
433: scrollbar.getModel().addChangeListener(modelListener);
434:
435: trackListener = createTrackListener();
436: scrollbar.addMouseListener(trackListener);
437: scrollbar.addMouseMotionListener(trackListener);
438:
439: scrollListener = createScrollListener();
440: scrollTimer = new Timer(150, scrollListener);
441: scrollTimer.setInitialDelay(450);
442:
443: buttonListener = createArrowButtonListener();
444: incrButton.addMouseListener(buttonListener);
445: decrButton.addMouseListener(buttonListener);
446: }
447:
448: public void installUI(final JComponent c) {
449: scrollbar = (JScrollBar) c;
450:
451: if (scrollbar.getOrientation() == HORIZONTAL) {
452: orientationStrategy = new HorizontalStrategy();
453: } else if (scrollbar.getOrientation() == VERTICAL) {
454: orientationStrategy = new VerticalStrategy();
455: }
456:
457: installDefaults();
458: installComponents();
459: installListeners();
460: installKeyboardActions();
461: }
462:
463: public void uninstallUI(final JComponent c) {
464: scrollbar = (JScrollBar) c;
465:
466: uninstallDefaults();
467: uninstallListeners();
468: uninstallComponents();
469: uninstallKeyboardActions();
470: }
471:
472: public void layoutContainer(final Container c) {
473: JScrollBar bar = ((JScrollBar) c);
474: BoundedRangeModel model = bar.getModel();
475: orientationStrategy.layoutScrollBar(bar);
476: orientationStrategy.calculateTrackBounds(model);
477: orientationStrategy.calculateThumbBounds(model);
478: }
479:
480: protected void layoutHScrollbar(final JScrollBar bar) {
481: incrButton.setBounds(bar.getWidth() - defaultButtonSize, 0,
482: defaultButtonSize, scrollbar.getHeight());
483: decrButton.setBounds(0, 0, defaultButtonSize, scrollbar
484: .getHeight());
485: }
486:
487: protected void layoutVScrollbar(final JScrollBar bar) {
488: incrButton.setBounds(0, bar.getHeight() - defaultButtonSize,
489: scrollbar.getWidth(), defaultButtonSize);
490: decrButton.setBounds(0, 0, scrollbar.getWidth(),
491: defaultButtonSize);
492: }
493:
494: public void paint(final Graphics g, final JComponent c) {
495: paintTrack(g, c, getTrackBounds());
496: paintThumb(g, c, getThumbBounds());
497: if (trackHighlight == INCREASE_HIGHLIGHT) {
498: paintIncreaseHighlight(g);
499: } else if (trackHighlight == DECREASE_HIGHLIGHT) {
500: paintDecreaseHighlight(g);
501: }
502: }
503:
504: protected void paintDecreaseHighlight(final Graphics g) {
505: orientationStrategy.paintDecreaseHighlight(g);
506: }
507:
508: protected void paintIncreaseHighlight(final Graphics g) {
509: orientationStrategy.paintIncreaseHighlight(g);
510: }
511:
512: protected void paintThumb(final Graphics g, final JComponent c,
513: final Rectangle r) {
514: Color oldColor = g.getColor();
515:
516: g.setColor(thumbColor);
517: g.fillRect(r.x, r.y, r.width, r.height);
518: g.setColor(Color.WHITE);
519: g.drawRect(r.x + 1, r.y + 1, r.width - 1, r.height - 1);
520: g.setColor(thumbDarkShadowColor);
521: g.drawPolyline(new int[] { r.x, r.x + r.width, r.x + r.width },
522: new int[] { r.y + r.height, r.y + r.height, r.y }, 3);
523: g.setColor(thumbLightShadowColor);
524: g.drawPolyline(new int[] { r.x + 1, r.x + r.width - 1,
525: r.x + r.width - 1 }, new int[] { r.y + r.height - 1,
526: r.y + r.height - 1, r.y + 1 }, 3);
527:
528: g.setColor(oldColor);
529: }
530:
531: protected void paintTrack(final Graphics g, final JComponent c,
532: final Rectangle r) {
533: Color oldColor = g.getColor();
534:
535: g.setColor(trackColor);
536: g.fillRect(r.x, r.y, r.width, r.height);
537:
538: g.setColor(oldColor);
539: }
540:
541: public Dimension preferredLayoutSize(final Container c) {
542: if (c instanceof JComponent) {
543: return getPreferredSize((JComponent) c);
544: } else {
545: return new Dimension(0, 0);
546: }
547: }
548:
549: protected void scrollByBlock(final int dir) {
550: updateScrollBarValue(scrollbar.getBlockIncrement(dir) * dir);
551: }
552:
553: protected void scrollByUnit(final int dir) {
554: updateScrollBarValue(scrollbar.getUnitIncrement(dir) * dir);
555: }
556:
557: protected void setThumbBounds(final int x, final int y,
558: final int w, final int h) {
559: thumbRect.setBounds(x, y, w, h);
560: }
561:
562: protected void uninstallComponents() {
563: scrollbar.remove(incrButton);
564: scrollbar.remove(decrButton);
565: incrButton = null;
566: decrButton = null;
567: }
568:
569: protected void uninstallDefaults() {
570: Utilities.uninstallColorsAndFont(scrollbar);
571: }
572:
573: protected void uninstallKeyboardActions() {
574: Utilities.uninstallKeyboardActions(scrollbar,
575: JComponent.WHEN_FOCUSED);
576: }
577:
578: protected void uninstallListeners() {
579: scrollbar.removePropertyChangeListener(propertyChangeListener);
580: propertyChangeListener = null;
581:
582: scrollbar.getModel().removeChangeListener(modelListener);
583: modelListener = null;
584:
585: scrollbar.removeMouseListener(trackListener);
586: scrollbar.removeMouseMotionListener(trackListener);
587: trackListener = null;
588:
589: scrollListener = null;
590: scrollTimer = null;
591:
592: incrButton.removeMouseListener(buttonListener);
593: decrButton.removeMouseListener(buttonListener);
594: buttonListener = null;
595: }
596:
597: private Action newPositiveUnitIncrementAction() {
598: return new AbstractAction() {
599: public void actionPerformed(final ActionEvent e) {
600: scrollByUnit(1);
601: }
602: };
603: }
604:
605: private Action newNegativeUnitIncrementAction() {
606: return new AbstractAction() {
607: public void actionPerformed(final ActionEvent e) {
608: scrollByUnit(-1);
609: }
610: };
611: }
612:
613: private Action newPositiveBlockIncrementAction() {
614: return new AbstractAction() {
615: public void actionPerformed(final ActionEvent e) {
616: scrollByBlock(1);
617: }
618: };
619: }
620:
621: private Action newNegativeBlockIncrementAction() {
622: return new AbstractAction() {
623: public void actionPerformed(final ActionEvent e) {
624: scrollByBlock(-1);
625: }
626: };
627: }
628:
629: private Action newMaxScrollAction() {
630: return new AbstractAction() {
631: public void actionPerformed(final ActionEvent e) {
632: scrollbar.setValue(scrollbar.getModel().getMaximum());
633: scrollbar.repaint();
634: }
635: };
636: }
637:
638: private Action newMinScrollAction() {
639: return new AbstractAction() {
640: public void actionPerformed(final ActionEvent e) {
641: scrollbar.setValue(scrollbar.getModel().getMinimum());
642: scrollbar.repaint();
643: }
644: };
645: }
646:
647: private void updateScrollBarValue(final int increment) {
648: scrollbar.setValue(scrollbar.getValue() + increment);
649: orientationStrategy.calculateThumbBounds(scrollbar.getModel());
650: scrollbar.repaint();
651: }
652:
653: private abstract class OrientationStrategy {
654: abstract void calculateTrackBounds(BoundedRangeModel model);
655:
656: abstract int calculateDirection(Point current, Point next);
657:
658: abstract JButton createIncreaseButton();
659:
660: abstract JButton createDecreaseButton();
661:
662: abstract void layoutScrollBar(JScrollBar bar);
663:
664: abstract Dimension getPreferredSize(JComponent c);
665:
666: abstract void paintIncreaseHighlight(Graphics g);
667:
668: abstract void paintDecreaseHighlight(Graphics g);
669:
670: abstract int getButtonSize(JButton b);
671:
672: abstract Rectangle newThumbBounds(int offset, int size);
673:
674: abstract int getOffset(MouseEvent e, int currentMouseX,
675: int currentMouseY);
676:
677: abstract int getTrackSize();
678:
679: abstract int getThumbSize(int proposedSize);
680:
681: void calculateThumbBounds(final BoundedRangeModel model) {
682: int extent = model.getExtent();
683: int viewSize = model.getMaximum() - model.getMinimum();
684: if (viewSize == 0) {
685: setThumbBounds(0, 0, 0, 0);
686: return;
687: }
688: int proposedThumbSize = (int) ((float) getTrackSize()
689: * extent / viewSize);
690: int thumbSize = getThumbSize(proposedThumbSize);
691: int availableTrackSize = getTrackSize() - thumbSize;
692: if (availableTrackSize <= 0) {
693: if (proposedThumbSize == thumbSize) {
694: Rectangle trackBounds = getTrackBounds();
695: setThumbBounds(trackBounds.x, trackBounds.y,
696: trackBounds.width, trackBounds.height);
697: } else {
698: setThumbBounds(0, 0, 0, 0);
699: }
700: } else {
701: int availableScrollingSize = viewSize - extent;
702: int offset = (availableScrollingSize > 0 ? availableTrackSize
703: * model.getValue() / availableScrollingSize
704: : 0)
705: + getButtonSize(decrButton);
706: Rectangle newThumbBounds = newThumbBounds(offset,
707: thumbSize);
708: setThumbBounds(newThumbBounds.x, newThumbBounds.y,
709: newThumbBounds.width, newThumbBounds.height);
710: trackListener.offset = offset;
711: }
712: }
713:
714: void setValueOnDragging(final MouseEvent e,
715: final int currentMouseX, final int currentMouseY,
716: final int initialModelValue) {
717: BoundedRangeModel model = scrollbar.getModel();
718: int extent = model.getExtent();
719: int viewSize = model.getMaximum() - model.getMinimum();
720: int availableScrollingSize = viewSize - extent;
721: int thumbSize = getThumbSize(Math.round(getTrackSize()
722: * extent / viewSize));
723: int availableTrackSize = getTrackSize() - thumbSize;
724: int offset = getOffset(e, currentMouseX, currentMouseY);
725: int modelIncrement = availableTrackSize != 0 ? offset
726: * availableScrollingSize / availableTrackSize : 0;
727: model.setValue(initialModelValue + modelIncrement);
728: }
729: }
730:
731: private class HorizontalStrategy extends OrientationStrategy {
732: void calculateTrackBounds(final BoundedRangeModel model) {
733: int visible = scrollbar.getWidth() - incrButton.getWidth()
734: - decrButton.getWidth();
735: trackRect.setBounds(decrButton.getWidth(), 0, visible,
736: scrollbar.getHeight());
737: }
738:
739: int getThumbSize(final int proposedSize) {
740: if (proposedSize < minimumThumbSize.width) {
741: return minimumThumbSize.width;
742: } else if (proposedSize > maximumThumbSize.width) {
743: return maximumThumbSize.width;
744: } else {
745: return proposedSize;
746: }
747: }
748:
749: JButton createIncreaseButton() {
750: return BasicScrollBarUI.this .createIncreaseButton(EAST);
751: }
752:
753: JButton createDecreaseButton() {
754: return BasicScrollBarUI.this .createDecreaseButton(WEST);
755: }
756:
757: void layoutScrollBar(final JScrollBar bar) {
758: layoutHScrollbar(bar);
759: }
760:
761: int calculateDirection(final Point current, final Point next) {
762: if (scrollbar.getComponentOrientation().isLeftToRight()) {
763: return (next.x > current.x) ? 1 : -1;
764: } else {
765: return (next.x > current.x) ? -1 : 1;
766: }
767: }
768:
769: Dimension getPreferredSize(final JComponent c) {
770: final int width = incrButton.getSize().width
771: + decrButton.getSize().width
772: + getThumbBounds().width;
773: final int height = Math.max(
774: incrButton.getPreferredSize().height, decrButton
775: .getPreferredSize().height);
776: return new Dimension(width, height);
777: }
778:
779: void paintIncreaseHighlight(final Graphics g) {
780: Color oldColor = g.getColor();
781:
782: g.setColor(trackHighlightColor);
783: g
784: .fillRect(
785: getThumbBounds().x + getThumbBounds().width
786: + 1,
787: 0,
788: getTrackBounds().width
789: - (getThumbBounds().x + getThumbBounds().width)
790: + getButtonSize(incrButton),
791: getTrackBounds().height - 1);
792:
793: g.setColor(oldColor);
794: }
795:
796: int getTrackSize() {
797: return getTrackBounds().width;
798: }
799:
800: void paintDecreaseHighlight(final Graphics g) {
801: Color oldColor = g.getColor();
802:
803: g.setColor(trackHighlightColor);
804: g.fillRect(getTrackBounds().x, 0, getThumbBounds().x
805: - decrButton.getWidth(),
806: getTrackBounds().height - 1);
807:
808: g.setColor(oldColor);
809: }
810:
811: int getButtonSize(final JButton b) {
812: return b.getWidth();
813: }
814:
815: Rectangle newThumbBounds(final int offset, final int size) {
816: if (scrollbar.getComponentOrientation().isLeftToRight()) {
817: return new Rectangle(offset, 0, size, scrollbar
818: .getHeight());
819: } else {
820: return new Rectangle(getTrackSize()
821: + getButtonSize(incrButton)
822: - (offset - getButtonSize(decrButton)) - size,
823: 0, size, scrollbar.getHeight());
824: }
825: }
826:
827: int getOffset(final MouseEvent e, final int currentMouseX,
828: final int currentMouseY) {
829: if (scrollbar.getComponentOrientation().isLeftToRight()) {
830: return e.getX() - currentMouseX;
831: } else {
832: return currentMouseX - e.getX();
833: }
834: }
835: }
836:
837: private class VerticalStrategy extends OrientationStrategy {
838: void calculateTrackBounds(final BoundedRangeModel model) {
839: int visible = scrollbar.getHeight()
840: - incrButton.getHeight() - decrButton.getHeight();
841: trackRect.setBounds(0, decrButton.getWidth(), scrollbar
842: .getWidth(), visible);
843: }
844:
845: int getThumbSize(final int proposedSize) {
846: if (proposedSize < minimumThumbSize.height) {
847: return minimumThumbSize.height;
848: } else if (proposedSize > maximumThumbSize.height) {
849: return maximumThumbSize.height;
850: } else {
851: return proposedSize;
852: }
853: }
854:
855: JButton createIncreaseButton() {
856: return BasicScrollBarUI.this .createIncreaseButton(SOUTH);
857: }
858:
859: JButton createDecreaseButton() {
860: return BasicScrollBarUI.this .createDecreaseButton(NORTH);
861: }
862:
863: void layoutScrollBar(final JScrollBar bar) {
864: layoutVScrollbar(bar);
865: }
866:
867: int calculateDirection(final Point current, final Point next) {
868: return (next.y > current.y) ? 1 : -1;
869: }
870:
871: public Dimension getPreferredSize(final JComponent c) {
872: final int width = Math.max(
873: incrButton.getPreferredSize().width, decrButton
874: .getPreferredSize().width);
875: final int height = incrButton.getSize().height
876: + decrButton.getSize().height
877: + getThumbBounds().height;
878: return new Dimension(width, height);
879: }
880:
881: int getTrackSize() {
882: return getTrackBounds().height;
883: }
884:
885: void paintIncreaseHighlight(final Graphics g) {
886: Color oldColor = g.getColor();
887:
888: g.setColor(trackHighlightColor);
889: g
890: .fillRect(
891: 0,
892: getThumbBounds().y
893: + getThumbBounds().height + 1,
894: getTrackBounds().width,
895: getTrackBounds().height
896: - (getThumbBounds().y + getThumbBounds().height)
897: + getButtonSize(incrButton));
898:
899: g.setColor(oldColor);
900: }
901:
902: void paintDecreaseHighlight(final Graphics g) {
903: Color oldColor = g.getColor();
904:
905: g.setColor(trackHighlightColor);
906: g.fillRect(0, getTrackBounds().y, getTrackBounds().width,
907: getThumbBounds().y - decrButton.getHeight());
908:
909: g.setColor(oldColor);
910: }
911:
912: int getButtonSize(final JButton b) {
913: return b.getHeight();
914: }
915:
916: Rectangle newThumbBounds(final int offset, final int size) {
917: return new Rectangle(0, offset, scrollbar.getWidth(), size);
918: }
919:
920: int getOffset(final MouseEvent e, final int currentMouseX,
921: final int currentMouseY) {
922: return e.getY() - currentMouseY;
923: }
924: }
925: }
|