<%@ page language="java" import="java.awt.*,java.awt.image.*,java.util.*,java.beans.*,iDSS.*,iDSS.utils.*,iDSS.webBeans.*,iDSS.beans.*,iDSS.beans2.*,iDSS.grids.*" errorPage="/idss/error/error.jsp"%>

<!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="gridManagerJBean" scope="session" class="iDSS.beans2.GridManagerJBean"/>
<jsp:useBean id="modelSetupControllerJBean" scope="session" class="iDSS.beans2.ModelSetupControllerJBean"/>
<jsp:useBean id="statusJBean" scope="session" class="iDSS.beans2.StatusJBean"/>


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


<%

    //PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    String actionButtonName = request.getParameter("action");
    //String localFileName = request.getParameter("fileNameCommand");
    String message;
    String userGridFileName = null;
    Vector selectedGrids = new Vector();

    gridManagerJBean.addPropertyChangeListener(modelSetupControllerJBean);
    statusJBean = modelSetupControllerJBean.getStatusBean();


    Cookie[] cookies = request.getCookies();
    boolean found = false;
    Cookie cookie = null;

    for(int i=0;i<cookies.length;i++) {

        cookie = cookies[i];

        if(cookie.getName().equals(IDSSAppConstants.userGridFileName)) {

            found = true;
            break;

        } //if(cookie.getName().equals(IDSSAppConstants.userGridFileName))

    } //for(int i=0;i<cookies.length;i++)


    if(found) {
        userGridFileName = cookie.getValue();
    } //if(found)




    if(userGridFileName != null) {

        message = gridManagerJBean.pickGrid(userGridFileName);

%>
        <p><%=message%>.</p>
<%


    } //if(userGridFileName != null)



/**

%>

    <p>userGridFileName : <%=userGridFileName%>.</p>
    <p>contextPath : <%=request.getContextPath()%>.</p>
    <p>pathInfo : <%=request.getPathInfo()%>.</p>
    <p>pathTranslated : <%=request.getPathTranslated()%>.</p>
    <p>ServletPath : <%=request.getServletPath()%>.</p>

<%
**/


    if(actionButtonName != null) {

/**
%>
        <p>The button you clicked is <%=actionButtonName%>. Local file name is <%=localFileName%>.</p>
<%
**/
        if (actionButtonName.equals(IDSSAppConstants.pickServerButtonText)) {

            gridManagerJBean.pickFromServer();

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


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

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

            if(gridNames != null) {

                if(gridNames.length > 0) {

                    String fileName = gridNames[0];
                    gridManagerJBean.setCurrentFileName(fileName);
                    gridManagerJBean.selectGrid();

                } //if(gridNames.length > 0)

            } //if(gridNames != null)

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


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


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

            if(gridNames != null) {

                if(gridNames.length > 0) {

                    String fileName = gridNames[0];
                    gridManagerJBean.setCurrentFileName(fileName);

                    BufferedImage img = gridManagerJBean.getImageAndLegend();
                    request.setAttribute(IDSSAppConstants.image,img);

                    %>
                        <jsp:forward page="imgDisp/drawImg.jsp"/>
                    <%

                } //if(gridNames.length > 0)

            } //if(gridNames != null)


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



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







<form method=get>


    <table width="70%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">
        <tr BGCOLOR="#C0C0C0">
            <th width="10%">Sel</th>
            <th width="30%">GridName</th>
            <th width="30%">FileName</th>
        </tr>

    <%
        Hashtable grids = gridManagerJBean.getGrids();

        Enumeration fileNames = grids.keys();

        while(fileNames.hasMoreElements()) {

            String fileName = (String)fileNames.nextElement();
            String gridName = ((Grid)grids.get(fileName)).getName();

    %>

        <tr width="70%" BGCOLOR="#80FFFF">
            <td width="10%"><input type=checkbox name="<%=IDSSAppConstants.selectButtonCommand%>" value="<%=fileName%>" justify="center"></td>
            <td width="30%"><%=fileName%> </td>
            <td width="30%"><%=gridName%> </td>
        </tr>

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

    </table>

    <table border="0">
        <tr>
            <td><input type=submit value="<%=IDSSAppConstants.drawButtonText%>" name="action" ></td>
            <td><input type=submit value="<%=IDSSAppConstants.selectButtonText%>" name="action" ></td>
            <td><input type=submit value="<%=IDSSAppConstants.disposeButtonText%>" name="action" ></td>
            <td><input type=submit value="<%=IDSSAppConstants.describeButtonText%>" name="action" ></td>
        </tr>
    </table>


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


</form>




<form action="/idss/uploadHandler/UploadHandler" enctype="multipart/form-data" method="post">

    <input type=hidden value="<%=request.getContextPath()%><%=request.getServletPath()%>" name="<%=IDSSAppConstants.callingComponent%>" >


    <table border="0">
        <tr>
            <td><input type=submit value="<%=IDSSAppConstants.pickButtonText%>" name="<%=IDSSAppConstants.pickButtonCommand%>" ></td>
            <td><input type=file value="<%=IDSSAppConstants.pickButtonText%>" name="<%=IDSSAppConstants.pickButtonCommand%>" ></td>
        </tr>
    </table>


</form>






</body>

</HTML>

