/*
 * LGP30.java
 *
 * Version:
 *    $Id$
 *
 * Revisions:
 *    $Log$
 *
 *  @author Sidney Marshall
 */

import java.io.*;


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

/**
 * A simple simulation of a LGP30
 */

public class LGP30 extends JFrame {
    // The menu bar

    JLabel feedback = new JLabel("OK");

    JPanel controlPanel = new JPanel( new BorderLayout() );
    JPanel runlights = new JPanel( new GridLayout( 1, 2 ) );
    JButton stop, compute;

    JPanel oscilloscope = new JPanel( new BorderLayout() );
    JPanel leftPanel = new JPanel( new GridLayout( 3, 1 ) );
    JPanel rightPanel = new JPanel( new GridLayout( 3, 1 ) );
    JLabel creg, ireg, areg;

    JTextArea text = new JTextArea(20, 80);
    //final Document doc = text.getDocument();

    Color gray = Color.lightGray;
    Color yellow = Color.yellow;

    int mode = 2;

    JButton modeButton[] = new JButton[3];
    JButton bpButton[] = new JButton[4];

    int breakpoint = 0;
    boolean transferControl = false;
    boolean sixBitInput = false;
    boolean running = false;
    boolean stopped = true; // control for execution thread

    public LGP30( String title ) {
	super( title );

        addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent e ) {
		dispose();
            }
            public void windowClosed( WindowEvent e ) {
		System.exit(0);
            }
        });

	stop = new JButton( "STOP" );
	stop.setBackground( yellow );
	runlights.add( stop );
	compute = new JButton( "COMPUTE" );
        compute.setBackground( gray );
	runlights.add( compute );
	controlPanel.add( runlights, "North" );

	JPanel panel = new JPanel( new GridLayout( 3, 6 ) );
	JButton button;
	modeButton[0] = new JButton("Normal");
        modeButton[0].setBackground( gray );
	modeButton[0].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		modeButton[mode].setBackground( gray );
		mode = 0;
		modeButton[mode].setBackground( yellow );
	    }
	});
	panel.add(modeButton[0]);
	modeButton[1] = new JButton("<html><center>One<p>Operation");
        modeButton[1].setBackground( gray );
	modeButton[1].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		modeButton[mode].setBackground( gray );
		mode = 1;
		modeButton[mode].setBackground( yellow );
		feedback.setText( "Stopped" );
                stop.setBackground( yellow );
                compute.setBackground( gray );
	    }
	});
	panel.add(modeButton[1]);
	modeButton[2] = new JButton("<html><center>Manual<p>Input");
	modeButton[2].setBackground( yellow );
	modeButton[2].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		modeButton[mode].setBackground( gray );
                mode = 2;
		modeButton[mode].setBackground( yellow );
	    }
	});
	panel.add(modeButton[2]);
	button = new JButton("<html><center>Stand<p>By");
        button.setBackground( gray );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		feedback.setText( "Not Simulated" );
	    }
	});
	panel.add(button);
	button = new JButton("Operate");
	button.setBackground( yellow );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		feedback.setText( "Not Simulated" );
	    }
	});
	panel.add(button);
	button = new JButton("<html><center>Standby<p>To Operate");
        button.setBackground( gray );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		feedback.setText( "Not Simulated" );
	    }
	});
	panel.add(button);
	button = new JButton("Start");
        button.setBackground( gray );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		feedback.setText( "Running" );
                stop.setBackground( gray );
                compute.setBackground( yellow );
	    }
	});
	panel.add(button);
	button = new JButton("<html><center>Clear<p>Counter");
        button.setBackground( gray );
	panel.add(button);
	button = new JButton("<html><center>Fill<p>Instruction");
        button.setBackground( gray );
	panel.add(button);
	button = new JButton("<html><center>Execute<p>Instruction");
        button.setBackground( gray );
	panel.add(button);
	button = new JButton("<html><center>Power<p>On");
	button.setBackground( yellow );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		feedback.setText( "Not Simulated" );
	    }
	});
	panel.add(button);
	button = new JButton("<html><center>Power<p>Off");
        button.setBackground( gray );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
                Object s = JOptionPane.showInputDialog(null,
                                                       "Choose one:", "Input",
                                            JOptionPane.INFORMATION_MESSAGE,
                                            null,
                                            new Object[]{"x","y","z"},"x");
		feedback.setText( (String)s );
	    }
	});
	panel.add(button);
	bpButton[0] = new JButton("<html><center>Break<p>Point<p>32");
        bpButton[0].setBackground( gray );
	bpButton[0].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		breakpoint ^= 32;
		if( ( breakpoint & 32 ) != 0 ) {
		    bpButton[0].setBackground( yellow );
		} else {
		    bpButton[0].setBackground( gray );
		}
	    }
	});
	panel.add(bpButton[0]);
	bpButton[1] = new JButton("<html><center>Break<p>Point<p>16");
        bpButton[1].setBackground( gray );
	bpButton[1].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		breakpoint ^= 16;
		if( ( breakpoint & 16 ) != 0 ) {
		    bpButton[1].setBackground( yellow );
		} else {
		    bpButton[1].setBackground( gray );
		}
	    }
	});
	panel.add(bpButton[1]);
	bpButton[2] = new JButton("<html><center>Break<p>Point<p>8");
        bpButton[2].setBackground( gray );
	bpButton[2].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		breakpoint ^= 8;
		if( ( breakpoint & 8 ) != 0 ) {
		    bpButton[2].setBackground( yellow );
		} else {
		    bpButton[2].setBackground( gray );
		}
	    }
	});
	panel.add(bpButton[2]);
	bpButton[3] = new JButton("<html><center>Break<p>Point<p>4");
        bpButton[3].setBackground( gray );
	bpButton[3].addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		breakpoint ^= 4;
		if( ( breakpoint & 4 ) != 0 ) {
		    bpButton[3].setBackground( yellow );
		} else {
		    bpButton[3].setBackground( gray );
		}
	    }
	});
	panel.add(bpButton[3]);
	button = new JButton("<html><center>6 Bit<p>Input");
        button.setBackground( gray );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		JButton b = (JButton)e.getSource();
		sixBitInput = !sixBitInput;
		if( sixBitInput ) {
		    b.setBackground( yellow );
		} else {
		    b.setBackground( gray );
		}
	    }
	});
	panel.add(button);
	button = new JButton("<html><center>Transfer<p>Control");
        button.setBackground( gray );
	button.addActionListener( new ActionListener() {
	    public void actionPerformed( ActionEvent e ) {
		JButton b = (JButton)e.getSource();
		transferControl = !transferControl;
		if( transferControl ) {
		    b.setBackground( yellow );
		} else {
		    b.setBackground( gray );
		}
	    }
	});
	panel.add(button);

	controlPanel.add( panel, "South" );

	leftPanel.add( new JLabel( "C " ) );
	leftPanel.add( new JLabel( "I " ) );
	leftPanel.add( new JLabel( "A " ) );

	creg = new JLabel( "C", SwingConstants.RIGHT );
	rightPanel.add( creg );
	ireg = new JLabel( "I", SwingConstants.RIGHT );
	rightPanel.add( ireg );
	areg = new JLabel( "A", SwingConstants.RIGHT );
	rightPanel.add( areg );
	oscilloscope.add( leftPanel, "West" );
	oscilloscope.add( rightPanel, "East" );
	controlPanel.add( oscilloscope, "East" );

	getContentPane().add( controlPanel, "North" );

	getContentPane().add( new JScrollPane( text ), "Center" );

	getContentPane().add( feedback, "South" );
    }

    public void setFeedback( final String message ) {
	SwingUtilities.invokeLater( new Runnable() {
	    public void run() {
		feedback.setText( message );
	    }
	});
    }

    public static void main( final String args[] ) {
	SwingUtilities.invokeLater( new Runnable() {
	    public void run() {
		int x = 30, y = 30; // initial window location
		final LGP30 c = new LGP30( "LGP 30 Simulator" );
		c.pack();
		c.setLocation( 100, 100 );
		c.setVisible( true );
	    }
	});
	
    } // main
} // LGP30

