Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemsakurai/e5d33575b9c3a20688d60f64c2a9995c to your computer and use it in GitHub Desktop.
Save kemsakurai/e5d33575b9c3a20688d60f64c2a9995c to your computer and use it in GitHub Desktop.
FeedbackPanel を一緒に使用する際は、FeedbackPanel#setMarkupId() でwicket id と同じ名称でid を設定したところ、上手く動作しました
import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.attributes.ThrottlingSettings;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.feedback.IFeedback;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.util.io.IClusterable;
import org.apache.wicket.util.time.Duration;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.wicketstuff.stateless.behaviors.StatelessAjaxFormSubmitBehavior;
/**
* StatelessAjaxFormValidatingBehavior
*/
public class StatelessAjaxFormValidatingBehavior extends Behavior {
private static final long serialVersionUID = 1L;
private final String event;
private final Duration throttleDelay;
private Form<?> form;
private boolean hasBeenRendered;
public StatelessAjaxFormValidatingBehavior(String event) {
this(event, (Duration) null);
}
public StatelessAjaxFormValidatingBehavior(String event, Duration throttleDelay) {
this.hasBeenRendered = false;
this.event = event;
this.throttleDelay = throttleDelay;
}
public void bind(Component component) {
super.bind(component);
if (component instanceof Form) {
this.form = (Form) component;
} else {
this.form = Form.findForm(component);
if (this.form == null) {
throw new WicketRuntimeException(AjaxFormValidatingBehavior.class.getSimpleName() + " should be bound to a Form component or a component that is inside a form!");
}
}
}
public void onConfigure(Component component) {
super.onConfigure(component);
if (!this.hasBeenRendered) {
this.hasBeenRendered = true;
this.form.visitChildren(FormComponent.class, new StatelessAjaxFormValidatingBehavior.FormValidateVisitor());
}
}
protected void onSubmit(AjaxRequestTarget target) {
this.addFeedbackPanels(target);
}
protected void onAfterSubmit(AjaxRequestTarget target) {
}
protected void onError(AjaxRequestTarget target) {
this.addFeedbackPanels(target);
}
protected final void addFeedbackPanels(final AjaxRequestTarget target) {
this.form.getPage().visitChildren(IFeedback.class, new IVisitor() {
@Override
public void component(Object o, IVisit iVisit) {
Component component = (Component) o;
component.configure();
if (component.isVisibleInHierarchy()) {
target.add(new Component[]{component});
} else {
iVisit.dontGoDeeper();
}
}
});
}
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
}
private class FormValidateVisitor implements IVisitor<FormComponent, Void>, IClusterable {
private static final long serialVersionUID = 1L;
private FormValidateVisitor() {
}
public void component(final FormComponent component, IVisit<Void> visit) {
StatelessAjaxFormSubmitBehavior behavior = new StatelessAjaxFormSubmitBehavior(StatelessAjaxFormValidatingBehavior.this.event) {
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
if (StatelessAjaxFormValidatingBehavior.this.throttleDelay != null) {
String id = "throttle-" + component.getMarkupId();
ThrottlingSettings throttlingSettings = new ThrottlingSettings(id, StatelessAjaxFormValidatingBehavior.this.throttleDelay);
attributes.setThrottlingSettings(throttlingSettings);
}
StatelessAjaxFormValidatingBehavior.this.updateAjaxAttributes(attributes);
}
protected void onSubmit(AjaxRequestTarget target) {
super.onSubmit(target);
StatelessAjaxFormValidatingBehavior.this.onSubmit(target);
}
protected void onAfterSubmit(AjaxRequestTarget target) {
super.onAfterSubmit(target);
StatelessAjaxFormValidatingBehavior.this.onAfterSubmit(target);
}
protected void onError(AjaxRequestTarget target) {
super.onError(target);
StatelessAjaxFormValidatingBehavior.this.onError(target);
}
};
component.add(new Behavior[]{behavior});
visit.dontGoDeeper();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment