XSlider.java :  » XML-UI » xui32 » com » xoetrope » swing » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » swing » XSlider.java
package com.xoetrope.swing;

import java.awt.Dimension;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Wraps JSlider and adds some convenience methods and configuration
 *
 * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * license from Xoetrope.</p>
 * <p> $Revision: 1.9 $</p>
 */
public class XSlider extends JSlider implements ChangeListener
{
  private final static int DEFAULT_WIDTH = 100;
  private final static int DEFAULT_HEIGHT = 15;

  private final static int DEFAULT_MIN = 0;
  private final static int DEFAULT_MAX = 100;  
  
  /**
   * Create a new slider
   */
  public XSlider()
  {
    initialize();
  }


  private void initialize()
  {
    setPreferredSize( new Dimension( 100, 25 ));

    setMinimum( DEFAULT_MIN );
    setMaximum( DEFAULT_MAX );

    setPaintTicks( true );
    setPaintLabels( true );
    setMajorTickSpacing( 50 );
    setMinorTickSpacing( 10 );

    addChangeListener( this );
  }
  
  /**
   * Set one or more attributes of the component.
   * <OL>
   * <LI>min, value=the minimum field value</LI>
   * <LI>max, value=the maximum field value</LI>
   * <LI>step, value=the field step size</LI>
   * </OL>
   * @param attribName the attribute name
   * @param attribValue the attribute value
   */
  public void setAttribute( String attribName, Object attribValue )
  {
    String attribNameLwr = attribName.toLowerCase();
    String attribValueLwr = ((String)attribValue).toLowerCase();
    if ( attribNameLwr.equals( "minimum" ) ) 
      setMinimum( Integer.parseInt( attribValueLwr ));
    else if ( attribNameLwr.equals( "maximum" ) )
      setMaximum( Integer.parseInt( attribValueLwr ));
    else if ( attribNameLwr.equals( "increment" ) )
      setIncrement( Integer.parseInt( attribValueLwr ));
  }

  /**
   * The state has changed
   * @param e the state change event
   */
  public void stateChanged( ChangeEvent e)
  {
  }


  /**
    * Determines if the object can accept the input focus in response to user tab
    * key presses.
    * @return false.
    */
  public boolean isFocusable()
  {
    return true;
  }

  /**
   * Sets the controls page size.
   * @param mi Set the slider increment
   */
  public void setIncrement( int mi )
  {
     setMajorTickSpacing( mi );
     setSnapToTicks( true );
  }

  /**
   * Gets the control's page size.
   * @return the slider increment
   */
  public int getIncrement()
  {
     return getMajorTickSpacing();
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.