Search in sources :

Example 1 with ComponentWithContext

use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.

the class TreeUtil_Test method testCollateVisibles.

@Test
public void testCollateVisibles() {
    // Simple case - everything visible.
    List<ComponentWithContext> visibles = TreeUtil.collateVisibles(root);
    UIContext uic = UIContextHolder.getCurrent();
    Assert.assertEquals("Incorrect number of visible components", 10, visibles.size());
    Assert.assertTrue("List should contain root", isInList(visibles, root, uic));
    Assert.assertTrue("List should contain containerChild", isInList(visibles, containerChild, uic));
    Assert.assertTrue("List should contain simpleChild", isInList(visibles, simpleChild, uic));
    Assert.assertTrue("List should contain repeaterChild", isInList(visibles, repeaterChild, uic));
    Assert.assertTrue("List should contain grandChild", isInList(visibles, grandChild, uic));
    Assert.assertTrue("List should contain repeatedComponent row 1", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("1")));
    Assert.assertTrue("List should contain repeatedComponent row 2", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("2")));
    Assert.assertTrue("List should contain repeatedComponent row 3", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("3")));
    Assert.assertTrue("List should contain card manager", isInList(visibles, cardManager, uic));
    Assert.assertTrue("List should contain visible card", isInList(visibles, card1, uic));
    // Make grandChild invisible - should only remove it
    grandChild.setVisible(false);
    visibles = TreeUtil.collateVisibles(root);
    Assert.assertEquals("Incorrect number of visible components", 9, visibles.size());
    Assert.assertTrue("List should contain root", isInList(visibles, root, uic));
    Assert.assertTrue("List should contain containerChild", isInList(visibles, containerChild, uic));
    Assert.assertTrue("List should contain simpleChild", isInList(visibles, simpleChild, uic));
    Assert.assertTrue("List should contain repeaterChild", isInList(visibles, repeaterChild, uic));
    Assert.assertTrue("List should contain repeatedComponent row 1", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("1")));
    Assert.assertTrue("List should contain repeatedComponent row 2", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("2")));
    Assert.assertTrue("List should contain repeatedComponent row 3", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("3")));
    Assert.assertTrue("List should contain card manager", isInList(visibles, cardManager, uic));
    Assert.assertTrue("List should contain visible card", isInList(visibles, card1, uic));
    grandChild.setVisible(true);
    // Make containerChild invisible - should remove it and grandChild
    containerChild.setVisible(false);
    visibles = TreeUtil.collateVisibles(root);
    Assert.assertEquals("Incorrect number of visible components", 8, visibles.size());
    Assert.assertTrue("List should contain root", isInList(visibles, root, uic));
    Assert.assertTrue("List should contain simpleChild", isInList(visibles, simpleChild, uic));
    Assert.assertTrue("List should contain repeaterChild", isInList(visibles, repeaterChild, uic));
    Assert.assertTrue("List should contain repeatedComponent row 1", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("1")));
    Assert.assertTrue("List should contain repeatedComponent row 2", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("2")));
    Assert.assertTrue("List should contain repeatedComponent row 3", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("3")));
    Assert.assertTrue("List should contain card manager", isInList(visibles, cardManager, uic));
    Assert.assertTrue("List should contain visible card", isInList(visibles, card1, uic));
    containerChild.setVisible(true);
    // Make repeaterChild invisible - should remove it and its 3 rows
    repeaterChild.setVisible(false);
    visibles = TreeUtil.collateVisibles(root);
    Assert.assertEquals("Incorrect number of visible components", 6, visibles.size());
    Assert.assertTrue("List should contain root", isInList(visibles, root, uic));
    Assert.assertTrue("List should contain containerChild", isInList(visibles, containerChild, uic));
    Assert.assertTrue("List should contain simpleChild", isInList(visibles, simpleChild, uic));
    Assert.assertTrue("List should contain grandChild", isInList(visibles, grandChild, uic));
    Assert.assertTrue("List should contain card manager", isInList(visibles, cardManager, uic));
    Assert.assertTrue("List should contain visible card", isInList(visibles, card1, uic));
    repeaterChild.setVisible(true);
    // Make 2nd row of repeater invisible
    setActiveContext(repeaterChild.getRowContext("2"));
    repeatedComponent.setVisible(false);
    setActiveContext(uic);
    visibles = TreeUtil.collateVisibles(root);
    Assert.assertEquals("Incorrect number of visible components", 9, visibles.size());
    Assert.assertTrue("List should contain root", isInList(visibles, root, uic));
    Assert.assertTrue("List should contain containerChild", isInList(visibles, containerChild, uic));
    Assert.assertTrue("List should contain simpleChild", isInList(visibles, simpleChild, uic));
    Assert.assertTrue("List should contain repeaterChild", isInList(visibles, repeaterChild, uic));
    Assert.assertTrue("List should contain grandChild", isInList(visibles, grandChild, uic));
    Assert.assertTrue("List should contain repeatedComponent row 1", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("1")));
    Assert.assertTrue("List should contain repeatedComponent row 3", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("3")));
    Assert.assertTrue("List should contain card manager", isInList(visibles, cardManager, uic));
    Assert.assertTrue("List should contain visible card", isInList(visibles, card1, uic));
    // Switch active cards
    cardManager.makeVisible(card2);
    visibles = TreeUtil.collateVisibles(root);
    Assert.assertEquals("Incorrect number of visible components", 9, visibles.size());
    Assert.assertTrue("List should contain root", isInList(visibles, root, uic));
    Assert.assertTrue("List should contain containerChild", isInList(visibles, containerChild, uic));
    Assert.assertTrue("List should contain simpleChild", isInList(visibles, simpleChild, uic));
    Assert.assertTrue("List should contain repeaterChild", isInList(visibles, repeaterChild, uic));
    Assert.assertTrue("List should contain grandChild", isInList(visibles, grandChild, uic));
    Assert.assertTrue("List should contain repeatedComponent row 1", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("1")));
    Assert.assertTrue("List should contain repeatedComponent row 3", isInList(visibles, repeatedComponent, repeaterChild.getRowContext("3")));
    Assert.assertTrue("List should contain card manager", isInList(visibles, cardManager, uic));
    Assert.assertTrue("List should contain visible card", isInList(visibles, card2, uic));
    // Make root invisible - should be an empty list
    root.setVisible(false);
    visibles = TreeUtil.collateVisibles(root);
    Assert.assertEquals("Incorrect number of visible components", 0, visibles.size());
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext) Test(org.junit.Test)

Example 2 with ComponentWithContext

use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.

the class AjaxInterceptor method paintResponse.

/**
 * Paint the ajax response.
 *
 * @param renderContext the render context
 * @param operation the ajax operation
 */
private void paintResponse(final RenderContext renderContext, final AjaxOperation operation) {
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    XmlStringBuilder xml = webRenderContext.getWriter();
    // Get trigger's context
    ComponentWithContext trigger = AjaxHelper.getCurrentTriggerAndContext();
    if (trigger == null) {
        throw new SystemException("No context available for trigger " + operation.getTriggerId());
    }
    for (String targetId : operation.getTargets()) {
        ComponentWithContext target;
        if (targetId.equals(operation.getTriggerId())) {
            target = trigger;
        } else {
            target = WebUtilities.getComponentById(targetId, true);
            if (target == null) {
                LOG.warn("Could not find ajax target to render [" + targetId + "]");
                continue;
            }
        }
        UIContextHolder.pushContext(target.getContext());
        try {
            xml.appendTagOpen("ui:ajaxtarget");
            xml.appendAttribute("id", targetId);
            xml.appendAttribute("action", operation.getAction().getDesc());
            xml.appendClose();
            target.getComponent().paint(renderContext);
            xml.appendEndTag("ui:ajaxtarget");
        } finally {
            UIContextHolder.popContext();
        }
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 3 with ComponentWithContext

use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.

the class TargetableInterceptor method paint.

/**
 * {@inheritDoc}
 */
@Override
public void paint(final RenderContext renderContext) {
    ComponentWithContext target = WebUtilities.getComponentById(targetId, true);
    if (target == null) {
        throw new SystemException("No target component found for id " + targetId);
    }
    UIContextHolder.pushContext(target.getContext());
    try {
        super.paint(renderContext);
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 4 with ComponentWithContext

use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.

the class WWindowInterceptor method preparePaint.

/**
 * Temporarily replaces the environment while the UI prepares to render.
 *
 * @param request the request being responded to.
 */
@Override
public void preparePaint(final Request request) {
    if (windowId == null) {
        super.preparePaint(request);
    } else {
        // Get the window component
        ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
        if (target == null) {
            throw new SystemException("No window component for id " + windowId);
        }
        UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
        Environment originalEnvironment = uic.getEnvironment();
        uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
        UIContextHolder.pushContext(target.getContext());
        try {
            super.preparePaint(request);
        } finally {
            uic.setEnvironment(originalEnvironment);
            UIContextHolder.popContext();
        }
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) Environment(com.github.bordertech.wcomponents.Environment) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 5 with ComponentWithContext

use of com.github.bordertech.wcomponents.ComponentWithContext in project wcomponents by BorderTech.

the class WWindowInterceptor method paint.

/**
 * Temporarily replaces the environment while the UI is being rendered.
 *
 * @param renderContext the context to render to.
 */
@Override
public void paint(final RenderContext renderContext) {
    if (windowId == null) {
        super.paint(renderContext);
    } else {
        // Get the window component
        ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
        if (target == null) {
            throw new SystemException("No window component for id " + windowId);
        }
        UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
        Environment originalEnvironment = uic.getEnvironment();
        uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
        UIContextHolder.pushContext(target.getContext());
        try {
            super.paint(renderContext);
        } finally {
            uic.setEnvironment(originalEnvironment);
            UIContextHolder.popContext();
        }
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) Environment(com.github.bordertech.wcomponents.Environment) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Aggregations

ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)22 UIContext (com.github.bordertech.wcomponents.UIContext)10 SystemException (com.github.bordertech.wcomponents.util.SystemException)10 WComponent (com.github.bordertech.wcomponents.WComponent)8 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)3 Environment (com.github.bordertech.wcomponents.Environment)3 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)3 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)3 ArrayList (java.util.ArrayList)3 VisitorResult (com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult)2 FindComponentByIdVisitor (com.github.bordertech.wcomponents.util.visitor.FindComponentByIdVisitor)2 Test (org.junit.Test)2 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)1 AjaxInternalTrigger (com.github.bordertech.wcomponents.AjaxInternalTrigger)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 WAudio (com.github.bordertech.wcomponents.WAudio)1 WContent (com.github.bordertech.wcomponents.WContent)1 WImage (com.github.bordertech.wcomponents.WImage)1 WVideo (com.github.bordertech.wcomponents.WVideo)1 WWindow (com.github.bordertech.wcomponents.WWindow)1