package com.mindprod.example;

import com.mindprod.common11.FontFactory;

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;

/**
 * example use of java.awt.List not to be confused with java.util.List.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green
 * @version 1.0
 */
public final class TestList
    {
    // ------------------------------ 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[] )
        {
        final Frame frame = new Frame();

        final List flavour = new List();
        flavour.setForeground( LABEL_FOREGROUND );
        flavour.setBackground( Color.WHITE );
        flavour.setFont( FontFactory.build( "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
            System.out.println( "--selection--" );

            // will be null if there are none or multiple selections.
            String choice = flavour.getSelectedItem();
            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--" );

            String[] choices = flavour.getSelectedItems();
            for ( String aChoice : choices )
                {
                System.out.println( aChoice );
                }
            int[] indexes = flavour.getSelectedIndexes();
            for ( int index : indexes )
                {
                System.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
    }