package org.apache.wicket.examples.ajax.builtin;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxCallListener;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.attributes.IAjaxCallListener;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.settings.ExceptionSettings;
@author
public class LinksPage extends BasePage
{
private int counter1 = 0;
private int counter2 = 0;
private int counter3 = 0;
@return
public int getCounter1()
{
return counter1;
}
@return
public int getCounter2()
{
return counter2;
}
@return
public int getCounter3()
{
return counter3;
}
public LinksPage()
{
final Label c1 = new Label("c1", new PropertyModel<>(this, "counter1"));
c1.setOutputMarkupId(true);
add(c1);
final Label c2 = new Label("c2", new PropertyModel<>(this, "counter2"));
c2.setOutputMarkupId(true);
add(c2);
final Label c3 = new Label("c3", new PropertyModel<>(this, "counter3"));
c3.setOutputMarkupId(true);
add(c3);
add(new AjaxLink<Void>("c1-link")
{
@Override
public void onClick(AjaxRequestTarget target)
{
counter1++;
target.add(c1);
}
});
add(new AjaxFallbackLink<Void>("c2-link")
{
@Override
public void onClick(Optional<AjaxRequestTarget> targetOptional)
{
counter2++;
targetOptional.ifPresent(target -> target.add(c2));
}
});
add(IndicatingAjaxLink.onClick("c3-link", (link, target) -> {
counter3++;
target.add(c3);
try
{
TimeUnit.SECONDS.sleep(5);
}
catch (InterruptedException e)
{
}
}));
add(new AjaxLink<Void>("success-link")
{
@Override
public void onClick(AjaxRequestTarget target)
{
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
super.updateAjaxAttributes(attributes);
IAjaxCallListener ajaxCallListener = new AjaxCallListener() {
@Override
public CharSequence getSuccessHandler(Component component)
{
return "alert('Success');";
}
@Override
public CharSequence getBeforeSendHandler(Component component)
{
return "alert('Before ajax call');";
}
@Override
public CharSequence getFailureHandler(Component component)
{
return "alert('Failure');";
}
};
attributes.getAjaxCallListeners().add(ajaxCallListener);
List<CharSequence> urlArgumentMethods = attributes.getDynamicExtraParameters();
urlArgumentMethods.add("return {'htmlname': document.documentElement.tagName};");
urlArgumentMethods.add("return {'bodyname': document.body.tagName};");
}
});
add(new AjaxLink<Void>("failure-link")
{
@Override
public void onClick(AjaxRequestTarget target)
{
getApplication().getExceptionSettings().setAjaxErrorHandlingStrategy(
ExceptionSettings.AjaxErrorStrategy.INVOKE_FAILURE_HANDLER);
throw new WicketRuntimeException("Failure link clicked");
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
super.updateAjaxAttributes(attributes);
IAjaxCallListener ajaxCallListener = new AjaxCallListener() {
@Override
public CharSequence getBeforeHandler(Component component)
{
return "alert('Before ajax call');";
}
@Override
public CharSequence getSuccessHandler(Component component)
{
return "alert('Success');";
}
@Override
public CharSequence getFailureHandler(Component component)
{
return "alert('Failure');";
}
};
attributes.getAjaxCallListeners().add(ajaxCallListener);
}
});
add(new AjaxLink<Void>("set-response-page") {
@Override
public void onClick(AjaxRequestTarget target) {
setResponsePage(LinksPage.class);
}
});
add(new AjaxLink<Void>("exception") {
@Override
public void onClick(AjaxRequestTarget target) {
getApplication().getExceptionSettings().setAjaxErrorHandlingStrategy(
ExceptionSettings.AjaxErrorStrategy.REDIRECT_TO_ERROR_PAGE);
throw new RuntimeException("test whether the exception handling works");
}
});
}
}