You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Richard W. Adams" <RW...@UP.COM> on 2012/04/25 15:00:31 UTC

Radio Choice Weirdness

My understanding of this class must be faulty. When Wicket calls my 
onSelectionChanged(), the argument is the display string, not the id 
value. For example, I'm expecting a milepost value like "123.456", but 
instead I get "End (123.456)" (the display value). Here's the code. can 
anyone see what I'm doing wrong?

private RadioChoice<String> createMilepostChoice(final String id, final 
TrackModel track) {

        final Milepost     startMP = track.getStartMP();
        final Milepost     endMP   = track.getEndMP  ();
        final List<String> choices = Arrays.asList(new 
String[]{format(START, startMP), format(END, endMP), OTHER});
        final ChoiceRenderer<String> renderer = new 
ChoiceRenderer<String>() {
                private static final long serialVersionUID = 1L;
        @Override public Object getDisplayValue(final String model) {
            return model;
        }
        @Override public String getIdValue(final String model, final int 
index) {
                String value;
                switch (index) {
                        case 0:
                                value = startMP.toString();
                                break;
                        case 1 :
                                value = endMP.toString();
                                break;
                        default :
                                value = "";
                }
                return value;
        }
        };
        final RadioChoice<String> choice = new RadioChoice<String>(id, 
choices, renderer) {
                private static final long serialVersionUID = 1L;
                @Override protected void onSelectionChanged(final Object 
new selection) {
                        final String selection = (String)newSelection;
                        if (track == primaryTrack) {
                                primaryMpChoice = selection;
                        } else {
                                secondaryMpChoice = selection;
                        }
                }
                @Override protected boolean 
wantOnSelectionChangedNotifications() {
                        return true;
                }
        };
        return choice;
}


**

This email and any attachments may contain information that is confidential and/or privileged for the sole use of the intended recipient.  Any use, review, disclosure, copying, distribution or reliance by others, and any forwarding of this email or its contents, without the express permission of the sender is strictly prohibited by law.  If you are not the intended recipient, please contact the sender immediately, delete the e-mail and destroy all copies.
**

Re: Radio Choice Weirdness

Posted by "Richard W. Adams" <RW...@UP.COM>.
Oops. Got it figured out. The page class was trying to store the choice in 
a String variable instead of a MilepostModel variable. All is well now. 
Thanks!

"RAM /abr./: Rarely Adequate Memory." 



From:   "Richard W. Adams" <RW...@UP.COM>
To:     users@wicket.apache.org
Cc:     cdschleh@up.com
Date:   04/25/2012 10:24 AM
Subject:        Re: Radio Choice Weirdness



Ok, I tried changing it to RadioChoice<MilepostModel> (code below). 
However, the onSelectionChanged() method STILL gets a String argument, as 
verified by the printf() output "New selection is a class 
java.lang.String: End (538.200)." But following that output, an exception 
is now thrown. I REALLY don't understand what's going on here. I've 
studied the Javadocs for RadioChoice, but they seem awfully sketchy, and 
don't shed any light on this.

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast 
to com.uprr.enm.web.track.mend.MilepostModel
        at com.uprr.enm.web.track.mend.MendStepChooseMP$4.getIdValue(
MendStepChooseMP.java:1)
        at 
org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.getModelValue(
AbstractSingleSelectChoice.java:166)
        at org.apache.wicket.markup.html.form.FormComponent.getValue(
FormComponent.java:911)
        at 
org.apache.wicket.markup.html.form.RadioChoice.onComponentTagBody(
RadioChoice.java:422)
        at org.apache.wicket.Component.renderComponent(Component.java:2725
)
        ... 198 more


==================================================================

