<%@ page language="java" import="java.io.*,java.util.*,java.beans.*,iDSS.*,iDSS.utils.*,iDSS.webBeans.*,iDSS.beans.*,iDSS.beans2.*,iDSS.grids.*,iDSS.sm.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE><%=IDSSAppConstants.programTitle%></TITLE>

<META HTTP-EQUIV="KEYWORDS" CONTENT="internet-based, DSS, decision support system, Java, Enterprise edition, J2EE">
<META HTTP-EQUIV="DESCRIPTION" CONTENT="">		
</HEAD>

<jsp:useBean id="modelSetupControllerJBean" scope="session" class="iDSS.beans2.ModelSetupControllerJBean"/>
<jsp:useBean id="statusJBean" scope="session" class="iDSS.beans2.StatusJBean"/>
<jsp:useBean id="survivalModelsListJBean" scope="session" class="iDSS.beans2.SurvivalModelsListJBean"/>


<body>
<h2><%=IDSSAppConstants.modelSetupVegetationModels%></h2>


<%




    String actionButtonName = request.getParameter("action");
    statusJBean = modelSetupControllerJBean.getStatusBean();

/**
    Vector smModels = new Vector();
    smModels.add("iDSS.sm.EllipticSurvivalModel");
    smModels.add("iDSS.sm.ExponentialSurvivalModel");
    //smModels.add("iDSS.sm.TimeVariantSurvivalModel");
**/

    Vector smModels = survivalModelsListJBean.getSmModelNames();


    String smClass = null;
    String landClass = null;
    SurvivalModel sm = null;

    if(actionButtonName != null) {


        smClass = request.getParameter("smClass");
        landClass = request.getParameter("landClass");


%>
        <p>The button you clicked is <%=actionButtonName%>.</p>
        <p>landClass : <%=landClass%></p>
        <p>smClass : <%=smClass%></p>
<%

        if (actionButtonName.equals(IDSSAppConstants.defineButtonText)) {

            sm = survivalModelsListJBean.define(smClass);

        } //if (actionButtonName.equals(IDSSAppConstants.defineButtonText))


        if (actionButtonName.equals(IDSSAppConstants.detailsButtonText)) {

            String[] classNames = request.getParameterValues(IDSSAppConstants.selectButtonCommand);

            if(classNames != null) {

                if(classNames.length > 0) {

                    landClass = classNames[0];
                    sm = survivalModelsListJBean.showDetails(landClass,false);

                } //if(classNames.length > 0)

            } //if(classNames != null)

        } //if (actionButtonName.equals(IDSSAppConstants.detailsButtonText))


        else if (actionButtonName.equals(IDSSAppConstants.acceptButtonText)) {


            if(smClass != null) {

                sm = survivalModelsListJBean.define(smClass);

                Enumeration paramNames = request.getParameterNames();
                Hashtable h = new Hashtable();

                while(paramNames.hasMoreElements()) {

                    String paramName = (String)paramNames.nextElement();
                    String paramValue = request.getParameter(paramName);

                    if(paramValue != null) {
                        h.put(paramName,paramValue);

                    } //if(paramValue != null)

                } //while(paramNames.hasMoreElements())


                sm.setParameters(h);


                //String fileName = FileNameDialog.getTempFileName();
                String fileName = "sm_" + landClass;
                File file = new File(IDSSAppConstants.smDir,fileName);
                boolean success = survivalModelsListJBean.setNewSurvivalModel(file,sm);

                if(success) {

                    survivalModelsListJBean.addToHashtable(landClass,file.toString());

                } //if(success)



            } //if(smClass != null)


        } //else if (actionButtonName.equals(IDSSAppConstants.acceptButtonText))



        else if (actionButtonName.equals(IDSSAppConstants.selectAllButtonText)) {

            Hashtable vegModels = survivalModelsListJBean.getSurvivalModels();
            modelSetupControllerJBean.setAllSurvivalModels(vegModels);
            statusJBean.setVegMod(IDSSAppConstants.doneText);

        } //else if (actionButtonName.equals(IDSSAppConstants.selectAllButtonText))




    } //if(actionButtonName != null)
%>







<form method=get>


    <table width="60%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">
        <tr BGCOLOR="#C0C0C0">
            <th width="5%">Sel</th>
            <th width="20%">Landclass</th>
            <th width="35%">Model Name</th>
        </tr>

    <%

        Hashtable vegModels = survivalModelsListJBean.getSurvivalModels();
        Enumeration classNames = vegModels.keys();

        while(classNames.hasMoreElements()) {

            String className = (String)classNames.nextElement();
            String modelFileName = (String)vegModels.get(className);

    %>

            <tr BGCOLOR="#80FFFF">
                <td align="center"><input type=checkbox name="<%=IDSSAppConstants.selectButtonCommand%>" value="<%=className%>" justify="center"></td>
                <td align="center"><%=className%> </td>
                <td><%=modelFileName%> </td>
            </tr>

    <%
        } //while(classNames.hasMoreElements())
    %>

    </table>

    <table border="0">
        <tr>
            <td><input type=submit value="<%=IDSSAppConstants.detailsButtonText%>" name="action" ></td>
            <td><input type=submit value="<%=IDSSAppConstants.defineButtonText%>" name="action" ></td>
            <td>
                <select name="landClass" size="1">
                    <%
                        Vector landClasses = modelSetupControllerJBean.getLandClasses();
                        for(int i=0;i<landClasses.size();i++) {
                            String lc = (String)landClasses.elementAt(i);
                            if(lc.equals(landClass)) {
                    %>
                                <option value="<%=lc%>" selected><%=lc%></option>
                    <%
                            } //if(lc.equals(landClass))

                            else {
                    %>
                                <option value="<%=lc%>"><%=lc%></option>
                    <%
                            } //else

                        } //for(int i=0;i<landClasses.size();i++)
                    %>
                </select>
            </td>
            <td>
                <select name="smClass" size="1">
                    <%
                        for(int i=0;i<smModels.size();i++) {
                            String smModel = (String)smModels.elementAt(i);
                            if(smModel.equals(smClass)) {
                    %>
                                <option value="<%=smModel%>" selected><%=smModel%></option>
                    <%
                            } //if(smModel.equals(smClass))

                            else {
                    %>
                                <option value="<%=smModel%>"><%=smModel%></option>
                    <%
                            } //else

                        } //for(int i=0;i<smModels.size();i++)
                    %>
                </select>
            </td>
        </tr>
    </table>


    <table border="0">
        <tr>
            <td><input type=submit value="<%=IDSSAppConstants.selectAllButtonText%>" name="action" ></td>
        </tr>
    </table>


</form>


<%

    if(sm != null && landClass != null) {

%>

        <form>
            <input type=hidden name="smClass" value="<%=smClass%>">
            <input type=hidden name="landClass" value="<%=landClass%>">
            <h3><%=sm.getName()%>, <%=smClass%>, <%=landClass%></h3>
            <table width="40%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">

                <%
                    Hashtable params = sm.getParameters();
                    Enumeration paramNames = params.keys();
                    while(paramNames.hasMoreElements()) {

                        String paramName = (String)paramNames.nextElement();
                        String value = params.get(paramName).toString();
                %>
                        <tr>
                            <td BGCOLOR="#C0C0C0" width="25%">
                                <%=paramName%>
                            </td>
                            <td BGCOLOR="#80FFFF" width="15%">
                                <input type="text" name="<%=paramName%>" value=<%=value%> size="20">
                            </td>
                        </tr>
                <%

                    } //while(paramNames.hasMoreElements())

                %>

            </table>

            <input type=submit name="action" value="<%=IDSSAppConstants.acceptButtonText%>">

        </form>

<%

    } //if(sm != null && landClass != null)

%>






</body>

</HTML>

