📄 basicscrollbarui.java
字号:
/* * @(#)BasicScrollBarUI.java 1.87 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.basic;import sun.swing.DefaultLookup;import sun.swing.UIAction;import java.awt.*;import java.awt.event.*;import java.beans.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;/** * Implementation of ScrollBarUI for the Basic Look and Feel * * @version 1.87 11/17/05 * @author Rich Schiavi * @author David Kloba * @author Hans Muller */public class BasicScrollBarUI extends ScrollBarUI implements LayoutManager, SwingConstants{ private static final int POSITIVE_SCROLL = 1; private static final int NEGATIVE_SCROLL = -1; 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. protected Dimension minimumThumbSize; protected Dimension maximumThumbSize; protected Color thumbHighlightColor; protected Color thumbLightShadowColor; protected Color thumbDarkShadowColor; protected Color thumbColor; protected Color trackColor; protected Color trackHighlightColor; protected JScrollBar scrollbar; protected JButton incrButton; protected JButton decrButton; protected boolean isDragging; protected TrackListener trackListener; protected ArrowButtonListener buttonListener; protected ModelListener modelListener; protected Rectangle thumbRect; protected Rectangle trackRect; protected int trackHighlight; protected static final int NO_HIGHLIGHT = 0; protected static final int DECREASE_HIGHLIGHT = 1; protected static final int INCREASE_HIGHLIGHT = 2; protected ScrollListener scrollListener; protected PropertyChangeListener propertyChangeListener; protected Timer scrollTimer; private final static int scrollSpeedThrottle = 60; // delay in milli seconds /** True indicates a middle click will absolutely position the * scrollbar. */ private boolean supportsAbsolutePositioning; /** Hint as to what width (when vertical) or height (when horizontal) * should be. */ private int scrollBarWidth; private Handler handler; private boolean thumbActive; /** * Determine whether scrollbar layout should use cached value or adjusted * value returned by scrollbar's <code>getValue</code>. */ private boolean useCachedValue = false; /** * The scrollbar value is cached to save real value if the view is adjusted. */ private int scrollBarValue; static void loadActionMap(LazyActionMap map) { map.put(new Actions(Actions.POSITIVE_UNIT_INCREMENT)); map.put(new Actions(Actions.POSITIVE_BLOCK_INCREMENT)); map.put(new Actions(Actions.NEGATIVE_UNIT_INCREMENT)); 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"); thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow"); thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow"); 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(); uninstallKeyboardActions(); thumbRect = null; scrollbar = null; incrButton = null; decrButton = null; } protected void installDefaults() { scrollBarWidth = UIManager.getInt("ScrollBar.width"); if (scrollBarWidth <= 0) { scrollBarWidth = 16; } minimumThumbSize = (Dimension)UIManager.get("ScrollBar.minimumThumbSize"); maximumThumbSize = (Dimension)UIManager.get("ScrollBar.maximumThumbSize"); Boolean absB = (Boolean)UIManager.get("ScrollBar.allowsAbsolutePositioning"); supportsAbsolutePositioning = (absB != null) ? absB.booleanValue() : false; trackHighlight = NO_HIGHLIGHT; if (scrollbar.getLayout() == null || (scrollbar.getLayout() instanceof UIResource)) { scrollbar.setLayout(this); } configureScrollBarColors(); LookAndFeel.installBorder(scrollbar, "ScrollBar.border"); LookAndFeel.installProperty(scrollbar, "opaque", Boolean.TRUE); scrollBarValue = scrollbar.getValue(); } protected void installComponents(){ switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: incrButton = createIncreaseButton(SOUTH); decrButton = createDecreaseButton(NORTH); break; case JScrollBar.HORIZONTAL: if (scrollbar.getComponentOrientation().isLeftToRight()) { incrButton = createIncreaseButton(EAST); decrButton = createDecreaseButton(WEST); } else { incrButton = createIncreaseButton(WEST); decrButton = createDecreaseButton(EAST); } break; } scrollbar.add(incrButton); 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(); scrollbar.addMouseListener(trackListener); scrollbar.addMouseMotionListener(trackListener); scrollbar.getModel().addChangeListener(modelListener); scrollbar.addPropertyChangeListener(propertyChangeListener); scrollbar.addFocusListener(getHandler()); if (incrButton != null) { incrButton.addMouseListener(buttonListener); } if (decrButton != null) { decrButton.addMouseListener(buttonListener); } 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); SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, inputMap); inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); } protected void uninstallKeyboardActions(){ SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, null); SwingUtilities.replaceUIActionMap(scrollbar, null); } private InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_FOCUSED) { InputMap keyMap = (InputMap)DefaultLookup.get( scrollbar, this, "ScrollBar.focusInputMap"); InputMap rtlKeyMap; if (scrollbar.getComponentOrientation().isLeftToRight() || ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.focusInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } else if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { InputMap keyMap = (InputMap)DefaultLookup.get( scrollbar, this, "ScrollBar.ancestorInputMap"); InputMap rtlKeyMap; if (scrollbar.getComponentOrientation().isLeftToRight() || ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.ancestorInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } return null; } protected void uninstallListeners() { scrollTimer.stop(); scrollTimer = null; if (decrButton != null){ decrButton.removeMouseListener(buttonListener); } if (incrButton != null){ incrButton.removeMouseListener(buttonListener); } scrollbar.getModel().removeChangeListener(modelListener); scrollbar.removeMouseListener(trackListener); scrollbar.removeMouseMotionListener(trackListener); scrollbar.removePropertyChangeListener(propertyChangeListener); scrollbar.removeFocusListener(getHandler()); handler = null; } protected void uninstallDefaults(){ LookAndFeel.uninstallBorder(scrollbar); if (scrollbar.getLayout() == this) { scrollbar.setLayout(null); } } private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } protected TrackListener createTrackListener(){ return new TrackListener(); } protected ArrowButtonListener createArrowButtonListener(){ return new ArrowButtonListener(); } protected ModelListener createModelListener(){ return new ModelListener(); } protected ScrollListener createScrollListener(){ return new ScrollListener(); } protected PropertyChangeListener createPropertyChangeListener() { return getHandler(); } private void updateThumbState(int x, int y) { Rectangle rect = getThumbBounds(); setThumbRollover(rect.contains(x, y)); } /** * Sets whether or not the mouse is currently over the thumb. * * @param active True indicates the thumb is currently active. * @since 1.5 */ protected void setThumbRollover(boolean active) { if (thumbActive != active) { thumbActive = active; scrollbar.repaint(getThumbBounds()); } } /** * Returns true if the mouse is currently over the thumb. * * @return true if the thumb is currently active * @since 1.5 */ public boolean isThumbRollover() { return thumbActive; } public void paint(Graphics g, JComponent c) { paintTrack(g, c, getTrackBounds()); Rectangle thumbBounds = getThumbBounds(); if (thumbBounds.intersects(g.getClipBounds())) { paintThumb(g, c, thumbBounds); } } /** * A vertical scrollbar's preferred width is the maximum of * preferred widths of the (non <code>null</code>) * increment/decrement buttons, * and the minimum width of the thumb. The preferred height is the * sum of the preferred heights of the same parts. The basis for * the preferred size of a horizontal scrollbar is similar. * <p> * The <code>preferredSize</code> is only computed once, subsequent * calls to this method just return a cached size. * * @param c the <code>JScrollBar</code> that's delegating this method to us * @return the preferred size of a Basic JScrollBar * @see #getMaximumSize
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -