Author: svenmeier
Date: Tue Jul  5 16:27:02 2011
New Revision: 1143128

URL: http://svn.apache.org/viewvc?rev=1143128&view=rev
Log:
WICKET-3784 Moved update logic for all Collections based FormComponents into 
single helper method FormComponent#updateCollectionModel(FormComponent).
I'm not sure a static method is the best place for this, but at least we have 
this logic unified in one place only, supporting all former use cases in 
ListMultipleChoice, CheckGroup and MultiFileUploadField.

Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroup.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/ListMultipleChoice.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroup.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroup.java?rev=1143128&r1=1143127&r2=1143128&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroup.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroup.java
 Tue Jul  5 16:27:02 2011
@@ -23,7 +23,6 @@ import org.apache.wicket.WicketRuntimeEx
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.model.IModel;
-import org.apache.wicket.model.Model;
 import org.apache.wicket.model.util.CollectionModel;
 import org.apache.wicket.util.convert.ConversionException;
 import org.apache.wicket.util.lang.Generics;
@@ -152,43 +151,13 @@ public class CheckGroup<T> extends FormC
        }
 
        /**
-        * If the model object exists, it is assumed to be a Collection, and it 
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.
-        * 
-        * @see FormComponent#updateModel()
-        * @throws UnsupportedOperationException
-        *             if the model object Collection cannot be modified
+        * See {@link FormComponent#updateCollectionModel(FormComponent)} for 
details on how the model
+        * is updated.
         */
        @Override
        public void updateModel()
        {
-               Collection<T> collection = getModelObject();
-               if (collection == null)
-               {
-                       collection = getConvertedInput();
-                       setDefaultModelObject(collection);
-               }
-               else
-               {
-                       modelChanging();
-                       collection.clear();
-                       collection.addAll(getConvertedInput());
-                       modelChanged();
-
-                       // call model.setObject()
-                       try
-                       {
-                               getModel().setObject(collection);
-                       }
-                       catch (Exception e)
-                       {
-                               // ignore this exception because it could be 
that there
-                               // is not setter for this collection.
-                               log.info("no setter for the property attached 
to " + this);
-                       }
-               }
+               FormComponent.updateCollectionModel(this);
        }
 
        @Override

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java?rev=1143128&r1=1143127&r2=1143128&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
 Tue Jul  5 16:27:02 2011
@@ -19,6 +19,7 @@ package org.apache.wicket.markup.html.fo
 import java.text.Format;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -37,6 +38,7 @@ import org.apache.wicket.behavior.Behavi
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.IPropertyReflectionAwareModel;
+import org.apache.wicket.model.Model;
 import org.apache.wicket.util.convert.ConversionException;
 import org.apache.wicket.util.convert.IConverter;
 import org.apache.wicket.util.lang.Args;
@@ -466,7 +468,7 @@ public abstract class FormComponent<T> e
         * @see IValidatorAddListener
         * 
         */
-       @SuppressWarnings({ "rawtypes", "unchecked" })
+       @SuppressWarnings( { "rawtypes", "unchecked" })
        public final FormComponent<T> add(final IValidator<? super T> validator)
        {
                if (validator == null)
@@ -1494,4 +1496,54 @@ public abstract class FormComponent<T> e
        {
                setDefaultModelObject(object);
        }
+
+       /**
+        * Update the model of a {@link FormComponent} containing a {@link 
Collection}.
+        * 
+        * If the model object does not yet exists, a new {@link ArrayList} 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 UnsupportedOperationException
+        *             if the existing model object Collection cannot be 
modified
+        */
+       protected static <S> void 
updateCollectionModel(FormComponent<Collection<S>> formComponent)
+       {
+               Collection<S> convertedInput = 
formComponent.getConvertedInput();
+
+               Collection<S> collection = formComponent.getModelObject();
+               if (collection == null)
+               {
+                       collection = new ArrayList<S>(convertedInput);
+                       formComponent.setDefaultModelObject(collection);
+               }
+               else
+               {
+                       formComponent.modelChanging();
+                       collection.clear();
+                       if (convertedInput != null)
+                       {
+                               collection.addAll(convertedInput);
+                       }
+                       formComponent.modelChanged();
+
+                       try
+                       {
+                               formComponent.getModel().setObject(collection);
+                       }
+                       catch (Exception e)
+                       {
+                               // ignore this exception because it could be 
that there
+                               // is not setter for this collection.
+                               logger.info("no setter for the property 
attached to " + formComponent);
+                       }
+               }
+       }
 }

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/ListMultipleChoice.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/ListMultipleChoice.java?rev=1143128&r1=1143127&r2=1143128&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/ListMultipleChoice.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/ListMultipleChoice.java
 Tue Jul  5 16:27:02 2011
@@ -22,15 +22,11 @@ import java.util.List;
 import java.util.StringTokenizer;
 
 import org.apache.wicket.MetaDataKey;
-import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.model.IModel;
-import org.apache.wicket.model.Model;
 import org.apache.wicket.util.convert.ConversionException;
 import org.apache.wicket.util.string.AppendingStringBuffer;
 import org.apache.wicket.util.string.Strings;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 
 /**
@@ -53,9 +49,6 @@ public class ListMultipleChoice<T> exten
                private static final long serialVersionUID = 1L;
        };
 
-       /** Log. */
-       private static final Logger log = 
LoggerFactory.getLogger(ListMultipleChoice.class);
-
        /** The default maximum number of rows to display. */
        private static int defaultMaxRows = 8;
 
@@ -367,52 +360,13 @@ public class ListMultipleChoice<T> exten
        }
 
        /**
-        * If the model object exists, it is assumed to be a Collection, and it 
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.
-        * 
-        * @see FormComponent#updateModel()
-        * @throws UnsupportedOperationException
-        *             if the model object Collection cannot be modified
+        * See {@link FormComponent#updateCollectionModel(FormComponent)} for 
details on how the model
+        * is updated.
         */
        @Override
        public void updateModel()
        {
-               Collection<T> selectedValues = getModelObject();
-               if (selectedValues != null)
-               {
-                       if (getDefaultModelObject() != selectedValues)
-                       {
-                               throw new WicketRuntimeException(
-                                       "Updating a ListMultipleChoice works by 
modifying the underlying model object in-place, so please make sure that 
getObject() always returns the same Collection instance!");
-                       }
-
-                       modelChanging();
-                       selectedValues.clear();
-                       final Collection<T> converted = getConvertedInput();
-                       if (converted != null)
-                       {
-                               selectedValues.addAll(converted);
-                       }
-                       modelChanged();
-                       // call model.setObject()
-                       try
-                       {
-                               getModel().setObject(selectedValues);
-                       }
-                       catch (Exception e)
-                       {
-                               // ignore this exception because it could be 
that there
-                               // is not setter for this collection.
-                               log.info("no setter for the property attached 
to " + this);
-                       }
-               }
-               else
-               {
-                       selectedValues = new ArrayList<T>(getConvertedInput());
-                       setDefaultModelObject(selectedValues);
-               }
+               FormComponent.updateCollectionModel(this);
        }
 
        /**

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.java?rev=1143128&r1=1143127&r2=1143128&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.java
 Tue Jul  5 16:27:02 2011
@@ -29,6 +29,7 @@ import org.apache.wicket.markup.html.Web
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.FormComponent;
 import org.apache.wicket.markup.html.form.FormComponentPanel;
 import org.apache.wicket.model.AbstractReadOnlyModel;
 import org.apache.wicket.model.IModel;
@@ -291,33 +292,13 @@ public class MultiFileUploadField extend
        }
 
        /**
-        * @see org.apache.wicket.markup.html.form.FormComponent#updateModel()
+        * See {@link FormComponent#updateCollectionModel(FormComponent)} for 
details on how the model
+        * is updated.
         */
        @Override
        public void updateModel()
        {
-               final Collection<FileUpload> collection = getModelObject();
-
-               // figure out if there is an existing model object collection 
for us to
-               // reuse
-               if (collection == null)
-               {
-                       // no existing collection, push the one we created
-                       setDefaultModelObject(getConvertedInput());
-               }
-               else
-               {
-                       // refresh the existing collection
-                       collection.clear();
-                       if (getConvertedInput() != null)
-                       {
-                               collection.addAll(getConvertedInput());
-                       }
-
-                       // push the collection in case the model is listening to
-                       // setobject calls
-                       setDefaultModelObject(collection);
-               }
+               FormComponent.updateCollectionModel(this);
        }
 
        /**


Reply via email to