/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Core;
import Readers.CSVReader;
import Readers.FileReader;
import Readers.WSSReader;
import Readers.XLSReader;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JTable;
import jxl.read.biff.BiffException;
/**
* Controls the flow of data from the input media to the data object
* It handles the file extensions and reads the data from varous supported file
* formats, creates a data object and returns it to the calling method.
* @author Amir
*/
public class DataManager {
private Data data;
private boolean dataImported;
/**
* Constructor for class Data Manager
*/
public DataManager(){
data = new Data();
dataImported = false;
}
/**
* Constructor for class Data Manager
* @param d
* Data object to be managed
*/
public DataManager(Data d){
data = d;
dataImported = true;
}
/**
* Reads the contents of the given data file
* @param dataFilePath
* Full path of the data file
* @throws FileNotFoundException
* @throws IOException
* @throws BiffException
* @throws Exception
*/
public void importData(String dataFilePath)throws FileNotFoundException, IOException, BiffException, Exception{
dataFilePath = dataFilePath.toLowerCase();
FileReader fileReader=null;
if(dataFilePath.endsWith("csv")){
BufferedReader in=null;
in = new BufferedReader(new java.io.FileReader(dataFilePath));
fileReader = new CSVReader(in);
}
if(dataFilePath.endsWith("xls")){
fileReader = new XLSReader(new File(dataFilePath));
}
if(dataFilePath.endsWith("wss")){
fileReader = new WSSReader(new File(dataFilePath));
}
if (fileReader!=null)
data = fileReader.readData();
else throw new Exception("Usupported File Extension");
dataImported = true;
}
/**
* Sets the data to be managed
* @param d
* Data object
*/
public void setData(Data d){
data=d;
dataImported = true;
}
/**
* Returns the previously imported data.
* Warning: Use this method only after importing the data using importData method,
* otherwise and exception will be thrown
* @return
* Imported data
* @throws Exception
*/
public Data getData() throws Exception{
if(!dataImported)
throw new Exception("Data is not imported yet! Try importing data first.");
return data;
}
/**
* Returns a table containing the values section of the data file
* Warning: Use this method only after importing the data using importData method,
* otherwise and exception will be thrown
* @return
* Table containing the imported data
* @throws Exception If data in not imporetd or contains error
*/
public JTable getDataValuesTable() throws Exception{
if(!dataImported)
throw new Exception("Data is not imported yet! Try importing data first.");
// Array containing the column names of the table
String[] columnNames = {"Node",
"Depth",
"Mod of Subgrade Reaction",
"Lateral Soil Movement",
"Limit Soil Pressure"};
// Retreive the data to be put inside the table
Object[][] dataValues = getDataValues(data);
final JTable dataTable = new JTable(dataValues, columnNames);
dataTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
dataTable.setFillsViewportHeight(true);
return dataTable;
}
/**
* Returns a table containing the header section of the data file
* Warning: Use this method only after importing the data using importData method,
* otherwise and exception will be thrown
* @return
* Table contaning the header section of data file
* @throws Exception
*/
public JTable getDataHeadersTable() throws Exception{
if(!dataImported)
throw new Exception("Data is not imported yet! Try importing data first.");
String[] columnNames = {"Number of Elements",
"Pile Length",
"EI",
"Diameter of Pile",
"Number of Incremental Soil Displacements"};
Object[][] headerValues = new Object[1][5];
headerValues[0][0] = data.getNumOfElements();
headerValues[0][1] = data.getPileLength();
headerValues[0][2] = data.getEI();
headerValues[0][3] = data.getPileDiameter();
headerValues[0][4] = data.getNumOfPiles();
JTable headersTable = new JTable(headerValues, columnNames);
headersTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
headersTable.setFillsViewportHeight(true);
return headersTable;
}
//Fills the two dimentional array with data fields
private Object[][] getDataValues(Data data) {
int numOfPileElements = data.getNumOfElements();
Object[][] dataValues = new Object[numOfPileElements+1][5];
for(int row=0;row<=numOfPileElements;row++){
for(int col=0;col<5;col++){
switch (col){
case 0:
dataValues[row][col] = row;
break;
case 1:
dataValues[row][col] = data.getPileNodeCoordinate(row);
break;
case 2:
dataValues[row][col] = data.getModOfSubgradeReaction(row);
break;
case 3:
dataValues[row][col] = data.getLateralSoilMovement(row);
break;
case 4:
dataValues[row][col] = data.getLimitSoilPressure(row);
break;
}
}
}
return dataValues;
}
}