Re: T5: select using a wrapped enum
I meant OptionGroups for the non-selectable labels. Make the HTML browser do the work for you. It actually looks nice, with the items indented beneath the matching labels. The indentation means you probably don't need the row of dashes. On 5/31/07, Martin Dietze <[EMAIL PROTECTED]> wrote: On Thu, May 31, 2007, Howard Lewis Ship wrote: > Have you considered using OptionGroups as the labels? To me it seemed that if I have to create a number of selects with similar structure, just plugging in the appropriate enum might actually be an elegant solution. Do you seen anything wrong with the way I implemented this? Cheers, Martin -- --- / http://herbert.the-little-red-haired-girl.org / - =+= - Are you attempting to tell me my duty, sir? - No. Just having fun trying to guess what they are. - 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
Re: T5: select using a wrapped enum
On Thu, May 31, 2007, Howard Lewis Ship wrote: > Have you considered using OptionGroups as the labels? To me it seemed that if I have to create a number of selects with similar structure, just plugging in the appropriate enum might actually be an elegant solution. Do you seen anything wrong with the way I implemented this? Cheers, Martin -- --- / http://herbert.the-little-red-haired-girl.org / - =+= - Are you attempting to tell me my duty, sir? - No. Just having fun trying to guess what they are. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: T5: select using a wrapped enum
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 getOptionGroups() { |return null; | } | public List getOptions() { |final List result = new ArrayList(); | |// 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(null), new String[0])); | |for(Salutation salut : Salutation.values()) { | result.add(new OptionModelImpl(_msgs.get("salutation_" + salut.name ()), | false, new CommonSelectView(salut), new String[0])); |} | |return result; | } |} The enum: | public enum Salutation { | MR, | MRS; | } The generic enum wrapper: | public class CommonSelectView { | 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> implements ValueEncoder> { | | private Class _cls; | public CommonValueEncoder(Class cls) { | _cls = cls; | } | | public String toClient(CommonSelectView value) { | if (value == null) { | return "null"; | } else if (value.getModel() == null) { | return "disabled"; | } | return value.getModel().name(); | } | | public CommonSelectView toValue(String clientValue) { | if ("null".equals( clientValue) || "disabled".equals(clientValue)) { | return null; | } | M m = Enum.valueOf(_cls, clientValue); | return new CommonSelectView(m); | } | } All this is used in this page class: | public class CreatePartner { | @Inject | private Messages _messages; | @Component | private Form _form; | @Persist | private CommonSelectView _contactSalutation; | | private static ValueEncoder> | _salutationValueEncoder = new CommonValueEncoder( 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> getSalutationValueEncoder() { | return _salutationValueEncoder; | } | | | public CommonSelectView getContactSalutation() { | return _contactSalutation; | } | | public void setContactSalutation(CommonSelectView contactSalutation) { | _contactSalutation = contactSalutation; | } | | } Finally, in the form I have this entry for the select: | | bitte wählen... | --- | Herr | Frau | -- --- / 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
T5: select using a wrapped enum
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 getOptionGroups() { |return null; | } | public List getOptions() { |final List result = new ArrayList(); | |// 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(null), new String[0])); | |for(Salutation salut : Salutation.values()) { | result.add(new OptionModelImpl(_msgs.get("salutation_" + salut.name()), | false, new CommonSelectView(salut), new String[0])); |} | |return result; | } |} The enum: | public enum Salutation { | MR, | MRS; | } The generic enum wrapper: | public class CommonSelectView { | 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> implements ValueEncoder> { | | private Class _cls; | public CommonValueEncoder(Class cls) { | _cls = cls; | } | | public String toClient(CommonSelectView value) { | if (value == null) { | return "null"; | } else if (value.getModel() == null) { | return "disabled"; | } | return value.getModel().name(); | } | | public CommonSelectView toValue(String clientValue) { | if ("null".equals( clientValue) || "disabled".equals(clientValue)) { | return null; | } | M m = Enum.valueOf(_cls, clientValue); | return new CommonSelectView(m); | } | } All this is used in this page class: | public class CreatePartner { | @Inject | private Messages _messages; | @Component | private Form _form; | @Persist | private CommonSelectView _contactSalutation; | | private static ValueEncoder> | _salutationValueEncoder = new CommonValueEncoder(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> getSalutationValueEncoder() { | return _salutationValueEncoder; | } | | | public CommonSelectView getContactSalutation() { | return _contactSalutation; | } | | public void setContactSalutation(CommonSelectView contactSalutation) { | _contactSalutation = contactSalutation; | } | | } Finally, in the form I have this entry for the select: | | bitte wählen... | --- | Herr | Frau | -- --- / 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]