Sunday, 29 April 2012

Wicket 1.5 and general models

NextReports Server is in process for Wicket 1.5 migration. A lot of things need to be addressed, but in particularly one of them has to be noticed.

Running reports in NextReports Server consists of a number of steps, one of them being selecting parameters. Depending of parameter's type, user can choose the values using a specific UI component like text field, check box, combo box, calendar, selection lists and so on.

Model used for a parameter is something generic like Serializable, because it's type can be anything. One problem appeared at migration from version 1.4 to version 1.5. A simple String TextField with a Serializable model (objectModel in our case):
TextField<String> textField = new TextField<String>("txtValue",
                new PropertyModel<String>(this, "objectModel"));
When we put a value in our text field, on submit we get : "is not a valid Serializable" error. That shows us Wicket 1.5 does not know that String declaration in our case is a Serializable type. To make models accept a general type , we should now create also the converter used by the component to specify the class type:
TextField<String> textField = new TextField<String>("txtValue",
                new PropertyModel<String>(this, "objectModel")) {
           
      @Override
      public <C> IConverter<C> getConverter(Class<C> type) {
          return new AbstractConverter() {

           public Object convertToObject(String value, Locale locale) {
                  return value;
              }

              @Override
              protected Class getTargetType() {
                  return String.class;
              }
                   
          };
      }

No comments:

Post a Comment