//
// Tapestry Web Application Framework
// Copyright (c) 2000-2002 by Howard Lewis Ship
//
// Howard Lewis Ship
// http://sf.net/projects/tapestry
// mailto:hship@users.sf.net
//
// This library is free software.
//
// You may redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License as published by the Free Software Foundation.
//
// Version 2.1 of the license should be included with this distribution in
// the file LICENSE, as well as License.html. If the license is not
// included with this distribution, you may find a copy at the FSF web
// site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
// Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied waranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//

package com.genscape.reports;

import net.sf.tapestry.IMarkupWriter;
import net.sf.tapestry.IRequestCycle;
import net.sf.tapestry.RequestCycleException;
import net.sf.tapestry.contrib.form.MultiplePropertySelection;
import net.sf.tapestry.contrib.form.IMultiplePropertySelectionRenderer;
import net.sf.tapestry.form.IPropertySelectionModel;

/**
 *  Implementation of {@link IMultiplePropertySelectionRenderer} that
 *  produces select box that allows multiple selections.
 *
 **/

public class MultiSelectBoxRenderer
    implements IMultiplePropertySelectionRenderer
{
    private static final int DEFAULT_HEIGHT = 10;
    private int height = DEFAULT_HEIGHT;

    public MultiSelectBoxRenderer(int size) {
        this.height = size;
    }

    public MultiSelectBoxRenderer() {
    }

    /**
     *  Writes the &lt;select&gt; element.
     *
     **/

    public void beginRender(
        MultiplePropertySelection component,
        IMarkupWriter writer,
        IRequestCycle cycle)
        throws RequestCycleException
    {
        writer.begin("select");
        writer.attribute("name", component.getName());
        writer.attribute("size", this.height);
        writer.attribute("multiple");
        writer.println();
    }

    /**
     *  Closes the &lt;select&gt; element.
     *
     **/

    public void endRender(
        MultiplePropertySelection component,
        IMarkupWriter writer,
        IRequestCycle cycle)
        throws RequestCycleException
    {
        writer.end(); // select
    }

    /**
     *  Writes the options for the select box
     *
     **/

    public void renderOption(
        MultiplePropertySelection component,
        IMarkupWriter writer,
        IRequestCycle cycle,
        IPropertySelectionModel model,
        Object option,
        int index,
        boolean selected)
        throws RequestCycleException
    {
        writer.begin("option");
        writer.attribute("value", model.getValue(index));
        writer.print(model.getLabel(index));
        writer.end();

        writer.println();
    }
}