package com.mindprod.example;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;

/**
 * example use use of javax.swing.JList The code is quite different from AWT List.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
public final class TestJList
    {
    // ------------------------------ FIELDS ------------------------------

    private static final Color LABEL_FOREGROUND = new Color( 0x0000b0 );

    // --------------------------- main() method ---------------------------

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        SwingUtilities.invokeLater( new Runnable()
        {
        public void run()
            {
            final JFrame jFrame = new JFrame();
            final Container contentPane = jFrame.getContentPane();
            contentPane.setLayout( new FlowLayout() );

            final JList flavour = new JList( new String[] {
                    "strawberry", "chocolate", "vanilla" } );
            flavour.setForeground( LABEL_FOREGROUND );
            flavour.setBackground( Color.WHITE );
            flavour.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
            // setting the selection
            flavour.setSelectedIndex( 0 );
            // alternatively by value
            flavour.setSelectedValue( "chocolate", false/* scroll */ );
            // allowing multiple selections, the default.
            flavour
                    .setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
            // set multiple selections
            flavour.setSelectedIndices( new int[] { 0, 2 } );
            // there is no setSelectedValues method
            flavour.addListSelectionListener( new ListSelectionListener()
            {
            /**
             * Called whenever the value of the selection changes.
             *
             * @param e the event that characterizes the change.
             */
            public void valueChanged( ListSelectionEvent e )
                {
                // detecting individual selection
                System.out.println( "--selection--" );

                // will be null if there are none or multiple selections.
                String choice = ( String ) flavour.getSelectedValue();
                System.out.println( choice );

                // will be -1 if there aare none or muliple selections.
                int which = flavour.getSelectedIndex();
                System.out.println( which );

                // detecting multiple selections
                System.out.println( "--multiples--" );

                Object[] choices = flavour.getSelectedValues();
                for ( Object aChoice : choices )
                    {
                    System.out.println( aChoice );
                    }
                int[] indexes = flavour.getSelectedIndices();
                for ( int index : indexes )
                    {
                    System.out.println( index );
                    }
                }
            } );
            contentPane.add( flavour );

            jFrame.setSize( 100, 100 );
            jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.validate();
            jFrame.setVisible( true );
            }
        } );
        }// end main
    }