i want to minimize the amount of code here this is the full class
/**
* @author yoav stern
*
* @since Jun 24, 2013
*/
package com.betamedia.tp.backoffice.components.utils.absractfilter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.Strings;
import com.betamedia.common.logging.Log;
import com.betamedia.common.logging.LogFactory;
import com.betamedia.common.search.criteria.SearchCriteria;
import com.betamedia.common.utils.BeanUtils;
import com.betamedia.tp.backoffice.components.grid.BMDefaultDataGrid;
import com.betamedia.tp.backoffice.utils.GridViewType;
public abstract class AbstractFilterPanel extends Panel {
private static final long serialVersionUID = 1L;
private static final String SUBMIT_FILLTER_FORM = "submit_fillter_form";
private static final String FORM = "form";
protected Map<String, String> filterCritriaMap;
@SuppressWarnings("unused")
protected static final Log log = LogFactory.getLog();
protected BMDefaultDataGrid tableTorender;
@SuppressWarnings("rawtypes")
private SearchCriteria searchCriteria;
@SuppressWarnings("rawtypes")
private SearchCriteria preDefinedSearchCriteria;
protected final FeedbackPanel feedbackPanel;
protected Form<Void> form;
protected AjaxButton applyBtn;
public AbstractFilterPanel(String id, IModel<?> model, BMDefaultDataGrid
toRender, SearchCriteria searchCriteriaIn, FeedbackPanel feed) {
super(id, model);
this.tableTorender = toRender;
this.setSearchCriteria(searchCriteriaIn);
this.feedbackPanel = feed;
}
public AbstractFilterPanel(String id, BMDefaultDataGrid toRender,
SearchCriteria searchCriteriaIn, FeedbackPanel feed) {
super(id);
this.tableTorender = toRender;
this.setSearchCriteria(searchCriteriaIn);
this.feedbackPanel = feed;
}
public AbstractFilterPanel(String id, BMDefaultDataGrid toRender,
SearchCriteria searchCriteriaIn, FeedbackPanel feed,
Map<String, String> fillterByList) {
this(id, toRender, searchCriteriaIn, feed);
this.filterCritriaMap = fillterByList;
setFilterToModel();
}
/**
* @param id
* @param model
* @param toRender
* @param searchCriteriaIn
* @param feed
* @param fillterByList
*/
public AbstractFilterPanel(String id, IModel<?> model, BMDefaultDataGrid
toRender, SearchCriteria searchCriteriaIn, FeedbackPanel feed,
Map<String, String> fillterByList) {
this(id, model, toRender, searchCriteriaIn, feed);
this.filterCritriaMap = fillterByList;
setFilterToModel();
}
/**
* @param fillterByList
*/
protected void setFilterToModel() {
Object defaultModelObject = getDefaultModelObject();
if (filterCritriaMap != null && !filterCritriaMap.isEmpty()) {
Iterator<Entry<String, String>> it = filterCritriaMap.entrySet().iterator(
);
while (it.hasNext()) {
Map.Entry<String, String> pairs = it.next();
insertLastPagePropertyToFilterModel(defaultModelObject, pairs);
}
}
onFillterSubmited();
if (getPreDefinedSearchCriteria() != null) {
getSearchCriteria().and(getPreDefinedSearchCriteria());
}
}
/**
* this function decide how to insert property value to model , in most
* cases would not be @override , unless the property which is string is an
* instance in the model and therefore should be handled otherwise
*
* @param defaultModelObject
* @param pairs
*/
protected void insertLastPagePropertyToFilterModel(Object defaultModelObject,
Map.Entry<String, String> pairs) {
BeanUtils.setProperty(defaultModelObject, pairs.getKey(), pairs.getValue(
));
}
protected void init() {
Injector.get().inject(this);
setOutputMarkupId(true);
form = new ExpendedForm(FORM);
form.setOutputMarkupId(true);
form.setMarkupId("fillter-form");
add(form);
form.add(new IFormValidator() {
private static final long serialVersionUID = 1L;
@Override
public void validate(Form<?> form) {
String err = formValidation();
if (!Strings.isEmpty(err)) {
form.error(err);
}
}
@Override
public FormComponent<?>[] getDependentFormComponents() {
return null;
}
});
form.add(doSubmit(tableTorender));
}
/**
* @return
*/
protected String getInputToClearIds() {
return "";
}
/**
* @param toRender
* @return doSubmit button
*/
private AjaxButton doSubmit(final BMDefaultDataGrid toRender) {
applyBtn = new AjaxButton(SUBMIT_FILLTER_FORM, form) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
getSearchCriteria().clear();
onFillterSubmited();
if (getPreDefinedSearchCriteria() != null) {
getSearchCriteria().and(getPreDefinedSearchCriteria());
}
toRender.resetSelectedItems();
target.add(toRender);
toRender.resetCurrentPage();
target.add(feedbackPanel);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
target.add(feedbackPanel);
}
@Override
public boolean isEnabled() {
return super.isEnabled() && isApplyBtnEnabled();
}
};
applyBtn.setOutputMarkupId(true);
return applyBtn;
}
/**
* this method will be called when filter is submitted
*/
public abstract void onFillterSubmited();
/**
* this method will be called when on clear is submitted
*/
public abstract void onClearSubmited();
protected boolean isClearVisible() {
return true;
}
protected String formValidation() {
return null;
}
private class ExpendedForm extends Form {
private static final long serialVersionUID = 1L;
public ExpendedForm(String id) {
super(id);
}
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
}
}
protected boolean isApplyBtnEnabled() {
return true;
}
public SearchCriteria getPreDefinedSearchCriteria() {
return preDefinedSearchCriteria;
}
public void setPreDefinedSearchCriteria(SearchCriteria
preDefinedSearchCriteria) {
this.preDefinedSearchCriteria = preDefinedSearchCriteria;
}
/**
* turn when coming from entity inVisibility the id of the entity came from
*
* @param cameFromGridView
*/
public void turnFilterCameFromInvisable(GridViewType cameFromGridView) {
throw new UnsupportedOperationException("this filter does not suppo");
}
public SearchCriteria getSearchCriteria() {
return searchCriteria;
}
public void setSearchCriteria(SearchCriteria searchCriteria) {
this.searchCriteria = searchCriteria;
}
}
On Tue, Jan 21, 2014 at 9:25 AM, Sven Meier <[email protected]> wrote:
> You have to add the AjaxButton to the component tree. I don't see this in
> your code.
>
> If a parent container is updated via AjaxRequestTarget, all its children
> will automatically be updated too.
>
> Regards
> Sven
>
>
> On 01/21/2014 08:19 AM, Yoav Stern wrote:
>
>> My scenario: on an ajax event I replace a webmarkupContainer with one of
>> my
>> components which have some wicket IBehaviorListener.
>>
>> The problem is that this replace happen due to Ajax request, and the
>> behavior gets listed on don ready:
>>
>> Wicket.Event.add(window, "domready", function(event) {
>> Wicket.Ajax.get({'u': 'some/url', 'c': 'linkId', 'e':'click'}));
>> // ... more event registrations and onDomReady scripts }
>>
>> I'm assuming that, being generated in the this event registration code
>> happens for all components that are added explicitly, but not for any
>> components that are added dynamically by me.
>>
>> How can I call the new container with the Ajax behaviors or change the
>> current implementation so the wicket Ajax behaviors would be called?
>>
>>
>> To be more precise I have a table with a filter which rendered by
>> replacing
>> web markup container with this TableWithFilter. This scenario happens on
>> Ajax so when the page loaded by the first time the TableWithFilter does
>> not
>> exist, it will appear on screen due to Ajax request that will occur
>>
>> TableWithFilter contains AbstractFilterPanel which has a
>> org.apache.wicket.
>> ajax.markup.html.form.AjaxButton which render the table by Ajax the
>> button
>> is
>>
>> initilze in the c'tor like this :
>>
>> public abstract class AbstractFilterPanel extends Panel {
>> public AbstractFilterPanel(String id, IModel<?> model, BMDefaultDataGrid
>> toRender, SearchCriteria searchCriteriaIn, FeedbackPanel feed) {
>> super(id, model);
>> this.tableTorender = toRender;
>> this.setSearchCriteria(searchCriteriaIn);
>> this.feedbackPanel = feed;
>>
>> }
>> //called by the AbstractFilterPanel sons
>> protected void init() {
>> Injector.get().inject(this);
>> setOutputMarkupId(true);
>> form = new ExpendedForm(FORM);
>> form.setOutputMarkupId(true);
>> form.setMarkupId("fillter-form");
>> add(form);
>>
>> new AjaxButton(SUBMIT_FILLTER_FORM, form) {
>>
>> private static final long serialVersionUID = 1L;
>>
>>
>> @Override
>>
>> protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
>>
>> getSearchCriteria().clear();
>>
>> onFillterSubmited();
>>
>> if (getPreDefinedSearchCriteria() != null) {
>>
>> getSearchCriteria().and(getPreDefinedSearchCriteria());
>>
>> }
>>
>> toRender.resetSelectedItems();
>>
>> target.add(toRender);
>>
>> toRender.resetCurrentPage();
>>
>> target.add(feedbackPanel);
>>
>> }
>>
>> @Override
>>
>> protected void onError(AjaxRequestTarget target, Form<?> form) {
>>
>> target.add(feedbackPanel);
>>
>> }
>>
>> @Override
>>
>> public boolean isEnabled() {
>>
>> return super.isEnabled() && isApplyBtnEnabled();
>>
>> }
>>
>> }
>>
>> }
>>
>> what should AbstractFilterPanel implement in order to add a javascript
>> that
>> register his appaly btn when it's rendered on page ?
>>
>>
>> Thanks in advance.
>>
>>
>