⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicsliderui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * @(#)BasicSliderUI.java	1.95 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.Component;import java.awt.Container;import java.awt.Adjustable;import java.awt.event.*;import java.awt.Graphics;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.Point;import java.awt.Insets;import java.awt.Color;import java.awt.IllegalComponentStateException;import java.awt.Polygon;import java.beans.*;import java.util.Dictionary;import java.util.Enumeration;import javax.swing.border.AbstractBorder;import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;/** * A Basic L&F implementation of SliderUI. * * @version 1.95 01/23/03 * @author Tom Santos */public class BasicSliderUI extends SliderUI{    public static final int POSITIVE_SCROLL = +1;    public static final int NEGATIVE_SCROLL = -1;    public static final int MIN_SCROLL = -2;    public static final int MAX_SCROLL = +2;    protected Timer scrollTimer;    protected JSlider slider;    protected Insets focusInsets = null;    protected Insets insetCache = null;    protected boolean leftToRightCache = true;    protected Rectangle focusRect = null;    protected Rectangle contentRect = null;    protected Rectangle labelRect = null;    protected Rectangle tickRect = null;    protected Rectangle trackRect = null;    protected Rectangle thumbRect = null;    protected int trackBuffer = 0;  // The distance that the track is from the side of the control    private static final Dimension PREFERRED_HORIZONTAL_SIZE = new Dimension(200, 21);    private static final Dimension PREFERRED_VERTICAL_SIZE = new Dimension(21, 200);    private static final Dimension MINIMUM_HORIZONTAL_SIZE = new Dimension(36, 21);    private static final Dimension MINIMUM_VERTICAL_SIZE = new Dimension(21, 36);    private transient boolean isDragging;    protected TrackListener trackListener;    protected ChangeListener changeListener;    protected ComponentListener componentListener;    protected FocusListener focusListener;    protected ScrollListener scrollListener;    protected PropertyChangeListener propertyChangeListener;    // Colors    private Color shadowColor;    private Color highlightColor;    private Color focusColor;    protected Color getShadowColor() {        return shadowColor;    }    protected Color getHighlightColor() {        return highlightColor;    }    protected Color getFocusColor() {        return focusColor;    }    /////////////////////////////////////////////////////////////////////////////    // ComponentUI Interface Implementation methods    /////////////////////////////////////////////////////////////////////////////    public static ComponentUI createUI(JComponent b)    {        return new BasicSliderUI((JSlider)b);    }    public BasicSliderUI(JSlider b)   {    }    public void installUI(JComponent c)   {        slider = (JSlider) c;        slider.setEnabled(slider.isEnabled());        slider.setOpaque(true);        isDragging = false;        trackListener = createTrackListener( slider );        changeListener = createChangeListener( slider );        componentListener = createComponentListener( slider );        focusListener = createFocusListener( slider );        scrollListener = createScrollListener( slider );	propertyChangeListener = createPropertyChangeListener( slider );	installDefaults( slider );	installListeners( slider );	installKeyboardActions( slider );        scrollTimer = new Timer( 100, scrollListener );        scrollTimer.setInitialDelay( 300 );   	insetCache = slider.getInsets();	leftToRightCache = BasicGraphicsUtils.isLeftToRight(slider);	focusRect = new Rectangle();	contentRect = new Rectangle();	labelRect = new Rectangle();	tickRect = new Rectangle();	trackRect = new Rectangle();	thumbRect = new Rectangle();	calculateGeometry(); // This figures out where the labels, ticks, track, and thumb are.    }       public void uninstallUI(JComponent c) {        if ( c != slider )            throw new IllegalComponentStateException(                                                    this + " was asked to deinstall() "                                                     + c + " when it only knows about "                                                     + slider + ".");        LookAndFeel.uninstallBorder(slider);        scrollTimer.stop();        scrollTimer = null;	uninstallListeners( slider );	uninstallKeyboardActions(slider);	focusInsets = null;	insetCache = null;	leftToRightCache = true;	focusRect = null;	contentRect = null;	labelRect = null;	tickRect = null;	trackRect = null;        thumbRect = null;        trackListener = null;        changeListener = null;        componentListener = null;        focusListener = null;        scrollListener = null;	propertyChangeListener = null;        slider = null;    }    protected void installDefaults( JSlider slider ) {        LookAndFeel.installBorder(slider, "Slider.border");        LookAndFeel.installColors(slider, "Slider.background", "Slider.foreground");        highlightColor = UIManager.getColor("Slider.highlight");        shadowColor = UIManager.getColor("Slider.shadow");        focusColor = UIManager.getColor("Slider.focus");	focusInsets = (Insets)UIManager.get( "Slider.focusInsets" );    }    protected TrackListener createTrackListener( JSlider slider ) {        return new TrackListener();    }    protected ChangeListener createChangeListener( JSlider slider ) {        return new ChangeHandler();    }    protected ComponentListener createComponentListener( JSlider slider ) {        return new ComponentHandler();    }    protected FocusListener createFocusListener( JSlider slider ) {        return new FocusHandler();    }    protected ScrollListener createScrollListener( JSlider slider ) {        return new ScrollListener();    }    protected PropertyChangeListener createPropertyChangeListener( JSlider slider ) {        return new PropertyChangeHandler();    }    protected void installListeners( JSlider slider ) {        slider.addMouseListener(trackListener);        slider.addMouseMotionListener(trackListener);        slider.addFocusListener(focusListener);        slider.addComponentListener(componentListener);        slider.addPropertyChangeListener( propertyChangeListener );        slider.getModel().addChangeListener(changeListener);    }    protected void uninstallListeners( JSlider slider ) {        slider.removeMouseListener(trackListener);        slider.removeMouseMotionListener(trackListener);        slider.removeFocusListener(focusListener);        slider.removeComponentListener(componentListener);        slider.removePropertyChangeListener( propertyChangeListener );        slider.getModel().removeChangeListener(changeListener);    }    protected void installKeyboardActions( JSlider slider ) {	InputMap km = getInputMap(JComponent.WHEN_FOCUSED);	SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED,				       km);	ActionMap am = getActionMap();	SwingUtilities.replaceUIActionMap(slider, am);    }    InputMap getInputMap(int condition) {	if (condition == JComponent.WHEN_FOCUSED) {	    InputMap keyMap = (InputMap)UIManager.get("Slider.focusInputMap");	    InputMap rtlKeyMap;	    if (slider.getComponentOrientation().isLeftToRight() ||		((rtlKeyMap = (InputMap)UIManager.get("Slider.focusInputMap.RightToLeft")) == null)) {		return keyMap;	    } else {		rtlKeyMap.setParent(keyMap);		return rtlKeyMap;	    }	}	return null;    }    ActionMap getActionMap() {	ActionMap map = (ActionMap)UIManager.get("Slider.actionMap");	if (map == null) {	    map = createActionMap();	    if (map != null) {		UIManager.getLookAndFeelDefaults().put                          ("Slider.actionMap", map);	    }	}	return map;    }    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 uninstallKeyboardActions( JSlider slider ) {	SwingUtilities.replaceUIActionMap(slider, null);	SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED,					 null);    }    public Dimension getPreferredHorizontalSize() {        return PREFERRED_HORIZONTAL_SIZE;    }    public Dimension getPreferredVerticalSize() {        return PREFERRED_VERTICAL_SIZE;    }    public Dimension getMinimumHorizontalSize() {        return MINIMUM_HORIZONTAL_SIZE;    }    public Dimension getMinimumVerticalSize() {        return MINIMUM_VERTICAL_SIZE;    }    public Dimension getPreferredSize(JComponent c)    {        recalculateIfInsetsChanged();        Dimension d;        if ( slider.getOrientation() == JSlider.VERTICAL ) {            d = new Dimension(getPreferredVerticalSize());	    d.width = insetCache.left + insetCache.right;	    d.width += focusInsets.left + focusInsets.right;	    d.width += trackRect.width + tickRect.width + labelRect.width;        }        else {            d = new Dimension(getPreferredHorizontalSize());	    d.height = insetCache.top + insetCache.bottom;	    d.height += focusInsets.top + focusInsets.bottom;	    d.height += trackRect.height + tickRect.height + labelRect.height;        }        return d;    }    public Dimension getMinimumSize(JComponent c)  {        recalculateIfInsetsChanged();        Dimension d;        if ( slider.getOrientation() == JSlider.VERTICAL ) {            d = new Dimension(getMinimumVerticalSize());	    d.width = insetCache.left + insetCache.right;	    d.width += focusInsets.left + focusInsets.right;	    d.width += trackRect.width + tickRect.width + labelRect.width;        }        else {            d = new Dimension(getMinimumHorizontalSize());	    d.height = insetCache.top + insetCache.bottom;	    d.height += focusInsets.top + focusInsets.bottom;	    d.height += trackRect.height + tickRect.height + labelRect.height;        }        return d;    }    public Dimension getMaximumSize(JComponent c) {        Dimension d = getPreferredSize(c);        if ( slider.getOrientation() == JSlider.VERTICAL ) {            d.height = Short.MAX_VALUE;        }        else {            d.width = Short.MAX_VALUE;        }        return d;    }    protected void calculateGeometry() {        calculateFocusRect();        calculateContentRect(); 	calculateThumbSize();	calculateTrackBuffer();	calculateTrackRect();	calculateTickRect();	calculateLabelRect();	calculateThumbLocation();    }      protected void calculateFocusRect() {        focusRect.x = insetCache.left;	focusRect.y = insetCache.top;	focusRect.width = slider.getWidth() - (insetCache.left + insetCache.right);	focusRect.height = slider.getHeight() - (insetCache.top + insetCache.bottom);    }      protected void calculateThumbSize() {	Dimension size = getThumbSize();	thumbRect.setSize( size.width, size.height );    }      protected void calculateContentRect() {        contentRect.x = focusRect.x + focusInsets.left;        contentRect.y = focusRect.y + focusInsets.top;        contentRect.width = focusRect.width - (focusInsets.left + focusInsets.right);        contentRect.height = focusRect.height - (focusInsets.top + focusInsets.bottom);    }    protected void calculateThumbLocation() {        if ( slider.getSnapToTicks() ) {	    int sliderValue = slider.getValue();	    int snappedValue = sliderValue; 	    int majorTickSpacing = slider.getMajorTickSpacing();	    int minorTickSpacing = slider.getMinorTickSpacing();	    int tickSpacing = 0;	    	    if ( minorTickSpacing > 0 ) {	        tickSpacing = minorTickSpacing;	    }	    else if ( majorTickSpacing > 0 ) {	        tickSpacing = majorTickSpacing;	    }	    if ( tickSpacing != 0 ) {	        // If it's not on a tick, change the value	        if ( (sliderValue - slider.getMinimum()) % tickSpacing != 0 ) {		    float temp = (float)(sliderValue - slider.getMinimum()) / (float)tickSpacing;		    int whichTick = Math.round( temp );		    snappedValue = slider.getMinimum() + (whichTick * tickSpacing);		}				if( snappedValue != sliderValue ) { 		    slider.setValue( snappedValue );		}	    }	}	        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {            int valuePosition = xPositionForValue(slider.getValue());	    thumbRect.x = valuePosition - (thumbRect.width / 2);	    thumbRect.y = trackRect.y;        }        else {            int valuePosition = yPositionForValue(slider.getValue());	    	    thumbRect.x = trackRect.x;	    thumbRect.y = valuePosition - (thumbRect.height / 2);        }    }

⌨️ 快捷键说明

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