//User Interface

package se082.gb;

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

import java.util.*;
import javax.swing.Timer;
import java.io.*;
import java.net.*;

class GraphicsUI {

  public static boolean ENABLE_MOUSE_SPECIAL = false;
  
  static GameWindow gamewindow;
  
  public static void main (String[] args) {
    if (args.length >= 1 && args[0].equals("ENABLE_MOUSE_SPECIAL"))
      ENABLE_MOUSE_SPECIAL = true;
    GameWindow gamewindow = new GameWindow();
    
    //sends gamewindow to screen
    gamewindow.pack();
    gamewindow.setVisible(true);

  }//end main

}//end GraphicsUI  
  /**
   * overview: an ApplicationWindow is the top level program window
   *           it contains an AnimationWindow, a ToolBar, and a MenuBar
   * @specfield: mode
   * @specfield: toolbar
   * @specfield: menubar	
   * @specfield: animationwindow
   */
class GameWindow extends JFrame {

  JPanel contentPane;
  String mode; //keeps track of current mode: build or play

  JPanel buildToolBar; //remembers toolbar to be used in build mode
  JPanel playToolBar; //remembers toolbar to be used in play mode

  JMenuBar buildMenuBar; //remembers menu bar to be used in build mode
  JMenuBar playMenuBar; //remembers menu bar to be used in play mode

  JLabel buildModeLabel; //to notify user of current mode - build
  JLabel playModeLabel; //to notify user of current mode - play

  JPanel fieldPanel;

  String buildState; //to know which build button has been pushed
                     //so that when a box is clicked, know what to do
                     //"triangle", "square", "circle", "ltflipper",
                     //"rtflipper", "absorber", "ball", "rotate", "action"
                     //"move", "delete","save","load"

  String playState; //to know which play button has been pushed
                    //"replay", "pause", "stop"

  GameWindow gw;
  
  //  int xUnit;
  //  int yUnit;

  Board board;

  //for connecting actions
  Gizmo triggerGizmo;
  Gizmo actionGizmo;

 

  /**
   * effects: initializes the application window with 
   *          FieldPanel, ToolBar, and MenuBar in build mode
   * 	 and sets up same parameters for paly mode
   * RI: GraphicsUI w always has a toolbar and a menubar
   * AF: GraphicsUI w has mode m 
   *     such that toolbar tb and menubar tm belong to m
   */
  public GameWindow() {
 
    
    //title of window = Gizmoball
    setTitle("Gizmoball");
    setResizable(false);
    //size of window ???***
    //setSize(300,200); //width, height 
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(new BorderLayout());
    contentPane.setPreferredSize(new Dimension(915,790));

    mode = "build";

    //construct the build menu bar
    buildMenuBar = makeBuildMenuBar();
    //setJMenuBar(menuBar); //sets the menubar to build //****test
    //buildMenuBar.setVisible(false); //disappears menubar... use to switch

    //initialize playMenuBar so that can switch later
    playMenuBar = makePlayMenuBar();
    setJMenuBar(buildMenuBar); //sets current menubar
    
    //construct the build tool bar
    buildToolBar = makeBuildToolBar();

    //initialize playToolBar so that can switch later
    playToolBar = makePlayToolBar();

    contentPane.add(buildToolBar, BorderLayout.EAST); //****test

    //ImageIcon buildModeIcon = new ImageIcon("buildGizmoBannerSmallest.gif");
    ImageIcon buildModeIcon = null;
    URL iconURLbuild = ClassLoader.getSystemResource("se082/gb/buildGizmoBannerSmallest.gif");
    if (iconURLbuild !=null){
      buildModeIcon = new ImageIcon(iconURLbuild);
    }
    buildModeLabel = new JLabel("" , buildModeIcon, SwingConstants.CENTER );

    
    //ImageIcon playModeIcon = new ImageIcon("playGizmoBannerSmallest.gif");
    ImageIcon playModeIcon = null;
    URL iconURLplay = ClassLoader.getSystemResource("se082/gb/buildGizmoBannerSmallest.gif");
    if (iconURLplay !=null){
      playModeIcon = new ImageIcon(iconURLplay);
    }
    playModeLabel = new JLabel("", playModeIcon, SwingConstants.CENTER);
    
    contentPane.add(buildModeLabel, BorderLayout.NORTH);

    //ImageIcon playModeIcon = new ImageIcon("playGizmoBanner.gif");
    //playModeLabel = new JLabel("", playModeIcon, SwingConstants.LEFT);
    //contentPane.add(playModeLabel, BorderLayout.NORTH); //****test2
    
    //just add some space to the left side
    JLabel spaceLeft = new JLabel(" ");
    contentPane.add(spaceLeft, BorderLayout.WEST);

    //add some space to bottom
    JLabel spaceBottom = new JLabel(" ");
    contentPane.add(spaceBottom, BorderLayout.WEST);    

    board = new Board();
    fieldPanel = new FieldPanel(this,board);
    //JFrame awFrame = new JFrame();
    //awFrame.getContentPane().add(animationWindow);
    //awFrame.setResizable(false);
    //animationWindow.setPreferredSize(new Dimension(500,500));
    //animationWindow.setSize(300,500);//width, height
    //contentPane.add(awFrame);


    gw = this;
    
    contentPane.add(fieldPanel);
    setContentPane(contentPane);

    
      
  }//end GameWindow constructor


   
  /**
   * @effect constructs buttons for toolbar
   *  with desired properties given a .gif image and tooltip text
   * @return JButton
   */
  private JButton buttonSetup(String xGif, String xTiptext) {

    ImageIcon xIcon = null;
    URL iconURL = ClassLoader.getSystemResource("se082/gb/"+xGif);
    if (iconURL != null) {
      xIcon = new ImageIcon(iconURL);
    }

    //ImageIcon xIcon = new ImageIcon(xGif); 
    JButton xButton = new JButton(xIcon);
    xButton.setBackground(Color.WHITE);
    xButton.setToolTipText(xTiptext);
    return xButton;
  }
  

  /**
   * modifies: ToolBar
   * effects: adds bumpers buttons: triangle, square, circle
   *           adds flipper buttons: left, right
   *  	 adds absorber button
   *  	 adds ball button
   *  	 adds option buttons: rotate, connect, move, delete
   *  	 adds i/o buttons: save, load
   *  	 adds game buttons: play, quit
   */
  protected JPanel makeBuildToolBar() {
    
    JPanel bToolBar= new JPanel();
    bToolBar.setBackground(Color.WHITE);


//     Box box = Box.createVerticalBox();    
//     JLabel setupLabel = new JLabel("GAME SETUP");
//     box.add(setupLabel);
    //box1 contains gizmos to add
    Box box1 = Box.createVerticalBox();
    JLabel addGizmosLabel = new JLabel("Add Gizmos");
    box1.add(addGizmosLabel);
    JButton triangleButton = buttonSetup("Triangle.gif",
					 "add triangle bumper gizmo");
    //*************************
    triangleButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  buildState = "triangle";
	  fieldPanel.grabFocus();
	}
      }); //end addActionListener    
    box1.add(triangleButton);
    
    JButton squareButton = buttonSetup("Square.gif",
				       "add square bumper gizmo");
    squareButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "square";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener    
    box1.add(squareButton);
    
    JButton circleButton = buttonSetup("Circle.gif",
				       "add circle bumper gizmo");
    circleButton.setMargin(new Insets(2,15,2,16));
    circleButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "circle";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener
    box1.add(circleButton);
    
    JButton ltFlipperButton = buttonSetup("ltflipper.gif",
					  "add left flipper gizmo");
    ltFlipperButton.setMargin(new Insets(2,13,2,14)); 
    ltFlipperButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "ltflipper";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener 
    box1.add(ltFlipperButton);

    JButton rtFlipperButton = buttonSetup("rtflipper.gif",
					  "add right flipper gizmo");
    rtFlipperButton.setMargin(new Insets(2,13,2,14));
    rtFlipperButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "rtflipper";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener    
    box1.add(rtFlipperButton);    

    JButton absorberButton = buttonSetup("Absorber.gif",
					  "add absorber gizmo");
    absorberButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "absorber";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener    
    box1.add(absorberButton);

    JButton jezzmoVButton = buttonSetup("jezzmoV.gif",
					  "add vertical jezzmo");
    jezzmoVButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "jezzmoV";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener    
    box1.add(jezzmoVButton);    

    JButton jezzmoHButton = buttonSetup("jezzmoH.gif",
					  "add horizontal jezzmo");
    jezzmoHButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "jezzmoH";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener    
    box1.add(jezzmoHButton); 
    
    //box2 contains ball
    Box box2 = Box.createVerticalBox();
    JLabel ballLabel = new JLabel("Add Ball");
    box2.add(ballLabel);
    
    JButton ballButton = buttonSetup("Ball.gif",
				     "add ball");
    ballButton.setMargin(new Insets(0,14,0,14));
    ballButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "ball";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener        
    box2.add(ballButton);    

    //box3 contains edit gizmo options
    Box box3 = Box.createVerticalBox();
    //JLabel editGizmosLabel = new JLabel("Edit Gizmos");
    //box3.add(editGizmosLabel);
    
    JButton rotateButton = buttonSetup("Rotate.gif",
				     "rotate bumper gizmo");
    rotateButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "rotate";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener   
    box3.add(rotateButton);
    
    JButton actionButton = buttonSetup("Action.gif",
				     "connect trigger gizmo action");    
    actionButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "action";

	showActionsWindow();
	
	//getContentPane().add
	fieldPanel.grabFocus();
      }
    }); //end addActionListener   
    box3.add(actionButton);

    JButton moveButton = buttonSetup("Move.gif",
				     "move gizmo");        
    moveButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "move";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener   
    box3.add(moveButton);

    JButton deleteButton = buttonSetup("Delete.gif",
				     "delete gizmo");          
    deleteButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "delete";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener   
    box3.add(deleteButton);

    //box4 contains game options
    Box box4 = Box.createVerticalBox();    
    //    JLabel gameOptionsLabel = new JLabel("GAME OPTIONS");
    //    box4.add(gameOptionsLabel);

    JButton saveButton = buttonSetup("Save.gif",
				     "save gizmoball playing field");     
    saveButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "save";
	int r = makeSave();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener   
    box4.add(saveButton);

    JButton loadButton = buttonSetup("Load.gif",
				     "load gizmoball playing field");      
    loadButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "load";
	makeLoad();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener   
    box4.add(loadButton);

    JButton playButton = buttonSetup("Play.gif",
				    "play gizmoball on current playing field");
    playButton.setMargin(new Insets(2,16,2,16)); 
    playButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	setToPlayMode();
	fieldPanel.grabFocus();
	
      }
    }); //end addActionListener
    box4.add(playButton);

    JButton exitButton = buttonSetup("Exit.gif",
				    "exit gizmoball");    
    exitButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  System.exit(0);
      }
    }); //end addActionListener
    box4.add(exitButton);    

    //box5 contains gizmos and ball to add to field
    Box box5 = Box.createVerticalBox();
