import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
    a GUI application playing with some more Java Swing GUI components
        and listeners

    @author Sharon Tuttle
    @version 2021-11-01
*/

public class ComponentPlay
{
    /**
        creates a simple frame to demo some more Java Swing GUI
	    components
            
        @param args not used here
    */

    public static void main(String[] args)
    {
        EventQueue.invokeLater(
	    () ->
	       {
                   ComponentPlayFrame mainFrame = new ComponentPlayFrame();
		   mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		   mainFrame.setVisible(true);
	       } );
    }
}

/**
    A frame that will demo more Java Swing GUI components
*/

class ComponentPlayFrame extends JFrame
{
    // data fields

    private static final int DEFAULT_WIDTH = 600;
    private static final int DEFAULT_HEIGHT = 450;
    
    /**
        construct a ComponentPlayFrame instance
    */

    public ComponentPlayFrame()
    {
	setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	setTitle("More Components");

	ComponentPlayPanel mainPanel = new ComponentPlayPanel();
	add(mainPanel);
    }
}

/**
    A panel containing subpanels, demoing more Java Swing GUI
        components
*/

class ComponentPlayPanel extends JPanel
{
    // data fields!

    private static final Font DISPLAY_FONT =
        new Font("SanSerif", Font.PLAIN, 30);

    private JCheckBox[] foodChoices;
    private static final String[] FOODS =
       {"Pizza", "Sushi", "Ice cream", "Taco"};
    private JTextField foodChoiceSummary;

    private static final String INITIAL_SIDE = "salad";
    private static final String[] SIDES =
        {"chips", "salad", "soup", "garlic bread", "panini"};
    private JTextField latestSide;
    
    /**
        constructs a ComponentPlayPanel instance
    */

    public ComponentPlayPanel()
    {
        setLayout(new GridLayout(1, 4));

        // 1st subpanel demo JCheckBox

        JPanel ckboxPanel = new JPanel();
	ckboxPanel.setLayout(new BorderLayout());
	ckboxPanel.setBorder(new EtchedBorder());

        JPanel ckboxSubPanel = new JPanel(new GridLayout(1, FOODS.length));

        ItemListener foodChoiceListener =
	    event ->
	    {
	        String foodResponse = "checked: ";

                // isSelected returns whether the calling checkbox
		//    is currently checked or not,
		// getText returns the label text for the calling
		//     checkbox

                for (int i = 0; i < FOODS.length; i++)
		{
		    if (foodChoices[i].isSelected())
		    {
		        foodResponse +=
			    foodChoices[i].getText() + " ";
                    }
		}

                foodChoiceSummary.setText(foodResponse);
	    };

        foodChoices = new JCheckBox[FOODS.length];

        for (int i = 0; i < FOODS.length; i++)
	{
	    foodChoices[i] = new JCheckBox(FOODS[i]);

            foodChoices[i].setFont(DISPLAY_FONT);

            foodChoices[i].addItemListener(foodChoiceListener);
	    ckboxSubPanel.add(foodChoices[i]);
	}

        ckboxPanel.add(ckboxSubPanel, BorderLayout.CENTER);
	
        // I'd like the food-summary textfield in the South...

        foodChoiceSummary = new JTextField(40);
	foodChoiceSummary.setFont(DISPLAY_FONT);
	foodChoiceSummary.setEditable(false);
	ckboxPanel.add(foodChoiceSummary, BorderLayout.SOUTH);

        add(ckboxPanel);

        // SECOND sub-panel: demo a logical group of JRadioButton objects

	JPanel sidesPanel = new JPanel();
	sidesPanel.setLayout(new BorderLayout());
	sidesPanel.setBorder(new EtchedBorder());

        JPanel rButtonPanel = new JPanel();
	rButtonPanel.setLayout(new GridLayout(2, 3));

	ActionListener sidesListener =
	    event ->
	    {
	        latestSide.setText("latest choice: " +
			           event.getActionCommand() );
	    };

	JRadioButton sidesRButtons[] =
	    new JRadioButton[SIDES.length];
	ButtonGroup sidesGroup = new ButtonGroup();

	boolean selected = false;

	for (int i = 0; i < SIDES.length; i++)
	{
	    if (SIDES[i].equals(INITIAL_SIDE))
	    {
		selected = true;
	    }

	    sidesRButtons[i] =
		new JRadioButton(SIDES[i], selected);

	    // remember: ADD the radio button to a button group!!!!!

	    sidesGroup.add(sidesRButtons[i]);

	    sidesRButtons[i].setFont(DISPLAY_FONT);
	    sidesRButtons[i].setActionCommand(SIDES[i]);
	    sidesRButtons[i].addActionListener(sidesListener);

	    rButtonPanel.add(sidesRButtons[i]);
	}	
				   
        latestSide = new JTextField("", 15);
	latestSide.setEditable(false);
	latestSide.setFont(DISPLAY_FONT);

	sidesPanel.add(latestSide, BorderLayout.NORTH);
	sidesPanel.add(rButtonPanel, BorderLayout.CENTER);
	add(sidesPanel);
    }
}