Search in sources :

Example 6 with AjaxFallbackLink

use of org.apache.wicket.ajax.markup.html.AjaxFallbackLink in project wicket by apache.

the class AjaxLinkClickTest method testFallbackLinkWithAjaxDisabled.

/**
 * Test that when AJAX is disabled, the AjaxFallbackLink is invoked with null as
 * AjaxRequestTarget.
 */
@Test
void testFallbackLinkWithAjaxDisabled() {
    final Page page = new MockPageWithLink();
    // Create a link, which we test is actually invoked
    page.add(new AjaxFallbackLink<Void>("ajaxLink") {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(Optional<AjaxRequestTarget> targetOptional) {
            linkClicked = true;
            targetOptional.ifPresent(target -> ajaxRequestTarget = target);
        }
    });
    tester.startPage(page);
    // Click the link with ajax disabled
    tester.clickLink("ajaxLink", false);
    assertTrue(linkClicked);
    assertNull(ajaxRequestTarget);
}
Also used : AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) Test(org.junit.jupiter.api.Test) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Page(org.apache.wicket.Page) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) Optional(java.util.Optional) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) AjaxFallbackLink(org.apache.wicket.ajax.markup.html.AjaxFallbackLink) WicketTestCase(org.apache.wicket.util.tester.WicketTestCase) Page(org.apache.wicket.Page) Test(org.junit.jupiter.api.Test)

Example 7 with AjaxFallbackLink

use of org.apache.wicket.ajax.markup.html.AjaxFallbackLink in project wicket by apache.

the class EnhancedPageView method addTreeControls.

private void addTreeControls() {
    Form<Void> form = new Form<>("form");
    add(form);
    form.add(new CheckBox("showStateless", new PropertyModel<Boolean>(this, "showStatefulAndParentsOnly")));
    form.add(new CheckBox("showBehaviors", new PropertyModel<Boolean>(this, "showBehaviors")));
    form.add(new CheckBoxMultipleChoice<>("visibleColumns", new PropertyModel<List<IColumn<TreeNode, Void>>>(this, "visibleColumns"), allColumns).setSuffix(" "));
    form.add(new AjaxFallbackButton("submit", form) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onAfterSubmit(Optional<AjaxRequestTarget> target) {
            target.ifPresent(t -> t.add(componentTree));
        }
    });
    add(new AjaxFallbackLink<Void>("expandAll") {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(Optional<AjaxRequestTarget> targetOptional) {
            expandState.expandAll();
            targetOptional.ifPresent(target -> target.add(componentTree));
        }
    });
    add(new AjaxFallbackLink<Void>("collapseAll") {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(Optional<AjaxRequestTarget> targetOptional) {
            expandState.collapseAll();
            targetOptional.ifPresent(target -> target.add(componentTree));
        }
    });
}
Also used : Form(org.apache.wicket.markup.html.form.Form) Arrays(java.util.Arrays) Behavior(org.apache.wicket.behavior.Behavior) WicketObjects(org.apache.wicket.core.util.lang.WicketObjects) Page(org.apache.wicket.Page) IColumn(org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn) PageView(org.apache.wicket.markup.html.debug.PageView) ICellPopulator(org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator) OddEvenItem(org.apache.wicket.markup.repeater.OddEvenItem) CheckBox(org.apache.wicket.markup.html.form.CheckBox) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AjaxFallbackButton(org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton) AbstractColumn(org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) CheckBoxMultipleChoice(org.apache.wicket.markup.html.form.CheckBoxMultipleChoice) Strings(org.apache.wicket.util.string.Strings) CssHeaderItem(org.apache.wicket.markup.head.CssHeaderItem) IModel(org.apache.wicket.model.IModel) Label(org.apache.wicket.markup.html.basic.Label) AbstractTree(org.apache.wicket.extensions.markup.html.repeater.tree.AbstractTree) Iterator(java.util.Iterator) SortableTreeProvider(org.apache.wicket.extensions.markup.html.repeater.util.SortableTreeProvider) Collection(java.util.Collection) IClusterable(org.apache.wicket.util.io.IClusterable) GenericPanel(org.apache.wicket.markup.html.panel.GenericPanel) Model(org.apache.wicket.model.Model) Set(java.util.Set) Component(org.apache.wicket.Component) MarkupContainer(org.apache.wicket.MarkupContainer) LoadableDetachableModel(org.apache.wicket.model.LoadableDetachableModel) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) DefaultTableTree(org.apache.wicket.extensions.markup.html.repeater.tree.DefaultTableTree) List(java.util.List) TreeColumn(org.apache.wicket.extensions.markup.html.repeater.tree.table.TreeColumn) CssResourceReference(org.apache.wicket.request.resource.CssResourceReference) PropertyModel(org.apache.wicket.model.PropertyModel) Bytes(org.apache.wicket.util.lang.Bytes) Item(org.apache.wicket.markup.repeater.Item) PropertyColumn(org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn) Optional(java.util.Optional) AjaxFallbackLink(org.apache.wicket.ajax.markup.html.AjaxFallbackLink) Comparator(java.util.Comparator) Collections(java.util.Collections) Form(org.apache.wicket.markup.html.form.Form) PropertyModel(org.apache.wicket.model.PropertyModel) AjaxFallbackButton(org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) CheckBox(org.apache.wicket.markup.html.form.CheckBox) ArrayList(java.util.ArrayList) List(java.util.List) CheckBoxMultipleChoice(org.apache.wicket.markup.html.form.CheckBoxMultipleChoice)

