synthsliderui.java

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

JAVA
1,576
字号
/* * @(#)SynthSliderUI.java    1.94 01/12/03 * * 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.Component;import java.awt.Container;import java.awt.Adjustable;import java.awt.event.*;import java.awt.Graphics;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;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 Synth L&F implementation of SliderUI. * * @version 1.15, 01/23/03 (originally from version 1.94 of BasicSliderUI) * @author Tom Santos * @author Joshua Outwater */class SynthSliderUI extends SliderUI implements SynthUI {    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 Insets insetCache = null;    protected boolean leftToRightCache = true;    protected Dimension contentDim = null;    protected Rectangle labelRect = null;    protected Rectangle tickRect = null;    protected Rectangle trackRect = null;    protected Rectangle thumbRect = null;    protected Rectangle valueRect = null;    protected boolean paintValue;    /** The distance that the track is from the side of the control. */    protected int trackBuffer = 0;    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 static int trackHeight;    private static int trackBorder;    private static int thumbWidth;    private static int thumbHeight;    private SynthStyle style;    private SynthStyle sliderTrackStyle;    private SynthStyle sliderThumbStyle;    // PENDING (joutwate):    // 2 pixel gap on left and right of track reserved for focus.    // If not focusable that space goes to the track.    /** Used to determine the color to paint the thumb. */    private transient boolean thumbActive;    protected Timer scrollTimer;    protected JSlider slider;    ///////////////////////////////////////////////////    // ComponentUI Interface Implementation methods    ///////////////////////////////////////////////////    public static ComponentUI createUI(JComponent c) {        return new SynthSliderUI();    }    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));    }    /**     * Returns the prefix used in looking up property values.     */    protected String getPropertyPrefix() {        return "Slider.";    }    public void installUI(JComponent c) {        this.slider = (JSlider)c;        slider.setEnabled(slider.isEnabled());        slider.setOpaque(true);        isDragging = false;                installDefaults();        installListeners();        installKeyboardActions();        insetCache = slider.getInsets();        leftToRightCache = SynthLookAndFeel.isLeftToRight(slider);        contentDim = new Dimension();        labelRect = new Rectangle();        tickRect = new Rectangle();        trackRect = new Rectangle();        thumbRect = new Rectangle();        valueRect = new Rectangle();        calculateGeometry();    }       public void uninstallUI(JComponent c) {    }    protected void installDefaults() {        fetchStyle(slider);        SynthContext context = getContext(slider, ENABLED);        // PENDING: change me.        highlightColor = UIManager.getColor("Slider.highlight");        shadowColor = UIManager.getColor("Slider.shadow");        context.dispose();    }    private void fetchStyle(JSlider c) {        SynthContext context = getContext(c, ENABLED);        SynthStyle oldStyle = style;        style = SynthLookAndFeel.updateStyle(context, this);        if (style != oldStyle) {            thumbWidth =                style.getInt(context, getPropertyPrefix() + "thumbWidth", 30);            thumbHeight =                style.getInt(context, getPropertyPrefix() + "thumbHeight", 14);            trackBorder =                style.getInt(context, getPropertyPrefix() + "trackBorder", 1);            trackHeight = thumbHeight + trackBorder * 2;            paintValue = style.getBoolean(context,                    getPropertyPrefix() + "paintValue", true);        }        context.dispose();        context = getContext(c, Region.SLIDER_TRACK, ENABLED);        sliderTrackStyle =            SynthLookAndFeel.updateStyle(context, this);        context.dispose();        context = getContext(c, Region.SLIDER_THUMB, ENABLED);        sliderThumbStyle =            SynthLookAndFeel.updateStyle(context, this);        context.dispose();    }    protected void uninstallDefaults() {        SynthContext context = getContext(slider, ENABLED);        style.uninstallDefaults(context);        context.dispose();        style = null;        context = getContext(slider, Region.SLIDER_TRACK, ENABLED);        sliderTrackStyle.uninstallDefaults(context);        context.dispose();        sliderTrackStyle = null;        context = getContext(slider, Region.SLIDER_THUMB, ENABLED);        sliderThumbStyle.uninstallDefaults(context);        context.dispose();        sliderThumbStyle = null;    }    protected void installListeners() {        if ((trackListener = createTrackListener()) != null) {            slider.addMouseListener(trackListener);            slider.addMouseMotionListener(trackListener);        }        if ((focusListener = createFocusListener()) != null) {            slider.addFocusListener(focusListener);        }        if ((componentListener = createComponentListener()) != null) {            slider.addComponentListener(componentListener);        }        if ((propertyChangeListener = createPropertyChangeListener()) != null) {            slider.addPropertyChangeListener(propertyChangeListener);        }        if ((changeListener = createChangeListener()) != null) {            slider.getModel().addChangeListener(changeListener);        }        if ((scrollListener = createScrollListener()) != null) {            scrollTimer = new Timer(100, scrollListener);            scrollTimer.setInitialDelay(300);        }    }    protected void uninstallListeners() {        if (trackListener != null) {            slider.removeMouseListener(trackListener);            slider.removeMouseMotionListener(trackListener);        }        if (focusListener != null) {            slider.removeFocusListener(focusListener);        }        if (componentListener != null) {            slider.removeComponentListener(componentListener);        }        if (propertyChangeListener != null) {            slider.removePropertyChangeListener(propertyChangeListener);        }        if (changeListener != null) {            slider.getModel().removeChangeListener(changeListener);        }        scrollTimer = null;    }    protected TrackListener createTrackListener() {        return new TrackListener();    }    protected ScrollListener createScrollListener() {        return new ScrollListener();    }    protected ComponentListener createComponentListener() {        return new ComponentHandler();    }    protected FocusListener createFocusListener() {        return new FocusHandler();    }    protected PropertyChangeListener createPropertyChangeListener() {        return new PropertyChangeHandler();    }    protected ChangeListener createChangeListener() {        return new ChangeHandler();    }    protected void installKeyboardActions() {        InputMap km = getInputMap(JComponent.WHEN_FOCUSED);        SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED, km);        LazyActionMap.installLazyActionMap(slider, SynthSliderUI.class,                                           "Slider.actionMap");    }    InputMap getInputMap(int condition) {        if (condition == JComponent.WHEN_FOCUSED) {            SynthContext context = getContext(slider, ENABLED);            InputMap keyMap = (InputMap)style.get(context,                "Slider.focusInputMap");            InputMap rtlKeyMap;            if (slider.getComponentOrientation().isLeftToRight() ||                    ((rtlKeyMap = (InputMap)style.get(context,                        "Slider.focusInputMap.RightToLeft")) == null)) {                context.dispose();                return keyMap;            } else {                rtlKeyMap.setParent(keyMap);                context.dispose();                return rtlKeyMap;            }        }        return null;    }    protected void uninstallKeyboardActions() {        SwingUtilities.replaceUIActionMap(slider, null);        SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED, null);    }    protected TrackListener createTrackListener(JSlider slider) {        return new TrackListener();    }    private void updateThumbState(int x, int y) {        setThumbActive(thumbRect.contains(x, y));    }    private void setThumbActive(boolean active) {        if (thumbActive != active) {            thumbActive = active;            slider.repaint(thumbRect);        }    }    public Dimension getPreferredSize(JComponent c)  {        recalculateIfInsetsChanged();        Dimension d = new Dimension(contentDim);        if (slider.getOrientation() == JSlider.VERTICAL) {            d.height = 200;        } else {            d.width = 200;        }        return d;    }    public Dimension getMinimumSize(JComponent c) {        recalculateIfInsetsChanged();        Dimension d = new Dimension(contentDim);        if (slider.getOrientation() == JSlider.VERTICAL) {            d.height = thumbRect.height + insetCache.top + insetCache.bottom;        } else {            d.width = thumbRect.width + insetCache.left + insetCache.right;        }        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() {        layout();        calculateThumbLocation();    }    protected void layout() {        SynthContext context = getContext(slider);        SynthGraphics synthGraphics = style.getSynthGraphics(context);        // Set the thumb size.        Dimension size = getThumbSize();        thumbRect.setSize(size.width, size.height);        // Get the insets for the track.        Insets trackInsets = new Insets(0, 0, 0, 0);        style.getInsets(getContext(slider, Region.SLIDER_TRACK), trackInsets);        if (slider.getOrientation() == JSlider.HORIZONTAL) {            // Calculate the height of all the subcomponents so we can center            // them.            valueRect.height = 0;            if (paintValue) {                valueRect.height =                    synthGraphics.getMaximumCharHeight(slider);            }

⌨️ 快捷键说明

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