//----------------------------------------------------------------------------------------------
private RadioChoice<MilepostModel> createTestChoice(final String id, final 

TrackModel track) {

        final List<MilepostModel> choices = new 
ArrayList<MilepostModel>();
        choices.add(new MilepostModel(track.getStartMP(), "Start"));
        choices.add(new MilepostModel(track.getEndMP(), "End"));
        choices.add(new MilepostModel("Other"));

        final ChoiceRenderer<MilepostModel> renderer = new 
ChoiceRenderer<MilepostModel>() {
                private static final long serialVersionUID = 1L;
        @Override public String getIdValue(final MilepostModel model, 
final int index) {
                final Milepost mp = model.getObject();
                return mp == null ? "" : mp.toString();
        }
        };
        final RadioChoice<MilepostModel> choice =
                new RadioChoice<MilepostModel>(id, choices, renderer) {
                private static final long serialVersionUID = 1L;
                @Override protected void onSelectionChanged(final Object 
newSelection) {
                        System.out.printf("New selection is a %s: %s%n", 
newSelection.getClass(), newSelection);
                }
                @Override protected boolean 
wantOnSelectionChangedNotifications() {
                        return true;
                }
        };
        return choice;
}

The milepost model class:

package com.uprr.enm.web.track.mend;

import org.apache.wicket.model.IModel;

import com.uprr.eni.read.vo.mp.Milepost;

//----------------------------------------------------------------------------------------------
/**
 * Data model for milepost values.
 * @author Dick Adams
 * @.copyright Union Pacific 2012
 */
class MilepostModel implements IModel<Milepost> {

private static final long serialVersionUID = 1L;

private final String text;
private Milepost mp;

//----------------------------------------------------------------------------------------------
/**
* Constructor.
* @param value The milepost value.
* @param text The textual description of the value. For example, if it 
represents the end
* of a milepost range, the text might be {@code End}.
*/
public MilepostModel(final Milepost value, final String text) {
        mp = value;
        this.text = text;
}
//----------------------------------------------------------------------------------------------
/**
 * Constructor with no value.
 * @param text The textual description of a {@code null} value.
 */
public MilepostModel(final String text) {
        this(null, text);
}
//----------------------------------------------------------------------------------------------
@Override public void detach() {
        mp = null;
}
//----------------------------------------------------------------------------------------------
@Override public Milepost getObject() {
        return mp;
}
//----------------------------------------------------------------------------------------------
@Override public void setObject(final Milepost object) {
        mp = object;
}
//----------------------------------------------------------------------------------------------
@Override public String toString() {
        return mp == null ? text : String.format("%s (%s)", text, 
mp.toString());
}
//----------------------------------------------------------------------------------------------
}





From:   Sven Meier <sv...@meiers.net>
To:     users@wicket.apache.org
Date:   04/25/2012 09:55 AM
Subject:        Re: Radio Choice Weirdness



 >When Wicket calls my onSelectionChanged(), the argument is the display 
string, not the id value.

Your RadioChoice is working on string choices so it will hand you the 
selected string.

I'd suggest to let your RadioChoice work with ints or preferably 
directly on the Milepost objects:

     final RadioChoice<Milepost> choice = new RadioChoice<Milepost>(...);

Sven

