You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Duy Do <do...@gmail.com> on 2010/08/06 07:05:18 UTC

Add/Remove Dynamic FormComponent In RepeatingView

Hi all,

For the moment I can add/remove a form component in RepeatingView on the
fly, but every time I add or remove a form component, the model values of
previous form component are lost. How can I keep them in RepeatingView?

Thanks,
Duy

Re: Add/Remove Dynamic FormComponent In RepeatingView

Posted by Duy Do <do...@gmail.com>.
Thanks a lot for your helps, Martin. I will give it a try.

On Fri, Aug 6, 2010 at 12:14 PM, Martin Makundi <
martin.makundi@koodaripalvelut.com> wrote:

> Use a reusemanager that manages the rawinput values.
>
> public class FormComponentReuseManager implements Serializable {
>  private final Map<Object, Map<String, Component>> idMapRow = new
> HashMap<Object, Map<String, Component>>();
>
>
>  /**
>   * @param <S>
>   * @param <T>
>   * @param rowId
>   * @param componentId
>   * @param newComponent
>   * @return FormComponent
>   */
>  @SuppressWarnings("unchecked")
>  public <S, T extends FormComponent<S>> T rememberOrReuse(Object
> rowId, String componentId, T newComponent) {
>    return (T) rememberOrReuse(rowId, componentId, (Component)
> newComponent);
>  }
>
>
>  /**
>   * @param <T>
>   * @param rowId
>   * @param newComponent
>   * @return FormComponent
>   */
>  public <T extends Component> T rememberOrReuse(Object rowId, T
> newComponent) {
>    return rememberOrReuse(rowId, newComponent.getId(), newComponent);
>  }
>  /**
>   * @param <T>
>   * @param rowId
>   * @param componentId
>   * @param newComponent
>   * @return FormComponent
>   */
>  public <T extends Component> T rememberOrReuse(Object rowId, String
> componentId, T newComponent) {
>    Map<String, Component> rowMap = createOrReuse(rowId);
>
>    @SuppressWarnings("unchecked")
>    T existingComponent = (T) rowMap.get(componentId);
>
>    if (newComponent instanceof FormComponent) {
>      // Never reuse the component itself, just reuse the
>      rowMap.put(componentId, newComponent);
>
>      if (existingComponent != null) {
>        WicketUtils.fakeRawInput((FormComponent<?>)newComponent,
> (FormComponent<?>)existingComponent);
>        // Transfer also the error messages
>        for (FeedbackMessage feedbackMessage :
> Session.get().getFeedbackMessages().messages(new
> ComponentFeedbackMessageFilter(existingComponent))) {
>          WicketUtils.replaceReporter(feedbackMessage,
> (FormComponent<?>) newComponent);
>        }
>      }
>
>      return newComponent;
>    }
>
>    // else
>    if (existingComponent == null) {
>      rowMap.put(componentId, newComponent);
>      return newComponent;
>    }
>
>    // else
>    return existingComponent;
>  }
>
>  /**
>   * @param rowId
>   * @return Map<String, FormComponent>
>   */
>  private Map<String, Component> createOrReuse(Object rowId) {
>    Map<String, Component> rowMap = idMapRow.get(rowId);
>
>    if ((rowMap == null) && (rowId instanceof AbstractDTO) &&
> (((AbstractDTO) rowId).getId() != null)) {
>      rowId = ((AbstractDTO) rowId).getId();
>      rowMap = idMapRow.get(rowId);
>    }
>
>    if (rowMap == null) {
>      rowMap = new HashMap<String, Component>();
>      idMapRow.put(rowId, rowMap);
>    }
>
>    return rowMap;
>  }
>
>  /**
>   *
>   */
>  public void clear() {
>    idMapRow.clear();
>  }
>
>  /**
>   * @param <S>
>   * @param <T>
>   * @param key
>   * @param formComponent
>   * @param behaviors
>   * @return T
>   */
>  public <S, T extends FormComponent<S>> T
> rememberOrReuseAndProvideFeedback(
>      Object key, T formComponent, IBehavior... behaviors) {
>    return rememberOrReuseAndProvideFeedback(key,
> formComponent.getId(), formComponent, behaviors);
>  }
>  /**
>   * @param <S>
>   * @param <T>
>   * @param key
>   * @param id
>   * @param formComponent
>   * @param behaviors
>   * @return T
>   */
>  public <S, T extends FormComponent<S>> T
> rememberOrReuseAndProvideFeedback(
>      Object key, String id, T formComponent, IBehavior... behaviors) {
>    formComponent.add(behaviors);
>    formComponent.setOutputMarkupId(true);
>    return rememberOrReuse(key, id, FeedbackStyler.add(formComponent));
>  }
> }
>
>
> **
> Martin
>
> 2010/8/6 Duy Do <do...@gmail.com>:
> > Hi all,
> >
> > For the moment I can add/remove a form component in RepeatingView on the
> > fly, but every time I add or remove a form component, the model values of
> > previous form component are lost. How can I keep them in RepeatingView?
> >
> > Thanks,
> > Duy
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Add/Remove Dynamic FormComponent In RepeatingView

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
Use a reusemanager that manages the rawinput values.

