import java.awt.*;
import java.util.StringTokenizer;

/**
 * originally derived from Blink example - @author Arthur van Hoff
 * @modified 96/04/24 Jim Hagen : use getBackground
  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
 * modified Chris Johnson August 96 -towards  Radio Buttons!!
  * mod 1 - repeat words in string
  * mod 2 - add some buttons
  * mod 3 - remove thread to see what happens
  * mod 4 - recolour and repaint only when press new button
  * mod 5 - try for multichoice radio button layout
 */
public class Multichoice extends java.applet.Applet {
  Font font;
  static int maxanswers = 10;
  int red, green, blue;
  Component acheck[] = new Component[maxanswers];
  Panel promptP;
  TextArea  atext[]=new TextArea[maxanswers], prompt;
  String  ans[] = new String[maxanswers],
    initialprompt, badprompt, goodprompt, rightanswer;
  int NAnswers;  // number of answers given
  int correct;   // number of the right answer
  static int pwidth = 50;    // max panel width (chars?)

    public void init() {
      int n;
	font = new java.awt.Font("TimesRoman", Font.BOLD, 16);

	// get as many answers as given with names answer1, answer2, etc. up to 9
	for (n = 1; n <= 9; n++) {
	  ans[n] = getParameter("answer" + n);
	  if (ans[n] != null) NAnswers = n;
	}

	// get dialog text for initial prompt, incorrect and correct answers
	initialprompt =  getParameter( "prompt");
	if(initialprompt == null) initialprompt = " ";
	badprompt =  getParameter("badprompt");
	badprompt = (badprompt == null)? "Wrong - try again" : badprompt;

	goodprompt =  getParameter( "goodprompt");
	goodprompt = (goodprompt == null)? "Correct!" : goodprompt;

	rightanswer = getParameter("correct");
	try {
	  correct  = (rightanswer==null)? 0 : Integer.parseInt(rightanswer);
	} catch (NumberFormatException e) {
	  initialprompt = "ERROR - badly formatted correct answer number:"+rightanswer;
	  correct = 1;
	}
	if((correct > NAnswers)| (correct <= 0)) {
	  initialprompt = "ERROR - correct answer is out of range of answers given";
	  correct = 1;
	}

	// radio buttons for answers - each in a panel with its text
	CheckboxGroup answergroup = new CheckboxGroup();
 
	this.setLayout(new FlowLayout(FlowLayout.CENTER));
  
	for (n = 1; n <= NAnswers; n++) {
	  /* put a checkbox and answer text together in a panel */
	  if(ans[n] != null) {
	    Panel answerP = new Panel();
	    acheck[n] = new Checkbox(n+". ", answergroup, false);
	    answerP.add(acheck[n]);

	    atext[n]=new TextArea(3, pwidth-5);
	    atext[n].setEditable(false);
	    answerP.add(atext[n]);
	    ans[n] = linechop(ans[n], pwidth -5);
	    atext[n].setText(ans[n]);

	    this.add(answerP);
	  }
	}
	promptP = new Panel();
	prompt=new TextArea(3, pwidth);	// made smaller by request
		prompt.setBackground(Color.yellow);
		prompt.setText(linechop(initialprompt, pwidth));
		prompt.setEditable(false);
		promptP.add(prompt);

       	this.add(promptP);
	this.resize(600, 100 * (1 + NAnswers));
	this.show();
    }


  public static String linechop(String s, int linewidth) {
    // chop a string into a multiple-line string with
    // max. width of line as specified
    // chop at token boundaries (space/tab/\ chars only)
    // use back-slash '\' to indicate forced linebreak - an explicit
    // linebreak in the input string is unreliably passed to this code,
    // so assume that \n is just whitespace.

    String res = "";
    int currwidth = 0;    // length of current line in res

	for (StringTokenizer t = new StringTokenizer(s, " \t\n\\", true);
	     t.hasMoreTokens() ; ) {
	    String word = t.nextToken();
	    if (word.equals( "\\")) {
	      // treat as explicit newline
	      // append the word to current line with single space separator
	      res = res + "\n";
	      currwidth = 0;
	    }
	    else if (word.equals("\n")|word.equals("\t")|word.equals(" ")) {
	      // substitute a space as word separator
	      res = res + " ";
	      currwidth++;
	    }
	    else {
	      int w =  word.length();

	      if ((currwidth + w > linewidth )) {
		// start a new line if word would overflow this one
		// NB this will overflow the new line if word is longer than linewidth
		res = res + "\n";
		currwidth = 0;
	      }
	      // append this word with a preceding space
	      res = res + word;
	      currwidth += w;
	    }
	}
	return res;
  }

		public boolean action(Event event, Object arg) {
		  int n;
		   if(event.target == acheck[correct]) {
		    prompt.setBackground(Color.green);
		    prompt.setText(linechop(goodprompt,pwidth));
		    atext[correct].setBackground(Color.green);
		    repaint();
		    return false;
		  }
		   else if(event.target != acheck[correct]) {
		      prompt.setText(linechop(badprompt, pwidth));
		      /* find and recolour the wrong answer that was checked */
		      for (n = 1; n <= NAnswers; n++) {
			if (event.target == acheck[n]) {
			  atext[n].setBackground(Color.pink);
			}
		      }
		      repaint();
		      return false;
		   }
		  else return super.action(event,arg);
		}

}

