package oats;

import java.util.*;
import java.io.*;
import javabook2.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Title:   ArrayLocator
 * Description: This tool helps the user find the appropriate array to use
 * Copyright:    Copyright (c)
 * Company:  mctest
 * @author John D. McGregor
 * @version 1.0
 */

public class ArrayLocator extends JFrame
    implements ActionListener{

  public ArrayLocator() {
    setTitle("Array Locator");
    setSize(500,500);
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    functionMenu = new JMenu("Functions");
    oaItem = new JMenuItem("Capture Table of Data");
    mappedItem = new JMenuItem("Fit Problem to Array");
    exitItem = new JMenuItem("Exit");
    oaItem.addActionListener(this);
    mappedItem.addActionListener(this);
    exitItem.addActionListener(this);
    functionMenu.add(mappedItem);
    functionMenu.add(oaItem);
    functionMenu.add(exitItem);
    menuBar.add(functionMenu);
    tableSetUp();
  }

  /**
   * This method fires off the individual menu items
   * @param evt
   */
  public void actionPerformed(ActionEvent evt){
    String arg = evt.getActionCommand();
    Object source = evt.getSource();
    if(arg.equals("Capture Table of Data"))
      this.table();
    if(arg.equals("Fit Problem to Array"))
      this.fit();
    else if(arg.equals("Exit"))
      setVisible(false);
  }

  private void tableSetUp(){
    int numFactors = inputBox.getInteger("Number of Factors");
    int numRows = inputBox.getInteger("Maximum Number of Levels for any Factor");
    model = new DefaultTableModel(numRows,numFactors);
    table = new JTable(model);
    columnNames = new String[numFactors];
    for(int i=0;i < numFactors; i++){
      columnNames[i]=inputBox.getString("Enter Name for Factor: "+i);
    }
    model.setColumnIdentifiers(columnNames);
    this.getContentPane();
    this.getContentPane().add(new JScrollPane(table), "Center");
    for(int i=0;i < model.getRowCount();i++){
      for(int j=0;j < model.getColumnCount();j++){
        model.setValueAt(" ",i,j);
      }
    }
  }

  private void table(){
    for(int i=0;i < model.getRowCount();i++){
      for(int j=0;j < model.getColumnCount();j++){
        //output.print((String)model.getValueAt(i,j)+"\t");
      }
      //output.printLine(" ");
    }
  }

  private void fit(){

  }

  public String[] getFactorNames(){
    return columnNames;
  }

  public static void main(String[] args){
    ArrayLocator t1 = new ArrayLocator();
    t1.setVisible(true);
  }

  private String[] columnNames;
  private Container contentPane;
  private DefaultTableModel model;
  private JTable table;
  private JMenuBar menuBar;
  private JMenuItem oaItem;
  private JMenuItem mappedItem;
  private JMenuItem exitItem;
  private JMenu functionMenu;
  private InputBox inputBox = new InputBox(this);
}