Example 8 with AjaxFallbackLink

use of org.apache.wicket.ajax.markup.html.AjaxFallbackLink in project midpoint by Evolveum.

the class TaskResultTabPanel method initLayout.

private void initLayout() {
    IModel<List<OperationResult>> resultModel = new ReadOnlyModel<>(() -> createOperationResultList());
    SelectableListDataProvider<SelectableBean<OperationResult>, OperationResult> provider = new SelectableListDataProvider<>(this, resultModel);
    BoxedTablePanel<SelectableBean<OperationResult>> resultTablePanel = new BoxedTablePanel<>(ID_OPERATION_RESULT, provider, initResultColumns());
    resultTablePanel.setOutputMarkupId(true);
    add(resultTablePanel);
    AjaxFallbackLink<Void> showResult = new AjaxFallbackLink<Void>(ID_SHOW_RESULT) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(Optional<AjaxRequestTarget> optionalTarget) {
            if (!optionalTarget.isPresent()) {
                LOGGER.warn("Cannot show result in interactive way, request target not present.");
                return;
            }
            AjaxRequestTarget target = optionalTarget.get();
            PrismObjectWrapper<TaskType> taskWrapper = TaskResultTabPanel.this.getModelObject();
            TaskType taskType = taskWrapper.getObject().asObjectable();
            OperationResult opResult = OperationResult.createOperationResult(taskType.getResult());
            OpResult result = opResult != null ? OpResult.getOpResult(getPageBase(), opResult) : null;
            OperationResultPanel body = new OperationResultPanel(getPageBase().getMainPopupBodyId(), new Model<>(result));
            body.add(new VisibleBehaviour(() -> opResult != null));
            body.setOutputMarkupId(true);
            getPageBase().showMainPopup(body, target);
        }
    };
    showResult.setOutputMarkupId(true);
    add(showResult);
}
Also used : VisibleBehaviour(com.evolveum.midpoint.web.component.util.VisibleBehaviour) OperationResultPanel(com.evolveum.midpoint.gui.api.component.result.OperationResultPanel) AjaxFallbackLink(org.apache.wicket.ajax.markup.html.AjaxFallbackLink) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ReadOnlyModel(com.evolveum.midpoint.gui.api.model.ReadOnlyModel) SelectableListDataProvider(com.evolveum.midpoint.web.component.util.SelectableListDataProvider) TaskType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType) OpResult(com.evolveum.midpoint.gui.api.component.result.OpResult) SelectableBean(com.evolveum.midpoint.web.component.util.SelectableBean) BoxedTablePanel(com.evolveum.midpoint.web.component.data.BoxedTablePanel)

Example 9 with AjaxFallbackLink

use of org.apache.wicket.ajax.markup.html.AjaxFallbackLink in project wicket by apache.

the class AjaxLinkClickTest method testAjaxFallbackLinkClick.

/**
 * Test that clickLink also works with AjaxFallbackLinks
 *
 * AjaxFallbackLinks should be clicked and interpreted as an AjaxLink, which means that
 * AjaxRequestTarget is not null.
 */