//  box5.add(box);
//    box5.add(Box.createVerticalStrut(10));    
    box5.add(box1);
    box5.add(Box.createVerticalStrut(6));
    box5.add(box2);
    
    //box6 contains edit gizmo and game options
    Box box6 = Box.createVerticalBox();
    box6.add(box3);
    //box6.add(Box.createVerticalStrut(10));
    box6.add(box4);   

    //boxA contains box5 and box6 into two columns
    Box boxA = Box.createHorizontalBox();
    boxA.add(box5);
    boxA.add(Box.createHorizontalStrut(4));
    boxA.add(box6);
    boxA.add(Box.createHorizontalStrut(2));
    boxA.setAlignmentY(Component.CENTER_ALIGNMENT);   

    bToolBar.setLayout(new BorderLayout());    
    bToolBar.add(boxA, BorderLayout.CENTER);

    //JScrollPane bSPToolBar = new JScrollPane(bToolBar);
    //    bSPToolBar.add(bToolBar);

    return bToolBar;    
    
  }//end makeBuildToolBar

  public void setBoard(Board b) {
    this.board = b;
  }
  
  /**
   * modifies: menuBar
   * effects: adds file, edit, help buttons to menuBar
   */
  protected JMenuBar makeBuildMenuBar() {

    //create new JMenuBar
    JMenuBar bMenuBar = new JMenuBar();
    bMenuBar.setBackground(Color.WHITE);

    //construct File pull-down
    JMenu fileMenu = new JMenu("File");
    fileMenu.setBackground(Color.WHITE);

    //file>new
   JMenuItem newItem = new JMenuItem("New Board");
    newItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  buildState = "newBoard";
	  fieldPanel.setVisible(false);
	  board = new Board();
	  fieldPanel = new FieldPanel(gw,board);
	  gw.contentPane.add(fieldPanel);
	  fieldPanel.setVisible(true);
	  fieldPanel.grabFocus();
      }
    }); //end addActionListener    
    fileMenu.add(newItem);
    
    //file>save
    JMenuItem saveItem = new JMenuItem("Save Board");
    saveItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  buildState = "save";
	  int r = makeSave();
	  fieldPanel.grabFocus();
      }
    }); //end addActionListener
    fileMenu.add(saveItem);
    
    //file>load
    JMenuItem loadItem = new JMenuItem("Load Board");
    loadItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "load";
	makeLoad();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener
    fileMenu.add(loadItem);
    //file>play
    JMenuItem playItem = new JMenuItem("Play Game");
    playItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  setToPlayMode();
	  fieldPanel.grabFocus();
      }
    }); //end addActionListener

    fileMenu.add(playItem);
    //file>quit
    JMenuItem exitItem = new JMenuItem("Exit Game");
    exitItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {

	  System.exit(0);
      }
    }); //end addActionListener
    fileMenu.add(exitItem);
    //add file pull-down to menubar
    bMenuBar.add(fileMenu);

    //construct Edit pull-down
    JMenu editMenu = new JMenu("Edit");
    editMenu.setBackground(Color.WHITE);
    //edit>rotate gizmo
    JMenuItem rotateItem = new JMenuItem("Rotate Gizmo");
    rotateItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "rotate";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener       
    editMenu.add(rotateItem);
    //edit>select gizmo actions
    JMenuItem gActionItem = new JMenuItem("Select Gizmo Actions");
    gActionItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "action";

	showActionsWindow();
	
	//getContentPane().add
	fieldPanel.grabFocus();
      }
    }); //end addActionListener       
    editMenu.add(gActionItem);
    //edit>move gizmo
    JMenuItem moveItem = new JMenuItem("Move Gizmo");
    moveItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "move";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener      
    editMenu.add(moveItem);
    //edit>delete
    JMenuItem deleteItem = new JMenuItem("Delete Gizmo");
    deleteItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	buildState = "delete";
	fieldPanel.grabFocus();
      }
    }); //end addActionListener       
    editMenu.add(deleteItem);
    //add edit pull-down to menubar
    bMenuBar.add(editMenu);    

    //construct Help pull-down
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setBackground(Color.WHITE);
    //help>instructions manual
    JMenuItem instructionsItem = new JMenuItem("Instructions Manual");
    instructionsItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {

	  showInstrManual();
	  fieldPanel.grabFocus();
	}
      }); //end addActionListener
    helpMenu.add(instructionsItem);
    //add help pull-down to menubar
    bMenuBar.add(helpMenu);    
    
    return bMenuBar;
  }//end makeBuildMenuBar

  /**
   * modifies: ToolBar
   * effects: adds stop, pause, restart, rebuild, and quit buttons
   */
  protected JPanel makePlayToolBar() {
   JPanel pToolBar= new JPanel();
   pToolBar.setBackground(Color.WHITE);
   //pToolBar.setOrientation(1);
   //pToolBar.setFloatable(false);


    Box box = Box.createVerticalBox();   

    JButton stopButton = buttonSetup("Stop.gif",
				    "stop current game");        
    stopButton.setMargin(new Insets(2,72,2,72)); //top, left, bottom, right
    stopButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	playState = "stop";
	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener  
    box.add(stopButton);

    JButton pauseButton = buttonSetup("Pause.gif",
				    "pause current game");     
    pauseButton.setMargin(new Insets(2,72,2,72)); //top, left, bottom, right
    pauseButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	playState = "pause";
	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener  
    box.add(pauseButton);

    JButton playButton = buttonSetup("Play.gif",
 				    "play current game");     
     playButton.setMargin(new Insets(2,72,2,72)); //top, left, bottom, right
     playButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
 	playState = "play";
 	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
       }
     }); //end addActionListener  
     box.add(playButton); 
    
    
    JButton restartButton = buttonSetup("Replay.gif",
				    "start over current game");
    restartButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	playState = "replay";
	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener  
    restartButton.setMargin(new Insets(2,72,2,72)); //top, left, bottom, right
    box.add(restartButton);

    JButton rebuildButton = buttonSetup("Rebuild.gif",
				    "re-build playing field");
    rebuildButton.setMargin(new Insets(2,70,2,70)); //top, left, bottom, right
    rebuildButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  playState = "rebuild";
	  setToBuildMode();
	  ((FieldPanel)fieldPanel).setTheTimer();
	  fieldPanel.setVisible(false);
	  fieldPanel.setVisible(true);
	  fieldPanel.grabFocus();
      }
    }); //end addActionListener
    box.add(rebuildButton);
    
        
    ImageIcon exitIcon = new ImageIcon("Exit.gif"); 
    JButton exitButton = new JButton(exitIcon);
    exitButton.setBackground(Color.WHITE);
    exitButton.setToolTipText("exit gizmoball");
    exitButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {

	  System.exit(0);
      }
    }); //end addActionListener
    exitButton.setMargin(new Insets(2,70,2,70)); //top, left, bottom, right
    box.add(exitButton);

    //Box box2 = Box.createVerticalBox();

    
    Box boxA = Box.createHorizontalBox();
    boxA.add(box);
    boxA.add(Box.createHorizontalStrut(5));
    //boxA.add(box2);
    
    pToolBar.setLayout(new BorderLayout());    
    pToolBar.add(boxA, BorderLayout.CENTER);

    
    return pToolBar;        
    
  }//end makePlayToolBar 
  
  /**
   * modifies: menuBar
   * effects: adds file, edit, help buttons to menuBar 
   * 	        (with different options than build mode)
   */
  protected JMenuBar makePlayMenuBar() {

    //create new JMenuBar
    JMenuBar pMenuBar = new JMenuBar();
    pMenuBar.setBackground(Color.WHITE);

    //construct File pull-down
    JMenu fileMenu = new JMenu("File");
    fileMenu.setBackground(Color.WHITE);
    //file>save
    JMenuItem saveItem = new JMenuItem("Save Board");
    saveItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  buildState = "save";
	  int r = makeSave();
	  fieldPanel.grabFocus();
      }
    }); //end addActionListener
    fileMenu.add(saveItem);
    //file>load
    JMenuItem loadItem = new JMenuItem("Load Board");
    loadItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  buildState = "load";
	  makeLoad();
	  fieldPanel.grabFocus();
      }
    }); //end addActionListener
    fileMenu.add(loadItem);
    //file>play
    JMenuItem playItem = new JMenuItem("Play Game");
    playItem.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
 	playState = "play";
 	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
       }
     }); //end addActionListener  
    fileMenu.add(playItem);
    //file>quit
    JMenuItem exitItem = new JMenuItem("Exit Game");
    exitItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {

	  System.exit(0);
      }
    }); //end addActionListener
    fileMenu.add(exitItem);
    //add file pull-down to menubar
    pMenuBar.add(fileMenu);

    //construct Options pull-down
    JMenu optionsMenu = new JMenu("Options");
    optionsMenu.setBackground(Color.WHITE);
    //options>stop game
    JMenuItem stopItem = new JMenuItem("Stop Game");
    stopItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	playState = "stop";
	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener  
    optionsMenu.add(stopItem);
    //options>pause game
    JMenuItem pauseItem = new JMenuItem("Pause Game");
    pauseItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	playState = "pause";
	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener  
    optionsMenu.add(pauseItem);
    //options> start over
    JMenuItem restartItem = new JMenuItem("Start Over");
    restartItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	playState = "replay";
	((FieldPanel)fieldPanel).setTheTimer();
	fieldPanel.grabFocus();
      }
    }); //end addActionListener  
    optionsMenu.add(restartItem);
    //options>rebuild
    JMenuItem rebuildItem = new JMenuItem("Rebuild Field");
    rebuildItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  playState = "rebuild";
	  setToBuildMode();
	  ((FieldPanel)fieldPanel).setTheTimer();
	  fieldPanel.setVisible(false);
	  fieldPanel.setVisible(true);
	  fieldPanel.grabFocus();
	}
      }); //end addActionListener
    optionsMenu.add(rebuildItem);
    //add options pull-down to menubar
    pMenuBar.add(optionsMenu);    

    //construct Help pull-down
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setBackground(Color.WHITE);
    //help>instructions manual
    JMenuItem instructionsItem = new JMenuItem("Instructions Manual");
    instructionsItem.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {

	  showInstrManual();
	  fieldPanel.grabFocus();

	}
      }); //end addActionListener
    helpMenu.add(instructionsItem);
    //add help pull-down to menubar
    pMenuBar.add(helpMenu);    
    
    return pMenuBar;
      
  }//end makePlayMenuBar


  /**
   * @return: String mode
   */
  public String getMode(){
    return mode;
  }//end setMode

  /**
   * @return: String buildState
   *   state is kept because e.g. user can click add triangle button
   *   and click on field many times adding multiple triangles
   *   so when clicked on field, must check what to do
   */
  public String getBuildState() {
    return buildState;
  }//end getBuildState()

  /**
   * @return playState
   */
  public String getPlayState() {
    return playState;
  }//end getBuildState()

  private void setActionGizmo(Gizmo aGizmo) {
    actionGizmo = aGizmo;
  }

  private void setTriggerGizmo(Gizmo tGizmo) {
    triggerGizmo = tGizmo;
  }

  /**
   * @effect elicits information from user to connect a trigger
   * (gizmo trigger or keystroke) to a gizmo's action
   */
  private void showActionsWindow() {
	//dialog window to ask for type of trigger to input:
	final JDialog jd = new JDialog(gw, "connect trigger to action", true);
	                                 //owner, title, modal?


	
	//prompt user for type of trigger: gizmo or keyStroke
	JRadioButton[] triggerArray = new JRadioButton[2];
	//	JButton[] triggerArray = new JButton[2];
	//	triggerArray[0] = new JButton("Gizmo Trigger");
	//	triggerArray[1] = new JButton("Keystroke Trigger");
	
	JRadioButton gt = new JRadioButton ("Gizmo Trigger");
	gt.addActionListener(new ActionListener() {
	   public void actionPerformed(ActionEvent e) {
	     // System.out.println("gizmo trigger selected");
	     jd.setVisible(false);

	    	     
	     //new dialogue to get info to set gizmo trigger
	     final JDialog gizT = new JDialog(gw, "connect gizmo trigger to action", false);

	     gizT.setSize(new Dimension(350,600));

	     JPanel choseGizPanel = new JPanel();
	    

	     Box choseGizTBox = Box.createVerticalBox();    
	     JLabel choseGizmoTLabel = new JLabel("Select Gizmo as Trigger: ");	                                 //owner, title, modal?
	     
	     choseGizTBox.setBorder(BorderFactory.createTitledBorder
				      (BorderFactory.createEtchedBorder(),
				       "select a gizmo"));

	     final JTextArea area = new JTextArea(2,20);
	     area.setEditable(false);
	     area.setText("to select a gizmo: \n 1) click on the gizmo in the  \building field. \n 2) click ok.");
	     JButton okButton = new JButton("OK");
	     okButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		   area.setText("you have selected gizmo whatever \n located at position x,y on the building grid.");
		   int x = ((FieldPanel)fieldPanel).getClickedMatrixPositionX();
		   int y = ((FieldPanel)fieldPanel).getClickedMatrixPositionY();
		   //System.out.println(x);
		   //System.out.println(y);
		   Gizmo tGizmo = board.isGizmoAt(x,y);

		   setTriggerGizmo(tGizmo);
		   
		   //if (board.isGizmoAt(x,y)!=null) {
		   //area.setText(tGizmo.toString());
		   area.setText("you have selected: " +
				tGizmo.getGizmoType() +
				"@ x=" +
				(tGizmo.getX()+1) + " y=" +(tGizmo.getY()+1)+
				"\nwith respect to top left grid\n");
		   //}
		 }
	       }); //end addActionListener

	     
	     choseGizTBox.add(choseGizmoTLabel);
	     choseGizTBox.add(area);
	     choseGizTBox.add(okButton);
	     choseGizTBox.setAlignmentX(Component.LEFT_ALIGNMENT);

	     //this creates a box that choses the gizmoacted upon
	     Box choseGizABox = Box.createVerticalBox();    
	     JLabel choseGizmoALabel = new JLabel("Select Gizmo to Act On: ");	                                 //owner, title, modal?
	     
	     choseGizABox.setBorder(BorderFactory.createTitledBorder
				      (BorderFactory.createEtchedBorder(),
				       "select a gizmo"));

	     final JTextArea areaA = new JTextArea(2,20);
	     areaA.setEditable(false);
	     areaA.setText("to select a gizmo: \n 1) click on the gizmo in the  \building field. \n 2) click ok.");
	     JButton okAButton = new JButton("OK");
	     okAButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		   areaA.setText("you have selected gizmo whatever \n located at position x,y on the building grid.");
		   int x = ((FieldPanel)fieldPanel).getClickedMatrixPositionX();
		   int y = ((FieldPanel)fieldPanel).getClickedMatrixPositionY();
		   //System.out.println(x);
		   //System.out.println(y);
		   Gizmo aGizmo = board.isGizmoAt(x,y);

		   setActionGizmo(aGizmo);
		   
// 		   ((FieldPanel)fieldPanel).setActionGizmo(actionGizmo);
		   //if (board.isGizmoAt(x,y)!=null) {
		   //areaA.setText(aGizmo.toString());
		   areaA.setText("you have selected: " +
				 aGizmo.getGizmoType() +
				 "@ x=" +
				 (aGizmo.getX()+1) + " y=" +(aGizmo.getY()+1)+
				 "\nwith respect to top left grid\n");
		   //}
		 }
	       }); //end addActionListener

	     choseGizABox.add(choseGizmoALabel);
	     choseGizABox.add(areaA);
	     choseGizABox.add(okAButton);
	     choseGizABox.setAlignmentX(Component.LEFT_ALIGNMENT);

	     //Box choseActionBox = Box.createVerticalBox();
	     JPanel choseActionPanel = new JPanel();
	     choseActionPanel.setBorder(BorderFactory.createTitledBorder
	     		      (BorderFactory.createEtchedBorder(),
	     		       "Select an Action to Perform"));
	     final JList actionList =
	       new JList(new String[] {"flip flipper",
				       "grow jezzmo",
				       "release balls from absorbers",
				       "change color to CYAN"});
	     actionList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
	     actionList.setSelectedIndex(0);
