Have you considered using OptionGroups as the labels?

On 5/31/07, Martin Dietze <[EMAIL PROTECTED]> wrote:

Hi,

I created a selectmodel based on a enum wrapped by a generic
wrapper and using a generic value encoder.

By this I intend to simplify creating select inputs consisting of
something like "please select" and "----" followed by the enum's
entries which are the only valid inputs for the select.

The code seems to work well, I the select options are created
properly and show up in the form, also selected values are
delivered after submit. However when rendering the result page
the previously submitted option is not selected.

I wonder whether my code is not OK or do I have to insert some
code on my own to get the last choice selected in the result
page?

Cheers,

Martin

<----------------------- snip ------------------------->

Here is my code:

The selectmodel:

|  public final class SalutationSelectModel extends AbstractSelectModel {
|  private final Messages _msgs;
|
|  public SalutationSelectModel(final Messages msgs) {
|    _msgs = msgs;
|  }
|
|  public List<OptionGroupModel> getOptionGroups() {
|    return null;
|  }
|  public List<OptionModel> getOptions() {
|    final List<OptionModel> result = new ArrayList<OptionModel>();
|
|    // Add "please select..." entry and disabled "------"...
|    result.add(new OptionModelImpl(_msgs.get("select_pleaseSelect"),
|      false, null, new String[0]));
|    result.add(new OptionModelImpl(_msgs.get( "select_separator"),
|      true, new CommonSelectView<Salutation>(null), new String[0]));
|
|    for(Salutation salut : Salutation.values()) {
|      result.add(new OptionModelImpl(_msgs.get("salutation_" + salut.name
()),
|      false, new CommonSelectView<Salutation>(salut), new String[0]));
|    }
|
|    return result;
|  }
|}

The enum:

| public enum Salutation {
|   MR,
|   MRS;
| }

The generic enum wrapper:

| public class CommonSelectView<M> {
|   private final M _model;
|   public CommonSelectView(M model) {
|     _model = model;
|   }
|   public M getModel() {
|     return _model;
|   }
| }

The generic value encoder lets me differenciate between
invalid inputs (like "please select") and disabled ones so
that I do not get more than one selected="selected" when
the page is displayed for the first time:

| ValueEncoder<M extends Enum<M>> implements
ValueEncoder<CommonSelectView<M>> {
|
|   private Class<M> _cls;
|   public CommonValueEncoder(Class<M> cls) {
|     _cls = cls;
|   }
|
|   public String toClient(CommonSelectView<M> value) {
|     if (value == null) {
|       return "null";
|     } else if (value.getModel() == null) {
|         return "disabled";
|     }
|     return value.getModel().name();
|   }
|
|   public CommonSelectView<M> toValue(String clientValue) {
|     if ("null".equals( clientValue) || "disabled".equals(clientValue)) {
|       return null;
|     }
|     M m = Enum.valueOf(_cls, clientValue);
|     return new CommonSelectView<M>(m);
|   }
| }

All this is used in this page class:

| public class CreatePartner {
|   @Inject
|   private Messages _messages;
|   @Component
|   private Form _form;
|   @Persist
|   private CommonSelectView<Salutation> _contactSalutation;
|
|   private static ValueEncoder<CommonSelectView<Salutation>>
|     _salutationValueEncoder = new CommonValueEncoder<Salutation>(
Salutation.class);
|   private SelectModel _salutationModel;
|
|   public CreatePartner() {
|     _salutationModel = new SalutationSelectModel( _messages );
|   }
|
|   @OnEvent(value="submit")
|   public void submit() {
|     _form.recordError("Submit received: "
|       + (_contactSalutation != null? _contactSalutation.getModel() :
"null"));
|   }
|
|   public SelectModel getSalutationModel() {
|     return _salutationModel;
|   }
|
|   public ValueEncoder<CommonSelectView<Salutation>>
getSalutationValueEncoder() {
|     return _salutationValueEncoder;
|   }
|
|
|   public CommonSelectView<Salutation> getContactSalutation() {
|     return _contactSalutation;
|   }
|
|   public void setContactSalutation(CommonSelectView<Salutation>
contactSalutation) {
|     _contactSalutation = contactSalutation;
|   }
|
| }

Finally, in the form I have this entry for the select:

| <select name="salutation" id="salutation" tabindex="10"
|   t:type="select"
|   t:id="salutation"
|   t:encoder="salutationValueEncoder"
|   t:model="salutationModel"
|   t:validate="required"
|   t:value="contactSalutation"
|   >
|   <option selected="selected">bitte wählen...</option>
|   <option disabled="disabled">---------------</option>
|   <option>Herr</option>
|   <option>Frau</option>
| </select>

--
----------- / http://herbert.the-little-red-haired-girl.org /
-------------
=+=
* Free Speech Online!!! Support the Blue Ribbon Campaign! *

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

Reply via email to