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

📄 basicsliderui.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* BasicSliderUI.java --   Copyright (C) 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.basic;import java.awt.Color;import java.awt.Component;import java.awt.ComponentOrientation;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.Point;import java.awt.Polygon;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.MouseEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.Dictionary;import java.util.Enumeration;import javax.swing.AbstractAction;import javax.swing.BoundedRangeModel;import javax.swing.JComponent;import javax.swing.JLabel;import javax.swing.JSlider;import javax.swing.LookAndFeel;import javax.swing.SwingUtilities;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.MouseInputAdapter;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.SliderUI;/** * <p> * BasicSliderUI.java This is the UI delegate in the Basic look and feel that * paints JSliders. * </p> *  * <p> * The UI delegate keeps track of 6 rectangles that place the various parts of * the JSlider inside the component. * </p> *  * <p> * The rectangles are organized as follows: * </p> * <pre> *     +-------------------------------------------------------+ <-- focusRect *     |                                                       | *     |  +==+-------------------+==+--------------------+==+<------ contentRect *     |  |  |                   |  |<---thumbRect       |  |  | *     |  |  |    TRACK          |  |                    |<--------- trackRect *     |  |  +-------------------+==+--------------------+  |  | *     |  |  |                                           |  |  | *     |  |  |          TICKS GO HERE                    |<-------- tickRect *     |  |  |                                           |  |  | *     |  +==+-------------------------------------------+==+  | *     |  |  |                                           |  |  | *     |  |  |                                           |  |<----- labelRect *     |  |  |                 LABELS GO HERE            |  |  | *     |  |  |                                           |  |  | *     |  |  |                                           |  |  | *     |  |  |                                           |  |  | *     |  |  |                                           |  |  | *     |  |                                              |  |  | * </pre> *  * <p> * The space between the contentRect and the focusRect are the FocusInsets. * </p> *  * <p> * The space between the focusRect and the component bounds is the insetCache * which are the component's insets. * </p> *  * <p> * The top of the thumb is the top of the contentRect. The trackRect has to be * as tall as the thumb. * </p> *  * <p> * The trackRect and tickRect do not start from the left edge of the * focusRect. They are trackBuffer away from each side of the focusRect. This * is so that the thumb has room to move. * </p> *  * <p> * The labelRect does start right against the contentRect's left and right * edges and it gets all remaining space. * </p> */public class BasicSliderUI extends SliderUI{  /**   * Helper class that listens to the {@link JSlider}'s model for changes.   *   * @specnote Apparently this class was intended to be protected,   *           but was made public by a compiler bug and is now   *           public for compatibility.   */  public class ChangeHandler implements ChangeListener  {    /**     * Called when the slider's model has been altered. The UI delegate should     * recalculate any rectangles that are dependent on the model for their     * positions and repaint.     *     * @param e A static {@link ChangeEvent} passed from the model.     */    public void stateChanged(ChangeEvent e)    {      // Maximum, minimum, and extent values will be taken      // care of automatically when the slider is repainted.      // Only thing that needs recalculation is the thumb.      calculateThumbLocation();      slider.repaint();    }  }  /**   * Helper class that listens for resize events.   *   * @specnote Apparently this class was intended to be protected,   *           but was made public by a compiler bug and is now   *           public for compatibility.   */  public class ComponentHandler extends ComponentAdapter  {    /**     * Called when the size of the component changes. The UI delegate should     * recalculate any rectangles that are dependent on the model for their     * positions and repaint.     *     * @param e A {@link ComponentEvent}.     */    public void componentResized(ComponentEvent e)    {      calculateGeometry();      slider.revalidate();      slider.repaint();    }  }  /**   * Helper class that listens for focus events.   *   * @specnote Apparently this class was intended to be protected,   *           but was made public by a compiler bug and is now   *           public for compatibility.   */  public class FocusHandler implements FocusListener  {    /**     * Called when the {@link JSlider} has gained focus.  It should repaint     * the slider with the focus drawn.     *     * @param e A {@link FocusEvent}.     */    public void focusGained(FocusEvent e)    {      // FIXME: implement.    }    /**     * Called when the {@link JSlider} has lost focus. It  should repaint the     * slider without the focus drawn.     *     * @param e A {@link FocusEvent}.     */    public void focusLost(FocusEvent e)    {      // FIXME: implement.    }  }  /**   * Helper class that listens for changes to the properties of the {@link   * JSlider}.   */  public class PropertyChangeHandler implements PropertyChangeListener  {    /**     * Called when one of the properties change. The UI should recalculate any     * rectangles if necessary and repaint.     *     * @param e A {@link PropertyChangeEvent}.     */    public void propertyChange(PropertyChangeEvent e)    {      // Check for orientation changes.      if (e.getPropertyName().equals("orientation"))	recalculateIfOrientationChanged();      else if (e.getPropertyName().equals("model"))        {	  BoundedRangeModel oldModel = (BoundedRangeModel) e.getOldValue();	  oldModel.removeChangeListener(changeListener);	  slider.getModel().addChangeListener(changeListener);	  calculateThumbLocation();        }      // elif the componentOrientation changes (this is a bound property,      // just undocumented) we change leftToRightCache. In Sun's       // implementation, the LTR cache changes on a repaint. This is strange      // since there is no need to do so. We could events here and       // update the cache.       // elif the border/insets change, we recalculateInsets.      slider.repaint();    }  }  /**   * Helper class that listens to our swing timer. This class is responsible   * for listening to the timer and moving the thumb in the proper direction   * every interval.   *   * @specnote Apparently this class was intended to be protected,   *           but was made public by a compiler bug and is now   *           public for compatibility.   */  public class ScrollListener implements ActionListener  {    /** Indicates which direction the thumb should scroll. */    private transient int direction;    /** Indicates whether we should scroll in blocks or in units. */    private transient boolean block;    /**     * Creates a new ScrollListener object.     */    public ScrollListener()    {      direction = POSITIVE_SCROLL;      block = false;    }    /**     * Creates a new ScrollListener object.     *     * @param dir The direction to scroll in.     * @param block If movement will be in blocks.     */    public ScrollListener(int dir, boolean block)    {      direction = dir;      this.block = block;    }    /**     * Called every time the swing timer reaches its interval. If the thumb     * needs to move, then this method will move the thumb one block or  unit     * in the direction desired. Otherwise, the timer can be stopped.     *     * @param e An {@link ActionEvent}.     */    public void actionPerformed(ActionEvent e)    {      if (! trackListener.shouldScroll(direction))        {	  scrollTimer.stop();	  return;        }      if (block)	scrollByBlock(direction);      else	scrollByUnit(direction);    }    /**     * Sets the direction to scroll in.     *     * @param direction The direction to scroll in.     */    public void setDirection(int direction)    {      this.direction = direction;    }    /**     * Sets whether movement will be in blocks.     *     * @param block If movement will be in blocks.     */    public void setScrollByBlock(boolean block)    {      this.block = block;    }  }  /**   * Helper class that listens for mouse events.   *   * @specnote Apparently this class was intended to be protected,   *           but was made public by a compiler bug and is now   *           public for compatibility.   */  public class TrackListener extends MouseInputAdapter  {    /** The current X position of the mouse. */    protected int currentMouseX;    /** The current Y position of the mouse. */    protected int currentMouseY;    /**     * The offset between the current slider value and the cursor's position.     */    protected int offset;    /**     * Called when the mouse has been dragged. This should find the mouse's     * current position and adjust the value of the {@link JSlider}     * accordingly.     *     * @param e A {@link MouseEvent}     */    public void mouseDragged(MouseEvent e)    {      currentMouseX = e.getX();      currentMouseY = e.getY();      if (slider.getValueIsAdjusting())        {	  int value;	  if (slider.getOrientation() == JSlider.HORIZONTAL)	    value = valueForXPosition(currentMouseX) - offset;	  else	    value = valueForYPosition(currentMouseY) - offset;	  slider.setValue(value);        }    }    /**     * Called when the mouse has moved over a component but no buttons have     * been pressed yet.     *     * @param e A {@link MouseEvent}     */    public void mouseMoved(MouseEvent e)    {      // Don't care that we're moved unless we're dragging.    }    /**     * Called when the mouse is pressed. When the press occurs on the thumb     * itself, the {@link JSlider} should have its value set to where the     * mouse was pressed. If the press occurs on the track, then the thumb     * should move one block towards the direction of the mouse.     *     * @param e A {@link MouseEvent}     */    public void mousePressed(MouseEvent e)    {      currentMouseX = e.getX();      currentMouseY = e.getY();      int value;      if (slider.getOrientation() == JSlider.HORIZONTAL)	value = valueForXPosition(currentMouseX);      else	value = valueForYPosition(currentMouseY);      if (slider.getSnapToTicks())	value = findClosestTick(value);      // If the thumb is hit, then we don't need to set the timers to move it.       if (! thumbRect.contains(e.getPoint()))        {	  // The mouse has hit some other part of the slider.	  // The value moves no matter where in the slider you hit.	  if (value > slider.getValue())	    scrollDueToClickInTrack(POSITIVE_SCROLL);	  else	    scrollDueToClickInTrack(NEGATIVE_SCROLL);        }      else        {	  slider.setValueIsAdjusting(true);	  offset = value - slider.getValue();        }    }    /**     * Called when the mouse is released.  This should stop the timer that     * scrolls the thumb.     *     * @param e A {@link MouseEvent}     */    public void mouseReleased(MouseEvent e)    {      currentMouseX = e.getX();      currentMouseY = e.getY();      if (slider.getValueIsAdjusting())        {	  slider.setValueIsAdjusting(false);	  if (slider.getSnapToTicks())	    slider.setValue(findClosestTick(slider.getValue()));        }

⌨️ 快捷键说明

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