
/**
 * Title:        OrthogonalArray<p>
 * Description:  This class is an abstract array. It has the nasics of being an
 * orthgonal array.<p>
 * Copyright:    Copyright (c) 2000<p>
 * Company:      mctest<p>
 * @author John D. McGregor
 * @version 1.0
 */

/*
The OATS Orthognal Array Test Design Tool
Copyright (C) 2000  McTest

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


package oats;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class OrthogonalArray {

  /**
   * Constructor that takes the number of factors in the array and the number
   * of test runs in the table
   * @param NumberFactors Number of factors to be in the array
   * @param NumberRows Number of test runs
   */
  public OrthogonalArray(int NumberFactors, int NumberRows) {
    numFactors = NumberFactors;
    maxRows = NumberRows;
    array = new String [maxRows][numFactors];
  }

  /**
   * Constructor that reads a file to find an existing array
   * @param fileName The name of the file containing the orthogonal array
   */
  public OrthogonalArray(String fileName){
    read(fileName);
  }

   /**
   * Constructor that reads a file to find an existing array
   * @param fileName The name of the file containing the orthogonal array
   */
  public OrthogonalArray(){

  }

  /**
   * Accessor that returns the number of levels in each factor
   * @return numLevels
   */
  public Vector getLevels(){
    return numLevels;
  }

  /**
   * Accessor that returns the number of factors represented by the array
   * @return numFactors
   */
  public int getNumberOfFactors(){
    return numFactors;
  }

  /**
   * Accessor that returns the number of test runs required by this array.
   * @return maxRows
   */
  public int getNumberOfRows(){
    return maxRows;
  }

  /**
   * Modifier method that sets the filename that is to be used to store the
   * array.
   * @param newName
   */
  public void setFileName(String newName){
    fileName = newName;
  }

  /**
   * Modifier method that sets the levels for all factors
   * @param levels A vector that contains the number of levels for each factor
   */
  public void putLevels(Vector levels){
    numLevels = levels;
  }

  /**
   * Modifier that places a specific value into the array
   * @param row - The row in the array
   * @param factor - The factor into which the value belongs
   * @param s - The array value
   */
  public void putElementAt(int row, int factor, String s){
    array[row][factor] = s;
  }

  /**
   * Accessor gets value from array
   * @param row
   * @param factor
   * @return element from array
   */
  public String getElementAt(int row, int factor){
    return array[row][factor];
  }

  /**
   * This method writes the array to a file
   * @param newName The filename to be used to save the array
   */
  public void save(String newName){
    FileWriter fileWriter;
    try{
      fileWriter = new FileWriter(newName);
      fileName = newName;
      fileWriter.write(new Integer(numFactors).toString()+"\n");
      fileWriter.write(new Integer(maxRows).toString()+"\n");
      for(int i=0;i<numFactors;i++){
        fileWriter.write(numLevels.elementAt(i).toString()+"\n");
      }
      for(int i=0;i<maxRows;i++){
        for(int j=0;j<numFactors;j++){
             fileWriter.write(array[i][j]+"\n");
        }
      }
      fileWriter.close();
    }catch(IOException e){e.printStackTrace();}
  }

  /**
   * This method reads the array from a file
   */
  public void read(String s){
    FileReader fileReader;
    BufferedReader bufReader;
    try{

      fileReader = new FileReader(s);
      bufReader = new BufferedReader(fileReader);

      numFactors = Integer.parseInt(bufReader.readLine());
      maxRows = Integer.parseInt(bufReader.readLine());
      //System.out.println("Values "+numFactors+" "+maxRows);
      array = new String[maxRows][numFactors];
      for(int i=0;i<numFactors;i++){
        numLevels.add(i, bufReader.readLine());
      }
      // System.out.println("S is "+s);
      for(int i=0;i<maxRows;i++){
      //  int tmp = ((Integer)numLevels.elementAt(i)).intValue();
        for(int j=0;j<numFactors;j++){
             array[i][j] = bufReader.readLine();
             //System.out.println(array[i][j]);
        }
      }
      bufReader.close();
      //display();
    }catch(IOException e){e.printStackTrace();}
  }

  /**
   * This method displays the array in a window
   * Probably the method should return the string that it builds
   * so that the GUI can control the display
   */
  /*
  public void display(){
    ta = new JTextField();
    sp = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    sp.setVisible(false);
    String total ="";
    for(int i=0;i<maxRows;i++){
        for(int j=0;j<numFactors;j++){
             total = total + array[i][j]+" ";
        }
        total = total + "\n";
    }
    //System.out.println(total);

    ta.setText(total);
    sp.setVisible(true);
    //ta.setVisible(true);
    //sp.setVisible(true);
  }
*/
  protected int numFactors;
  protected int maxRows;
  protected Vector numLevels = new Vector(10);
  protected String [][] array;
  protected String fileName;
  protected JTextField ta;
  protected JScrollPane sp;

}