On 04/25/2012 03:00 PM, Richard W. Adams wrote:
> My understanding of this class must be faulty. When Wicket calls my
> onSelectionChanged(), the argument is the display string, not the id
> value. For example, I'm expecting a milepost value like "123.456", but
> instead I get "End (123.456)" (the display value). Here's the code. can
> anyone see what I'm doing wrong?
>
> private RadioChoice<String>  createMilepostChoice(final String id, final
> TrackModel track) {
>
>          final Milepost     startMP = track.getStartMP();
>          final Milepost     endMP   = track.getEndMP  ();
>          final List<String>  choices = Arrays.asList(new
> String[]{format(START, startMP), format(END, endMP), OTHER});
>          final ChoiceRenderer<String>  renderer = new
> ChoiceRenderer<String>() {
>                  private static final long serialVersionUID = 1L;
>          @Override public Object getDisplayValue(final String model) {
>              return model;
>          }
>          @Override public String getIdValue(final String model, final 
int
> index) {
>                  String value;
>                  switch (index) {
>                          case 0:
>                                  value = startMP.toString();
>                                  break;
>                          case 1 :
>                                  value = endMP.toString();
>                                  break;
>                          default :
>                                  value = "";
>                  }
>                  return value;
>          }
>          };
>          final RadioChoice<String>  choice = new RadioChoice<String>(id,
> choices, renderer) {
>                  private static final long serialVersionUID = 1L;
>                  @Override protected void onSelectionChanged(final 
Object
> new selection) {
>                          final String selection = (String)newSelection;
>                          if (track == primaryTrack) {
>                                  primaryMpChoice = selection;
>                          } else {
>                                  secondaryMpChoice = selection;
>                          }
>                  }
>                  @Override protected boolean
> wantOnSelectionChangedNotifications() {
>                          return true;
>                  }
>          };
>          return choice;
> }
>
>
> **
>
> This email and any attachments may contain information that is 
confidential and/or privileged for the sole use of the intended recipient. 

 Any use, review, disclosure, copying, distribution or reliance by others, 

and any forwarding of this email or its contents, without the express 
permission of the sender is strictly prohibited by law.  If you are not 
the intended recipient, please contact the sender immediately, delete the 
e-mail and destroy all copies.
> **
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org




**

This email and any attachments may contain information that is 
confidential and/or privileged for the sole use of the intended recipient. 
 Any use, review, disclosure, copying, distribution or reliance by others, 
and any forwarding of this email or its contents, without the express 
permission of the sender is strictly prohibited by law.  If you are not 
the intended recipient, please contact the sender immediately, delete the 
e-mail and destroy all copies.
**



**

This email and any attachments may contain information that is confidential and/or privileged for the sole use of the intended recipient.  Any use, review, disclosure, copying, distribution or reliance by others, and any forwarding of this email or its contents, without the express permission of the sender is strictly prohibited by law.  If you are not the intended recipient, please contact the sender immediately, delete the e-mail and destroy all copies.
**

Re: Radio Choice Weirdness

Posted by "Richard W. Adams" <RW...@UP.COM>.
Ok, I tried changing it to RadioChoice<MilepostModel> (code below). 
However, the onSelectionChanged() method STILL gets a String argument, as 
verified by the printf() output "New selection is a class 
java.lang.String: End (538.200)." But following that output, an exception 
is now thrown. I REALLY don't understand what's going on here. I've 
studied the Javadocs for RadioChoice, but they seem awfully sketchy, and 
don't shed any light on this.

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast 
to com.uprr.enm.web.track.mend.MilepostModel
        at com.uprr.enm.web.track.mend.MendStepChooseMP$4.getIdValue(
MendStepChooseMP.java:1)
        at 
org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.getModelValue(
AbstractSingleSelectChoice.java:166)
        at org.apache.wicket.markup.html.form.FormComponent.getValue(
FormComponent.java:911)
        at 
org.apache.wicket.markup.html.form.RadioChoice.onComponentTagBody(
RadioChoice.java:422)
        at org.apache.wicket.Component.renderComponent(Component.java:2725
)
        ... 198 more


==================================================================

//----------------------------------------------------------------------------------------------
private RadioChoice<MilepostModel> createTestChoice(final String id, final 
TrackModel track) {

        final List<MilepostModel> choices = new 
ArrayList<MilepostModel>();
        choices.add(new MilepostModel(track.getStartMP(), "Start"));
        choices.add(new MilepostModel(track.getEndMP(), "End"));
        choices.add(new MilepostModel("Other"));

        final ChoiceRenderer<MilepostModel> renderer = new 
ChoiceRenderer<MilepostModel>() {
                private static final long serialVersionUID = 1L;
        @Override public String getIdValue(final MilepostModel model, 
final int index) {
                final Milepost mp = model.getObject();
                return mp == null ? "" : mp.toString();
        }
        };
        final RadioChoice<MilepostModel> choice =
                new RadioChoice<MilepostModel>(id, choices, renderer) {
                private static final long serialVersionUID = 1L;
                @Override protected void onSelectionChanged(final Object 
newSelection) {
                        System.out.printf("New selection is a %s: %s%n", 
newSelection.getClass(), newSelection);
                }
                @Override protected boolean 
wantOnSelectionChangedNotifications() {
                        return true;
                }
        };
        return choice;
}

