/*==========================================================*/
/*  Two Screens Example                                     */
/*  Author: Bary W Pollack                                  */
/*  Course: Capella TS5502 - Programming Strategies         */
/*  File: TwoScreens.java                                   */
/*  Last Modified: Aug. 12, 2008                            */
/*                                                          */
/*  There are two screens. Initially one is visible.        */
/*  Enter some text in the field...                         */
/*  Flip copies the data to and activates the other screen. */
/*  Both copies the data and activates both screens.        */
/*  Gone deactivates the current screen.                    */
/*==========================================================*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 *    The main class; launches the GUI
 */

public class TwoScreens {

    public static void main(String[] args) {
        // Launch the GUI
        new GUI();
    } //end main

} // end class TwoScreens


/**
 *    The GUI sets up both Screens
 */

class GUI {

    GUI() {
        Screen s1 = new Screen("First Screen",  "Screen Header ONE", -160);
        Screen s2 = new Screen("Second Screen", "Screen Header TWO", +160);

        s1.provideAccess(s2);
        s2.provideAccess(s1);

        s1.setVisible(true);
        s2.setVisible(false);
    }

} // end class GUI


/**
 *    A Screen has a header, an initial x-displacement, and three buttons
 */

class Screen extends JFrame {

    private Screen otherScreen;
    private JTextField jtData = new JTextField(14);
    private JButton btnFlip   = new JButton("Flip");
    private JButton btnBoth   = new JButton("Both");
    private JButton btnGone   = new JButton("Gone");

    Screen(String title, String topLine, int dx) {
        super(title);
        JPanel jp;
        JPanel jpOuterPanel = new JPanel();
        jpOuterPanel.setLayout(new BoxLayout(jpOuterPanel, BoxLayout.Y_AXIS));

        // Just a "spacer"
        jp = new JPanel();
        jp.add(new JLabel(""));        
        jpOuterPanel.add(jp);

        // Set up the header line
        jp = new JPanel();
        jp.add(new JLabel(topLine));
        jpOuterPanel.add(jp);

        // Set up the JTextField
        jp = new JPanel();
        jp.add(new JLabel("Data:"));
        jtData.setText("Please enter some data");
        jp.add(jtData);
        jpOuterPanel.add(jp);

        // Set up the Screen's button(s)
        jp = new JPanel();
        btnFlip.addActionListener(new FlipButtonHandler());
        jp.add(btnFlip);
        btnBoth.addActionListener(new BothButtonHandler());
        jp.add(btnBoth);
        btnGone.addActionListener(new GoneButtonHandler());
        btnGone.setEnabled(false);
        jp.add(btnGone);
        jpOuterPanel.add(jp);

        // Just a "spacer"
        jp = new JPanel();
        jp.add(new JLabel(""));        
        jpOuterPanel.add(jp);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);              // Set the size of the Screen
        setLocationRelativeTo(null);    // Position the Screen at the center of the Desktop
        Point p = getLocation();
        setLocation(p.x + dx, p.y);

        // Get Screen ready for display
        setContentPane(jpOuterPanel);
        setResizable(false);
        setVisible(false);
    }

    // Provide future access to the other Screen
    public void provideAccess(Screen otherScreen) {
        this.otherScreen = otherScreen;
    }

    // Make other Screen visible; copy data
    private class FlipButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            otherScreen.jtData.setText(jtData.getText());
            btnGone.setEnabled(false);
            otherScreen.btnGone.setEnabled(false);
            otherScreen.setVisible(true);
            setVisible(false);
            otherScreen.jtData.requestFocus();
        }
    }

    // Make both Screens visible; copy data
    private class BothButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            otherScreen.jtData.setText(jtData.getText());
            btnGone.setEnabled(true);
            otherScreen.btnGone.setEnabled(true);
            otherScreen.setVisible(true);
            setVisible(true);
            jtData.requestFocus();
        }
    }

    // Make this Screen invisible
    private class GoneButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            btnGone.setEnabled(false);
            otherScreen.btnGone.setEnabled(false);
            setVisible(false);
            otherScreen.jtData.requestFocus();
        }
    }

} // end class Screen class