public class FormComponentReuseManager implements Serializable {
  private final Map<Object, Map<String, Component>> idMapRow = new
HashMap<Object, Map<String, Component>>();


  /**
   * @param <S>
   * @param <T>
   * @param rowId
   * @param componentId
   * @param newComponent
   * @return FormComponent
   */
  @SuppressWarnings("unchecked")
  public <S, T extends FormComponent<S>> T rememberOrReuse(Object
rowId, String componentId, T newComponent) {
    return (T) rememberOrReuse(rowId, componentId, (Component) newComponent);
  }


  /**
   * @param <T>
   * @param rowId
   * @param newComponent
   * @return FormComponent
   */
  public <T extends Component> T rememberOrReuse(Object rowId, T newComponent) {
    return rememberOrReuse(rowId, newComponent.getId(), newComponent);
  }
  /**
   * @param <T>
   * @param rowId
   * @param componentId
   * @param newComponent
   * @return FormComponent
   */
  public <T extends Component> T rememberOrReuse(Object rowId, String
componentId, T newComponent) {
    Map<String, Component> rowMap = createOrReuse(rowId);

    @SuppressWarnings("unchecked")
    T existingComponent = (T) rowMap.get(componentId);

    if (newComponent instanceof FormComponent) {
      // Never reuse the component itself, just reuse the
      rowMap.put(componentId, newComponent);

      if (existingComponent != null) {
        WicketUtils.fakeRawInput((FormComponent<?>)newComponent,
(FormComponent<?>)existingComponent);
        // Transfer also the error messages
        for (FeedbackMessage feedbackMessage :
Session.get().getFeedbackMessages().messages(new
ComponentFeedbackMessageFilter(existingComponent))) {
          WicketUtils.replaceReporter(feedbackMessage,
(FormComponent<?>) newComponent);
        }
      }

      return newComponent;
    }

    // else
    if (existingComponent == null) {
      rowMap.put(componentId, newComponent);
      return newComponent;
    }

    // else
    return existingComponent;
  }

  /**
   * @param rowId
   * @return Map<String, FormComponent>
   */
  private Map<String, Component> createOrReuse(Object rowId) {
    Map<String, Component> rowMap = idMapRow.get(rowId);

    if ((rowMap == null) && (rowId instanceof AbstractDTO) &&
(((AbstractDTO) rowId).getId() != null)) {
      rowId = ((AbstractDTO) rowId).getId();
      rowMap = idMapRow.get(rowId);
    }

    if (rowMap == null) {
      rowMap = new HashMap<String, Component>();
      idMapRow.put(rowId, rowMap);
    }

    return rowMap;
  }

  /**
   *
   */
  public void clear() {
    idMapRow.clear();
  }

  /**
   * @param <S>
   * @param <T>
   * @param key
   * @param formComponent
   * @param behaviors
   * @return T
   */
  public <S, T extends FormComponent<S>> T rememberOrReuseAndProvideFeedback(
      Object key, T formComponent, IBehavior... behaviors) {
    return rememberOrReuseAndProvideFeedback(key,
formComponent.getId(), formComponent, behaviors);
  }
  /**
   * @param <S>
   * @param <T>
   * @param key
   * @param id
   * @param formComponent
   * @param behaviors
   * @return T
   */
  public <S, T extends FormComponent<S>> T rememberOrReuseAndProvideFeedback(
      Object key, String id, T formComponent, IBehavior... behaviors) {
    formComponent.add(behaviors);
    formComponent.setOutputMarkupId(true);
    return rememberOrReuse(key, id, FeedbackStyler.add(formComponent));
  }
}


**
Martin

2010/8/6 Duy Do <do...@gmail.com>:
> Hi all,
>
> For the moment I can add/remove a form component in RepeatingView on the
> fly, but every time I add or remove a form component, the model values of
> previous form component are lost. How can I keep them in RepeatingView?
>
> Thanks,
> Duy
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org