开发者

Need a strategy to lookup property for dynamically generated dropdown component

I need to set the null value string for a dropDownChoice component. The default value for null choice is "Choose One", which is hardcoded in the Wicket AbstractSingleChoice class.

The usual way of overriding this is to define a property resource.

However my DropdownChoice component is dynamically generated at runtime, and added to a panel (which is within a datatable). The DropDownChoice is not specifically referenced in the markup.

I'm looking for suggestions on how to programmatically override the default null value string in this situation. It would be nice if the DropDownChoice constructor had an ad开发者_开发问答ditional parameter for this.

Do I need to programmatically create a new property resource?

Hope this makes sense.


You can override AbstractSingleSelectChoice.getNullKey() and AbstractSingleSelectChoice.getNullValidKey() in order to make Wicket retrieve a localized resource from a .properties or .xml file that does not have the component id as part of the key. This is documented in this JIRA issue: Open DropDownChoice null value internationalization key

For instance:

DropDownChoice ddc = new DropDownChoice(id, model, choices){
   @Override
   protected String getNullKey(){ return "customDdcNullValue"; }
   @Override
   protected String getNullValidKey(){ return "customDdcNullValue"; }
}

Will retrieve the <option>'s text from the customDdcNullValue property.

These methods seem to have been added in version 1.4.4, if you're on 1.3, you could always use an IChoiceRenderer with the DropDownChoice that returns the proper String in the case of null value. Note that you could retrieve it from resources (with getString(), or StringResourceModel), or grab the value from database/cache if necessary. This may depend on how are you already localizing the choices for non-null values.

Also, you could use the source code of AbstractSingleSelectChoice.getDefaultChoice() to roll your own DropDownChoice with this behavior.

UPDATE: Another way of handling this is to provide your own custom value that represents null, or no-choice. Just remember to use setNullValid(false) on the DropDownChoice to prevent null from appearing when there is a selected value, and initialize the ddc's model with your no-selection value when you still don't know its value in order to avoid Wicket provide null in the getDefaultChoice() call.

When defining the value for your no-selection option, pay special attention to this part of AbstractSingleSelectChoice.getDefaultChoice():

// Null is not valid. Is it selected anyway?
if ((selected == null) || getNoSelectionValue().equals(selected) ||
      selected.equals(EMPTY_STRING))
{

Take into account that getNoSelectionValue() returns -1, so please do not use -1, or an empty string, as your custom null value. I learnt this the hard way, believe me, it's not pleasing.


You should use localisation feature to achieve it. There is a method org.apache.wicket.resource.loader.IStringResourceLoader.loadStringResource(org.apache.wicket.Component,java.lang.String) which returns a string value to display for a given component. The string will be a resource key, for your case it is the "nullValid" string, IIRC. So, you can implement your custom resource manager which will pick data from a database instead of .properties files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