/******************************************************************************
Illustration of how various sort algorithms work.

----------------------------------------------------
Copyright (c) Gunnar Gotshalks. All Rights Reserved.

Permission to use, copy, modify, and distribute this software
and its documentation for NON-COMMERCIAL purposes and
without fee is hereby granted. 
*******************************************************************************/

package FlexOr.searchAndSort;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import FlexOr.utility.*;

@SuppressWarnings("serial")
public class TestSorters extends Applet implements ActionListener {
  Button b1 = new Button("Compare Sort Algorithms");
  Button b2 = new Button("Examine Selection Sort");
  Button b3 = new Button("Examine Bubble Sort");
  Button b4 = new Button("Examine Insert Sort");
  Button b5 = new Button("Examine Quicksort");
  Button b6 = new Button("Examine Heapsort");
  Button b7 = new Button("Examine MergeSort");
  Label msg = new Label("",Label.CENTER);
  
  static final String B1ac = "b1";
  static final String B2ac = "b2";
  static final String B3ac = "b3";
  static final String B4ac = "b4";
  static final String B5ac = "b5";
  static final String B6ac = "b6";
  static final String B7ac = "b7";

  static final String title1 ="Sort Comparison";
  static final String class1 ="FlexOr.searchAndSort.SortComparison";
  static final int width1 = 900;
  static final int height1 = 500;
  
  static final String title2 = "Examine Selection Sort";
  static final String class2 = "FlexOr.searchAndSort.SelectionTest";
  static final int width2 = 500;
  static final int height2 = 600;
  
  static final String title3 = "Examine Bubble Sort";
  static final String class3 = "FlexOr.searchAndSort.BubbleTest";
  static final int width3 = 500;
  static final int height3 = 600;
  
  static final String title4 = "Examine Insert Sort";
  static final String class4 = "FlexOr.searchAndSort.InsertTest";
  static final int width4 = 500;
  static final int height4 = 600;
  
  static final String title5 = "Examine Quicksort";
  static final String class5 = "FlexOr.searchAndSort.QuicksortTest";
  static final int width5 = 500;
  static final int height5 = 600;
  
  static final String title6 = "Examine Heapsort";
  static final String class6 = "FlexOr.searchAndSort.HeapsortTest";
  static final int width6 = 500;
  static final int height6 = 600;
  
  static final String title7 = "Examine Mergesort";
  static final String class7 = "FlexOr.searchAndSort.MergesortTest";
  static final int width7 = 750;
  static final int height7 = 600;
    
  int frameNumber = 1;

public void init() {
  b1.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b1.setActionCommand(B1ac);
  b1.addActionListener(this);
  
  b2.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b2.setActionCommand(B2ac);
  b2.addActionListener(this);
  
  b3.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b3.setActionCommand(B3ac); 
  b3.addActionListener(this);
  
  b4.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b4.setActionCommand(B4ac);
  b4.addActionListener(this);
  
  b5.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b5.setActionCommand(B5ac);
  b5.addActionListener(this);
  
  b6.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b6.setActionCommand(B6ac);
  b6.addActionListener(this);
  
  b7.setFont(new Font("Helvetica", Font.PLAIN, 12));
  b7.setActionCommand(B7ac);
  b7.addActionListener(this);

  setLayout(new GridLayout(0,1,5,5));
  add(b1) ; add(b2); add(b3); add(b4); add(b5); add(b6); add(b7);
  add(msg);
}

public void actionPerformed(ActionEvent event) {
  msg.setText("Please wait while the window comes up...");

  String command = event.getActionCommand();
  try { if (command == B1ac) {
           WindowUtility.createWindow(title1,class1,width1,height1,b1); }
    else if (command == B2ac) {
           WindowUtility.createWindow(title2,class2,width2,height2,b2); }
    else if (command == B3ac) {
           WindowUtility.createWindow(title3,class3,width3,height3,b3); }
    else if (command == B4ac) {
           WindowUtility.createWindow(title4,class4,width4,height4,b4); }
    else if (command == B5ac) {
           WindowUtility.createWindow(title5,class5,width5,height5,b5); }
    else if (command == B6ac) {
           WindowUtility.createWindow(title6,class6,width6,height6,b6); }
    else { WindowUtility.createWindow(title7,class7,width7,height7,b7); }
  }
  catch (Exception e) { msg.setText(e.getMessage()); }
  msg.setText("");
}

/****************************************************************************
Permit program to run as an application.  Example from "Understanding Object
Oriented Programming with Java", Timothy Budd, Addison-Wesley, 1998, p346.

Does not read HTML parameters.
****************************************************************************/
  
  protected boolean inApplet = true;

  public static void main(String [] args) {
    TestSorters ts = new TestSorters();
    ts.inApplet = false;
    Frame theApp = ts.app();
    theApp.setVisible(true);
  }

  private Frame app() { return new AppletFrame(this); }

  @SuppressWarnings("serial")
private class AppletFrame extends Frame {  // An inner class.
    public AppletFrame(Applet applet) {
      setTitle("Test Sorters Application");
      setSize(400,200);
      applet.init(); applet.start();
      add("Center", applet);

      addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.out.println("Trying to close the window.");
          System.exit(0); }});
  }}
}