The milepost model class:

package com.uprr.enm.web.track.mend;

import org.apache.wicket.model.IModel;

import com.uprr.eni.read.vo.mp.Milepost;

//----------------------------------------------------------------------------------------------
/**
 * Data model for milepost values.
 * @author Dick Adams
 * @.copyright Union Pacific 2012
 */
class MilepostModel implements IModel<Milepost> {

private static final long serialVersionUID = 1L;

private final String text;
private Milepost mp;

//----------------------------------------------------------------------------------------------
/**
* Constructor.
* @param value The milepost value.
* @param text The textual description of the value. For example, if it 
represents the end
* of a milepost range, the text might be {@code End}.
*/
public MilepostModel(final Milepost value, final String text) {
        mp = value;
        this.text = text;
}
//----------------------------------------------------------------------------------------------
/**
 * Constructor with no value.
 * @param text The textual description of a {@code null} value.
 */
public MilepostModel(final String text) {
        this(null, text);
}
//----------------------------------------------------------------------------------------------
@Override public void detach() {
        mp = null;
}
//----------------------------------------------------------------------------------------------
@Override public Milepost getObject() {
        return mp;
}
//----------------------------------------------------------------------------------------------
@Override public void setObject(final Milepost object) {
        mp = object;
}
//----------------------------------------------------------------------------------------------
@Override public String toString() {
        return mp == null ? text : String.format("%s (%s)", text, 
mp.toString());
}
//----------------------------------------------------------------------------------------------
}





From:   Sven Meier <sv...@meiers.net>
To:     users@wicket.apache.org
Date:   04/25/2012 09:55 AM
Subject:        Re: Radio Choice Weirdness



 >When Wicket calls my onSelectionChanged(), the argument is the display 
string, not the id value.

Your RadioChoice is working on string choices so it will hand you the 
selected string.

I'd suggest to let your RadioChoice work with ints or preferably 
directly on the Milepost objects:

     final RadioChoice<Milepost> choice = new RadioChoice<Milepost>(...);

Sven

On 04/25/2012 03:00 PM, Richard W. Adams wrote:
> My understanding of this class must be faulty. When Wicket calls my
> onSelectionChanged(), the argument is the display string, not the id
> value. For example, I'm expecting a milepost value like "123.456", but
> instead I get "End (123.456)" (the display value). Here's the code. can
> anyone see what I'm doing wrong?
>
> private RadioChoice<String>  createMilepostChoice(final String id, final
> TrackModel track) {
>
>          final Milepost     startMP = track.getStartMP();
>          final Milepost     endMP   = track.getEndMP  ();
>          final List<String>  choices = Arrays.asList(new
> String[]{format(START, startMP), format(END, endMP), OTHER});
>          final ChoiceRenderer<String>  renderer = new
> ChoiceRenderer<String>() {
>                  private static final long serialVersionUID = 1L;
>          @Override public Object getDisplayValue(final String model) {
>              return model;
>          }
>          @Override public String getIdValue(final String model, final 
int
> index) {
>                  String value;
>                  switch (index) {
>                          case 0:
>                                  value = startMP.toString();
>                                  break;
>                          case 1 :
>                                  value = endMP.toString();
>                                  break;
>                          default :
>                                  value = "";
>                  }
>                  return value;
>          }
>          };
>          final RadioChoice<String>  choice = new RadioChoice<String>(id,
> choices, renderer) {
>                  private static final long serialVersionUID = 1L;
>                  @Override protected void onSelectionChanged(final 
Object
> new selection) {
>                          final String selection = (String)newSelection;
>                          if (track == primaryTrack) {
>                                  primaryMpChoice = selection;
>                          } else {
>                                  secondaryMpChoice = selection;
>                          }
>                  }
>                  @Override protected boolean
> wantOnSelectionChangedNotifications() {
>                          return true;
>                  }
>          };
>          return choice;
> }
>
>
> **
>
> This email and any attachments may contain information that is 
confidential and/or privileged for the sole use of the intended recipient. 
 Any use, review, disclosure, copying, distribution or reliance by others, 