// 	     actionList.addListSelectionListener(new ListSelectionListener() {
// 		 public void actionPerformed(ListSelectionEvent e) {
		   
// 		   System.out.println( actionList.getSelectedValue());
// 		 }
// 	       }); //end addActionListener
	     
	     
	     choseActionPanel.add(actionList);

	     //

	     JButton doneButton = new JButton ("Set Trigger to Gizmo Action");
	     doneButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		   // if (triggerGizmo!= null && actionGizmo!=null) {
		   //  System.out.println(triggerGizmo);
		   //  System.out.println(actionGizmo);

		   String actionString =(String)actionList.getSelectedValue();
		   //System.out.println(actionString);
		   
		   //System.out.println(actionGizmo);
		   //System.out.println(triggerGizmo);
		   
		   if (actionString.equals("flip flipper")) {
		     UpdateFlipperAction fua =
		       new UpdateFlipperAction(actionGizmo);
		     //Trigger trig = triggerGizmo.getTrigger();
		     //trig.addActions(fua);

		     board.addGizmosTrigger(triggerGizmo.getX(),
					triggerGizmo.getY(),
					fua);
		   }
		   if (actionString.equals("grow jezzmo")) {
		       
		       GrowAction gaH = new GrowAction(actionGizmo,board);
		       board.addGizmosTrigger(triggerGizmo.getX(),
						 triggerGizmo.getY(),
						 gaH);
		   }
		   if (actionString.equals("release balls from absorbers")) {
		       
		       AbsorberAction ra = new AbsorberAction(actionGizmo,
							      board);
		       board.addGizmosTrigger(triggerGizmo.getX(),
						 triggerGizmo.getY(),
						 ra);
		   }
		   
		   if (actionString.equals("change color to CYAN")) {
		     ColorChangeAction cca = new ColorChangeAction(actionGizmo,
							       Color.CYAN);
		     board.addGizmosTrigger(triggerGizmo.getX(),
					    triggerGizmo.getY(),
					    cca);		     
		   }
