Search in sources :

Example 1 with IObjectClassAwareModel

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();
    }
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IObjectClassAwareModel(org.apache.wicket.model.IObjectClassAwareModel) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ConversionException(org.apache.wicket.util.convert.ConversionException)

Example 2 with IObjectClassAwareModel

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));
}
Also used : IWritableRequestParameters(org.apache.wicket.request.IWritableRequestParameters) Model(org.apache.wicket.model.Model) IObjectClassAwareModel(org.apache.wicket.model.IObjectClassAwareModel) IModel(org.apache.wicket.model.IModel) IObjectClassAwareModel(org.apache.wicket.model.IObjectClassAwareModel) StringValue(org.apache.wicket.util.string.StringValue) Test(org.junit.Test)

Aggregations

IObjectClassAwareModel (org.apache.wicket.model.IObjectClassAwareModel)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 IModel (org.apache.wicket.model.IModel)1 Model (org.apache.wicket.model.Model)1 IWritableRequestParameters (org.apache.wicket.request.IWritableRequestParameters)1 ConversionException (org.apache.wicket.util.convert.ConversionException)1 StringValue (org.apache.wicket.util.string.StringValue)1 Test (org.junit.Test)1