synthsliderui.java

来自「JAVA 所有包」· Java 代码 · 共 889 行 · 第 1/3 页

JAVA
889
字号
/* * @(#)SynthSliderUI.java    1.94 01/12/03 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.synth;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.*;import javax.swing.plaf.basic.BasicSliderUI;import sun.swing.plaf.synth.SynthUI;import sun.swing.SwingUtilities2;/** * Synth's SliderUI. * * @version 1.30, 02/07/06 * @author Joshua Outwater */class SynthSliderUI extends BasicSliderUI implements PropertyChangeListener,        SynthUI {    protected Dimension contentDim = new Dimension();    protected Rectangle valueRect = new Rectangle();    protected boolean paintValue;    private int trackHeight;    private int trackBorder;    private int thumbWidth;    private int thumbHeight;    private SynthStyle style;    private SynthStyle sliderTrackStyle;    private SynthStyle sliderThumbStyle;    /** Used to determine the color to paint the thumb. */    private transient boolean thumbActive;    ///////////////////////////////////////////////////    // ComponentUI Interface Implementation methods    ///////////////////////////////////////////////////    public static ComponentUI createUI(JComponent c) {        return new SynthSliderUI((JSlider)c);    }    public SynthSliderUI(JSlider c) {        super(c);    }    protected void installDefaults(JSlider slider) {        updateStyle(slider);    }    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(JSlider slider) {        super.installListeners(slider);        slider.addPropertyChangeListener(this);    }    protected void uninstallListeners(JSlider slider) {        slider.removePropertyChangeListener(this);        super.uninstallListeners(slider);    }    private void updateStyle(JSlider c) {        SynthContext context = getContext(c, ENABLED);        SynthStyle oldStyle = style;        style = SynthLookAndFeel.updateStyle(context, this);        if (style != oldStyle) {            thumbWidth =                style.getInt(context, "Slider.thumbWidth", 30);            thumbHeight =                style.getInt(context, "Slider.thumbHeight", 14);            trackBorder =                style.getInt(context, "Slider.trackBorder", 1);            trackHeight = thumbHeight + trackBorder * 2;            paintValue = style.getBoolean(context,                    "Slider.paintValue", true);            if (oldStyle != null) {                uninstallKeyboardActions(c);                installKeyboardActions(c);            }        }        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 TrackListener createTrackListener(JSlider s) {        return new SynthTrackListener();    }    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 int getBaseline(JComponent c, int width, int height) {        if (c == null) {            throw new NullPointerException("Component must be non-null");        }        if (width < 0 || height < 0) {            throw new IllegalArgumentException(                    "Width and height must be >= 0");        }        if (slider.getPaintLabels() && labelsHaveSameBaselines()) {            // Get the insets for the track.            Insets trackInsets = new Insets(0, 0, 0, 0);            SynthContext trackContext = getContext(slider,                                                   Region.SLIDER_TRACK);            style.getInsets(trackContext, trackInsets);            trackContext.dispose();            if (slider.getOrientation() == JSlider.HORIZONTAL) {                int valueHeight = 0;                if (paintValue) {                    SynthContext context = getContext(slider);                    valueHeight = context.getStyle().getGraphicsUtils(context).                            getMaximumCharHeight(context);                    context.dispose();                }                int tickHeight = 0;                if (slider.getPaintTicks()) {                    tickHeight = getTickLength();                }                int labelHeight = getHeightOfTallestLabel();                int contentHeight = valueHeight + trackHeight +                        trackInsets.top + trackInsets.bottom +                        tickHeight + labelHeight + 4;                int centerY = height / 2 - contentHeight / 2;                centerY += valueHeight + 2;                centerY += trackHeight + trackInsets.top + trackInsets.bottom;                centerY += tickHeight + 2;                Component label = (Component)slider.getLabelTable().                                   elements().nextElement();                Dimension pref = label.getPreferredSize();                return centerY + label.getBaseline(pref.width, pref.height);            }            else { // VERTICAL                Integer value = slider.getInverted() ? getLowestValue() :                                                       getHighestValue();                if (value != null) {                    int valueY = insetCache.top;                    int valueHeight = 0;                    if (paintValue) {                        SynthContext context = getContext(slider);                        valueHeight = context.getStyle().getGraphicsUtils(                                context).getMaximumCharHeight(context);                        context.dispose();                    }                    int contentHeight = height - insetCache.top -                            insetCache.bottom;                    int trackY = valueY + valueHeight;                    int trackHeight = contentHeight - valueHeight;                    int yPosition = yPositionForValue(value.intValue(), trackY,                                                      trackHeight);                    Component label = (Component)slider.getLabelTable().                            get(value);                    Dimension pref = label.getPreferredSize();                    return yPosition - pref.height / 2 +                            label.getBaseline(pref.width, pref.height);                }            }        }        return -1;    }    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;    }    protected void calculateGeometry() {        layout();        calculateThumbLocation();    }    protected void layout() {        SynthContext context = getContext(slider);        SynthGraphicsUtils synthGraphics = style.getGraphicsUtils(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);        SynthContext trackContext = getContext(slider, Region.SLIDER_TRACK);        style.getInsets(trackContext, trackInsets);        trackContext.dispose();        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(context);            }            trackRect.height = trackHeight;            tickRect.height = 0;            if (slider.getPaintTicks()) {                tickRect.height = getTickLength();            }            labelRect.height = 0;            if (slider.getPaintLabels()) {                labelRect.height = getHeightOfTallestLabel();            }            contentDim.height = valueRect.height + trackRect.height                + trackInsets.top + trackInsets.bottom                + tickRect.height + labelRect.height + 4;            contentDim.width = slider.getWidth() - insetCache.left                - insetCache.right;            // Check if any of the labels will paint out of bounds.            int pad = 0;            if (slider.getPaintLabels()) {                // Calculate the track rectangle.  It is necessary for                // xPositionForValue to return correct values.                trackRect.x = insetCache.left;                trackRect.width = contentDim.width;

⌨️ 快捷键说明

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