// 		   if (actionString.equals("change color to GREEN")) {
// 		     ColorChangeAction cca = new ColorChangeAction(actionGizmo,
// 							       Color.GREEN);
// 		     board.addGizmosTrigger(triggerGizmo.getX(),
// 					    triggerGizmo.getY(),
// 					    cca);
// 		   }
// 		   if (actionString.equals("change color to RED")) {
// 		     ColorChangeAction cca = new ColorChangeAction(actionGizmo,
// 							       Color.RED);
// 		     board.addGizmosTrigger(triggerGizmo.getX(),
// 					    triggerGizmo.getY(),
// 					    cca);
// 		   }
// 		   if (actionString.equals("change color to ORANGE")) {
// 		     ColorChangeAction cca = new ColorChangeAction(actionGizmo,
// 							       Color.ORANGE);
// 		     board.addGizmosTrigger(triggerGizmo.getX(),
// 					    triggerGizmo.getY(),
// 					    cca);
// 		   }

		   
		   
		   //System.out.println("selected string: " +
		   //	      (String)actionList.getSelectedValue());
// 		   System.out.println("action gizmo is: " + ((FieldPanel)fieldPanel).getActionGizmo());
		   // }
		   gizT.setVisible(false);
		 }
	       }); //end addActionListener	     
	     
	     
	     choseGizPanel.add(choseGizTBox);
	     choseGizPanel.add(choseGizABox);
	     choseGizPanel.add(choseActionPanel);
	     choseGizPanel.add(doneButton);
	     
	     gizT.getContentPane().add(choseGizPanel);
	     gizT.show();

	     //888888888888888888
	   }
	 }); //end addActionListener
	triggerArray[0] = gt;

	JRadioButton kt =  new JRadioButton ("User Trigger");
	kt.addActionListener(new ActionListener() {
	   public void actionPerformed(ActionEvent e) {
	     //System.out.println("user trigger selected");
	     //close dialog asking if gizmo or keystroke trigger
	     jd.setVisible(false);
	     
	     //make dialog for key triggers
	     final JDialog keyT = new JDialog(gw, "connect keystroke trigger to action", false);

	     keyT.setSize(new Dimension(400,600));


	     

	     Box choseBox = Box.createVerticalBox();

	     JLabel choseLabel = new JLabel("Select Gizmo to Act on: ");	                                 //owner, title, modal?
	     
	     choseBox.setBorder(BorderFactory.createTitledBorder
				      (BorderFactory.createEtchedBorder(),
				       "select a gizmo"));

	     final JTextArea area = new JTextArea(2,20);
	     area.setEditable(false);
	     area.setText("to select a gizmo: \n 1) click on the gizmo in the  \building field. \n 2) click ok.");
	     JButton okButton = new JButton("OK");
	     okButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		   //area.setText("you have selected gizmo whatever \n located at position x,y on the building grid.");
		   int x = ((FieldPanel)fieldPanel).getClickedMatrixPositionX();
		   int y = ((FieldPanel)fieldPanel).getClickedMatrixPositionY();
		   //System.out.println(x);
		   //System.out.println(y);
		   Gizmo giz = board.isGizmoAt(x,y);
		   setActionGizmo(giz);
		   //if (board.isGizmoAt(x,y)!=null) {
		   //area.setText(giz.toString());
		   area.setText("you have selected: " +
				 giz.getGizmoType() +
				 "@ x=" +
				 (giz.getX()+1) + " y=" +(giz.getY()+1)+
				 "\nwith respect to top left grid\n");
		   //}
		 }
	       }); //end addActionListener

	     JPanel chosePanel = new JPanel();	     
	     choseBox.add(choseLabel);
	     choseBox.add(area);
	     choseBox.add(okButton);
	     choseBox.setAlignmentX(Component.LEFT_ALIGNMENT);
	     chosePanel.add(choseBox);

	     //Box aBox = Box.createVerticalBox();   
	     
	     JPanel aPanel = new JPanel();
	     aPanel.setBorder(BorderFactory.createTitledBorder
	     		      (BorderFactory.createEtchedBorder(),
	     		       "Select an Action to Perform on Gizmo"));
	     final JList actionList =
	       new JList(new String[] {"flip flipper",
				       "release balls from absorbers",
				       "grow jezzmo",
// 				       "reset jezzmo",
				       "change color to CYAN"});
	     actionList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
	     actionList.setSelectedIndex(0);
	     
	     aPanel.add(actionList);


	     

	     //String[] lettersArray = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
	     
	     final JList keyList = new JList(new String[] {"Left Mouse Click","Middle Mouse Click", "Right Mouse Click","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"});

	     //list of ascii key values that correspond to
	     //letters in list keyList
	     final Integer[] asciiList = new Integer[] {new Integer(MouseEvent.BUTTON1),new Integer(MouseEvent.BUTTON2),new Integer(MouseEvent.BUTTON3),new Integer(KeyEvent.VK_A), new Integer(KeyEvent.VK_B), new Integer(KeyEvent.VK_C), new Integer(KeyEvent.VK_D), new Integer(KeyEvent.VK_E), new Integer(KeyEvent.VK_F), new Integer(KeyEvent.VK_G), new Integer(KeyEvent.VK_H), new Integer(KeyEvent.VK_I), new Integer(KeyEvent.VK_J), new Integer(KeyEvent.VK_K), new Integer(KeyEvent.VK_L), new Integer(KeyEvent.VK_M), new Integer(KeyEvent.VK_N), new Integer(KeyEvent.VK_O), new Integer(KeyEvent.VK_P), new Integer(KeyEvent.VK_Q), new Integer(KeyEvent.VK_R), new Integer(KeyEvent.VK_S), new Integer(KeyEvent.VK_T), new Integer(KeyEvent.VK_U), new Integer(KeyEvent.VK_V), new Integer(KeyEvent.VK_W), new Integer(KeyEvent.VK_X), new Integer(KeyEvent.VK_Y), new Integer(KeyEvent.VK_Z)};

	     keyList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
	     keyList.setSelectedIndex(0);
	     //choseActionPanel.add(keyList);

	     //final String ks = (String)keyList.getSelectedValue();
	     
	     
	     JButton doneButton = new JButton ("Set Trigger to Gizmo Action");
	     doneButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		   String fileInstr = "";
		   String ks = (String)keyList.getSelectedValue();
		   //System.out.println("keylist selected value: "+ks);