@Test
public void testAjaxFallbackLinkClick() {
    final Page page = new MockPageWithLink();
    // Create a link, which we test is actually invoked
    page.add(new AjaxFallbackLink<Void>("ajaxLink") {

        private static final long serialVersionUID = 1L;

        public void onClick(Optional<AjaxRequestTarget> targetOptional) {
            linkClicked = true;
            targetOptional.ifPresent(target -> ajaxRequestTarget = target);
        }
    });
    tester.startPage(page);
    tester.clickLink("ajaxLink");
    assertTrue(linkClicked);
    assertNotNull(ajaxRequestTarget);
}
Also used : AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) Page(org.apache.wicket.Page) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) Optional(java.util.Optional) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) Test(org.junit.Test) AjaxFallbackLink(org.apache.wicket.ajax.markup.html.AjaxFallbackLink) WicketTestCase(org.apache.wicket.util.tester.WicketTestCase) Before(org.junit.Before) Page(org.apache.wicket.Page) Test(org.junit.Test)

Example 10 with AjaxFallbackLink

use of org.apache.wicket.ajax.markup.html.AjaxFallbackLink in project wicket by apache.

the class BaseWicketTester method clickLink.

/**
 * Click the {@link Link} in the last rendered Page.
 * <p>
 * This method also works for {@link AjaxLink}, {@link AjaxFallbackLink} and
 * {@link AjaxSubmitLink}.
 * <p>
 * On AjaxLinks and AjaxFallbackLinks the onClick method is invoked with a valid
 * AjaxRequestTarget. In that way you can test the flow of your application when using AJAX.
 * <p>
 * When clicking an AjaxSubmitLink the form, which the AjaxSubmitLink is attached to is first
 * submitted, and then the onSubmit method on AjaxSubmitLink is invoked. If you have changed
 * some values in the form during your test, these will also be submitted. This should not be
 * used as a replacement for the {@link FormTester} to test your forms. It should be used to
 * test that the code in your onSubmit method in AjaxSubmitLink actually works.
 * <p>
 * This method is also able to simulate that AJAX (javascript) is disabled on the client. This
 * is done by setting the isAjax parameter to false. If you have an AjaxFallbackLink you can
 * then check that it doesn't fail when invoked as a normal link.
 *
 * @param path
 *            path to <code>Link</code> component
 * @param isAjax
 *            Whether to simulate that AJAX (javascript) is enabled or not. If it's false then
 *            AjaxLink and AjaxSubmitLink will fail, since it wouldn't work in real life.
 *            AjaxFallbackLink will be invoked with null as the AjaxRequestTarget parameter.
 */
