Discussion:
Add/Remove Dynamic FormComponent In RepeatingView
Duy Do
2010-08-06 05:05:18 UTC
Permalink
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
Martin Makundi
2010-08-06 05:14:02 UTC
Permalink
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
Post by Duy Do
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-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
Duy Do
2010-08-06 10:13:11 UTC
Permalink
Thanks a lot for your helps, Martin. I will give it a try.

On Fri, Aug 6, 2010 at 12:14 PM, Martin Makundi <
Post by Martin Makundi
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>>();
/**
*/
@SuppressWarnings("unchecked")
public <S, T extends FormComponent<S>> T rememberOrReuse(Object
rowId, String componentId, T newComponent) {
return (T) rememberOrReuse(rowId, componentId, (Component)
newComponent);
}
/**
*/
public <T extends Component> T rememberOrReuse(Object rowId, T newComponent) {
return rememberOrReuse(rowId, newComponent.getId(), newComponent);
}
/**
*/
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
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;
}
/**
*/
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();
}
/**
*/
public <S, T extends FormComponent<S>> T
rememberOrReuseAndProvideFeedback(
Object key, T formComponent, IBehavior... behaviors) {
return rememberOrReuseAndProvideFeedback(key,
formComponent.getId(), formComponent, behaviors);
}
/**
*/
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
Post by Duy Do
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
---------------------------------------------------------------------
Loading...