// TS8802 Project by John Phillips on 6/24/2003 last revised 6/26/2003

// This applet generates practice problems / quizzes for students
// who will take a Computer Organization course from me. The main
// feature of the program is its use of polymorphism which allows
// new quiz topics to be added with ease.

// To add a new topic do the following:
// 1. Add the topic name to the names[] so that it will show up on the menu.
// 2. Add a new case to the NewProblem button switch statement.
// 3. Add a new class that extends the Problem class.

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

public class Project extends JApplet {

  // myProb is of type Problem which is an abstract class. This lets us use
  // polymorphism to have many different problem types.
  private Problem myProb;

  // The following objects make up the primary user interface.
  private JLabel promptLabel, titleLabel;
  private JTextField answerBox;
  private JTextArea questionArea, hintArea;
  private JButton newProblemButton, showHintButton, showAnswerButton, checkAnswerButton;
  private JComboBox problemSelector;

  // These are the menu items that populate the problem selector combo box.
  private String names[] = { 
    "binary to decimal", 
    "decimal to binary",
    "binary to octal",
    "octal to binary",
    "binary to hex",
    "hex to binary",
    "terminology"
  };

  // These objects help control the look and feel of the applet.
  private JPanel westPanel, centerPanel, centerPanel2, northPanel, southPanel, mainPanel; 
 
  private Color buttonColor, checkAnswerButtonColor;
  private Color questionAreaColor, hintAreaColor, answerBoxColor;
  private Color backgroundColor;

  private Font plainFont, boldFont, monospacedFont;


