/*
 * @author <a href="mailto:novotny@aei.mpg.de">Jason Novotny</a>
 * @author <a href="mailto:oliver.wehrens@aei.mpg.de">Oliver Wehrens</a>
 * @version $Id$
 */
package org.gridlab.gridsphere.provider.portletui.tags;

import org.gridlab.gridsphere.provider.portletui.beans.ListBoxBean;
import org.gridlab.gridsphere.provider.portletui.beans.ListBoxItemBean;
import org.gridlab.gridsphere.provider.portletui.tags.ContainerTag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Vector;

/**
 * A <code>ListBoxTag</code> represents a list box element
 */
public class ListBoxTag extends ContainerTag {

    protected ListBoxBean listbox = null;
    protected boolean isMultiple = false;
    protected int size = 1;
    protected String onChange = null;

    /**
     * Returns the (html) size of the field.
     *
     * @return size of the field
     */
    public int getSize() {
        return size;
    }

    /**
     * Sets the (html) size of the field
     *
     * @param size size of the field
     */
    public void setSize(int size) {
        this.size = size;
    }

    /**
     * Returns the onChange JavaScript function
     *
     * @return onChange JavaScript function
     */
    public String getOnChange() {
        return onChange;
    }

    /**
     * Sets the onChange JavaScript function
     *
     * @param onChange the onChange JavaScript function
     */
    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }


    /**
     * Sets m  ultiple selection
     *
     * @param isMultiple is true if listbox provides multiple selections, false otherwise
     */
    public void setMultipleSelection(boolean isMultiple) {
        this.isMultiple = isMultiple;
    }

    public void setListBoxBean(ListBoxBean listbox) {
        this.listbox = listbox;
    }

    public ListBoxBean getListBoxBean() {
        return listbox;
    }

    /**
     * Indicates if multiple selection is provided
     *
     * @return true if this listbox supports multiple selection, false otherwise
     */
    public boolean getMultipleSelection() {
        return isMultiple;
    }

    public int doStartTag() throws JspException {
        list = new Vector();

        if (!beanId.equals("")) {
            listbox = (ListBoxBean) pageContext.getAttribute(getBeanKey(), PageContext.REQUEST_SCOPE);
            if (listbox == null) {
                listbox = new ListBoxBean((HttpServletRequest)pageContext.getRequest(), beanId);
                listbox.setSize(size);
                listbox.setOnChange(onChange);
                this.setBaseComponentBean(listbox);
            } else {
                if (size != 1) listbox.setSize(size);
                this.updateBaseComponentBean(listbox);
                if (onChange != null) listbox.setOnChange(onChange);
            }
        } else {
            listbox = new ListBoxBean();
            listbox.setSize(size);
            listbox.setOnChange(onChange);
            this.setBaseComponentBean(listbox);
        }
        try {
            JspWriter out = pageContext.getOut();
            out.print(listbox.toStartString());
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }
        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag() throws JspException {

        Iterator it = list.iterator();
        while (it.hasNext()) {
            ListBoxItemBean itembean = (ListBoxItemBean) it.next();
            listbox.addBean(itembean);
        }
        try {
            JspWriter out = pageContext.getOut();
            out.print(listbox.toEndString());
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }

        return EVAL_PAGE;
    }

}

@@ -2566,60 +2566,8 @@
 ean(
-(HttpServletRequest)pageContext.getRequest(), beanId
 );%0A 

use empty constructor when constructing beans in tags