/*
 * 
 * Created on Jun 4, 2005
 * 
 * Gy�rgy S�veges
 * Etixpert Kft.
 * Copyright(c) 2005
 * 
 */
package com.etixpert.evolution.gui.servlets.elements;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.fmt.*;

/**
 * @author Suveges Gyorgy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract  class Select extends FormElement {
    protected boolean emptyRow = false;
    /**
     * @param session
     * @param jspSite
     * @param key
     */
    public Select(HttpSession session, String jspSite, String key) {
        super(session, jspSite, key);
        // TODO Auto-generated constructor stub
    }
    
    public Select(HttpSession session, String jspSite, String key, String defaultValue) {
        super(session, jspSite, key, defaultValue);
        // TODO Auto-generated constructor stub
    }

    protected abstract void reset() throws Exception;
    
    protected abstract boolean hasNext() throws Exception;
    
    protected abstract String[] next() throws Exception;
    
    
    protected String generateOptions() throws Exception {
        StringBuffer sb = new StringBuffer();
        reset();
        if (emptyRow)
            sb.append("<option value\"\"></option>\n");
        while (hasNext()) {
            String[] pair = next();
            sb.append("<option value=\"").append(pair[0]).append("\"");
            if (pair[0].equalsIgnoreCase(getValue())) sb.append(" selected");
            sb.append(">").append(localize?LocaleSupport.getLocalizedMessage(pageContext,pair[1]):pair[1]).append("</option>\n");
        }
        
        return sb.toString();
        
    }
    
    /* (non-Javadoc)
     * @see com.etixpert.evolution.gui.servlets.elements.FormElement#toHTML()
     */
    public String toHTML() throws Exception {
        // TODO Auto-generated method stub
        StringBuffer sb = new StringBuffer();
        sb.append("<select name=\"").append(key).append("\" ");
        sb.append(propertiesToHTML()).append(">\n");
        sb.append(generateOptions());
        sb.append("</select>");
        return sb.toString();
        
        
    }

    /**
     * @return Returns the emptyRow.
     */
    public boolean isThereEmptyRow() {
        return emptyRow;
    }
    /**
     * @param emptyRow The emptyRow to set.
     */
    public void setEmptyRow(boolean emptyRow) {
        this.emptyRow = emptyRow;
    }
}
