/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// SimpleSliderPane.java

//
// Description:
//	Slider Pane  contains a slider and text field
//	  
// Author:
//	Peter Pilgrim Tue Jun 15 22:21:42 BST 1999
//	  
// RCS HEADER 
// 
// $Author$
// $Date$
// $Source$
// $Revision$    $State$    $Locker$
//
// ================================================================================
// History
//	  $Log$
// 
// 

package xenon.soundkit.gui;

import java.beans.*;				// for PropertyChangeListener
import java.awt.*;				// for Frame, Dialog, Component
import java.awt.event.*;			// for ActionEvent, ActionListener
import javax.swing.*;				// for JButton, JPanel et al
import javax.swing.event.*;			// for ChangeEvent
import javax.swing.border.*;			// for TitledBorder

public class SimpleSliderPane extends JPanel
implements ChangeListener, PropertyChangeListener
{
    protected JSlider		slider;
    protected WholeNumberField	sliderValue;
    protected int		min1, max1, value1;
    
    public SimpleSliderPane( 
	String title, int min1, int value1, int max1 )    
    {
	setLayout( new BoxLayout( this, BoxLayout.X_AXIS ));

	if ( min1 > max1 )
	    new IllegalArgumentException( 
		"impossible bounded range; min1:"+min1+" > max1:"+max1 );

	if ( value1 < min1 )
	    value1 = min1;
	if ( value1 > max1 )
	    value1 = max1;

	this.min1 = min1;
	this.max1 = max1;
	this.value1 = value1;

	TitledBorder titledBorder = new TitledBorder( title );
	titledBorder.setTitleFont( new Font( "SanSerif", Font.PLAIN, 10 ));
	setBorder( titledBorder );

	String tooltipText = "minimum:"+min1+"  maximum:"+max1+"  ";
	Font sliderFont = new Font( "Dialog", Font.PLAIN, 8 );
	sliderValue = new WholeNumberField( min1, value1, max1, 5 );
	sliderValue.setFont( sliderFont );
	sliderValue.setToolTipText( tooltipText );
	add( sliderValue );

	slider = new JSlider( JSlider.HORIZONTAL, min1, max1, value1 ) {
	    public Dimension getPreferredSize() {
		Dimension size=new Dimension( super.getPreferredSize() );
		if ( size.width < 270 ) 
		size.width = 270;
		return size;
	    }
	};
	slider.setToolTipText( tooltipText );
	slider.setFont( sliderFont );
	
	int minorTick = (max1 - min1)/50;
	slider.setMinorTickSpacing( minorTick );
	int majorTick = (max1 - min1)/10;
	slider.setMinorTickSpacing( majorTick );
	slider.setPaintTicks( true );
	slider.setPaintLabels( true );
	slider.setSnapToTicks( false );

	// int labelInterval = (max1 - min1)/3;
	// slider.setLabelTable(slider.createStandardLabels(labelInterval));

	// JSlider with the isFilled client property set (only with
	// the Metal look-and-feel)
	slider.putClientProperty( "JSlider.isFilled", Boolean.TRUE );

	add( slider );
	
	slider.addChangeListener(this);
	sliderValue.addPropertyChangeListener(this);
    }

    public void setValue( int newValue )
    {
	slider.setValue( newValue );
    }

    public int getValue()
    {
	return slider.getValue();
    }
    
    public void stateChanged( ChangeEvent evt ) 
    {
	Object src = evt.getSource();
	if ( src == slider ) {
	    int newValue = slider.getValue();
	    sliderValue.setText( Integer.toString( newValue ));
	}
    }
    
    public void propertyChange( PropertyChangeEvent evt )
    {
	Object src = evt.getSource();
	String propName = evt.getPropertyName();
	if ( propName.equals( WholeNumberField.VALUE_CHANGED_PROPERTY )) {
	    if ( src == sliderValue ) {
		int newValue = sliderValue.getValue();
		if ( newValue < min1 )
		    newValue = min1;
		if ( newValue > max1 )
		    newValue = max1;
		slider.setValue(newValue);
	    }
	}
    }
    
}

// fini
