basicscrollbarui.java

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

JAVA
1,300
字号
  protected Rectangle thumbRect;  /** Indicates that the decrease highlight should be painted. */  protected static final int DECREASE_HIGHLIGHT = 1;  /** Indicates that the increase highlight should be painted. */  protected static final int INCREASE_HIGHLIGHT = 2;  /** Indicates that no highlight should be painted. */  protected static final int NO_HIGHLIGHT = 0;  /** Indicates that the scrolling direction is positive. */  private static final int POSITIVE_SCROLL = 1;  /** Indicates that the scrolling direction is negative. */  private static final int NEGATIVE_SCROLL = -1;  /** The cached preferred size for the scrollbar. */  private transient Dimension preferredSize;  /** The current highlight status. */  protected int trackHighlight;  /** FIXME: Use this for something (presumably mouseDragged) */  protected boolean isDragging;  /** The timer used to move the thumb when the mouse is held. */  protected Timer scrollTimer;  /** The scrollbar this UI is acting for. */  protected JScrollBar scrollbar;  /**   * This method adds a component to the layout.   *   * @param name The name to associate with the component that is added.   * @param child The Component to add.   */  public void addLayoutComponent(String name, Component child)  {    // You should not be adding stuff to this component.    // The contents are fixed.  }  /**   * This method configures the scrollbar's colors. This can be  done by   * looking up the standard colors from the Look and Feel defaults.   */  protected void configureScrollBarColors()  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    trackColor = defaults.getColor("ScrollBar.track");    trackHighlightColor = defaults.getColor("ScrollBar.trackHighlight");    thumbColor = defaults.getColor("ScrollBar.thumb");    thumbHighlightColor = defaults.getColor("ScrollBar.thumbHighlight");    thumbDarkShadowColor = defaults.getColor("ScrollBar.thumbDarkShadow");    thumbLightShadowColor = defaults.getColor("ScrollBar.thumbLightShadow");  }  /**   * This method creates an ArrowButtonListener.   *   * @return A new ArrowButtonListener.   */  protected ArrowButtonListener createArrowButtonListener()  {    return new ArrowButtonListener();  }  /**   * This method creates a new JButton with the appropriate icon for the   * orientation.   *   * @param orientation The orientation this JButton uses.   *   * @return The increase JButton.   */  protected JButton createIncreaseButton(int orientation)  {    if (incrButton == null)      incrButton = new BasicArrowButton((orientation == SwingConstants.HORIZONTAL)                                        ? SwingConstants.EAST                                        : SwingConstants.SOUTH);    else      {	if (orientation == SwingConstants.HORIZONTAL)	  ((BasicArrowButton) incrButton).setDirection(SwingConstants.EAST);	else	  ((BasicArrowButton) incrButton).setDirection(SwingConstants.SOUTH);      }    return incrButton;  }  /**   * This method creates a new JButton with the appropriate icon for the   * orientation.   *   * @param orientation The orientation this JButton uses.   *   * @return The decrease JButton.   */  protected JButton createDecreaseButton(int orientation)  {    if (decrButton == null)      decrButton = new BasicArrowButton((orientation == SwingConstants.HORIZONTAL)                                        ? SwingConstants.WEST                                        : SwingConstants.NORTH);    else      {	if (orientation == SwingConstants.HORIZONTAL)	  ((BasicArrowButton) decrButton).setDirection(SwingConstants.WEST);	else	  ((BasicArrowButton) decrButton).setDirection(SwingConstants.NORTH);      }    return decrButton;  }  /**   * This method creates a new ModelListener.   *   * @return A new ModelListener.   */  protected ModelListener createModelListener()  {    return new ModelListener();  }  /**   * This method creates a new PropertyChangeListener.   *   * @return A new PropertyChangeListener.   */  protected PropertyChangeListener createPropertyChangeListener()  {    return new PropertyChangeHandler();  }  /**   * This method creates a new ScrollListener.   *   * @return A new ScrollListener.   */  protected ScrollListener createScrollListener()  {    return new ScrollListener();  }  /**   * This method creates a new TrackListener.   *   * @return A new TrackListener.   */  protected TrackListener createTrackListener()  {    return new TrackListener();  }  /**   * This method returns a new BasicScrollBarUI.   *   * @param c The JComponent to create a UI for.   *   * @return A new BasicScrollBarUI.   */  public static ComponentUI createUI(JComponent c)  {    return new BasicScrollBarUI();  }  /**   * This method returns the maximum size for this JComponent.   *   * @param c The JComponent to measure the maximum size for.   *   * @return The maximum size for the component.   */  public Dimension getMaximumSize(JComponent c)  {    return getPreferredSize(c);  }  /**   * This method returns the maximum thumb size.   *   * @return The maximum thumb size.   */  protected Dimension getMaximumThumbSize()  {    return maximumThumbSize;  }  /**   * This method returns the minimum size for this JComponent.   *   * @param c The JComponent to measure the minimum size for.   *   * @return The minimum size for the component.   */  public Dimension getMinimumSize(JComponent c)  {    return getPreferredSize(c);  }  /**   * This method returns the minimum thumb size.   *   * @return The minimum thumb size.   */  protected Dimension getMinimumThumbSize()  {    return minimumThumbSize;  }  /**   * This method calculates the preferred size since calling   * getPreferredSize() returns a cached value.   */  private void calculatePreferredSize()  {    // System.err.println(this + ".calculatePreferredSize()");    int height;    int width;    height = width = 0;    if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)      {	width += incrButton.getPreferredSize().getWidth();	width += decrButton.getPreferredSize().getWidth();	width += (scrollbar.getMaximum() - scrollbar.getMinimum());	height = Math.max(incrButton.getPreferredSize().height,	                  decrButton.getPreferredSize().height);	height = Math.max(getMinimumThumbSize().height, height);	height = Math.max(20, height);	height = Math.min(getMaximumThumbSize().height, height);      }    else      {	height += incrButton.getPreferredSize().getHeight();	height += decrButton.getPreferredSize().getHeight();	height += (scrollbar.getMaximum() - scrollbar.getMinimum());	width = Math.max(incrButton.getPreferredSize().width,	                 decrButton.getPreferredSize().width);	width = Math.max(getMinimumThumbSize().width, width);	width = Math.max(20, width);	width = Math.min(getMaximumThumbSize().width, width);      }    Insets insets = scrollbar.getInsets();    height += insets.top + insets.bottom;    width += insets.left + insets.right;    preferredSize = new Dimension(width, height);  }  /**   * This method returns a cached value of the preferredSize. The only   * restrictions are: If the scrollbar is horizontal, the height should be   * the maximum of the height of the JButtons and  the minimum width of the   * thumb. For vertical scrollbars, the  calculation is similar (swap width   * for height and vice versa).   *   * @param c The JComponent to measure.   *   * @return The preferredSize.   */  public Dimension getPreferredSize(JComponent c)  {    calculatePreferredSize();    return preferredSize;  }  /**   * This method returns the thumb's bounds based on the  current value of the   * scrollbar. This method updates the cached value and returns that.   *   * @return The thumb bounds.   */  protected Rectangle getThumbBounds()  {    int max = scrollbar.getMaximum();    int min = scrollbar.getMinimum();    int value = scrollbar.getValue();    int extent = scrollbar.getVisibleAmount();    // System.err.println(this + ".getThumbBounds()");    if (max == min)      {	thumbRect.x = trackRect.x;	thumbRect.y = trackRect.y;	if (scrollbar.getOrientation() == HORIZONTAL)	  {	    thumbRect.width = getMinimumThumbSize().width;	    thumbRect.height = trackRect.height;	  }	else	  {	    thumbRect.width = trackRect.width;	    thumbRect.height = getMinimumThumbSize().height;	  }	return thumbRect;      }    if (scrollbar.getOrientation() == HORIZONTAL)      {	thumbRect.x = trackRect.x;	thumbRect.x += (value - min) * trackRect.width / (max - min);	thumbRect.y = trackRect.y;	thumbRect.width = extent * trackRect.width / (max - min);	thumbRect.height = trackRect.height;      }    else      {	thumbRect.x = trackRect.x;	thumbRect.y = trackRect.y + value * trackRect.height / (max - min);	thumbRect.width = trackRect.width;	thumbRect.height = extent * trackRect.height / (max - min);      }    return thumbRect;  }  /**   * This method calculates the bounds of the track. This method updates the   * cached value and returns it.   *   * @return The track's bounds.   */  protected Rectangle getTrackBounds()  {    SwingUtilities.calculateInnerArea(scrollbar, trackRect);    if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)      {	trackRect.width -= incrButton.getPreferredSize().getWidth();	trackRect.width -= decrButton.getPreferredSize().getWidth();	trackRect.x += decrButton.getPreferredSize().getWidth();      }    else      {	trackRect.height -= incrButton.getPreferredSize().getHeight();	trackRect.height -= decrButton.getPreferredSize().getHeight();	trackRect.y += incrButton.getPreferredSize().getHeight();      }    return trackRect;  }  /**   * This method installs any addition Components that  are a part of or   * related to this scrollbar.   */  protected void installComponents()  {    incrButton = createIncreaseButton(scrollbar.getOrientation());    scrollbar.add(incrButton);    decrButton = createDecreaseButton(scrollbar.getOrientation());    scrollbar.add(decrButton);  }  /**   * This method installs the defaults for the scrollbar specified by the   * Basic Look and Feel.   */  protected void installDefaults()  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    scrollbar.setForeground(defaults.getColor("ScrollBar.foreground"));    scrollbar.setBackground(defaults.getColor("ScrollBar.background"));    scrollbar.setBorder(defaults.getBorder("ScrollBar.border"));    scrollbar.setOpaque(true);    maximumThumbSize = defaults.getDimension("ScrollBar.maximumThumbSize");    minimumThumbSize = defaults.getDimension("ScrollBar.minimumThumbSize");  }  /**   * This method installs the keyboard actions for the scrollbar.   */  protected void installKeyboardActions()  {    // FIXME: implement.  }  /**   * This method installs any listeners for the scrollbar. This method also   * installs listeners for things such as the JButtons and the timer.   */  protected void installListeners()  {    scrollListener = createScrollListener();    trackListener = createTrackListener();    buttonListener = createArrowButtonListener();    modelListener = createModelListener();    propertyChangeListener = createPropertyChangeListener();    scrollbar.addMouseMotionListener(trackListener);    scrollbar.addMouseListener(trackListener);    incrButton.addMouseListener(buttonListener);    decrButton.addMouseListener(buttonListener);    scrollbar.addPropertyChangeListener(propertyChangeListener);    scrollbar.getModel().addChangeListener(modelListener);    scrollTimer.addActionListener(scrollListener);  }  /**   * This method installs the UI for the component. This can include setting   * up listeners, defaults,  and components. This also includes initializing   * any data objects.   *   * @param c The JComponent to install.   */  public void installUI(JComponent c)  {    super.installUI(c);    if (c instanceof JScrollBar)      {	scrollbar = (JScrollBar) c;	trackRect = new Rectangle();	thumbRect = new Rectangle();	scrollTimer = new Timer(200, null);

⌨️ 快捷键说明

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