/*	This Applet was created by Michael Morton for use by the Math Forum.
	You may use pieces of code only with the consent of the Math Forum. 
	*/

/*	These are the Controls for the Traffic Jam Applet. There are choice
	menus for colors/level, and buttons to show/hide the History window, and to 
	ReDraw the History window.
	*/

import java.awt.*;

public class Controls extends Panel {
  Jam outerparent;
  Font control_font;
  Choice bgjamcolors,fgjamcolors,level;
  Button redraw, show, reset;
  String comments = 
  "This is an Applet created by Michael W.S. Morton for the Math Forum. Any \n" +
  "pieces of code may be used only with consent of the Math Forum. The \n" +
  "instructions for this puzzle/game are at: \n" +
  "http://forum.swarthmore.edu/workshops/sum96/traffic.jam.html \n" +
  "Special thanks goes to Melissa Dershewitz for the people images. Please \n" +
  "direct any Questions/Comments to Mike Morton: \n"+
  "\t\tmorton@forum.swarthmore.edu. \n"+
  "Enjoy.";
  
  Controls(Jam target) {
    this.outerparent = target;
    
    //Trying to set Font for Controls
    control_font = new Font("TimesRoman", Font.PLAIN, 20);
    
    //Setup Choice Menus
    fgjamcolors = new Choice();
    add_items(fgjamcolors); 	//Add colors to the Choice Menu
    fgjamcolors.select("White");	// Select the first item
    bgjamcolors = new Choice();
    add_items(bgjamcolors);		//Add colors to the Choice Menu
    bgjamcolors.select("Black");	// Select the first item
    
    // Setup the level Choices
    level = new Choice();
    level.addItem("Easy");
    level.addItem("Medium");
    level.addItem("Hard");
    level.select("Medium");
    
    //Add Labels and Menus to Control Frame
    add(new Label("Foreground Jam Color:"));
    add(fgjamcolors);
    add(new Label("Background Jam Color:"));
    add(bgjamcolors);
    add(new Label("Level of Difficulty"));
    add(level);
    
    //Setup Buttons
    redraw = new Button("ReDraw the History");
    add(redraw);
    show = new Button("Show History");
    add(show);
    reset = new Button("Reset");
    add(reset);
    
    // Add comments
    add(new TextArea(comments,10,50));
  }
  
  //Add colors to a Choice Menu
  public void add_items(Choice name) {
    name.addItem("Black");
    name.addItem("Red");
    name.addItem("Green");
    name.addItem("Blue");
    name.addItem("White");
    
  }		
  
  //Handle Choice menu clicks.
  public boolean action(Event evt, Object arg) {
    if (evt.target instanceof Choice) {
      //Check which color to update and call color_update.
      if(evt.target == fgjamcolors)	
	outerparent.update_jamcolor("fg", (String)arg);  
      else if (evt.target == bgjamcolors)
	outerparent.update_jamcolor("bg", (String)arg);
      // Check if level is clicked, and call the update_level
      // function in the JamCan class.
      else if (evt.target == level) {
	outerparent.can.update_level
	  (level.getSelectedIndex()+1);
	outerparent.history.reset_pics
	  (level.getSelectedIndex()+1);
      }
    }
    if (evt.target instanceof Button) {
      if(evt.target == redraw) {
	outerparent.history.redraw = 1;
	outerparent.history.repaint();
      }	
      if(evt.target == show) {
	if(show.getLabel().equals("Show History")) {
	  outerparent.history.show();
	  outerparent.history.redraw = 1;
	  outerparent.history.repaint();
	  show.setLabel("Hide History");
	}
	else if(show.getLabel().equals("Hide History")) {
	  outerparent.history.hide();
	  show.setLabel("Show History");
	}
      }
      if(evt.target == reset) {
	outerparent.can.update_level
	  (outerparent.can.jam_level);
	outerparent.history.reset_pics
	  (outerparent.can.jam_level);
      }
    }
    return true;
  }
  
}