// 		   if(ks.equals("Mouse Click")) {
// 		     fileInstr = "left-click press";
// 		   }
// 		   else {
// 		     fileInstr = "down";
// 		   }
		      

		   //gets the index of the selected letter 
		   int keyListIndex = keyList.getSelectedIndex();
		   //if(keyListIndex ==0) {
		     //xxx--;
		   //}
		   //System.out.println(keyListIndex);

		   //uses the index to look in array and
		   //determines what ascii key code belongs to the letter
		   int asciiKeyValue = asciiList[keyListIndex].intValue();
		   //System.out.println(asciiKeyValue);
		   
		   //System.out.println(actionGizmo);

		   String al = (String)actionList.getSelectedValue();
		   //System.out.println(al);

		   if (al.equals("change color to CYAN")) {
		     
		     ColorChangeAction cca = new ColorChangeAction(actionGizmo,
							       Color.CYAN);
		       if(asciiKeyValue<4) {
			 setMouseTriggers(asciiKeyValue, cca);
// 			 if(asciiKeyValue ==1) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, cca,
// 						     "left-click press");
// 			   board.addKeyStrokeTrigger(asciiKeyValue, cca,
// 						     "left-click release");
// 			 }
// 			 if(asciiKeyValue ==2) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, cca,
// 						     "middle-click press");
// 			   board.addKeyStrokeTrigger(asciiKeyValue, cca,
// 						     "middle-click release");			   
// 			 }
// 			 if(asciiKeyValue ==3) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, cca,
// 						     "left-click press");
// 			   board.addKeyStrokeTrigger(asciiKeyValue, cca,
// 						     "left-click release");
// 			 }
		       }
		       else{
			 board.addKeyStrokeTrigger(asciiKeyValue, cca, "down");
			 //System.out.println("trigger: "+asciiKeyValue + " "
			 //+cca);
		       }
		   }
		   
		   else if(actionGizmo instanceof Flipper) {

 		     if (al.equals("flip flipper")) {
//   		       Action a = new UpdateFlipperAction(actionGizmo);
//   		       board.addKeyStrokeTrigger(asciiKeyValue, a);
		       Action a = new UpdateFlipperAction(actionGizmo);
		       
		       //if it is a mouse click that triggers action
		       //notify board of appropriate click		       
		       if(asciiKeyValue<4) {
			 //setMouseTriggers(asciiKeyValue, a);
			 if(asciiKeyValue ==1) {
			   board.addKeyStrokeTrigger(asciiKeyValue, a,
						     "left-click press");
			   board.addKeyStrokeTrigger(asciiKeyValue, a,
						     "left-click release");  
			 }
			 if(asciiKeyValue ==2) {
			   board.addKeyStrokeTrigger(asciiKeyValue, a,
						     "middle-click press");
			   board.addKeyStrokeTrigger(asciiKeyValue, a,
						     "middle-click release");
			 }
			 if(asciiKeyValue ==3) {
			   board.addKeyStrokeTrigger(asciiKeyValue, a,
						     "right-click press");
			   board.addKeyStrokeTrigger(asciiKeyValue, a,
						     "right-click release");			   
			 }
		       }
		       //otherwise it is a keystroke down that triggers action
		       //so notify board of approbriate button direction    
		       else {
			 board.addKeyStrokeTrigger(asciiKeyValue, a, "down");  
			 board.addKeyStrokeTrigger(asciiKeyValue, a, "up");
		                   
		       }    
  		     }
		   }
		   else if(actionGizmo instanceof Absorber) {
		     if(al.equals("release balls from absorbers")) {
	
		       Action ab = new AbsorberAction(actionGizmo,board);

		       //if it is a mouse click that triggers action
		       //notify board of appropriate click

		       
		       //77777777777777777777777
 		       if(asciiKeyValue<4) {

			 setMouseTriggers(asciiKeyValue, ab);
// 			 if(asciiKeyValue ==1) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, ab,
// 						     "left-click press");
// 			   board.addKeyStrokeTrigger(asciiKeyValue, ab,
// 						     "left-click release");  
// 			 }
// 			 if(asciiKeyValue ==2) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, ab,
// 						     "middle-click press");
// 			   board.addKeyStrokeTrigger(asciiKeyValue, ab,
// 						     "middle-click release");			   
// 			 }
// 			 if(asciiKeyValue ==3) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, ab,
// 						     "left-click press");
// 			   board.addKeyStrokeTrigger(asciiKeyValue, ab,
// 						     "left-click release");			   
// 			 }
 		       }

		       //otherwise it is a keystroke down that triggers action
		       //so notify board of approbriate button direction
		       else {
			 board.addKeyStrokeTrigger(asciiKeyValue,ab,
						   "down");
		       }
		     }
		   }
		   else if(actionGizmo instanceof Jezzmo) {
		     if(al.equals("grow jezzmo")) {
		  
		       GrowAction gaH = new GrowAction(actionGizmo,board);
		       //if it is a mouse click that triggers action
		       //notify board of appropriate click

		      

			 //7777777777777777777
 		       if(asciiKeyValue<4) {
			 
			 setMouseTriggers(asciiKeyValue, gaH);
// 			 if(asciiKeyValue ==1) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, gaH,
// 						     "left-click press");
// 			 }
// 			 if(asciiKeyValue ==2) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, gaH,
// 						     "middle-click press");
// 			 }
// 			 if(asciiKeyValue ==3) {
// 			   board.addKeyStrokeTrigger(asciiKeyValue, gaH,
// 						     "left-click press");
// 			 }
  		       }
		       
		     
		       //otherwise it is a keystroke down that triggers action
		       //so notify board of approbriate button direction
		       else {
			 board.addKeyStrokeTrigger(asciiKeyValue,gaH,
						   "down");
		       }     
		     }
// 		     else if (al.equals("reset jezzmo")) {
// 		       ResetAction ra = new ResetAction(actionGizmo);
// 		       if(ks.equals("Mouse Click")) {
// 			 board.addKeyStrokeTrigger(asciiKeyValue, ra,
// 						   "left-click press");
// 			             //other option = "right-click press"      
// 		       }
// 		       else {

// 			 board.addKeyStrokeTrigger(asciiKeyValue,ra,
// 						   "down");
// 		       }
// 		     }
		   }

		   
		   keyT.setVisible(false);
		   }
		   
	       }); //end addActionListener	     

	     JScrollPane keysSPane = new JScrollPane(keyList);

	     Box lettersBox = Box.createVerticalBox();    
	     lettersBox.setBorder(BorderFactory.createTitledBorder
				      (BorderFactory.createEtchedBorder(),
				       "select a keystroke or mouse click"));
	     lettersBox.add(keysSPane);
	     
	     Box aBox = Box.createVerticalBox();   
	     JPanel keysPanel = new JPanel();
	     aBox.add(chosePanel);
	     aBox.add(aPanel);
	     aBox.add(lettersBox);
	     aBox.add(doneButton);
	     keysPanel.add(aBox);
	     
	     keyT.getContentPane().add(keysPanel);
	     keyT.show();
	      
	  }
	 }); //end addActionListener	
	triggerArray[1] = kt;

	ButtonGroup group = new ButtonGroup();
	group.add(gt);
	group.add(kt);
	
	//JList triggerList = new JList(new String[] {"Gizmo Trigger",
	//				    "Keystroke Trigger"});
	//triggerArray.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);

	//JDialog dialog = pane.createDialog(null, "Trigger Type");
	
	//ImageIcon actionIcon = new ImageIcon("Action.gif");	
	// int response = JOptionPane.showOptionDialog(null,