public void clickLink(String path, boolean isAjax) {
    Component linkComponent = getComponentFromLastRenderedPage(path);
    checkUsability(linkComponent, true);
    // than a normal link
    if (linkComponent instanceof AjaxLink) {
        // If it's not ajax we fail
        if (isAjax == false) {
            fail("Link " + path + "is an AjaxLink and will " + "not be invoked when AJAX (javascript) is disabled.");
        }
        List<AjaxEventBehavior> behaviors = WicketTesterHelper.findAjaxEventBehaviors(linkComponent, "click");
        for (AjaxEventBehavior behavior : behaviors) {
            executeBehavior(behavior);
        }
    } else // from it using reflection so we know what to submit.
    if (linkComponent instanceof AjaxSubmitLink) {
        // If it's not ajax we fail
        if (isAjax == false) {
            fail("Link " + path + " is an AjaxSubmitLink and " + "will not be invoked when AJAX (javascript) is disabled.");
        }
        AjaxSubmitLink link = (AjaxSubmitLink) linkComponent;
        String pageRelativePath = link.getInputName();
        request.getPostParameters().setParameterValue(pageRelativePath, "x");
        submitAjaxFormSubmitBehavior(link, (AjaxFormSubmitBehavior) WicketTesterHelper.findAjaxEventBehavior(link, "click"));
    } else // if the link is an IAjaxLink, use it (do check if AJAX is expected)
    if (isAjax && (linkComponent instanceof IAjaxLink || linkComponent instanceof AjaxFallbackLink)) {
        List<AjaxEventBehavior> behaviors = WicketTesterHelper.findAjaxEventBehaviors(linkComponent, "click");
        for (AjaxEventBehavior behavior : behaviors) {
            executeBehavior(behavior);
        }
    } else /*
		 * If the link is a submitlink then we pretend to have clicked it
		 */
    if (linkComponent instanceof SubmitLink) {
        SubmitLink submitLink = (SubmitLink) linkComponent;
        String pageRelativePath = submitLink.getInputName();
        request.getPostParameters().setParameterValue(pageRelativePath, "x");
        serializeFormToRequest(submitLink.getForm());
        submitForm(submitLink.getForm().getPageRelativePath());
    } else if (linkComponent instanceof ExternalLink) {
        ExternalLink externalLink = (ExternalLink) linkComponent;
        String href = externalLink.getDefaultModelObjectAsString();
        try {
            getResponse().sendRedirect(href);
            recordRequestResponse();
            setupNextRequestCycle();
        } catch (IOException iox) {
            throw new WicketRuntimeException("An error occurred while redirecting to: " + href, iox);
        }
    } else // if the link is a normal link (or ResourceLink)
    if (linkComponent instanceof AbstractLink) {
        AbstractLink link = (AbstractLink) linkComponent;
        /*
			 * If the link is a bookmarkable link, then we need to transfer the parameters to the
			 * next request.
			 */
        if (link instanceof BookmarkablePageLink) {
            BookmarkablePageLink<?> bookmarkablePageLink = (BookmarkablePageLink<?>) link;
            try {
                Method getParametersMethod = BookmarkablePageLink.class.getDeclaredMethod("getPageParameters", (Class<?>[]) null);
                getParametersMethod.setAccessible(true);
                PageParameters parameters = (PageParameters) getParametersMethod.invoke(bookmarkablePageLink, (Object[]) null);
                startPage(bookmarkablePageLink.getPageClass(), parameters);
            } catch (Exception e) {
                throw new WicketRuntimeException("Internal error in WicketTester. " + "Please report this in Wicket's Issue Tracker.", e);
            }
        } else if (link instanceof ResourceLink) {
            try {
                Method getURL = ResourceLink.class.getDeclaredMethod("getURL");
                getURL.setAccessible(true);
                CharSequence url = (CharSequence) getURL.invoke(link);
                executeUrl(url.toString());
            } catch (Exception x) {
                throw new RuntimeException("An error occurred while clicking on a ResourceLink", x);
            }
        } else {
            executeListener(link);
        }
    } else // The link requires AJAX
    if (linkComponent instanceof IAjaxLink && isAjax == false) {
        fail("Link " + path + "is an IAjaxLink and will " + "not be invoked when AJAX (javascript) is disabled.");
    } else {
        fail("Link " + path + " is not an instance of AbstractLink or IAjaxLink");
    }
}
Also used : AjaxEventBehavior(org.apache.wicket.ajax.AjaxEventBehavior) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) AjaxFallbackLink(org.apache.wicket.ajax.markup.html.AjaxFallbackLink) AjaxSubmitLink(org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink) IAjaxLink(org.apache.wicket.ajax.markup.html.IAjaxLink) IOException(java.io.IOException) Method(java.lang.reflect.Method) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ExternalLink(org.apache.wicket.markup.html.link.ExternalLink) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ParseException(java.text.ParseException) BookmarkablePageLink(org.apache.wicket.markup.html.link.BookmarkablePageLink) AjaxFormSubmitBehavior(org.apache.wicket.ajax.form.AjaxFormSubmitBehavior) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) SubmitLink(org.apache.wicket.markup.html.form.SubmitLink) AjaxSubmitLink(org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink) ResourceLink(org.apache.wicket.markup.html.link.ResourceLink) Component(org.apache.wicket.Component) FormComponent(org.apache.wicket.markup.html.form.FormComponent) IAjaxLink(org.apache.wicket.ajax.markup.html.IAjaxLink) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) AbstractLink(org.apache.wicket.markup.html.link.AbstractLink)

Aggregations

AjaxFallbackLink (org.apache.wicket.ajax.markup.html.AjaxFallbackLink)12 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)11 Optional (java.util.Optional)5 Page (org.apache.wicket.Page)5 AjaxLink (org.apache.wicket.ajax.markup.html.AjaxLink)5 List (java.util.List)4 PropertyModel (org.apache.wicket.model.PropertyModel)4 WicketTestCase (org.apache.wicket.util.tester.WicketTestCase)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 ListDataProvider (com.evolveum.midpoint.web.component.util.ListDataProvider)3 VisibleBehaviour (com.evolveum.midpoint.web.component.util.VisibleBehaviour)3 ArrayList (java.util.ArrayList)3 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)3 Label (org.apache.wicket.markup.html.basic.Label)3 IModel (org.apache.wicket.model.IModel)3 Model (org.apache.wicket.model.Model)3 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)3 OperationResultPanel (com.evolveum.midpoint.gui.api.component.result.OperationResultPanel)2 ReadOnlyModel (com.evolveum.midpoint.gui.api.model.ReadOnlyModel)2 BoxedTablePanel (com.evolveum.midpoint.web.component.data.BoxedTablePanel)2