// @topic T11703 Swing demo A10 -- GUI window with radio buttons, menus, and dialogs
// @brief class FrameRadioButtons extends FrameApplication implements ActionListener
/**
 * @author ik
 */

package pckg;

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

/**
 * <code>FrameRadioButtons</code> is the demo class for UI window
 * with two buttons.
 */
public class FrameRadioButtons extends FrameApplication implements ActionListener
{
    // ----- Data Attributes ----------
    JButton mButChangeColor; // Button that changes background color
    JButton mButQuit;        // Quit button
    JPanel mMainPanel;
    GroupRadioButtons mRadioPanel;
    Color mSystemColor;
    JLabel mLabel;
    JMenuBar mBar;
    JMenu mFile;
    JMenuItem mFileOpen;
    JMenuItem mFileExit;
    JMenu mView;
    JMenuItem mViewOptions;

    // Menu action listeners
    //MenuFileNewListener mMenuFileNewListener;
    //MenuFileExitListener mMenuFileExitListener;

    // ----- Operations ----------
    /** 
    * Class constructor takes care of all basic setup steps.
    */
    public FrameRadioButtons(String aCaption)
    {
        super(aCaption);
        setMenu();
        setGridLayout();
        this.setVisible(true);
    } // method: FrameRadioButtons

    /** 
    * Confugures menu items
    */
    private void setMenu()
    {
        //mMenuFileNewListener = new MenuFileNewListener("New");
        //mMenuFileExitListener = new MenuFileExitListener("Exit");

        mBar = new JMenuBar();     //menu bar
        this.setJMenuBar(mBar);    //add to JFrame

        mFile = new JMenu("File"); //top-level menu
        mFile.setMnemonic('F');
        mBar.add(mFile);           //add to menu bar

        mFileOpen = new JMenuItem("Open"); //menu items
        mFileOpen.setMnemonic('o');
        mFileOpen.addActionListener(this);

        mFileExit = new JMenuItem("Exit");
        mFileExit.setMnemonic('x');
        mFileExit.addActionListener(this);

        mFile.add(mFileOpen);      //add to menu
        mFile.addSeparator();      //put in separator
        mFile.add(mFileExit);

        mView = new JMenu("View");
        mView.setMnemonic('V');
        mBar.add(mView);

        mViewOptions = new JMenuItem("Options");
        mViewOptions.setMnemonic('t');
        mViewOptions.addActionListener(this);
        mView.add(mViewOptions);

    }//void setMenu

    /** 
    * Confugures user interface components
    */
    private void setGridLayout()
    {
        // Two panels
        mMainPanel = new JPanel();
        mRadioPanel = new GroupRadioButtons(this);

        // push buttons
        mButChangeColor = new JButton("Change Color");
        mButQuit = new JButton("Quit");
        mButChangeColor.addActionListener(this);
        mButQuit.addActionListener(this);

        mLabel = new JLabel("Click the color button twice to see the effect:");
        mMainPanel.add(mLabel);
        mMainPanel.add(mButChangeColor);
        mMainPanel.add(mButQuit);
        mSystemColor = mMainPanel.getBackground();

        // Use grid with 2 rows and one column:
        this.setLayout(new GridLayout(2, 1));
        this.getContentPane().add(mRadioPanel);
        this.getContentPane().add(mMainPanel);
        this.pack();

    } // method: setGridLayout

    /** 
    * Handles push button and menu user clicks
    */
    public void actionPerformed(ActionEvent event)
    {
        Object obj = event.getSource();
        if (obj == mButChangeColor)
        {
            switchColors();
        }
        else if (obj == mButQuit)
        {
            System.exit(0);
        }
        else if (obj == mFileExit)
        {
            System.exit(0);
        }
        else if (obj == mFileOpen)
        {
            int response = JOptionPane.showConfirmDialog(
                this,
                "Would you like to open a file?",
                "An unnecessary question",
                JOptionPane.YES_NO_OPTION
                );

            if (response == JOptionPane.YES_OPTION)
            {
                JFileChooser fc = new JFileChooser();
                response = fc.showOpenDialog(this);

                if (response == JFileChooser.APPROVE_OPTION)
                {
                    File file = fc.getSelectedFile();
                    //This is where a real application would open the file:
                    mLabel.setText("Opening: " + file.getName());
                }
                else
                {
                    mLabel.setText("File dialog cancelled by user.");
                }
            }
            else if (response == JOptionPane.NO_OPTION)
            {
                mLabel.setText("Open File? [NO]");
            }
            else
            {
                mLabel.setText("Open File? [Cancelled by user]");
            }

        }
        else if (obj == mViewOptions)
        {
            //Create and set up the window.
            JFrame frame = new JFrame("PanelWithTabs");

            // Prevents dialog from being closed by the user
            frame.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

            DialogViewOptions dlgViewOptions = new DialogViewOptions(frame, this);

            dlgViewOptions.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            dlgViewOptions.pack();

            //Display the window.
            frame.pack();
            frame.setSize(500, 400);// resize window
            frame.setResizable(false);
            frame.setVisible(true);
            dlgViewOptions.setText("HELLO, DIALOG!");
        }

    } // method: actionPerformed

    /** 
    * Switches background color from original to custom and back
    */
    private void switchColors()
    {

        if (mSystemColor == mMainPanel.getBackground())
        {
            mMainPanel.setBackground(mRadioPanel.getColor());
        }
        else
        {
            mMainPanel.setBackground(mSystemColor);
        }

        this.repaint();
    } // method: switchColors

    /** Sets the text of the text field. */
    public void setBottomLabelText(String aText)
    {
        mLabel.setText(aText);
    }

} // class: FrameRadioButtons