/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io.builders;

import java.beans.PropertyVetoException;

import com.approximatrix.draco.analysis.RunANOVA;
import com.approximatrix.draco.analysis.ui.ANOVADialog;
import com.approximatrix.draco.analysis.ui.AnalysisChildDialog;
import com.approximatrix.draco.application.ChildFrameRegistrar;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.fit.RunLS;
import com.approximatrix.draco.fit.RunLSAutocorrelation;
import com.approximatrix.draco.fit.RunNLLS;
import com.approximatrix.draco.fit.RunTSLS;
import com.approximatrix.draco.fit.SimpleMLEFit;
import com.approximatrix.draco.fit.ui.FitChildDialog;
import com.approximatrix.draco.fit.ui.LeastSquaresFit;
import com.approximatrix.draco.fit.ui.NonLinearLSFit;
import com.approximatrix.draco.fit.ui.SimpleIterativeFit;
import com.approximatrix.draco.fit.ui.TSLeastSquaresFit;
import com.approximatrix.draco.io.XMLio.AnalysisLoadStore;

public class AnalysisBuilder {

	public static void BuildAnalysis(AnalysisLoadStore analysis, ChildFrameRegistrar parent, StatTableModel model) {
		
		//StatTableModel model = parent.getWindowTableModel();
		
		// Get the type
		Class type = analysis.getAnalysisClass();
		FitChildDialog fit_temp = null;
		AnalysisChildDialog simple_temp = null; 
		
		// Need to select the type and open the appropriate window
		if(type == RunLS.class || type ==  RunLSAutocorrelation.class ) { // Least Squares
			fit_temp = (FitChildDialog)parent.openWindow(new LeastSquaresFit(model));
			
		} else if(type == RunTSLS.class) { // Two Stage Least Squares
			fit_temp = (FitChildDialog)parent.openWindow(new TSLeastSquaresFit(model));
			
		} else if(type == RunNLLS.class) { // Non-Linear Least Squares
			fit_temp = (FitChildDialog)parent.openWindow(new NonLinearLSFit(model));
			
		} else if(type == SimpleMLEFit.class) { // MLE (Logit/Probit)
			fit_temp = (FitChildDialog)parent.openWindow(new SimpleIterativeFit(model,0));
		
		} else if(type == RunANOVA.class) { // ANOVA
			simple_temp = (AnalysisChildDialog)parent.openWindow(new ANOVADialog(model));
		}
		
		if(fit_temp != null) {
			fit_temp.setAnalysis(analysis);
			try {
				fit_temp.setIcon(true);
			} catch(PropertyVetoException pve) {
				System.err.println("Can't iconify: "+fit_temp.getTitle());
				pve.printStackTrace();
			}
		} else if(simple_temp != null) {
			simple_temp.setAnalysis(analysis);
			try {
				simple_temp.setIcon(true);
			} catch(PropertyVetoException pve) {
				System.err.println("Can't iconify: "+simple_temp.getTitle());
				pve.printStackTrace();
			}
		}	
	}
	
}
