// ListTestUIMixed.java
//
// Uses, in this diretory:
//     ListTestUI.java
//
// To compile under Java 1.4 (and avoid warnings under 1.5), type:
//    javac -source 1.4 ListTestUIMixed.java

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.Hashtable;

public class ListTestUIMixed extends JFrame
                              implements ListTestUI
{
   final static String lineBreak
                         = System.getProperty(
                                     "line.separator");

   private JTextArea resultsArea;

   private static String[] options
                       = new String[] {"N", "A", "I", "J",
                                       "R", "E", "H", "B"};
   private static Hashtable table;

   static {
      // Number of letter options should match
      // number of longer string options:
      assert ( options.length == ListTestUI.options.length )
             : "options.length=" + options.length
                    + " and ListTestUI.options.length="
                    + ListTestUI.options.length
                    + " should be equal.";

      // Pair up letter options with longer string options:
      table = new Hashtable();
      for (int i = 0; i < options.length; i++)
         table.put(options[i], ListTestUI.options[i]);
   }  // static initialization block

   public ListTestUIMixed()
   {
      setSize(450, 300);
      setLocation(100, 100);
      setTitle("ShortSequenceLinkedList tester");

      Container contentPane = getContentPane();

      resultsArea = new JTextArea();
      resultsArea.setEditable(false);
      contentPane.add(new JScrollPane(resultsArea),
                      BorderLayout.CENTER);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }  // constructor

   public String askWhichMethod()
   {
      System.out.println("Select a method to test.");
      System.out.println();
      for ( int i = 0; i < options.length; i++ )
         System.out.println("   " + options[i] + " - "
                                + table.get(options[i]));
      System.out.println();

      String answer = ConsoleInput.askSelection(
                            "Enter your selection:",
                            options);

      return (String) table.get(answer.toUpperCase());
   }  // method askWhichMethod

   public int askIndex()
   {
      return Integer.parseInt(
                      ConsoleInput.ask(
                            "Enter an index:"));
   }  // method askIndex

   public short askData()
   {
      return Short.parseShort(
                      ConsoleInput.ask(
                           "Enter a data item:"));
   }  // method askData

   public boolean askYesNo(String question, String abbrevQuestion)
   {
      System.out.println(question);
      return ConsoleInput.askYesNo(abbrevQuestion);
   }  // method askYesNo


   public void displayErrorMessage(RuntimeException re)
   {
      re.printStackTrace();
   }  // method display error message


   public void displayAnnouncementMessage(String message,
                                          String title)
   {
      System.out.println(message);
   }  // method displayAnnoucementMessage

   public void displayResults(String[] text)
   {
      for ( int i = 0; i < text.length; i++ )
      {
         resultsArea.append(text[i]);
         resultsArea.append(lineBreak);
      }  //for i
      resultsArea.append(lineBreak);
      System.out.println("Results sent to"
                         + " ShortSequenceLinkedList "
                         + "tester window.");
   }  // method

   public void runTestsRepeatedly()
   {
      do  {   // Test both lists repeatedly
         ListTest.runTests();
      } while ( askYesNo("Test the two lists again (and possibly"
                                + " create/destroy them first)?",
                         "Test again?") );
   }  // method runTestsRepeatedly
} // class ListTestUIMixed