📄 basicscrollbarui.java
字号:
/* * @(#)BasicScrollBarUI.java 1.75 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.basic;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.75 01/23/03 * @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; 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; 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; uninstallDefaults(); uninstallComponents(); uninstallListeners(); uninstallKeyboardActions(); c.remove(incrButton); c.remove(decrButton); c.setLayout(null); 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; 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.setLayout(this); scrollbar.add(incrButton); scrollbar.add(decrButton); scrollbar.setEnabled(scrollbar.isEnabled()); scrollbar.setOpaque(true); configureScrollBarColors(); LookAndFeel.installBorder(scrollbar, "ScrollBar.border"); } protected void installComponents(){ } protected void uninstallComponents(){ } protected void installListeners(){ trackListener = createTrackListener(); buttonListener = createArrowButtonListener(); modelListener = createModelListener(); propertyChangeListener = createPropertyChangeListener(); scrollbar.addMouseListener(trackListener); scrollbar.addMouseMotionListener(trackListener); scrollbar.getModel().addChangeListener(modelListener); scrollbar.addPropertyChangeListener(propertyChangeListener); 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(){ ActionMap map = getActionMap(); SwingUtilities.replaceUIActionMap(scrollbar, map); InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, 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)UIManager.get("ScrollBar.focusInputMap"); InputMap rtlKeyMap; if (scrollbar.getComponentOrientation().isLeftToRight() || ((rtlKeyMap = (InputMap)UIManager.get("ScrollBar.focusInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } return null; } private ActionMap getActionMap() { ActionMap map = (ActionMap)UIManager.get("ScrollBar.actionMap"); if (map == null) { map = createActionMap(); if (map != null) { UIManager.getLookAndFeelDefaults().put("ScrollBar.actionMap", map); } } return map; } private ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("positiveUnitIncrement", new SharedActionScroller (POSITIVE_SCROLL, false)); map.put("positiveBlockIncrement", new SharedActionScroller (POSITIVE_SCROLL, true)); map.put("negativeUnitIncrement", new SharedActionScroller (NEGATIVE_SCROLL, false)); map.put("negativeBlockIncrement", new SharedActionScroller (NEGATIVE_SCROLL, true)); map.put("minScroll", new SharedActionScroller(MIN_SCROLL, true)); map.put("maxScroll", new SharedActionScroller(MAX_SCROLL, true)); return map; } 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); } protected void uninstallDefaults(){ LookAndFeel.uninstallBorder(scrollbar); } 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 new PropertyChangeHandler(); } public void paint(Graphics g, JComponent c) { paintTrack(g, c, getTrackBounds()); paintThumb(g, c, getThumbBounds()); } /** * 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 * @see #getMinimumSize */ public Dimension getPreferredSize(JComponent c) { return (scrollbar.getOrientation() == JScrollBar.VERTICAL) ? new Dimension(scrollBarWidth, 48) : new Dimension(48, scrollBarWidth); } /** * A vertical scrollbar's minimum width is the largest * minimum width of the (non <code>null</code>) increment/decrement buttons, * and the minimum width of the thumb. The minimum height is the * sum of the minimum heights of the same parts. The basis for * the preferred size of a horizontal scrollbar is similar. * <p> * The <code>minimumSize</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 minimum size of a basic <code>JScrollBar</code> * @see #getMaximumSize * @see #getPreferredSize */ public Dimension getMinimumSize(JComponent c) { return getPreferredSize(c); } /** * @param c The JScrollBar that's delegating this method to us. * @return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); * @see #getMinimumSize * @see #getPreferredSize */ public Dimension getMaximumSize(JComponent c) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } 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")); } 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")); } protected void paintDecreaseHighlight(Graphics g) { Insets insets = scrollbar.getInsets(); Rectangle thumbR = getThumbBounds(); g.setColor(trackHighlightColor); if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { int x = insets.left; int y = decrButton.getY() + decrButton.getHeight(); int w = scrollbar.getWidth() - (insets.left + insets.right); int h = thumbR.y - y; g.fillRect(x, y, w, h); } else { int x, w; if (scrollbar.getComponentOrientation().isLeftToRight()) { x = decrButton.getX() + decrButton.getWidth(); w = thumbR.x - x; } else { x = thumbR.x + thumbR.width; w = decrButton.getX() - x; } int y = insets.top; int h = scrollbar.getHeight() - (insets.top + insets.bottom); g.fillRect(x, y, w, h); } } protected void paintIncreaseHighlight(Graphics g) { Insets insets = scrollbar.getInsets(); Rectangle thumbR = getThumbBounds(); g.setColor(trackHighlightColor); if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { int x = insets.left; int y = thumbR.y + thumbR.height; int w = scrollbar.getWidth() - (insets.left + insets.right); int h = incrButton.getY() - y; g.fillRect(x, y, w, h); } else { int x, w; if (scrollbar.getComponentOrientation().isLeftToRight()) { x = thumbR.x + thumbR.width; w = incrButton.getX() - x; } else { x = incrButton.getX() + incrButton.getWidth(); w = thumbR.x - x; } int y = insets.top; int h = scrollbar.getHeight() - (insets.top + insets.bottom); g.fillRect(x, y, w, h); } } protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -