synthscrollbarui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,375 行 · 第 1/3 页

JAVA
1,375
字号
/* * @(#)SynthScrollBarUI.java	1.24 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;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.24, 01/23/03 (based on BasicScrollBarUI v 1.74) * @author Rich Schiavi * @author David Kloba * @author Hans Muller */// ScrollBar consists of the following parts://   Button to increase the current value//   Button to decrese the current value//   Track//   Thumbclass SynthScrollBarUI extends ScrollBarUI implements SynthUI, LayoutManager, SwingConstants, FocusListener {    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;    private static final Insets tmpInsets = new Insets(0, 0, 0, 0);    private SynthStyle style;    private SynthStyle thumbStyle;    private SynthStyle trackStyle;    protected Dimension minimumThumbSize;    protected Dimension maximumThumbSize;    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;    /**     * True if the mouse is over the thumb.     */    private boolean thumbActive;    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 SynthScrollBarUI();    }    public static void loadActionMap(ActionMap map) {        // NOTE: this needs to remain static. If you have a need to        // have Actions that reference the UI in the ActionMap,        // then you'll also need to change the registeration of the        // ActionMap.        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));    }    public void installUI(JComponent c)   {	scrollbar = (JScrollBar)c;        thumbRect = new Rectangle(0, 0, 0, 0);        trackRect = new Rectangle(0, 0, 0, 0);	installDefaults();	installComponents();	installKeyboardActions();	installListeners();    }    public void uninstallUI(JComponent c) {	scrollbar = (JScrollBar)c;	uninstallListeners();	uninstallKeyboardActions();	uninstallComponents();	uninstallDefaults();	c.setLayout(null);	thumbRect = null;	scrollbar = null;	incrButton = null;	decrButton = null;    }        protected void installDefaults() {        fetchStyle(scrollbar);	trackHighlight = NO_HIGHLIGHT;        scrollbar.setLayout(this);    }    private void fetchStyle(JScrollBar c) {        SynthContext context = getContext(c, ENABLED);        SynthStyle oldStyle = style;        style = SynthLookAndFeel.updateStyle(context, this);        // Add properties other than JComponent colors, Borders and        // opacity settings here:        if (style != oldStyle) {            Insets insets = c.getInsets();            scrollBarWidth =                style.getInt(context, "ScrollBar.thumbHeight", 14);            minimumThumbSize = new Dimension();            if (c.getOrientation() == JScrollBar.VERTICAL) {                    minimumThumbSize.width = scrollBarWidth;                    minimumThumbSize.height = 7;                    scrollBarWidth += insets.left + insets.right;            } else {                    minimumThumbSize.width = 7;                    minimumThumbSize.height = scrollBarWidth;                    scrollBarWidth += insets.top + insets.bottom;            }            maximumThumbSize = (Dimension)style.get(context,                        "ScrollBar.maximumThumbSize");            supportsAbsolutePositioning = style.getBoolean(context,                        "ScrollBar.allowsAbsolutePositioning", false);        }        context.dispose();        context = getContext(c, Region.SCROLL_BAR_TRACK, ENABLED);        trackStyle = SynthLookAndFeel.updateStyle(context, this);        context.dispose();        context = getContext(c, Region.SCROLL_BAR_THUMB, ENABLED);        thumbStyle = SynthLookAndFeel.updateStyle(context, this);        context.dispose();    }    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);    }    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);        if (incrButton != null) {            incrButton.addMouseListener(buttonListener);	}        if (decrButton != null)	{            decrButton.addMouseListener(buttonListener);	}	scrollListener = createScrollListener();	scrollTimer = new Timer(scrollSpeedThrottle, scrollListener);	scrollTimer.setInitialDelay(300);  // default InitialDelay?        // PENDING: there should be a property for this to avoid installing        // the listener unless necessary.        scrollbar.addFocusListener(this);    }    protected void installKeyboardActions() {        LazyActionMap.installLazyActionMap(scrollbar, SynthScrollBarUI.class,                                           "ScrollBar.actionMap");	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) {            SynthContext context = getContext(scrollbar, ENABLED);            SynthStyle style = context.getStyle();            InputMap keyMap = (InputMap)style.get(context,                                                  "ScrollBar.focusInputMap");            InputMap rtlKeyMap;            if (scrollbar.getComponentOrientation().isLeftToRight() &&                ((rtlKeyMap = (InputMap)style.get(context,                      "ScrollBar.focusInputMap.RightToLeft")) != null)) {                rtlKeyMap.setParent(keyMap);                keyMap = rtlKeyMap;            }            context.dispose();            return keyMap;	}	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(this);    }    protected void uninstallDefaults(){        SynthContext context = getContext(scrollbar, ENABLED);        style.uninstallDefaults(context);        context.dispose();        style = null;        context = getContext(scrollbar, Region.SCROLL_BAR_TRACK, ENABLED);        trackStyle.uninstallDefaults(context);        context.dispose();        trackStyle = null;        context = getContext(scrollbar, Region.SCROLL_BAR_THUMB, ENABLED);        thumbStyle.uninstallDefaults(context);        context.dispose();        thumbStyle = null;    }    public SynthContext getContext(JComponent c) {        return getContext(c, getComponentState(c));    }    private SynthContext getContext(JComponent c, int state) {        return SynthContext.getContext(SynthContext.class, c,                    SynthLookAndFeel.getRegion(c), style, state);    }    private Region getRegion(JComponent c) {        return SynthLookAndFeel.getRegion(c);    }    private int getComponentState(JComponent c) {        return SynthLookAndFeel.getComponentState(c);    }    private SynthContext getContext(JComponent c, Region region) {        return getContext(c, region, getComponentState(c, region));    }    private SynthContext getContext(JComponent c, Region region, int state) {        SynthStyle style = trackStyle;        if (region == Region.SCROLL_BAR_THUMB) {            style = thumbStyle;        }        return SynthContext.getContext(SynthContext.class, c, region, style,                                       state);    }    private int getComponentState(JComponent c, Region region) {        if (region == Region.SCROLL_BAR_THUMB && thumbActive &&c.isEnabled()) {            return MOUSE_OVER;        }        return SynthLookAndFeel.getComponentState(c);    }    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 update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        paint(context, g);        context.dispose();    }    public void paint(Graphics g, JComponent c) {        SynthContext context = getContext(c);        paint(context, g);        context.dispose();    }    protected void paint(SynthContext context, Graphics g) {        SynthContext subcontext = getContext(scrollbar,                                             Region.SCROLL_BAR_TRACK);	paintTrack(subcontext, g, getTrackBounds());        subcontext.dispose();        subcontext = getContext(scrollbar, Region.SCROLL_BAR_THUMB);	paintThumb(subcontext, g, getThumbBounds());        subcontext.dispose();    }    protected void paintTrack(SynthContext ss, Graphics g,                              Rectangle trackBounds) {        SynthLookAndFeel.updateSubregion(ss, g, trackBounds);    }    protected void paintThumb(SynthContext ss, Graphics g,                              Rectangle thumbBounds) {        SynthLookAndFeel.updateSubregion(ss, g, thumbBounds);    }    int getTrackHighlight() {        return trackHighlight;    }    Rectangle getDecreaseHighlightRegion(Rectangle rect) {	Insets insets = scrollbar.getInsets(tmpInsets);	Rectangle thumbR = getThumbBounds();	if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {            rect.x = insets.left;	    rect.y = decrButton.getY() + decrButton.getHeight();	    rect.width = scrollbar.getWidth() - (insets.left + insets.right);	    rect.height = thumbR.y - rect.y;	}	else {	    rect.x = decrButton.getX() + decrButton.getHeight();	    rect.y = insets.top;	    rect.width = thumbR.x - rect.x;	    rect.height = scrollbar.getHeight() - (insets.top + insets.bottom);	}        return rect;    }    Rectangle getIncreaseHighlightRegion(Rectangle rect) {	Insets insets = scrollbar.getInsets(tmpInsets);	Rectangle thumbR = getThumbBounds();	if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {	    rect.x = insets.left;	    rect.y = thumbR.y + thumbR.height;	    rect.width = scrollbar.getWidth() - (insets.left + insets.right);	    rect.height = incrButton.getY() - rect.y;	} 	else {	    rect.x = thumbR.x + thumbR.width;	    rect.y = insets.top;	    rect.width = incrButton.getX() - rect.x;	    rect.height = scrollbar.getHeight() - (insets.top + insets.bottom);	}        return rect;    }    /**     * 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 

⌨️ 快捷键说明

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