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

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

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

   private JTextArea resultsArea;

   public ListTestUIDialog()
   {
      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);

      JButton bTest = new JButton("Click here (repeatedly if desired)"
                                  + " to test two linked lists.");
      bTest.addActionListener(this);
      contentPane.add(bTest, BorderLayout.SOUTH);

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

   public String askWhichMethod()
   {
      Object selectedOption
                 = JOptionPane.showInputDialog(this,
                                   "Select a method to test:",
                                   "Test which method?",
                                   JOptionPane.QUESTION_MESSAGE,
                                   null,
                                   ListTestUI.options,
                                   ListTestUI.options[0]);

      return (String) selectedOption;
   }  // method askWhichMethod

   public int askIndex()
   {
      return Integer.parseInt(
                      JOptionPane.showInputDialog(this,
                                    "Enter an index:"));
   }  // method askIndex

   public short askData()
   {
      return Short.parseShort(
                      JOptionPane.showInputDialog(this,
                                    "Enter a data item:"));
   }  // method askData

   public boolean askYesNo(String question, String abbrevQuestion)
   {
      int choice = JOptionPane.showConfirmDialog(this,
                                question,
                                abbrevQuestion,
                                JOptionPane.YES_NO_OPTION);
      return (choice == 0);
   }  // method askYesNo


   public void displayErrorMessage(RuntimeException re)
   {
      JOptionPane.showMessageDialog(this,
                                    re.getMessage(),
                                    re.getClass().getName(),
                                    JOptionPane.ERROR_MESSAGE);
      re.printStackTrace();
   }  // method

   public void displayAnnouncementMessage(String message,
                                          String title)
   {
      JTextArea messageArea = new JTextArea(message);
      JOptionPane.showMessageDialog(this,
                                    messageArea,
                                    title,
                                    JOptionPane.INFORMATION_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);
   }  // method displayResults

   public void runTestsRepeatedly()  {}
   // This is a do-nothing because event-driven programming
   // has its own infinite loop, which we need not duplicate.

   public void actionPerformed(ActionEvent ae)
   {
      ListTest.runTests();
   }  // method actionPerformed
} // class ListTestUIDialog