// 					"Select Type of Trigger",
// 					"Trigger Type",
// 					JOptionPane.YES_NO_CANCEL_OPTION,
// 					JOptionPane.QUESTION_MESSAGE,
// 					null,
// 					triggerArray,
// 					triggerArray[0]);

	JOptionPane jop = new JOptionPane("Select Type of Trigger",	
					  JOptionPane.QUESTION_MESSAGE,
					  JOptionPane.YES_NO_CANCEL_OPTION,
					  null,
					  triggerArray,
					  triggerArray[0]);


	jd.getContentPane().add(jop, "Center");
	
	jd.setSize(new Dimension(300,200));

	jd.show();
  }

  private void setMouseTriggers(int asciiKeyValue, Action a) {
    if(asciiKeyValue<4) {
      if(asciiKeyValue ==1) {
	board.addKeyStrokeTrigger(asciiKeyValue, a,
				  "left-click press");
// 	board.addKeyStrokeTrigger(asciiKeyValue, a,
// 				  "left-click release");  
      }
      if(asciiKeyValue ==2) {
	board.addKeyStrokeTrigger(asciiKeyValue, a,
				  "middle-click press");
// 	board.addKeyStrokeTrigger(asciiKeyValue, a,
// 				  "middle-click release");
	
      }
      if(asciiKeyValue ==3) {
	board.addKeyStrokeTrigger(asciiKeyValue, a,
				  "right-click press");
// 	board.addKeyStrokeTrigger(asciiKeyValue, a,
// 				  "left-click release");
	
      }
    }
  }
  
  private void setToPlayMode() {

    //if no filename is associated with the board, query user for filename
    if(board.getFileName().equals("")){
      int r = makeSave(); 

      if (r != JFileChooser.CANCEL_OPTION) {
	buildToolBar.setVisible(false);
	contentPane.add(playToolBar, BorderLayout.EAST);
	playToolBar.setVisible(true);
	
	buildModeLabel.setVisible(false);
	contentPane.add(playModeLabel, BorderLayout.NORTH);
	playModeLabel.setVisible(true);
	
	buildMenuBar.setVisible(false);
	setJMenuBar(playMenuBar);
	playMenuBar.setVisible(true);
	
	mode = "play";
	
	setContentPane(contentPane);	    
      }
    }
    //otherwise save board under current filename
    else {
      FileIO fio = new FileIO(34);
      fio.save(board, board.getFileName()); //board,filename

      buildToolBar.setVisible(false);
      contentPane.add(playToolBar, BorderLayout.EAST);
      playToolBar.setVisible(true);
	
      buildModeLabel.setVisible(false);
      contentPane.add(playModeLabel, BorderLayout.NORTH);
      playModeLabel.setVisible(true);
	
      buildMenuBar.setVisible(false);
      setJMenuBar(playMenuBar);
      playMenuBar.setVisible(true);
	
      mode = "play";
	
      setContentPane(contentPane);	        
    }
  }
  
  private void setToBuildMode() {
    
    	playToolBar.setVisible(false);
	contentPane.add(buildToolBar, BorderLayout.EAST);
	buildToolBar.setVisible(true);

	playModeLabel.setVisible(false);
	contentPane.add(buildModeLabel, BorderLayout.NORTH);
	buildModeLabel.setVisible(true);

	playMenuBar.setVisible(false);
	setJMenuBar(buildMenuBar);
	buildMenuBar.setVisible(true);

	mode = "build";
	
	setContentPane(contentPane);
  }

