?? basicsliderui.java
字號:
/* * @(#)BasicSliderUI.java 1.100 03/12/19 * * Copyright 2004 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.*;import sun.swing.DefaultLookup;import sun.swing.UIAction;/** * A Basic L&F implementation of SliderUI. * * @version 1.100 12/19/03 * @author Tom Santos */public class BasicSliderUI extends SliderUI{ // Old actions forward to an instance of this. private static final Actions SHARED_ACTION = new Actions(); 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 transient boolean isDragging; protected TrackListener trackListener; protected ChangeListener changeListener; protected ComponentListener componentListener; protected FocusListener focusListener; protected ScrollListener scrollListener; protected PropertyChangeListener propertyChangeListener; private Handler handler; // 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; } /** * Returns true if the user is dragging the slider. * * @return true if the user is dragging the slider * @since 1.5 */ protected boolean isDragging() { return isDragging; } ///////////////////////////////////////////////////////////////////////////// // 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()); LookAndFeel.installProperty(slider, "opaque", Boolean.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 getHandler(); } protected ComponentListener createComponentListener(JSlider slider) { return getHandler(); } protected FocusListener createFocusListener(JSlider slider) { return getHandler(); } protected ScrollListener createScrollListener( JSlider slider ) { return new ScrollListener(); } protected PropertyChangeListener createPropertyChangeListener( JSlider slider) { return getHandler(); } private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } 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); handler = null; } protected void installKeyboardActions( JSlider slider ) { InputMap km = getInputMap(JComponent.WHEN_FOCUSED, slider); SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED, km); LazyActionMap.installLazyActionMap(slider, BasicSliderUI.class, "Slider.actionMap"); } InputMap getInputMap(int condition, JSlider slider) { if (condition == JComponent.WHEN_FOCUSED) { InputMap keyMap = (InputMap)DefaultLookup.get(slider, this, "Slider.focusInputMap"); InputMap rtlKeyMap; if (slider.getComponentOrientation().isLeftToRight() || ((rtlKeyMap = (InputMap)DefaultLookup.get(slider, this, "Slider.focusInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } return null; } /** * Populates ComboBox's actions. */ static void loadActionMap(LazyActionMap map) { map.put(new Actions(Actions.POSITIVE_UNIT_INCREMENT)); map.put(new Actions(Actions.POSITIVE_BLOCK_INCREMENT)); map.put(new Actions(Actions.NEGATIVE_UNIT_INCREMENT)); map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT)); map.put(new Actions(Actions.MIN_SCROLL_INCREMENT)); map.put(new Actions(Actions.MAX_SCROLL_INCREMENT)); } protected void uninstallKeyboardActions( JSlider slider ) { SwingUtilities.replaceUIActionMap(slider, null); SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED, null); } public Dimension getPreferredHorizontalSize() { Dimension horizDim = (Dimension)DefaultLookup.get(slider, this, "Slider.horizontalSize"); if (horizDim == null) { horizDim = new Dimension(200, 21); } return horizDim; } public Dimension getPreferredVerticalSize() { Dimension vertDim = (Dimension)DefaultLookup.get(slider, this, "Slider.verticalSize"); if (vertDim == null) { vertDim = new Dimension(21, 200); } return vertDim; } public Dimension getMinimumHorizontalSize() { Dimension minHorizDim = (Dimension)DefaultLookup.get(slider, this, "Slider.minimumHorizontalSize"); if (minHorizDim == null) { minHorizDim = new Dimension(36, 21); } return minHorizDim; } public Dimension getMinimumVerticalSize() { Dimension minVertDim = (Dimension)DefaultLookup.get(slider, this, "Slider.minimumVerticalSize"); if (minVertDim == null) { minVertDim = new Dimension(21, 36); } return minVertDim; } 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); } } protected void calculateTrackBuffer() { if ( slider.getPaintLabels() && slider.getLabelTable() != null ) { Component highLabel = getHighestValueLabel(); Component lowLabel = getLowestValueLabel(); if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -