/*
 * [TestList.java]
 *
 * Summary: example use of java.awt.List not to be confused with java.util.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 java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static java.lang.System.*;

/**
 * example use of java.awt.List not to be confused with java.util.List.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestList
    {
    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[] )
        {
        final Frame frame = new Frame();
        final List flavour = new List();
        flavour.setForeground( FOREGROUND_FOR_LABEL );
        flavour.setBackground( Color.WHITE );
        flavour.setFont( new Font( "Dialog", 15, Font.PLAIN ) );
        // adding possible choices
        flavour.add( "strawberry" );
        flavour.add( "chocolate" );
        flavour.add( "vanilla" );
        // setting the selection
        flavour.select( 0 );
        // there is no select( String )
        // allowing multiple selections
        flavour.setMultipleMode( true );
        // There is no select( int[] ) or select( String[] )
        flavour.addItemListener( new ItemListener()
            {
            /**
             * Invoked when an item has been selected or deselected by the user.
             * The code written for this method performs the operations that
             * need to occur when an item is selected (or deselected).
             */
            public void itemStateChanged( ItemEvent e )
                {
                // detecting individual selection
                out.println( "--selection--" );
                // will be null if there are none or multiple selections.
                String choice = flavour.getSelectedItem();
                out.println( choice );
                // will be -1 if there aare none or muliple selections.
                int which = flavour.getSelectedIndex();
                out.println( which );
                // detecting multiple selections
                out.println( "--multiples--" );
                String[] choices = flavour.getSelectedItems();
                for ( String aChoice : choices )
                    {
                    out.println( aChoice );
                    }
                int[] indexes = flavour.getSelectedIndexes();
                for ( int index : indexes )
                    {
                    out.println( index );
                    }
                }
            } );
        frame.add( flavour );
        frame.setSize( 100, 100 );
        frame.addWindowListener( new WindowAdapter()
            {
            /**
             * Handle request to shutdown.
             * @param e event giving details of closing.
             */
            public void windowClosing( WindowEvent e )
                {
                System.exit( 0 );
                } // end WindowClosing
            } // end anonymous class
        );// end addWindowListener line
        frame.validate();
        frame.setVisible( true );
        } // end main
    }