//   private void makeSave() {
//     buildState = "save";
//     String response = JOptionPane.showInputDialog("Save As:" , "UnNamedBoard");
//     System.out.println(response);    
//   }

  private int makeSave() {
    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(new File("."));
    int r = jfc.showSaveDialog(null);
    if (r == JFileChooser.APPROVE_OPTION){
      String filename = jfc.getSelectedFile().getName();
      //System.out.println(filename);
      FileIO fio = new FileIO(34);
       fio.save(board, filename); //board,filename
       //System.out.println("in save");
    }
    return r;
  }
  
  private void makeLoad() {
    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(new File("."));
    int r = jfc.showOpenDialog(null);
    if (r == JFileChooser.APPROVE_OPTION){
      String filename = jfc.getSelectedFile().getName();
      //System.out.println(filename);
      FileIO fio = new FileIO(34);
       board = new Board();
      fio.load(board, filename); //board,filename
      //fieldPanel.setBoard(b);
      //GameWindow gw = new GameWindow();
      fieldPanel.setVisible(false);
      ((FieldPanel)fieldPanel).setTheBoard(board);
      //filedPanel.paintLoaded();
      //FieldPanel fp = new FieldPanel(this, b);
      // ((FieldPanel)fieldPanel).paintLoaded();
      fieldPanel.setVisible(true);
     
      
//       for (int i=0; i<b.getHeight(); i++) {
// 	for (int j=0; j<b.getWidth(); i++) {
// 	  Gizmo giz = b.isGizmoAt(i, j);
// 	  if (giz!= null) {
// 	    giz.paint(g);
// 	  }
// 	}
//       }
      
    }
  }
  
  public void showInstrManual() {
	  //make new window that displays instructions manual in text area
	  JFrame uManualFrame = new JFrame();
	  uManualFrame.setTitle("User Manual");
	  //uManualFrame.setSize(150,100); //width,height
	  
	  JPanel cPane = new JPanel();
	  cPane.setBackground(Color.WHITE);
	  cPane.setLayout(new BorderLayout());
	  cPane.setPreferredSize(new Dimension(550,600));
	 
	  JTextArea instrManArea = new JTextArea(30,45);
	  instrManArea.setEditable(false);

	  // InputStreamReader isr = new InputStreamReader("se082/gb/userMan.txt");
	  // BufferedReader br = new InputStreamReader(isr);
	  
	  instrManArea.setText("Gizmoball Instructions Manual \n\n"
+"USER MANUAL\n\n"+

"Overview:\n\n"+

"The game can be run in two ways:\n\n"+

"The game has two modes: build and play. The program begins in build mode. In \nbuild mode, the user constructs the playing field for a gizmoball game. The play \nmode alows the user to play a game in the currently displayed gizmoball field. \nThe program also has an ENABLE_MOUSE_SPECIAL option with which the program \n can be run (see details below).\n\n"+

"Adding Gizmos:\n\n"+

"Bumper (circle, square, and triangle), flipper, and jezzmo gizmos along with \nballs can be added by first clicking the respective button in the tool bar and \nthen clicking on the desired position for the gizmo in the playing field. Until \nanother button is selected from the tool bar, successive clicks in the playing \nfield will continue to add this particular gizmo or balls.\n\n" +

"If a gizmo exists in a particular location, the user will not be allowed to \nplace another gizmo at the same location. This being the case, to ensure flipper \nmovement, the user will not be able to place a gizmo in the two grid locations \ninto which the flipper is expected to flip during play mode.\n\n"+

"Modifying Gizmos:\n\n"+

"Modifications (rotate, move, and delete) are performed by clicking the \nrespective edit button followed by clicking on desired gizmo's position in the playing \nfield. This editing function is in effect until another button from the tool \nbar is selected. Modifications can also be made from respective selections from \nthe edit pull-down menu. \n\n"+

"The rotate modification function will rotate gizmos (triangle, flipper, and \njezzmos) 90 degrees each time the user clicks on the gizmo after selecting the \nrotate function.\n\n"+

"The actions modification options will prompt the user for information in a \ndialog window. The user can select between two types of triggers that will elicit \nactions to occurs: 1) gizmo trigger 2) user trigger. Gizmo triggers cause an \naction to occur on a gizmo when a ball hits a gizmo. The trigger gizmo is the \ngizmo that triggers an action to be performed on the gizmo set to be acted on. \nA user trigger causes an action to occur on a gizmo when the user presses a \nkey press or clicks a mouse. The user can set a gizmo trigger or a user trigger \nby clicking on the appropriate radio button. The game will prompt the user for \ninformation to create these triggers. To select a gizmo, the user must click \non the gizmo in the playing field, then press the ok button. to select an \nacti\non, the user clicks on the appropriate list element. To finish, the user \nclicks 'set trigger to gizmo action'. If an inappropriate trigger is set, it will \nnot be performed (e.g. flipping a jezzmo).\n\n"+

"After clicking on the move modifying option, the objects on the playing field \ncan be moved when the user clicks on the gizmo and then clicks on the new \nlocation desired for the gizmo. Again, a gizmo will not be allowed to move into a \nnew position where a gizmo already exists.\n\n"+

"The delete option will delete the gizmo from the board. After the delete option \nis selected from the tool bar, the user can click on each of the gizmos the \nuser would like to delete.\n\n"+

"Creating Connections Between Triggers and Actions:\n\n"+

"When the connection button is pressed, the user is asked to select a trigger \n(either a keystroke or a gizmo collision) to link to an action. If the user \nselects a keystroke, the UI will prompt the user for the key. If the user selects \na gizmo, the UI will prompt the user for a gizmo (which is selected by clicking \non the gizmo on the playing field). Next, the user must select an action \nfrom a provided list of actions to complete the binding of trigger to action.\n\n"+

"Set as default, a button press of the '0' (zero) key will always cause a ball \nto be released from an absorber. \n\n"+

"Available actions to be set by the user include flipping flippers, releasing \nballs from absorbers, growing jezzmos, and changing the gizmo color to cyan.\n"+

"Saving and Loading Games:\n\n"+

"In addition to selections in the file menu, the game can also be saved and \nloaded using buttons displayed to the side of the playing field in build mode. \nAfter selecting the proper buttons, the user will be prompted for the appropriate \nfile i/o information (i.e. file name).\n\n"+

"Adding balls:\n\n"+

"Multiple balls can be added to the playing field; however, balls cannot be \nmoved or deleted, so place them carefully!\n\n"+

"Play:\n\n"+

"Pushing the play button will play a game on the in the currently displayed \ngizmoball field. The currently displayed game can also be played by selecting play \nfrom the file pull-down menu. If the User has not yet saved the game, the play \nbutton will prompt the user for a file name to save the file.  \n\n"+

"Once in the play mode of the game, the game can be paused, stopped, restarted, \nor rebuilt by pushing the respective buttons or by selecting the the respective \ncommand from the file pull-down menu. The user can switch back to build mode \nby clicking the build button or by selecting this option from the file pull-d\nown menu in the menu bar. During play, the user can cause actions by doing the \ntriggering mouse clicks or key presses.\n\n"+

"User specified actions that trigger flippers will cause the flipper to flip \nupon press and flip back upon release. For jezzmos user specified grow actions \nwill grow upon press (of key or mouse button) and will reset upon the next \npress. when an absorber is triggered all balls trapped in any absorber, at the time \nof the trigger being triggered, will be released. Growing jezzmos disappear \nwhen hit by the ball.\n\n"+

"From play mode, the board will be reset in many ways. The stop button resets \nthe board and will begin playing again upon the user pressing play. The restart \nbutton resets the game and immediately begins play. If the rebuild option is s\nelected, the board will be reset to its state before play to rebuild upon the \nreset board.\n\n"+

"play w/ ENABLE_MOUSE_SPECIAL option:\n\n"+

"The game can be entered in its default mode, but the user can also enter the \ngame with the ENABLE_MOUSE_SPECIAL option activated by running the program with \n'ENABLE_MOUSE_SPECIAL' following the default run command. In this mode, mouse-\nclicking a movable gizmo (flipper or jezzmo) or an absorber will cause their \nrespective actions to take place: a flipper will flip up and down, a jezzmo will \ngrow and reset, and an absorber will release all balls trapped in absorbers \nat the time of the click. Therefor this is an option for a quick game if the \nuser should prefer not to set specific key and mouse presses.\n\n"+

"Help:\n\n"+

"There is a help option in the menu bar for the user to access the user manual \nand other information relevent to using the UI.\n\n"

			       );
	      //can the instructions manual be imported from a text file???***
	  
	  JScrollPane sp = new JScrollPane(instrManArea);

	  //bToolBar.add(boxA, BorderLayout.CENTER);	  
	  cPane.add(sp, BorderLayout.CENTER);

	  uManualFrame.getContentPane().add(cPane);
	  
	  uManualFrame.pack();
	  uManualFrame.setVisible(true);
  }
  
}//end GameWindow class