  // The user interface is setup and contolled by this method.
  public void init()
  {
    // Define some colors and fonts for our GUI
    checkAnswerButtonColor = Color.green;
    buttonColor = new Color( 250, 250, 220 );
    questionAreaColor = new Color( 240, 240, 210 );
    hintAreaColor = new Color( 240, 240, 210 );
    answerBoxColor = new Color( 250, 250, 220 );
    backgroundColor = new Color( 230, 230, 200 );

    plainFont = new Font( "San Serif", Font.PLAIN, 13 );
    boldFont = new Font( "San Serif", Font.BOLD, 14 );
    monospacedFont = new Font( "Monospaced", Font.PLAIN, 14 );

    // Create the main applet container
    Container container = getContentPane();
    container.setLayout( new FlowLayout() );
    container.setBackground( Color.white );


    // ***** MAIN PANEL *****
    // For looks we will have a main panel with a border that will
    // contain all of the other panels using a border layout.

    mainPanel = new JPanel();
    mainPanel.setLayout( new BorderLayout( 5, 5 ) );
    mainPanel.setBorder( BorderFactory.createCompoundBorder(
      BorderFactory.createMatteBorder( 5, 5, 5, 5, backgroundColor ),
      BorderFactory.createLoweredBevelBorder() )
    );
    mainPanel.setBackground( backgroundColor );


    // ***** NORTH PANEL *****
    // The north panel contains the title and the problem type selection menu.

    northPanel = new JPanel();
    northPanel.setLayout( new BoxLayout( northPanel, BoxLayout.X_AXIS ) );
    titleLabel = new JLabel( "CIS 3330 Problem Generator - Select Problem Type:  " );
    titleLabel.setFont( boldFont );
    northPanel.add( titleLabel );

    problemSelector = new JComboBox( names );
    problemSelector.setBackground( buttonColor );
    problemSelector.addItemListener(
      new ItemListener() {
        public void itemStateChanged( ItemEvent event )
        {
          if( event.getStateChange() == ItemEvent.SELECTED )
          {
            answerBox.setText( "" );
            questionArea.setText( "Click the 'New Problem' button to get started." );
            hintArea.setText( "" );
          }
        }
      }
    );

    northPanel.add( problemSelector );
    northPanel.add( Box.createRigidArea( new Dimension( 100, 8 ) ) );
    northPanel.setBackground( backgroundColor );
    northPanel.setBorder( BorderFactory.createMatteBorder( 5, 5, 5, 5, backgroundColor ) );


    // ***** WEST PANEL *****
    // The west panel contains the 'New Problem', 'Show Hint', and 'Show Answer' buttons.

    westPanel = new JPanel();
    westPanel.setLayout( new BoxLayout( westPanel, BoxLayout.Y_AXIS ) );
    westPanel.setBackground( backgroundColor );
    westPanel.setBorder( BorderFactory.createMatteBorder( 25, 5, 5, 5, backgroundColor ) );

    newProblemButton = new JButton( "New Problem" );
    newProblemButton.setBackground( buttonColor );
    newProblemButton.addActionListener(
      new ActionListener() {
        public void actionPerformed( ActionEvent e ) 
        { 
          showStatus( "" );
          answerBox.setText( "" );
          hintArea.setText( "" );
          switch( problemSelector.getSelectedIndex() ) {
            case 0: myProb = new BinToDec(); break;
            case 1: myProb = new DecToBin(); break;
            case 2: myProb = new BinToOct(); break;
            case 3: myProb = new OctToBin(); break;
            case 4: myProb = new BinToHex(); break;
            case 5: myProb = new HexToBin(); break;
            case 6: myProb = new Terms(); break;
          }
          questionArea.setText( myProb.getQuestion() );
        }
      }
    );

    showHintButton = new JButton( "Show Hint" );
    showHintButton.setBackground( buttonColor );
    showHintButton.addActionListener(
      new ActionListener() {
        public void actionPerformed( ActionEvent e ) 
        { 
          hintArea.setText( myProb.hint() ); 
        }
      }
    );
 
    showAnswerButton = new JButton( "Show Answer" );
    showAnswerButton.setBackground( buttonColor );
    showAnswerButton.addActionListener(
      new ActionListener() {
        public void actionPerformed( ActionEvent e )
        {
          hintArea.setText( "The answer is: " + myProb.getAnswer() );
        }
      }
    );

    westPanel.add( newProblemButton ); 
    westPanel.add( Box.createRigidArea(new Dimension( 12,8) ) );
    westPanel.add( showHintButton ); 
    westPanel.add( Box.createRigidArea(new Dimension( 12,8) ) );
    westPanel.add( showAnswerButton );


    // ***** CENTER PANEL *****
    // The center panel contains the question area, the answer box, and the check it button.

    centerPanel = new JPanel();
    centerPanel.setLayout( new BoxLayout( centerPanel, BoxLayout.Y_AXIS ) );

    centerPanel2 = new JPanel();
    centerPanel2.setLayout( new BoxLayout( centerPanel2, BoxLayout.X_AXIS ) );

    questionArea = new JTextArea( 10, 15 );
    questionArea.setText( "Click the 'New Problem' button to get started." );
    questionArea.setBackground( questionAreaColor );
    questionArea.setEditable( false );
    questionArea.setLineWrap( true );
    questionArea.setWrapStyleWord( true );
    questionArea.setFont( monospacedFont );

    promptLabel = new JLabel( "Enter your answer: ");

    answerBox = new JTextField( 10 );
    answerBox.setBackground( answerBoxColor );

    checkAnswerButton = new JButton( "Check It" );
    checkAnswerButton.setBackground( checkAnswerButtonColor );
    checkAnswerButton.addActionListener(
      new ActionListener() {
        public void actionPerformed( ActionEvent e )
        {
          hintArea.setText( "Check It button clicked" );
          if( myProb.checkIt( answerBox.getText() ) )
            hintArea.setText( "Correct! Please click the 'New Problem' button to try another." );
          else
            hintArea.setText( "Not Correct. Please try again." );
        }
      }
    );

    centerPanel2.add( promptLabel );
    centerPanel2.add( answerBox );
    centerPanel2.add( checkAnswerButton );

    centerPanel.add( questionArea );
    centerPanel.add( centerPanel2 );
    centerPanel2.setBackground( backgroundColor );


    // ***** SOUTH PANEL *****
    // The south panel contains the hint / message area.

    southPanel = new JPanel();
    southPanel.setLayout( new BoxLayout( southPanel, BoxLayout.X_AXIS ) );
    southPanel.setBorder( BorderFactory.createMatteBorder( 5, 5, 5, 5, backgroundColor ) );

    hintArea = new JTextArea( 5,50 );
    hintArea.setFont( plainFont );
    //hintArea.setText( "HINT: Click the 'New Problem' button to get started." );
    hintArea.setBackground( hintAreaColor );
    hintArea.setEditable( false );
    hintArea.setLineWrap( true );
    hintArea.setWrapStyleWord( true );

    southPanel.add(hintArea);

    // A label is used on the east side of the border layout to improve the looks.
    JLabel lblSpace = new JLabel( " " );

    mainPanel.add( northPanel, BorderLayout.NORTH );
    mainPanel.add( westPanel, BorderLayout.WEST );
    mainPanel.add( centerPanel, BorderLayout.CENTER );
    mainPanel.add( southPanel, BorderLayout.SOUTH );
    mainPanel.add( lblSpace, BorderLayout.EAST );

    container.add( mainPanel );
  }
}