/*
 * PermFrEnd - Tim Tyler 2000.
 * 
 * A front end for the Fast Walsh Transform in Java.
 * This code has been placed in the public domain.
 * You can do what you like with it.
 * Note that this code comes with no warranty.
 *
 */

/*
 * ToDo:
 *
 * Abort button.
 *
 */

import java.awt.*;
   import java.applet.*;
   import java.awt.event.*;

   public class PermFrEnd extends java.applet.Applet implements ActionListener {
      static Panel input, output, input1, input2, input3, output1, output2;
      static TextArea input_area, output_area;
   
      static int data[] = new int[256];
      static int data1[];
      static int data2[];
   
      static int i=0, j=0, k=0, n=0;
   
      static String text_area_contents;
      static int leng;
      static int non_linearity;
      static int total;
      static int total_number;
   
      Button FWT_button, clear_button;
      static TextField input_number;
      final static String FWTSTRING = "Permute";
      final static String CLEARSTRING = "Clear";
      Executor executor;
   
   // constructor
      public PermFrEnd() {
         setLayout(new BorderLayout());
      
         output = new Panel();
      
         output.setLayout(new BorderLayout());
      
         output1=new Panel();
         output1.add(new Label("Permutation size:",Label.CENTER));
      
         input_number = new TextField("XXXXXX");
      
         output1.add("Center",input_number);
      
         FWT_button = new Button(FWTSTRING);
         output1.add(FWT_button);
         FWT_button.addActionListener(this);
      
         clear_button = new Button(CLEARSTRING);
         clear_button.addActionListener(this);
      
         output1.add(clear_button);
      
         output.add("North",output1);
      
         output2=new Panel();
         output2.setLayout(new BorderLayout());
      
         output_area = new TextArea();
         output_area.setEditable(false);
      
         output2.add("Center",output_area);
      
         add("North",output);
         add("Center",output2);
      
         input_number.setText("");
      }
   
   
      public void actionPerformed(ActionEvent ev) {
         String label = ev.getActionCommand(); // (String)arg;
      
         n=0;
      
         if (label.equals(CLEARSTRING)) {
            input_number.setText("");
            output_area.setText("");
         }
         else
         {
            if (label.equals(FWTSTRING)) {
               if (!input_number.getText().equals("")) {
                  output_area.setText("");
               
                  text_area_contents = input_number.getText();
               
                  total_number = Integer.parseInt(text_area_contents);
               
                  output_area.append("All " + Permutations.factorial(total_number) + " permutations of " + total_number +" elements:\n");
                  executor = 
                        new Executor() {
                           public void execute() {
                              int n = Permutations.data.length;
                           
                              PermFrEnd.output_area.append("{");
                           
                              for (i = 0; i < (n - 1); i++) {
                                 PermFrEnd.output_area.append("" + Permutations.data[i] + ", ");
                              }
                           
                              PermFrEnd.output_area.append("" + Permutations.data[n - 1] + "}\n");
                           
                           }
                        };
               
                  Permutations.permute(total_number, executor);
               
                  output_area.append("\n*** Finished ***\n");
               }
               else
               {
                  output_area.setText("Error: no permutation size specified.\n");
               }
            }
         }
      }
   
   }

