Search in sources :

Example 1 with WWindow

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

the class WWindowInterceptor_Test method testServiceRequest.

@Test
public void testServiceRequest() {
    final int initialServletStep = 123;
    final int initialWindowStep = 111;
    // Create app
    WApplication app = new WApplication();
    WWindow window = new WWindow();
    window.setContent(new MockLabel("dummy"));
    app.add(window);
    WContent content = new WContent();
    content.setContentAccess(new InternalResource("/wcomponents-test.properties", "test"));
    app.add(content);
    app.setLocked(true);
    // Set up servlet env
    WServlet.WServletEnvironment servletEnvironment = new WServlet.WServletEnvironment("/app", "", "");
    servletEnvironment.setStep(initialServletStep);
    // Set user session
    UIContext uic = createUIContext();
    uic.setUI(app);
    uic.setEnvironment(servletEnvironment);
    setActiveContext(uic);
    window.setStep(initialWindowStep);
    window.display();
    // Target the content first - should pass through and update the environment's step
    WWindowInterceptor interceptor = new WWindowInterceptor(true);
    TestInterceptor testInterceptor = new TestInterceptor();
    interceptor.setBackingComponent(testInterceptor);
    interceptor.attachUI(app);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, content.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Servlet step should have changed after serviceRequest", initialServletStep + 1, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Servlet step should have changed after preparePaint", initialServletStep + 2, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Servlet step should have changed after paint", initialServletStep + 3, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    servletEnvironment.setStep(initialServletStep);
    request = new MockRequest();
    request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Window step should have changed after serviceRequest", initialWindowStep + 1, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Window step should have changed after preparePaintnot have changed", initialWindowStep + 2, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Window step should have changed after paint", initialWindowStep + 3, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    String actualTargetId = testInterceptor.hiddenParams.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    Assert.assertEquals("Hidden params target id should be window id", window.getId(), actualTargetId);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) InternalResource(com.github.bordertech.wcomponents.InternalResource) UIContext(com.github.bordertech.wcomponents.UIContext) WWindow(com.github.bordertech.wcomponents.WWindow) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) WContent(com.github.bordertech.wcomponents.WContent) WApplication(com.github.bordertech.wcomponents.WApplication) MockLabel(com.github.bordertech.wcomponents.MockLabel) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with WWindow

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

the class DefaultPageShell method getApplicationTitle.

/**
 * Retrieves the application title for the given context. This mess is required due to WWindow essentially existing
 * as a separate UI root. If WWindow is eventually removed, then we can just retrieve the title from the root
 * WApplication.
 *
 * @param uic the context to check.
 * @return the current application title.
 */
private String getApplicationTitle(final UIContext uic) {
    WComponent root = uic.getUI();
    String title = root instanceof WApplication ? ((WApplication) root).getTitle() : null;
    Map<String, String> params = uic.getEnvironment().getHiddenParameters();
    String target = params.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    if (target != null) {
        ComponentWithContext targetComp = WebUtilities.getComponentById(target, true);
        if (targetComp != null && targetComp.getComponent() instanceof WWindow) {
            try {
                UIContextHolder.pushContext(targetComp.getContext());
                title = ((WWindow) targetComp.getComponent()).getTitle();
            } finally {
                UIContextHolder.popContext();
            }
        }
    }
    return title == null ? "" : title;
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WApplication(com.github.bordertech.wcomponents.WApplication) WWindow(com.github.bordertech.wcomponents.WWindow) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 3 with WWindow

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

the class TreeUtil method doTraverse.

/**
 * Internal implementation of tree traversal method.
 *
 * @param node the node to traverse.
 * @param visibleOnly if true, only visit visible components.
 * @param visitor the visitor to notify as the tree is traversed.
 * @return how the traversal should continue.
 */
private static VisitorResult doTraverse(final WComponent node, final boolean visibleOnly, final WComponentTreeVisitor visitor) {
    if (visibleOnly) {
        // Certain components have their visibility altered to implement custom processing.
        if (node instanceof WInvisibleContainer) {
            WComponent parent = node.getParent();
            // If inside a CardManager, skip the InvisibleContainer and process the visible card.
            if (parent instanceof WCardManager) {
                WComponent visible = ((WCardManager) node.getParent()).getVisible();
                if (visible == null) {
                    return VisitorResult.ABORT_BRANCH;
                }
                return doTraverse(visible, visibleOnly, visitor);
            } else if (parent instanceof WWindow) {
                // Abort branch if WWindow is not in ACTIVE state
                if (((WWindow) parent).getState() != WWindow.ACTIVE_STATE) {
                    return VisitorResult.ABORT_BRANCH;
                }
            }
        } else if (node instanceof WRepeatRoot) {
        // Let be processed.
        } else if (!node.isVisible()) {
            // For most components, we just need to see if they're marked as visible
            return VisitorResult.ABORT_BRANCH;
        }
    }
    VisitorResult result = visitor.visit(node);
    switch(result) {
        case ABORT_BRANCH:
            // Continue processing, but not down this branch
            return VisitorResult.CONTINUE;
        case CONTINUE:
            // Process repeater rows
            if (node instanceof WRepeater) {
                // Get parent repeater
                WRepeater repeater = (WRepeater) node;
                // Get row contexts
                List<UIContext> rowContextList = repeater.getRowContexts();
                WRepeatRoot repeatRoot = (WRepeatRoot) repeater.getRepeatedComponent().getParent();
                for (UIContext rowContext : rowContextList) {
                    UIContextHolder.pushContext(rowContext);
                    try {
                        result = doTraverse(repeatRoot, visibleOnly, visitor);
                    } finally {
                        UIContextHolder.popContext();
                    }
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            } else if (node instanceof Container) {
                Container container = (Container) node;
                for (int i = 0; i < container.getChildCount(); i++) {
                    result = doTraverse(container.getChildAt(i), visibleOnly, visitor);
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            }
            return VisitorResult.CONTINUE;
        default:
            // Abort entire traversal
            return VisitorResult.ABORT;
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) VisitorResult(com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult) Container(com.github.bordertech.wcomponents.Container) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeatRoot(com.github.bordertech.wcomponents.WRepeater.WRepeatRoot) UIContext(com.github.bordertech.wcomponents.UIContext) WCardManager(com.github.bordertech.wcomponents.WCardManager) WWindow(com.github.bordertech.wcomponents.WWindow) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeater(com.github.bordertech.wcomponents.WRepeater)

Aggregations

WWindow (com.github.bordertech.wcomponents.WWindow)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WApplication (com.github.bordertech.wcomponents.WApplication)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)1 Container (com.github.bordertech.wcomponents.Container)1 InternalResource (com.github.bordertech.wcomponents.InternalResource)1 MockLabel (com.github.bordertech.wcomponents.MockLabel)1 WCardManager (com.github.bordertech.wcomponents.WCardManager)1 WContent (com.github.bordertech.wcomponents.WContent)1 WInvisibleContainer (com.github.bordertech.wcomponents.WInvisibleContainer)1 WRepeater (com.github.bordertech.wcomponents.WRepeater)1 WRepeatRoot (com.github.bordertech.wcomponents.WRepeater.WRepeatRoot)1 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)1 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)1 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)1 VisitorResult (com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult)1 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)1 PrintWriter (java.io.PrintWriter)1 Test (org.junit.Test)1