/*
 * [TestJSlider.java]
 *
 * Summary: demonstrate the use of a calibrated javax.swing.JSlider.
 *
 * Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2014-06-03 initial version
 */
package com.mindprod.example;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DecimalFormat;
import java.util.Hashtable;

import static java.lang.System.*;

/**
 * demonstrate the use of a calibrated javax.swing.JSlider.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1 2014-06-03 notes about Nimbus and opaque
 * @since 2014-06-03
 */
public final class TestJSlider
    {
    /**
     * used to calibrate marginWidthSlider
     */
    private static final DecimalFormat DF1 = new DecimalFormat( "0.0" );

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        SwingUtilities.invokeLater( new Runnable()
            {
            /**
             * fire up a JFrame on the Swing thread
             */
            public void run()
                {
                try
                    {
                    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
                    }
                catch ( Exception e )
                    {
                    err.println( "Can't install Nimbus L&F" );
                    }
                final JFrame jFrame = new JFrame();
                final Container contentPane = jFrame.getContentPane();
                contentPane.setBackground( new Color( 0xf7fbd7 ) );
                // allow margins 0 to 2 inches by 1/20 of an inch.
                // put minor ticks ever 1/10 inch
                // put major ticks every inch.
                // initial value 0.30 = 6/20ths
                final JSlider marginWidthSlider = new JSlider( JSlider.HORIZONTAL, 0 /* min */, 20 * 2 /* max */, 3 * 2 /* initial */ );
                marginWidthSlider.setMajorTickSpacing( 20 );
                marginWidthSlider.setMinorTickSpacing( 2 );
                marginWidthSlider.setPaintTicks( true );
                // convert label from integers to JLabel text fractions.
                // There is one Jlabel for 0, .5, 1.0, 1.5, 2.0
                @SuppressWarnings( "unchecked" ) Hashtable<Integer, JLabel> sliderLabels = marginWidthSlider.createStandardLabels( 10, 0 );
                for ( int i = 0; i <= 20 * 2; i += 20 / 2 )
                    {
                    sliderLabels.get( i ).setText( DF1.format( ( double ) i / 20 ) );
                    }
                marginWidthSlider.setLabelTable( sliderLabels );
                marginWidthSlider.setPaintLabels( true );
                marginWidthSlider.setPaintTrack( true );
                marginWidthSlider.addChangeListener( new ChangeListener()
                    {
                    @Override
                    public void stateChanged( final ChangeEvent e )
                        {
                        final int marginInTwentieths = marginWidthSlider.getValue();
                        out.println( marginInTwentieths );
                        }
                    }
                );
                contentPane.setLayout( new GridBagLayout() );
                contentPane.add( marginWidthSlider,
                        new GridBagConstraints( 0,
                                0,
                                1,
                                1,
                                1.0,
                                1.0,
                                GridBagConstraints.CENTER,
                                GridBagConstraints.HORIZONTAL,
                                new Insets( 20, 20, 20, 20 ),
                                0,
                                0 ) );
                jFrame.setSize( 400, 150 );
                jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                jFrame.validate();
                jFrame.setVisible( true );
                }
            } );
        } // end main
    } // end class