Differences between revisions 2 and 3
Revision 2 as of 2008-01-20 00:32:56
Size: 2272
Editor: TedSteen
Comment:
Revision 3 as of 2009-09-20 23:20:46
Size: 2272
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Named property selection model

NamedSelectModel.java

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.tapestry.OptionGroupModel;
import org.apache.tapestry.OptionModel;
import org.apache.tapestry.SelectModel;
import org.apache.tapestry.util.AbstractSelectModel;

public class NamedSelectModel extends AbstractSelectModel
{
    private List<OptionModel> options = new ArrayList<OptionModel>();
    private SelectModel model;

    public NamedSelectModel(final String label, SelectModel model)
    {
        this.model = model;
        options.add(new OptionModel() {

            public Map<String, String> getAttributes()
            {
                return null;
            }

            public String getLabel()
            {
                return label;
            }

            public Object getValue()
            {
                return null;
            }

            public boolean isDisabled()
            {
                return true;
            }
        });
        options.addAll(model.getOptions());
    }
    
    public List<OptionGroupModel> getOptionGroups()
    {
        return model.getOptionGroups();
    }

    public List<OptionModel> getOptions()
    {
        return options;
    }
}

Usage

Test.java

import java.util.Arrays;

import org.apache.tapestry.SelectModel;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.ioc.services.TypeCoercer;
import org.darkbits.medina.web.base.ShopBase;
import org.darkbits.medina.web.util.NamedSelectModel;

public class Test
{
    @Inject
    private TypeCoercer coercer;
    
    @Persist
    private String colour;
    
    public SelectModel getSelectModel()
    {
        return new NamedSelectModel("Select a colour", coercer.coerce(Arrays.asList("Red","Green","Blue"), SelectModel.class));
    }

    public String getColour()
    {
        return colour;
    }

    public void setColour(String colour)
    {
        this.colour = colour;
    }
}

template

Colour: <t:select model="selectModel" value="colour" />

Tapestry5NamedSelectModel (last edited 2009-09-20 23:20:46 by localhost)