use of org.apache.wicket.model.IObjectClassAwareModel in project wicket by apache.
the class FormComponent method updateCollectionModel.
/**
* Update the model of a {@link FormComponent} containing a {@link Collection}.
*
* If the model object does not yet exists, a new suitable collection is filled with the converted
* input and used as the new model object. Otherwise the existing collection is modified
* in-place, then {@link Model#setObject(Object)} is called with the same instance: it allows
* the Model to be notified of changes even when {@link Model#getObject()} returns a different
* {@link Collection} at every invocation.
*
* @param <S>
* collection type
* @param formComponent
* the form component to update
* @see FormComponent#updateModel()
* @throws WicketRuntimeException
* if the existing model object collection is unmodifiable and no setter exists
*/
public static <S> void updateCollectionModel(FormComponent<Collection<S>> formComponent) {
Collection<S> convertedInput = formComponent.getConvertedInput();
if (convertedInput == null) {
convertedInput = Collections.emptyList();
}
Collection<S> collection = formComponent.getModelObject();
if (collection == null) {
Class<?> hint = null;
if (formComponent.getModel() instanceof IObjectClassAwareModel) {
hint = ((IObjectClassAwareModel) formComponent.getModel()).getObjectClass();
}
if (hint == null) {
hint = List.class;
}
collection = newCollection(hint, convertedInput);
formComponent.setModelObject(collection);
} else {
boolean modified = false;
formComponent.modelChanging();
try {
collection.clear();
collection.addAll(convertedInput);
modified = true;
} catch (UnsupportedOperationException unmodifiable) {
if (logger.isDebugEnabled()) {
logger.debug("An error occurred while trying to modify the collection attached to " + formComponent, unmodifiable);
}
collection = newCollection(collection.getClass(), convertedInput);
}
try {
formComponent.getModel().setObject(collection);
} catch (Exception noSetter) {
if (!modified) {
throw new WicketRuntimeException("An error occurred while trying to set the collection attached to " + formComponent, noSetter);
} else if (logger.isDebugEnabled()) {
logger.debug("An error occurred while trying to set the collection attached to " + formComponent, noSetter);
}
}
formComponent.modelChanged();
}
}
use of org.apache.wicket.model.IObjectClassAwareModel in project wicket by apache.
the class AjaxEditableTest method testModelObjectClassInference.
/**
* <a href="https://issues.apache.org/jira/browse/WICKET-4259">WICKET-4259</a>
*/
@Test
public void testModelObjectClassInference() {
class IntegerModel extends Model<Integer> implements IObjectClassAwareModel<Integer> {
@Override
public Class<Integer> getObjectClass() {
return Integer.class;
}
}
IModel<Integer> integerModel = new IntegerModel();
AjaxEditableLabel<Integer> editableLabel = new AjaxEditableLabel<Integer>("test", integerModel);
editableLabel.getEditor().setVisible(true);
IWritableRequestParameters postParameters = (IWritableRequestParameters) tester.getRequestCycle().getRequest().getPostParameters();
postParameters.setParameterValues(editableLabel.getEditor().getInputName(), Arrays.asList(new StringValue[] { StringValue.valueOf("5") }));
editableLabel.getEditor().processInput();
assertThat(integerModel.getObject(), instanceOf(Integer.class));
}
Aggregations