< prev index next >
src/java.desktop/share/classes/javax/swing/plaf/basic/BasicScrollBarUI.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 58,96 ****
--- 58,120 ----
private static final int MIN_SCROLL = 2;
private static final int MAX_SCROLL = 3;
// NOTE: DO NOT use this field directly, SynthScrollBarUI assumes you'll
// call getMinimumThumbSize to access it.
+ /** Minimum thumb size */
protected Dimension minimumThumbSize;
+ /** Maximum thumb size */
protected Dimension maximumThumbSize;
+ /** Thumb highlight color */
protected Color thumbHighlightColor;
+ /** Thumb light shadow color */
protected Color thumbLightShadowColor;
+ /** Thumb dark shadow color */
protected Color thumbDarkShadowColor;
+ /** Thumb color */
protected Color thumbColor;
+ /** Track color */
protected Color trackColor;
+ /** Track highlight color */
protected Color trackHighlightColor;
+ /** Scrollbar */
protected JScrollBar scrollbar;
+ /** Increment button */
protected JButton incrButton;
+ /** Decrement button */
protected JButton decrButton;
+ /** Dragging */
protected boolean isDragging;
+ /** Track listener */
protected TrackListener trackListener;
+ /** Button listener */
protected ArrowButtonListener buttonListener;
+ /** Model listener */
protected ModelListener modelListener;
+ /** Thumb rectangle */
protected Rectangle thumbRect;
+ /** Track rectangle */
protected Rectangle trackRect;
+ /** Track highlight */
protected int trackHighlight;
+ /** No highlight */
protected static final int NO_HIGHLIGHT = 0;
+ /** Decrease highlight */
protected static final int DECREASE_HIGHLIGHT = 1;
+ /** Increase highlight */
protected static final int INCREASE_HIGHLIGHT = 2;
+ /** Scroll listener */
protected ScrollListener scrollListener;
+ /** Property change listener */
protected PropertyChangeListener propertyChangeListener;
+ /** Scroll timer */
protected Timer scrollTimer;
private final static int scrollSpeedThrottle = 60; // delay in milli seconds
/**
*** 146,161 ****
map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT));
map.put(new Actions(Actions.MIN_SCROLL));
map.put(new Actions(Actions.MAX_SCROLL));
}
!
public static ComponentUI createUI(JComponent c) {
return new BasicScrollBarUI();
}
!
protected void configureScrollBarColors()
{
LookAndFeel.installColors(scrollbar, "ScrollBar.background",
"ScrollBar.foreground");
thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
--- 170,191 ----
map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT));
map.put(new Actions(Actions.MIN_SCROLL));
map.put(new Actions(Actions.MAX_SCROLL));
}
! /**
! * Creates the UI.
! * @param c the component
! * @return the UI
! */
public static ComponentUI createUI(JComponent c) {
return new BasicScrollBarUI();
}
! /**
! * Configures the scroll bar colors.
! */
protected void configureScrollBarColors()
{
LookAndFeel.installColors(scrollbar, "ScrollBar.background",
"ScrollBar.foreground");
thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
*** 164,184 ****
thumbColor = UIManager.getColor("ScrollBar.thumb");
trackColor = UIManager.getColor("ScrollBar.track");
trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
}
!
public void installUI(JComponent c) {
scrollbar = (JScrollBar)c;
thumbRect = new Rectangle(0, 0, 0, 0);
trackRect = new Rectangle(0, 0, 0, 0);
installDefaults();
installComponents();
installListeners();
installKeyboardActions();
}
public void uninstallUI(JComponent c) {
scrollbar = (JScrollBar)c;
uninstallListeners();
uninstallDefaults();
uninstallComponents();
--- 194,221 ----
thumbColor = UIManager.getColor("ScrollBar.thumb");
trackColor = UIManager.getColor("ScrollBar.track");
trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
}
! /**
! * Installs the UI.
! * @param c the component
! */
public void installUI(JComponent c) {
scrollbar = (JScrollBar)c;
thumbRect = new Rectangle(0, 0, 0, 0);
trackRect = new Rectangle(0, 0, 0, 0);
installDefaults();
installComponents();
installListeners();
installKeyboardActions();
}
+ /**
+ * Uninstalls the UI.
+ * @param c the component
+ */
public void uninstallUI(JComponent c) {
scrollbar = (JScrollBar)c;
uninstallListeners();
uninstallDefaults();
uninstallComponents();
*** 187,197 ****
scrollbar = null;
incrButton = null;
decrButton = null;
}
!
protected void installDefaults()
{
scrollBarWidth = UIManager.getInt("ScrollBar.width");
if (scrollBarWidth <= 0) {
scrollBarWidth = 16;
--- 224,236 ----
scrollbar = null;
incrButton = null;
decrButton = null;
}
! /**
! * Installs the defaults.
! */
protected void installDefaults()
{
scrollBarWidth = UIManager.getInt("ScrollBar.width");
if (scrollBarWidth <= 0) {
scrollBarWidth = 16;
*** 238,248 ****
decrGap *= 0.714;
}
}
}
!
protected void installComponents(){
switch (scrollbar.getOrientation()) {
case JScrollBar.VERTICAL:
incrButton = createIncreaseButton(SOUTH);
decrButton = createDecreaseButton(NORTH);
--- 277,289 ----
decrGap *= 0.714;
}
}
}
! /**
! * Installs the components.
! */
protected void installComponents(){
switch (scrollbar.getOrientation()) {
case JScrollBar.VERTICAL:
incrButton = createIncreaseButton(SOUTH);
decrButton = createDecreaseButton(NORTH);
*** 262,277 ****
scrollbar.add(decrButton);
// Force the children's enabled state to be updated.
scrollbar.setEnabled(scrollbar.isEnabled());
}
protected void uninstallComponents(){
scrollbar.remove(incrButton);
scrollbar.remove(decrButton);
}
!
protected void installListeners(){
trackListener = createTrackListener();
buttonListener = createArrowButtonListener();
modelListener = createModelListener();
propertyChangeListener = createPropertyChangeListener();
--- 303,323 ----
scrollbar.add(decrButton);
// Force the children's enabled state to be updated.
scrollbar.setEnabled(scrollbar.isEnabled());
}
+ /**
+ * Uninstalls the components.
+ */
protected void uninstallComponents(){
scrollbar.remove(incrButton);
scrollbar.remove(decrButton);
}
! /**
! * Installs the listeners.
! */
protected void installListeners(){
trackListener = createTrackListener();
buttonListener = createArrowButtonListener();
modelListener = createModelListener();
propertyChangeListener = createPropertyChangeListener();
*** 292,302 ****
scrollListener = createScrollListener();
scrollTimer = new Timer(scrollSpeedThrottle, scrollListener);
scrollTimer.setInitialDelay(300); // default InitialDelay?
}
!
protected void installKeyboardActions(){
LazyActionMap.installLazyActionMap(scrollbar, BasicScrollBarUI.class,
"ScrollBar.actionMap");
InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
--- 338,350 ----
scrollListener = createScrollListener();
scrollTimer = new Timer(scrollSpeedThrottle, scrollListener);
scrollTimer.setInitialDelay(300); // default InitialDelay?
}
! /**
! * Installs the keyboard actions.
! */
protected void installKeyboardActions(){
LazyActionMap.installLazyActionMap(scrollbar, BasicScrollBarUI.class,
"ScrollBar.actionMap");
InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
*** 305,314 ****
--- 353,365 ----
inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
SwingUtilities.replaceUIInputMap(scrollbar,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
}
+ /**
+ * Uninstalls the keyboard actions.
+ */
protected void uninstallKeyboardActions(){
SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED,
null);
SwingUtilities.replaceUIActionMap(scrollbar, null);
}
*** 341,351 ****
}
}
return null;
}
!
protected void uninstallListeners() {
scrollTimer.stop();
scrollTimer = null;
if (decrButton != null){
--- 392,404 ----
}
}
return null;
}
! /**
! * Uninstall the listeners.
! */
protected void uninstallListeners() {
scrollTimer.stop();
scrollTimer = null;
if (decrButton != null){
*** 361,371 ****
scrollbar.removePropertyChangeListener(propertyChangeListener);
scrollbar.removeFocusListener(getHandler());
handler = null;
}
!
protected void uninstallDefaults(){
LookAndFeel.uninstallBorder(scrollbar);
if (scrollbar.getLayout() == this) {
scrollbar.setLayout(null);
}
--- 414,426 ----
scrollbar.removePropertyChangeListener(propertyChangeListener);
scrollbar.removeFocusListener(getHandler());
handler = null;
}
! /**
! * Uninstalls the defaults.
! */
protected void uninstallDefaults(){
LookAndFeel.uninstallBorder(scrollbar);
if (scrollbar.getLayout() == this) {
scrollbar.setLayout(null);
}
*** 377,402 ****
--- 432,477 ----
handler = new Handler();
}
return handler;
}
+ /**
+ * Creates a track listener.
+ * @return a track listener
+ */
protected TrackListener createTrackListener(){
return new TrackListener();
}
+ /**
+ * Creates an arrow button listener.
+ * @return an arrow button listener
+ */
protected ArrowButtonListener createArrowButtonListener(){
return new ArrowButtonListener();
}
+ /**
+ * Creates a model listener.
+ * @return a model listener
+ */
protected ModelListener createModelListener(){
return new ModelListener();
}
+ /**
+ * Creates a scroll listener.
+ * @return a scroll listener
+ */
protected ScrollListener createScrollListener(){
return new ScrollListener();
}
+ /**
+ * Creates a property change listener.
+ * @return a property change listener
+ */
protected PropertyChangeListener createPropertyChangeListener() {
return getHandler();
}
private void updateThumbState(int x, int y) {
*** 468,494 ****
--- 543,583 ----
*/
public Dimension getMaximumSize(JComponent c) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
+ /**
+ * Creates a decrease button.
+ * @param orientation the orientation
+ * @return a decrease button
+ */
protected JButton createDecreaseButton(int orientation) {
return new BasicArrowButton(orientation,
UIManager.getColor("ScrollBar.thumb"),
UIManager.getColor("ScrollBar.thumbShadow"),
UIManager.getColor("ScrollBar.thumbDarkShadow"),
UIManager.getColor("ScrollBar.thumbHighlight"));
}
+ /**
+ * Creates an increase button.
+ * @param orientation the orientation
+ * @return an increase button
+ */
protected JButton createIncreaseButton(int orientation) {
return new BasicArrowButton(orientation,
UIManager.getColor("ScrollBar.thumb"),
UIManager.getColor("ScrollBar.thumbShadow"),
UIManager.getColor("ScrollBar.thumbDarkShadow"),
UIManager.getColor("ScrollBar.thumbHighlight"));
}
+ /**
+ * Paints the decrease highlight.
+ * @param g the graphics
+ */
protected void paintDecreaseHighlight(Graphics g)
{
Insets insets = scrollbar.getInsets();
Rectangle thumbR = getThumbBounds();
g.setColor(trackHighlightColor);
*** 517,526 ****
--- 606,619 ----
g.fillRect(x, y, w, h);
}
}
+ /**
+ * Paints the increase highlight.
+ * @param g the graphics
+ */
protected void paintIncreaseHighlight(Graphics g)
{
Insets insets = scrollbar.getInsets();
Rectangle thumbR = getThumbBounds();
g.setColor(trackHighlightColor);
*** 550,559 ****
--- 643,658 ----
g.fillRect(x, y, w, h);
}
}
+ /**
+ * Paints the track.
+ * @param g the graphics
+ * @param c the component
+ * @param trackBounds the track bounds
+ */
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
{
g.setColor(trackColor);
g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
*** 563,573 ****
else if(trackHighlight == INCREASE_HIGHLIGHT) {
paintIncreaseHighlight(g);
}
}
!
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
{
if(thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
return;
}
--- 662,677 ----
else if(trackHighlight == INCREASE_HIGHLIGHT) {
paintIncreaseHighlight(g);
}
}
! /**
! * Paints the thumb.
! * @param g the graphics
! * @param c the component
! * @param thumbBounds the thumb bounds
! */
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
{
if(thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
return;
}
*** 642,651 ****
--- 746,759 ----
private int getValue(JScrollBar sb) {
return (useCachedValue) ? scrollBarValue : sb.getValue();
}
+ /**
+ * Laysouts a vertical scroll bar.
+ * @param sb the scroll bar
+ */
protected void layoutVScrollbar(JScrollBar sb)
{
Dimension sbSize = sb.getSize();
Insets sbInsets = sb.getInsets();
*** 740,750 ****
}
setThumbBounds(itemX, thumbY, itemW, thumbH);
}
}
!
protected void layoutHScrollbar(JScrollBar sb)
{
Dimension sbSize = sb.getSize();
Insets sbInsets = sb.getInsets();
--- 848,861 ----
}
setThumbBounds(itemX, thumbY, itemW, thumbH);
}
}
! /**
! * Laysouts a vertical scroll bar.
! * @param sb the scroll bar
! */
protected void layoutHScrollbar(JScrollBar sb)
{
Dimension sbSize = sb.getSize();
Insets sbInsets = sb.getInsets();
*** 964,973 ****
--- 1075,1088 ----
}
scrollbar.setValue(newValue);
}
+ /**
+ * Scrolls by block.
+ * @param direction the direction to scroll
+ */
protected void scrollByBlock(int direction)
{
scrollByBlock(scrollbar, direction);
trackHighlight = direction > 0 ? INCREASE_HIGHLIGHT : DECREASE_HIGHLIGHT;
Rectangle dirtyRect = getTrackBounds();
*** 1031,1040 ****
--- 1146,1159 ----
}
scrollbar.setValue(newValue);
}
}
+ /**
+ * Scrolls by unit.
+ * @param direction the direction to scroll
+ */
protected void scrollByUnit(int direction) {
scrollByUnits(scrollbar, direction, 1, false);
}
/**
*** 1048,1058 ****
return supportsAbsolutePositioning;
}
/**
* A listener to listen for model changes.
- *
*/
protected class ModelListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
if (!useCachedValue) {
scrollBarValue = scrollbar.getValue();
--- 1167,1176 ----
*** 1067,1080 ****
* Track mouse drags.
*/
protected class TrackListener
extends MouseAdapter implements MouseMotionListener
{
protected transient int offset;
! protected transient int currentMouseX, currentMouseY;
private transient int direction = +1;
public void mouseReleased(MouseEvent e)
{
if (isDragging) {
updateThumbState(e.getX(), e.getY());
}
--- 1185,1203 ----
* Track mouse drags.
*/
protected class TrackListener
extends MouseAdapter implements MouseMotionListener
{
+ /** The offset */
protected transient int offset;
! /** Current mouse x position */
! protected transient int currentMouseX;
! /** Current mouse y position */
! protected transient int currentMouseY;
private transient int direction = +1;
+ /** {@inheritDoc} */
public void mouseReleased(MouseEvent e)
{
if (isDragging) {
updateThumbState(e.getX(), e.getY());
}
*** 1337,1346 ****
--- 1460,1470 ----
}
break;
}
}
+ /** {@inheritDoc} */
public void mouseMoved(MouseEvent e) {
if (!isDragging) {
updateThumbState(e.getX(), e.getY());
}
}
*** 1404,1426 ****
--- 1528,1565 ----
protected class ScrollListener implements ActionListener
{
int direction = +1;
boolean useBlockIncrement;
+ /** Constructs a {@code ScrollListener}. */
public ScrollListener() {
direction = +1;
useBlockIncrement = false;
}
+ /**
+ * Constructs a {@code ScrollListener}.
+ * @param dir direction
+ * @param block use block increment
+ */
public ScrollListener(int dir, boolean block) {
direction = dir;
useBlockIncrement = block;
}
+ /**
+ * Sets the direction.
+ * @param direction the new direction
+ */
public void setDirection(int direction) { this.direction = direction; }
+ /**
+ * Sets the scrolling by block
+ * @param block whether or not to scroll by block
+ */
public void setScrollByBlock(boolean block) { this.useBlockIncrement = block; }
+ /** {@inheritDoc} */
public void actionPerformed(ActionEvent e) {
if(useBlockIncrement) {
scrollByBlock(direction);
// Stop scrolling if the thumb catches up with the mouse
if(scrollbar.getOrientation() == JScrollBar.VERTICAL) {
*** 1495,1511 ****
orient == HORIZONTAL ? EAST : NORTH);
}
}
}
public class PropertyChangeHandler implements PropertyChangeListener
{
// NOTE: This class exists only for backward compatibility. All
// its functionality has been moved into Handler. If you need to add
// new functionality add it to the Handler, but make sure this
// class calls into the Handler.
!
public void propertyChange(PropertyChangeEvent e) {
getHandler().propertyChange(e);
}
}
--- 1634,1651 ----
orient == HORIZONTAL ? EAST : NORTH);
}
}
}
+ /** Property change handler */
public class PropertyChangeHandler implements PropertyChangeListener
{
// NOTE: This class exists only for backward compatibility. All
// its functionality has been moved into Handler. If you need to add
// new functionality add it to the Handler, but make sure this
// class calls into the Handler.
! /** {@inheritDoc} */
public void propertyChange(PropertyChangeEvent e) {
getHandler().propertyChange(e);
}
}
< prev index next >