📄 synthsliderui.java
字号:
/* * @(#)SynthSliderUI.java 1.94 01/12/03 * * Copyright 2004 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 com.sun.java.swing.SwingUtilities2;/** * Synth's SliderUI. * * @version 1.21, 12/19/03 * @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() { 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 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; int centerY = slider.getHeight() / 2 - contentDim.height / 2; // Layout the components. valueRect.x = trackRect.x = tickRect.x = labelRect.x = insetCache.left; valueRect.width = trackRect.width = tickRect.width = labelRect.width = contentDim.width; valueRect.y = centerY; centerY += valueRect.height + 2; trackRect.y = centerY + trackInsets.top; centerY += trackRect.height + trackInsets.top + trackInsets.bottom; tickRect.y = centerY; centerY += tickRect.height + 2; labelRect.y = centerY; centerY += labelRect.height; } else { // Calculate the width of all the subcomponents so we can center // them. trackRect.width = trackHeight; tickRect.width = 0; if (slider.getPaintTicks()) { tickRect.width = getTickLength(); } labelRect.width = 0; if (slider.getPaintLabels()) { labelRect.width = getWidthOfWidestLabel(); } valueRect.y = insetCache.top; valueRect.height = 0; if (paintValue) { valueRect.height = synthGraphics.getMaximumCharHeight(context); } contentDim.width = trackRect.width + trackInsets.left + trackInsets.right + tickRect.width + labelRect.width + 2 + insetCache.left + insetCache.right; contentDim.height = slider.getHeight() - insetCache.top - insetCache.bottom; int startX = slider.getWidth() / 2 - contentDim.width / 2; // Get the max width of the min or max value of the slider. FontMetrics fm = slider.getFontMetrics(slider.getFont()); valueRect.width = Math.max( synthGraphics.computeStringWidth(context, slider.getFont(), fm, "" + slider.getMaximum()), synthGraphics.computeStringWidth(context, slider.getFont(), fm, "" + slider.getMinimum())); // Check to see if we need to make the width larger due to the size // of the value string. The value string is centered above the // track. if (valueRect.width > (trackRect.width + trackInsets.left + trackInsets.right)) { int diff = (valueRect.width - (trackRect.width + trackInsets.left + trackInsets.right)) / 2; contentDim.width += diff; startX += diff; } // Layout the components. trackRect.y = tickRect.y = labelRect.y = valueRect.y + valueRect.height; trackRect.height = tickRect.height = labelRect.height = contentDim.height - valueRect.height; if (SynthLookAndFeel.isLeftToRight(slider)) { trackRect.x = startX + trackInsets.left; startX += trackRect.width + trackInsets.right + trackInsets.left; tickRect.x = startX; startX += tickRect.width + 2; labelRect.x = startX; } else { labelRect.x = startX; startX += labelRect.width + 2; tickRect.x = startX; startX += tickRect.width + trackInsets.left; trackRect.x = startX; } } context.dispose(); } 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 + trackBorder; } else { int valuePosition = yPositionForValue(slider.getValue()); thumbRect.x = trackRect.x + trackBorder; thumbRect.y = valuePosition - (thumbRect.height / 2); } } protected void calculateTickRect() { if (slider.getOrientation() == JSlider.HORIZONTAL) { tickRect.x = trackRect.x; tickRect.y = trackRect.y + trackRect.height + 2 + getTickLength(); tickRect.width = trackRect.width; tickRect.height = getTickLength(); if (!slider.getPaintTicks()) { --tickRect.y; tickRect.height = 0; } } else { if (SynthLookAndFeel.isLeftToRight(slider)) { tickRect.x = trackRect.x + trackRect.width; tickRect.width = getTickLength(); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -