Search in sources :

Example 11 with ComponentWithContext

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

the class AjaxDebugStructureInterceptor method paint.

/**
 * Override paint to only output the debugging info for the current targets.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    if (!DebugUtil.isDebugFeaturesEnabled() || !(renderContext instanceof WebXmlRenderContext)) {
        getBackingComponent().paint(renderContext);
        return;
    }
    AjaxOperation operation = AjaxHelper.getCurrentOperation();
    if (operation == null) {
        getBackingComponent().paint(renderContext);
        return;
    }
    getBackingComponent().paint(renderContext);
    XmlStringBuilder xml = ((WebXmlRenderContext) renderContext).getWriter();
    xml.appendTag("ui:debug");
    for (String targetId : operation.getTargets()) {
        ComponentWithContext target = WebUtilities.getComponentById(targetId, true);
        if (target != null) {
            writeDebugInfo(target.getComponent(), xml);
        }
    }
    xml.appendEndTag("ui:debug");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 12 with ComponentWithContext

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

the class AjaxInterceptor method serviceRequest.

/**
 * {@inheritDoc}
 */
@Override
public void serviceRequest(final Request request) {
    String triggerId = request.getParameter(WServlet.AJAX_TRIGGER_PARAM_NAME);
    AjaxOperation ajaxOperation = AjaxHelper.getCurrentOperation();
    if (ajaxOperation == null) {
        throw new IllegalStateException("No AJAX operation available for trigger " + triggerId + ".");
    }
    ComponentWithContext triggerWithContext = AjaxHelper.getCurrentTriggerAndContext();
    if (triggerWithContext == null) {
        throw new IllegalStateException("No component/context available for AJAX trigger " + triggerId + ".");
    }
    UIContext uic = UIContextHolder.getCurrent();
    // Reset the focus for this new request.
    uic.setFocussed(null, null);
    // We've hit the action phase, so we do want focus on this app.
    uic.setFocusRequired(true);
    // Process trigger only
    if (isProcessTriggerOnly(triggerWithContext, ajaxOperation)) {
        // Get user context
        UIContext tuic = triggerWithContext.getContext();
        UIContextHolder.pushContext(tuic);
        try {
            WComponent trigger = triggerWithContext.getComponent();
            trigger.serviceRequest(request);
            // Manually invoke laters as the InvokeLaters in the service request is not run due to the trigger
            // having a "parent"
            tuic.doInvokeLaters();
        } finally {
            UIContextHolder.popContext();
        }
    } else if ("GET".equals(request.getMethod())) {
        // GET only supports the above scenarios
        throw new IllegalStateException("GET is not supported for the AJAX trigger " + triggerId + ".");
    } else {
        // service the request
        super.serviceRequest(request);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) UIContext(com.github.bordertech.wcomponents.UIContext) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 13 with ComponentWithContext

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

the class ByWComponentPath method findElements.

/**
 * {@inheritDoc}
 */
@Override
public List<WebElement> findElements(final SearchContext searchContext) {
    List<WebElement> result = new ArrayList<>();
    ComponentWithContext[] components = null;
    UIContextHolder.pushContext(getContext());
    try {
        components = TreeUtil.findWComponents(getComponent(), path, visibleOnly);
    } finally {
        UIContextHolder.popContext();
    }
    if (components.length != 0) {
        componentClass = components[0].getComponent().getClass();
    }
    for (ComponentWithContext comp : components) {
        WComponent cmp = comp.getComponent();
        UIContext cmpUic = comp.getContext();
        UIContextHolder.pushContext(cmpUic);
        try {
            List<WebElement> resultForComp = findElement(searchContext, cmpUic, cmp, getValue());
            result.addAll(resultForComp);
        } finally {
            UIContextHolder.popContext();
        }
    }
    return result;
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) UIContext(com.github.bordertech.wcomponents.UIContext) ArrayList(java.util.ArrayList) WebElement(org.openqa.selenium.WebElement) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 14 with ComponentWithContext

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

the class TreeUtil_Test method testFindWComponent.

@Test
public void testFindWComponent() {
    UIContext uic = UIContextHolder.getCurrent();
    ComponentWithContext result = TreeUtil.findWComponent(root, new String[] { "WApplication" });
    Assert.assertSame("Incorrect component returned for find WApplication", root, result.getComponent());
    Assert.assertSame("Incorrect context returned for find WApplication", uic, result.getContext());
    result = TreeUtil.findWComponent(root, new String[] { "WTextArea" });
    Assert.assertSame("Incorrect component returned for find WTextArea", grandChild, result.getComponent());
    Assert.assertSame("Incorrect context returned for find WTextArea", uic, result.getContext());
    result = TreeUtil.findWComponent(root, new String[] { "WContainer", "WTextArea" });
    Assert.assertSame("Incorrect component returned for find WContainer/WTextArea", grandChild, result.getComponent());
    Assert.assertSame("Incorrect context returned for find WContainer/WTextArea", uic, result.getContext());
    result = TreeUtil.findWComponent(root, new String[] { "WText[1]" });
    Assert.assertSame("Incorrect component returned for find WText[1]", repeatedComponent, result.getComponent());
    UIContext row2Context = repeaterChild.getRowContext("2");
    Assert.assertSame("Incorrect context returned for find WText[1]", row2Context, result.getContext());
    result = TreeUtil.findWComponent(root, new String[] { "WApplication", "WText[1]" });
    Assert.assertSame("Incorrect component returned for find WApplication/WText[1]", repeatedComponent, result.getComponent());
    row2Context = repeaterChild.getRowContext("2");
    Assert.assertSame("Incorrect context returned for find WApplication/WText[1]", row2Context, result.getContext());
    result = TreeUtil.findWComponent(root, new String[] { "WContainer", "WRepeater" });
    Assert.assertNull("Should not have a result for an invalid path", result);
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext) Test(org.junit.Test)

Example 15 with ComponentWithContext

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

the class AjaxInterceptor method paintContainerResponse.

/**
 * Paint the ajax container response.
 *
 * @param renderContext the render context
 * @param operation the ajax operation
 */
private void paintContainerResponse(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());
    }
    xml.appendTagOpen("ui:ajaxtarget");
    xml.appendAttribute("id", operation.getTargetContainerId());
    xml.appendAttribute("action", AjaxOperation.AjaxAction.REPLACE_CONTENT.getDesc());
    xml.appendClose();
    // Paint targets - Assume targets are in the same context as the trigger
    UIContextHolder.pushContext(trigger.getContext());
    try {
        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;
                }
            }
            target.getComponent().paint(renderContext);
        }
    } finally {
        UIContextHolder.popContext();
    }
    xml.appendEndTag("ui:ajaxtarget");
}
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)

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