/*
 * [TestJList.java]
 *
 * Summary: example use use of javax.swing.JList The code is quite different from AWT List.
 *
 * Copyright: (c) 2009-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 2009-01-01 initial version
 */
package com.mindprod.example;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
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;
import java.util.List;

import static java.lang.System.*;

/**
 * example use use of javax.swing.JList The code is quite different from AWT List.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestJList
    {
    private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 );

    /**
     * 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<String> flavour = new JList<>( new String[] {
                        "strawberry", "chocolate", "vanilla" } );
                flavour.setForeground( FOREGROUND_FOR_LABEL );
                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
                        out.println( "--selection--" );
                        // will be null if there are none or multiple selections.
                        String choice = flavour.getSelectedValue();
                        out.println( choice );
                        // will be -1 if there are none or multiple selections.
                        int which = flavour.getSelectedIndex();
                        out.println( which );
                        // detecting multiple selections
                        out.println( "--multiples--" );
                        // in JDK 1.6
                        // Object[] choices = flavour.getSelectedValues();
                        // In JDK 1.7
                        List<String> choices = flavour.getSelectedValuesList();
                        for ( String aChoice : choices )
                            {
                            out.println( aChoice );
                            }
                        int[] indexes = flavour.getSelectedIndices();
                        for ( int index : indexes )
                            {
                            out.println( index );
                            }
                        }
                    } );
                contentPane.add( flavour );
                jFrame.setSize( 100, 100 );
                jFrame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
                jFrame.validate();
                jFrame.setVisible( true );
                }
            } );
        } // end main
    }