/*
 * [TestJScrollPane.java]
 *
 * Summary: demonstrate the use of javax.swing.JScrollPane.
 *
 * Copyright: (c) 2011-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 2011-09-20 initial version
 */
package com.mindprod.example;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import java.awt.Container;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

/**
 * demonstrate the use of javax.swing.JScrollPane.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2011-09-20 initial version
 * @since 2011-09-20
 */
public final class TestJScrollPane
    {
    /**
     * is user busy with the scroller?
     */
    private static boolean userBusyScrolling;

    /**
     * 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()
                {
                final JFrame jFrame = new JFrame();
                final Container contentPane = jFrame.getContentPane();
                // The user hitting enter inserts a \n character into the text.
                final JEditorPane jEditorPane = new JEditorPane();
                jEditorPane.setContentType( "text/html" );
                jEditorPane.setEditable( false );
                jEditorPane.setText( "<html lang=\"en-CA\"><body>The destructive Seven Blunders of the World that cause " +
                                     "violence:\n" +
                                     "<ol><li>Wealth without work.</li>\n" +
                                     "<li>Pleasure without conscience.</li>\n" +
                                     "<li>Knowledge without character.</li>\n" +
                                     "<li>Commerce without morality.</li>\n" +
                                     "<li>Science without humanity.</li>\n" +
                                     "<li>Religion without sacrifice.</li>\n" +
                                     "<li>Politics without principle.</li></ol>\n" +
                                     "~ Mahatma Gandhi</body></html>\n" );
                // add what you want to scroll, e.g. a JTextPane, JTextArea or JPanel
                // other option is VERTICAL_SCROLLBAR_NEVER to turn off bar.
                final JScrollPane scroller = new JScrollPane( jEditorPane,
                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
                // control the speed effect of the wheelmouse
                scroller.getVerticalScrollBar().setUnitIncrement( 16 );
                scroller.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener()
                    {
                    /**
                     * detect user fiddling with the scroller
                     */
                    public void adjustmentValueChanged( AdjustmentEvent e )
                        {
                        userBusyScrolling = e.getValueIsAdjusting();
                        }
                    } );
                // GOTCHA! does nothing. Must wait until rendering complete.
                scroller.getVerticalScrollBar().setValue( 0 );
                // Using the default VERTICAL_SCROLLBAR_AS_NEEDED will use scrollbars even when
                // not strictly needed.  If you want precise control you must decide for yourself
                // dynamically.
                scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
                contentPane.add( scroller  /* not jEditorPane! */ );
                jFrame.setSize( 270, 120 );
                jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                jFrame.validate();
                jFrame.setVisible( true );
                }
            } );
        } // end main
    }