basicsliderui.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,219 行 · 第 1/5 页

JAVA
2,219
字号
      if (direction == POSITIVE_SCROLL)	return (value > slider.getValue());      else	return (value < slider.getValue());    }  }  /** The preferred height of the thumb. */  private transient int thumbHeight;  /** The preferred width of the thumb. */  private transient int thumbWidth;  /** The preferred height of the tick rectangle. */  private transient int tickHeight;  /** Listener for changes from the model. */  protected ChangeListener changeListener;  /** Listener for changes to the {@link JSlider}. */  protected PropertyChangeListener propertyChangeListener;  /** Listener for the scrollTimer. */  protected ScrollListener scrollListener;  /** Listener for component resizing. */  protected ComponentListener componentListener;  /** Listener for focus handling. */  protected FocusListener focusListener;  /** Listener for mouse events. */  protected TrackListener trackListener;  /** The insets between the FocusRectangle and the ContentRectangle. */  protected Insets focusInsets;  /** The {@link JSlider}'s insets. */  protected Insets insetCache;  /** Rectangle describing content bounds. See diagram above. */  protected Rectangle contentRect;  /** Rectangle describing focus bounds. See diagram above. */  protected Rectangle focusRect;  /** Rectangle describing the thumb's bounds. See diagram above. */  protected Rectangle thumbRect;  /** Rectangle describing the tick bounds. See diagram above. */  protected Rectangle tickRect;  /** Rectangle describing the label bounds. See diagram above. */  protected Rectangle labelRect;  /** Rectangle describing the track bounds. See diagram above. */  protected Rectangle trackRect;  /** FIXME: use this somewhere. */  public static final int MAX_SCROLL = 2;  /** FIXME: use this somewhere. */  public static final int MIN_SCROLL = -2;  /** A constant describing scrolling towards the minimum. */  public static final int NEGATIVE_SCROLL = -1;  /** A constant describing scrolling towards the maximum. */  public static final int POSITIVE_SCROLL = 1;  /** The gap between the edges of the contentRect and trackRect. */  protected int trackBuffer;  /** Whether this slider is actually drawn left to right. */  protected boolean leftToRightCache;  /** A timer that periodically moves the thumb. */  protected Timer scrollTimer;  /** A reference to the {@link JSlider} that this UI was created for. */  protected JSlider slider;  /** The shadow color. */  private transient Color shadowColor;  /** The highlight color. */  private transient Color highlightColor;  /** The focus color. */  private transient Color focusColor;  /**   * Creates a new Basic look and feel Slider UI.   *   * @param b The {@link JSlider} that this UI was created for.   */  public BasicSliderUI(JSlider b)  {    super();  }  /**   * Gets the shadow color to be used for this slider. The shadow color is the   * color used for drawing the top and left edges of the track.   *   * @return The shadow color.   */  protected Color getShadowColor()  {    return shadowColor;  }  /**   * Gets the highlight color to be used for this slider. The highlight color   * is the color used for drawing the bottom and right edges of the track.   *   * @return The highlight color.   */  protected Color getHighlightColor()  {    return highlightColor;  }  /**   * Gets the focus color to be used for this slider. The focus color is the   * color used for drawing the focus rectangle when the component gains   * focus.   *   * @return The focus color.   */  protected Color getFocusColor()  {    return focusColor;  }  /**   * Factory method to create a BasicSliderUI for the given {@link   * JComponent}, which should be a {@link JSlider}.   *   * @param b The {@link JComponent} a UI is being created for.   *   * @return A BasicSliderUI for the {@link JComponent}.   */  public static ComponentUI createUI(JComponent b)  {    return new BasicSliderUI((JSlider) b);  }  /**   * Installs and initializes all fields for this UI delegate. Any properties   * of the UI that need to be initialized and/or set to defaults will be   * done now. It will also install any listeners necessary.   *   * @param c The {@link JComponent} that is having this UI installed.   */  public void installUI(JComponent c)  {    super.installUI(c);    if (c instanceof JSlider)      {	slider = (JSlider) c;	focusRect = new Rectangle();	contentRect = new Rectangle();	thumbRect = new Rectangle();	trackRect = new Rectangle();	tickRect = new Rectangle();	labelRect = new Rectangle();	insetCache = slider.getInsets();	leftToRightCache = ! slider.getInverted();	scrollTimer = new Timer(200, null);	scrollTimer.setRepeats(true);	installDefaults(slider);	installListeners(slider);	installKeyboardActions(slider);	calculateFocusRect();	calculateContentRect();	calculateThumbSize();	calculateTrackBuffer();	calculateTrackRect();	calculateThumbLocation();	calculateTickRect();	calculateLabelRect();      }  }  /**   * Performs the opposite of installUI. Any properties or resources that need   * to be cleaned up will be done now. It will also uninstall any listeners   * it has. In addition, any properties of this UI will be nulled.   *   * @param c The {@link JComponent} that is having this UI uninstalled.   */  public void uninstallUI(JComponent c)  {    super.uninstallUI(c);    uninstallKeyboardActions(slider);    uninstallListeners(slider);    scrollTimer = null;    focusRect = null;    contentRect = null;    thumbRect = null;    trackRect = null;    tickRect = null;    labelRect = null;    focusInsets = null;  }  /**   * Initializes any default properties that this UI has from the defaults for   * the Basic look and feel.   *   * @param slider The {@link JSlider} that is having this UI installed.   */  protected void installDefaults(JSlider slider)  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    slider.setForeground(defaults.getColor("Slider.foreground"));    slider.setBackground(defaults.getColor("Slider.background"));    shadowColor = defaults.getColor("Slider.shadow");    highlightColor = defaults.getColor("Slider.highlight");    focusColor = defaults.getColor("Slider.focus");    slider.setBorder(defaults.getBorder("Slider.border"));    slider.setOpaque(true);    thumbHeight = defaults.getInt("Slider.thumbHeight");    thumbWidth = defaults.getInt("Slider.thumbWidth");    tickHeight = defaults.getInt("Slider.tickHeight");    focusInsets = defaults.getInsets("Slider.focusInsets");  }  /**   * Creates a new {@link TrackListener}.   *   * @param slider The {@link JSlider} that this {@link TrackListener} is   *        created for.   *   * @return A new {@link TrackListener}.   */  protected TrackListener createTrackListener(JSlider slider)  {    return new TrackListener();  }  /**   * Creates a new {@link ChangeListener}.   *   * @param slider The {@link JSlider} that this {@link ChangeListener} is   *        created for.   *   * @return A new {@link ChangeListener}.   */  protected ChangeListener createChangeListener(JSlider slider)  {    return new ChangeHandler();  }  /**   * Creates a new {@link ComponentListener}.   *   * @param slider The {@link JSlider} that this {@link ComponentListener} is   *        created for.   *   * @return A new {@link ComponentListener}.   */  protected ComponentListener createComponentListener(JSlider slider)  {    return new ComponentHandler();  }  /**   * Creates a new {@link FocusListener}.   *   * @param slider The {@link JSlider} that this {@link FocusListener} is   *        created for.   *   * @return A new {@link FocusListener}.   */  protected FocusListener createFocusListener(JSlider slider)  {    return new FocusHandler();  }  /**   * Creates a new {@link ScrollListener}.   *   * @param slider The {@link JSlider} that this {@link ScrollListener} is   *        created for.   *   * @return A new {@link ScrollListener}.   */  protected ScrollListener createScrollListener(JSlider slider)  {    return new ScrollListener();  }  /**   * Creates a new {@link PropertyChangeListener}.   *   * @param slider The {@link JSlider} that this {@link   *        PropertyChangeListener} is created for.   *   * @return A new {@link PropertyChangeListener}.   */  protected PropertyChangeListener createPropertyChangeListener(JSlider slider)  {    return new PropertyChangeHandler();  }  /**   * Creates and registers all the listeners for this UI delegate. This   * includes creating the ScrollListener and registering it to the timer.   *   * @param slider The {@link JSlider} is having listeners installed.   */  protected void installListeners(JSlider slider)  {    propertyChangeListener = createPropertyChangeListener(slider);    componentListener = createComponentListener(slider);    trackListener = createTrackListener(slider);    focusListener = createFocusListener(slider);    changeListener = createChangeListener(slider);    scrollListener = createScrollListener(slider);    slider.addPropertyChangeListener(propertyChangeListener);    slider.addComponentListener(componentListener);    slider.addMouseListener(trackListener);    slider.addMouseMotionListener(trackListener);    slider.addFocusListener(focusListener);    slider.getModel().addChangeListener(changeListener);    scrollTimer.addActionListener(scrollListener);  }  /**   * Unregisters all the listeners that this UI delegate was using. In   * addition, it will also null any listeners that it was using.   *   * @param slider The {@link JSlider} that is having listeners removed.   */  protected void uninstallListeners(JSlider slider)  {    slider.removePropertyChangeListener(propertyChangeListener);    slider.removeComponentListener(componentListener);    slider.removeMouseListener(trackListener);    slider.removeMouseMotionListener(trackListener);    slider.removeFocusListener(focusListener);    slider.getModel().removeChangeListener(changeListener);    scrollTimer.removeActionListener(scrollListener);    propertyChangeListener = null;    componentListener = null;    trackListener = null;    focusListener = null;    changeListener = null;    scrollListener = null;  }  /**   * Installs any keyboard actions. The list of keys that need to be bound are   * listed in Basic look and feel's defaults.   *   * @param slider The {@link JSlider} that is having keyboard actions   *        installed.   */  protected void installKeyboardActions(JSlider slider)  {    // FIXME: implement.  }  /**   * Uninstalls any keyboard actions. The list of keys used  are listed in   * Basic look and feel's defaults.   *   * @param slider The {@link JSlider} that is having keyboard actions   *        uninstalled.   */  protected void uninstallKeyboardActions(JSlider slider)  {    // FIXME: implement.  }  /* XXX: This is all after experimentation with SUN's implementation.     PreferredHorizontalSize seems to be 200x21.     PreferredVerticalSize seems to be 21x200.     MinimumHorizontalSize seems to be 36x21.     MinimumVerticalSize seems to be 21x36.     PreferredSize seems to be 200x63. Or Components.getBounds?     MinimumSize seems to be 36x63.     MaximumSize seems to be 32767x63.   */  /**   * This method returns the preferred size when the slider is horizontally   * oriented.   *   * @return The dimensions of the preferred horizontal size.   */  public Dimension getPreferredHorizontalSize()  {    Insets insets = slider.getInsets();    // The width should cover all the labels (which are usually the    // deciding factor of the width)    int width = getWidthOfWidestLabel() * (slider.getLabelTable() == null ? 0                                                                          : slider.getLabelTable()                                                                                  .size());    // If there are not enough labels.    // This number is pretty much arbitrary, but it looks nice.    if (width < 200)      width = 200;    // We can only draw inside of the focusRectangle, so we have to    // pad it with insets.    width += insets.left + insets.right + focusInsets.left + focusInsets.right;    // Height is determined by the thumb, the ticks and the labels.    int height = thumbHeight;    if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0        || slider.getMinorTickSpacing() > 0)      height += tickHeight;    if (slider.getPaintLabels())

⌨️ 快捷键说明

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