basicscrollbarui.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,300 行 · 第 1/3 页

JAVA
1,300
字号
/* 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., 59 Temple Place, Suite 330, Boston, MA02111-1307 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.Point;import java.awt.Polygon;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.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.Timer;import javax.swing.UIDefaults;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	scrollListener.setDirection(NEGATIVE_SCROLL);      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();    }  }  /**   * 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)    {      //       System.err.println(this + ".stateChanged()");      calculatePreferredSize();      layoutContainer(scrollbar);      getThumbBounds();      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);	  getThumbBounds();        }      else if (e.getPropertyName().equals("orientation"))        {	  incrButton.removeMouseListener(buttonListener);	  decrButton.removeMouseListener(buttonListener);	  incrButton = createIncreaseButton(scrollbar.getOrientation());	  decrButton = createDecreaseButton(scrollbar.getOrientation());	  incrButton.addMouseListener(buttonListener);	  decrButton.addMouseListener(buttonListener);	  calculatePreferredSize();	  layoutContainer(scrollbar);        }      layoutContainer(scrollbar);      scrollbar.repaint();    }  }  /**   * 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 (value == scrollbar.getValue())	return;      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.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.	  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)    {      trackHighlight = NO_HIGHLIGHT;      scrollTimer.stop();      if (scrollbar.getValueIsAdjusting())	scrollbar.setValueIsAdjusting(false);      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 (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;  /** The dimensions of the maximum thumb size. */  protected Dimension maximumThumbSize;  /** The dimensions of the minimum thumb size. */  protected Dimension minimumThumbSize;  /** The color of the thumb. */  protected Color thumbColor;  /** The outer shadow of the thumb. */  protected Color thumbDarkShadowColor;  /** The top and left edge color for the thumb. */  protected Color thumbHighlightColor;  /** The outer light shadow for the thumb. */  protected Color thumbLightShadowColor;  /** The color that is used when the mouse press occurs in the track. */  protected Color trackHighlightColor;  /** The color of the track. */  protected Color trackColor;  /** The size and position of the track. */  protected Rectangle trackRect;  /** The size and position of the thumb. */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?