/**  Compiler command generating GUI-ically.
*          © Lee Higbie          &     Arctic Region Supercomputing Center, 2006
*          higbie at arsc dot edu      www.arsc.edu
*          This program may be copied and modified provided
*          this notice is preserved and a reasonable attempt
*          is made to notify Lee Higbie of the changes made.
*/

import higbie.*;              // Debug 
import java.awt.*;            // all the windowing stuff
import java.awt.event.*;
import java.io.*;             
import javax.swing.*;
import java.util.*;             

public class CompilerGUIc extends JFrame implements ActionListener, WindowListener {
   // class variables apply to all objects of the class


   static String title        = "Compiler Commander";
   static TextArea directions = new TextArea ("Select the compiler and as many "
                                 +"of the options\n" 
                                 + "as you wish.  If no ouptut file name is "
                                 + "provided, then the\n" 
                                 + "output will only be displayed below.\n",
                                 4, 50);
   static String [] debargs   = {"1"};
   static Debug deb;


   int   width = 900, height = 300, rowNo = 0;

   GridBagLayout      gbl  = new GridBagLayout();
   GridBagConstraints gbc  = new GridBagConstraints();
   Button   disposeBtn     = new Button("Exit"),
            updateBtn      = new Button("Update"),
            compileBtn     = new Button("Create Fortran Stmnt");
   //    Note that only the buttons need listeners.  Remaining items will be 
   //    interrogated when their values are needed.
   //java.awt.List compilerChoice  = new java.awt.List();   // defaults to 4 optns
   JList compilerChoice;
   JSlider  optimizationLvl      = new JSlider(0, 10, 10);
   Label    optimizationLbl      = new Label  ("None     Medium     Aggressive");

   Checkbox preserveOrdrLbl   = new Checkbox ("Preserve arithmetic order", false);
   String []   compilerNames;
   int         compilerCount;

   //***************  **********************  *******    main
   /**   @param     args   the command line arguments
   *     @return   
   */
   public static void main (String [] args) {
      File [] compileStmntFiles;
      Vector <FileReader> compileStmntNames = new Vector <FileReader>();
      if (args.length == 1) {
         System.out.println ("Hello "+ args[0] +" with "+ args.length +" args.");
      } 
      if (args.length > 0) {
          deb = new Debug(args);
      } else {         // end if
         System.out.println ("Hello Pirate (argless user).");
         deb = new Debug();
      }                // end else, no arguments on command line
      try {          //  read in the compilerNames of the compiler statement files
         BufferedReader fileNamesRdr = new BufferedReader(
                           new FileReader("CompileStmntNames"));
         for (int i = 0; true; i++) {
            String st = fileNamesRdr.readLine();
            if (st == null) { break; }
            compileStmntNames.addElement(new FileReader(st));
            deb.print("file", i +" "+ st +".");
         }
         } catch (IOException ioe) {         // end try
            System.out.println("Problem with file with names of files: "+ioe);
            System.exit(4);
      }        // end try/catch block
      
      CompilerGUIc cg = new CompilerGUIc(title, compileStmntNames);
   }     // end method main

   /**   Constructor with title and FileReader
    *    @param t    the title of the Frame for the GUI
    *    @param vfr  the Vector of FileReaders for compile stmnt info
   */
   public CompilerGUIc (String ttl, Vector <FileReader> vfr) {
      super (ttl);
      compilerCount = vfr.size();
      BufferedReader[] bfrs = new BufferedReader[compilerCount];
      compilerNames = new String [compilerCount];
      for (int i = 0; i < compilerCount; i++) { 
         bfrs[i] = new BufferedReader(vfr.elementAt(i));
      }     // end for i loop: 
      try {          //  read in data file top lines
         for (int i = 0; i < compilerCount; i++) { 
            compilerNames[i] = bfrs[i] .readLine();
            deb.print("filedata", i +" "+ compilerNames[i]);
         }     // end for i loop: 
         } catch (IOException ioe) {         // end try
            System.out.println("I/O error: "+ioe);
            System.exit(4);
      }        // end try/catch block
      
      setSize(new Dimension(width, height));
      setLayout(gbl);
      gbc.anchor  = GridBagConstraints.CENTER;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.gridheight = 1;

      // top row = row zero
      gbc.gridy   = rowNo++;
      gbc.gridx   = 0;
      gbc.gridwidth  = GridBagConstraints.REMAINDER;
      gbl.setConstraints(directions, gbc);
      add(directions);

      // rows one and 2
      gbc.gridy      = rowNo++;
      gbc.gridx      = 0;
      gbc.gridwidth  = 1;
      gbc.gridheight = 2;
      int wid = 0;
      compilerChoice  = new JList(compilerNames);
      for (int i = 0; i < compilerCount; i++) { 
         //compilerChoice.add(compilerNames[i]);
         wid = Math.max(wid, compilerNames[i].length());
      }     // end for i loop:
      deb.print("start", wid +" "+  compilerNames[0].length());
      compilerChoice.setSize(wid * 8, 40);
      //compilerChoice.setBounds(0,0,wid*15, 40);
      gbl.setConstraints(compilerChoice, gbc);
      add(compilerChoice);
      gbc.gridx      = 1;
      gbc.gridwidth  = 2;
      gbc.gridheight = 1;
      optimizationLvl.setSnapToTicks(false);
      optimizationLvl.setMinorTickSpacing(10);
      gbl.setConstraints(optimizationLvl, gbc);
      add(optimizationLvl);
      gbc.gridy      = rowNo++;
      gbl.setConstraints(optimizationLbl, gbc);
      add(optimizationLbl);
      deb.print("start", compilerChoice.getBounds() 
                +" "+ compilerChoice.getSize()+" "+ compilerChoice.getPreferredSize());

      // row 3
      gbc.gridy      = rowNo++;
      gbc.gridx      = 0;
      gbc.gridwidth  = 1;
      gbc.gridheight = 1;
      gbl.setConstraints(preserveOrdrLbl, gbc);
      add(preserveOrdrLbl);

      // last row
      gbc.gridy      = rowNo++;
      gbc.gridx      = 0;
      gbc.gridwidth  = 1;
      gbc.gridheight = 1;
      gbl.setConstraints(compileBtn, gbc);
      add(compileBtn);
      gbc.gridx      = 1;
      gbl.setConstraints(updateBtn, gbc);
      add(updateBtn);
      gbc.gridx      = 2;
      gbl.setConstraints(disposeBtn, gbc);
      add(disposeBtn);

      setVisible(true);
      setSize(new Dimension(width, height));
      disposeBtn.addActionListener(this);
      addWindowListener(this);
      this.pack();
   }
   public void actionPerformed(ActionEvent ae) {
      System.out.println("Dialog "+ae);
      deb.finalizeDebug();
      this.dispose();
   }
   public void windowActivated   (WindowEvent we){ }
   public void windowClosed      (WindowEvent we){
      System.out.println ("CompilerGUI closed.");
      deb.finalizeDebug();
      System.exit(0); 
   }
   public void windowClosing     (WindowEvent we){ 
      System.out.println ("closing CompilerGUI.");
      deb.print("1", we +" at WindowClosing" );
      deb.finalizeDebug();
      System.exit(0); 
   }
   public void windowDeactivated (WindowEvent we){ }
   public void windowDeiconified (WindowEvent we){ }
   public void windowIconified   (WindowEvent we){ }
   public void windowOpened      (WindowEvent we){ }

}      // end CompilerGUIc class 