and any forwarding of this email or its contents, without the express 
permission of the sender is strictly prohibited by law.  If you are not 
the intended recipient, please contact the sender immediately, delete the 
e-mail and destroy all copies.
> **
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org




**

This email and any attachments may contain information that is confidential and/or privileged for the sole use of the intended recipient.  Any use, review, disclosure, copying, distribution or reliance by others, and any forwarding of this email or its contents, without the express permission of the sender is strictly prohibited by law.  If you are not the intended recipient, please contact the sender immediately, delete the e-mail and destroy all copies.
**

Re: Radio Choice Weirdness

Posted by Sven Meier <sv...@meiers.net>.
 >When Wicket calls my onSelectionChanged(), the argument is the display 
string, not the id value.

Your RadioChoice is working on string choices so it will hand you the 
selected string.

I'd suggest to let your RadioChoice work with ints or preferably 
directly on the Milepost objects:

     final RadioChoice<Milepost> choice = new RadioChoice<Milepost>(...);

Sven

On 04/25/2012 03:00 PM, Richard W. Adams wrote:
> My understanding of this class must be faulty. When Wicket calls my
> onSelectionChanged(), the argument is the display string, not the id
> value. For example, I'm expecting a milepost value like "123.456", but
> instead I get "End (123.456)" (the display value). Here's the code. can
> anyone see what I'm doing wrong?
>
> private RadioChoice<String>  createMilepostChoice(final String id, final
> TrackModel track) {
>
>          final Milepost     startMP = track.getStartMP();
>          final Milepost     endMP   = track.getEndMP  ();
>          final List<String>  choices = Arrays.asList(new
> String[]{format(START, startMP), format(END, endMP), OTHER});
>          final ChoiceRenderer<String>  renderer = new
> ChoiceRenderer<String>() {
>                  private static final long serialVersionUID = 1L;
>          @Override public Object getDisplayValue(final String model) {
>              return model;
>          }
>          @Override public String getIdValue(final String model, final int
> index) {
>                  String value;
>                  switch (index) {
>                          case 0:
>                                  value = startMP.toString();
>                                  break;
>                          case 1 :
>                                  value = endMP.toString();
>                                  break;
>                          default :
>                                  value = "";
>                  }
>                  return value;
>          }
>          };
>          final RadioChoice<String>  choice = new RadioChoice<String>(id,
> choices, renderer) {
>                  private static final long serialVersionUID = 1L;
>                  @Override protected void onSelectionChanged(final Object
> new selection) {
>                          final String selection = (String)newSelection;
>                          if (track == primaryTrack) {
>                                  primaryMpChoice = selection;
>                          } else {
>                                  secondaryMpChoice = selection;
>                          }
>                  }
>                  @Override protected boolean
> wantOnSelectionChangedNotifications() {
>                          return true;
>                  }
>          };
>          return choice;
> }
>
>
> **
>
> This email and any attachments may contain information that is confidential and/or privileged for the sole use of the intended recipient.  Any use, review, disclosure, copying, distribution or reliance by others, and any forwarding of this email or its contents, without the express permission of the sender is strictly prohibited by law.  If you are not the intended recipient, please contact the sender immediately, delete the e-mail and destroy all copies.
> **
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org