📄 basicscrollbarui.java
字号:
/* BasicScrollBarUI.java -- Copyright (C) 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.basic;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.BoundedRangeModel;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JScrollBar;import javax.swing.LookAndFeel;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.ScrollBarUI;/** * The Basic Look and Feel UI delegate for JScrollBar. */public class BasicScrollBarUI extends ScrollBarUI implements LayoutManager, SwingConstants{ /** * A helper class that listens to the two JButtons on each end of the * JScrollBar. */ protected class ArrowButtonListener extends MouseAdapter { /** * Move the thumb in the direction specified by the button's arrow. If * this button is held down, then it should keep moving the thumb. * * @param e The MouseEvent fired by the JButton. */ public void mousePressed(MouseEvent e) { scrollTimer.stop(); scrollListener.setScrollByBlock(false); if (e.getSource() == incrButton) scrollListener.setDirection(POSITIVE_SCROLL); else if (e.getSource() == decrButton) scrollListener.setDirection(NEGATIVE_SCROLL); scrollTimer.setDelay(100); scrollTimer.start(); } /** * Stops the thumb when the JButton is released. * * @param e The MouseEvent fired by the JButton. */ public void mouseReleased(MouseEvent e) { scrollTimer.stop(); scrollTimer.setDelay(300); if (e.getSource() == incrButton) scrollByUnit(POSITIVE_SCROLL); else if (e.getSource() == decrButton) scrollByUnit(NEGATIVE_SCROLL); } } /** * A helper class that listens to the ScrollBar's model for ChangeEvents. */ protected class ModelListener implements ChangeListener { /** * Called when the model changes. * * @param e The ChangeEvent fired by the model. */ public void stateChanged(ChangeEvent e) { calculatePreferredSize(); updateThumbRect(); scrollbar.repaint(); } } /** * A helper class that listens to the ScrollBar's properties. */ public class PropertyChangeHandler implements PropertyChangeListener { /** * Called when one of the ScrollBar's properties change. * * @param e The PropertyChangeEvent fired by the ScrollBar. */ public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals("model")) { ((BoundedRangeModel) e.getOldValue()).removeChangeListener(modelListener); scrollbar.getModel().addChangeListener(modelListener); updateThumbRect(); } else if (e.getPropertyName().equals("orientation")) { uninstallListeners(); uninstallComponents(); uninstallDefaults(); installDefaults(); installComponents(); installListeners(); } else if (e.getPropertyName().equals("enabled")) { Boolean b = (Boolean) e.getNewValue(); if (incrButton != null) incrButton.setEnabled(b.booleanValue()); if (decrButton != null) decrButton.setEnabled(b.booleanValue()); } } } /** * A helper class that listens for events from the timer that is used to * move the thumb. */ protected class ScrollListener implements ActionListener { /** The direction the thumb moves in. */ private transient int direction; /** Whether movement will be in blocks. */ private transient boolean block; /** * Creates a new ScrollListener object. The default is scrolling * positively with block movement. */ public ScrollListener() { direction = POSITIVE_SCROLL; block = true; } /** * Creates a new ScrollListener object using the given direction and * block. * * @param dir The direction to move in. * @param block Whether movement will be in blocks. */ public ScrollListener(int dir, boolean block) { direction = dir; this.block = block; } /** * Sets the direction to scroll in. * * @param direction The direction to scroll in. */ public void setDirection(int direction) { this.direction = direction; } /** * Sets whether scrolling will be done in blocks. * * @param block Whether scrolling will be in blocks. */ public void setScrollByBlock(boolean block) { this.block = block; } /** * Called every time the timer reaches its interval. * * @param e The ActionEvent fired by the timer. */ public void actionPerformed(ActionEvent e) { if (block) { // Only need to check it if it's block scrolling // We only block scroll if the click occurs // in the track. if (!trackListener.shouldScroll(direction)) { trackHighlight = NO_HIGHLIGHT; scrollbar.repaint(); return; } scrollByBlock(direction); } else scrollByUnit(direction); } } /** * Helper class that listens for movement on the track. */ protected class TrackListener extends MouseAdapter implements MouseMotionListener { /** The current X coordinate of the mouse. */ protected int currentMouseX; /** The current Y coordinate of the mouse. */ protected int currentMouseY; /** * The offset between the current mouse cursor and the current value of * the scrollbar. */ protected int offset; /** * This method is called when the mouse is being dragged. * * @param e The MouseEvent given. */ public void mouseDragged(MouseEvent e) { currentMouseX = e.getX(); currentMouseY = e.getY(); if (scrollbar.getValueIsAdjusting()) { int value; if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL) value = valueForXPosition(currentMouseX) - offset; else value = valueForYPosition(currentMouseY) - offset; scrollbar.setValue(value); } } /** * This method is called when the mouse is moved. * * @param e The MouseEvent given. */ public void mouseMoved(MouseEvent e) { // Not interested in where the mouse // is unless it is being dragged. } /** * This method is called when the mouse is pressed. When it is pressed, * the thumb should move in blocks towards the cursor. * * @param e The MouseEvent given. */ public void mousePressed(MouseEvent e) { currentMouseX = e.getX(); currentMouseY = e.getY(); int value; if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL) value = valueForXPosition(currentMouseX); else value = valueForYPosition(currentMouseY); if (! thumbRect.contains(e.getPoint())) { scrollTimer.stop(); scrollListener.setScrollByBlock(true); if (value > scrollbar.getValue()) { trackHighlight = INCREASE_HIGHLIGHT; scrollListener.setDirection(POSITIVE_SCROLL); } else { trackHighlight = DECREASE_HIGHLIGHT; scrollListener.setDirection(NEGATIVE_SCROLL); } scrollTimer.setDelay(100); scrollTimer.start(); } else { // We'd like to keep track of where the cursor // is inside the thumb. // This works because the scrollbar's value represents // "lower" edge of the thumb. The value at which // the cursor is at must be greater or equal // to that value. scrollListener.setScrollByBlock(false); scrollbar.setValueIsAdjusting(true); offset = value - scrollbar.getValue(); } scrollbar.repaint(); } /** * This method is called when the mouse is released. It should stop * movement on the thumb * * @param e The MouseEvent given. */ public void mouseReleased(MouseEvent e) { scrollTimer.stop(); scrollTimer.setDelay(300); currentMouseX = e.getX(); currentMouseY = e.getY(); if (shouldScroll(POSITIVE_SCROLL)) scrollByBlock(POSITIVE_SCROLL); else if (shouldScroll(NEGATIVE_SCROLL)) scrollByBlock(NEGATIVE_SCROLL); trackHighlight = NO_HIGHLIGHT; scrollListener.setScrollByBlock(false); scrollbar.setValueIsAdjusting(true); scrollbar.repaint(); } /** * A helper method that decides whether we should keep scrolling in the * given direction. * * @param direction The direction to check for. * * @return Whether the thumb should keep scrolling. */ public boolean shouldScroll(int direction) { int value; if (scrollbar.getOrientation() == HORIZONTAL) value = valueForXPosition(currentMouseX); else value = valueForYPosition(currentMouseY); if (thumbRect.contains(currentMouseX, currentMouseY)) return false; if (direction == POSITIVE_SCROLL) return (value > scrollbar.getValue()); else return (value < scrollbar.getValue()); } } /** The listener that listens to the JButtons. */ protected ArrowButtonListener buttonListener; /** The listener that listens to the model. */ protected ModelListener modelListener; /** The listener that listens to the scrollbar for property changes. */ protected PropertyChangeListener propertyChangeListener; /** The listener that listens to the timer. */ protected ScrollListener scrollListener; /** The listener that listens for MouseEvents on the track. */ protected TrackListener trackListener; /** The JButton that decrements the scrollbar's value. */ protected JButton decrButton; /** The JButton that increments the scrollbar's value. */ protected JButton incrButton;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -