Search in sources :

Example 6 with AjaxOperation

use of com.github.bordertech.wcomponents.AjaxOperation 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 7 with AjaxOperation

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

the class AjaxPageShellInterceptor method paint.

/**
 * Paints the targeted ajax regions. The format of the response is an agreement between the server and the client
 * side handling our AJAX response.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    XmlStringBuilder xml = webRenderContext.getWriter();
    AjaxOperation operation = AjaxHelper.getCurrentOperation();
    if (operation == null) {
        // the request attribute that we place in the ui contenxt in the action phase can't be null
        throw new SystemException("Can't paint AJAX response. Couldn't find the expected reference to the AjaxOperation.");
    }
    UIContext uic = UIContextHolder.getCurrent();
    String focusId = uic.getFocussedId();
    xml.append(XMLUtil.getXMLDeclarationWithThemeXslt(uic));
    xml.appendTagOpen("ui:ajaxresponse");
    xml.append(XMLUtil.STANDARD_NAMESPACES);
    xml.appendOptionalAttribute("defaultFocusId", uic.isFocusRequired() && !Util.empty(focusId), focusId);
    xml.appendClose();
    getBackingComponent().paint(renderContext);
    xml.appendEndTag("ui:ajaxresponse");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 8 with AjaxOperation

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

the class AjaxSetupInterceptor method serviceRequest.

/**
 * Setup the AJAX operation details.
 *
 * @param request the request being serviced.
 */
@Override
public void serviceRequest(final Request request) {
    // Get trigger id
    String triggerId = request.getParameter(WServlet.AJAX_TRIGGER_PARAM_NAME);
    if (triggerId == null) {
        throw new SystemException("No AJAX trigger id to on request");
    }
    // Find the Component for this trigger
    ComponentWithContext trigger = WebUtilities.getComponentById(triggerId, true);
    if (trigger == null) {
        throw new SystemException("No component found for AJAX trigger " + triggerId + ".");
    }
    WComponent triggerComponent = trigger.getComponent();
    // Check for an internal AJAX request (if client has flagged it as internal)
    boolean internal = request.getParameter(WServlet.AJAX_TRIGGER_INTERNAL_PARAM_NAME) != null;
    AjaxOperation ajaxOperation = null;
    if (internal) {
        if (!(triggerComponent instanceof AjaxInternalTrigger)) {
            throw new SystemException("AJAX trigger [" + triggerId + "] does not support internal actions.");
        }
        // Create internal operation
        ajaxOperation = new AjaxOperation(triggerId);
    } else {
        // Check for a registered AJAX operation
        ajaxOperation = AjaxHelper.getAjaxOperation(triggerId);
        // TODO This is only required until all components start using the Internal param flag
        if (ajaxOperation != null && "GET".equals(request.getMethod()) && triggerComponent instanceof AjaxInternalTrigger) {
            // Create internal operation
            ajaxOperation = new AjaxOperation(triggerId);
        }
    }
    // If no operation registered, check if the trigger supports internal AJAX then assume it is an Internal Action
    if (ajaxOperation == null && trigger.getComponent() instanceof AjaxInternalTrigger) {
        // Create internal operation
        ajaxOperation = new AjaxOperation(triggerId);
    }
    // No Valid operation
    if (ajaxOperation == null) {
        throw new SystemException("No AJAX operation has been registered for trigger " + triggerId + ".");
    }
    // Set current operation
    AjaxHelper.setCurrentOperationDetails(ajaxOperation, trigger);
    // Process Service Request
    super.serviceRequest(request);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) AjaxInternalTrigger(com.github.bordertech.wcomponents.AjaxInternalTrigger) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) SystemException(com.github.bordertech.wcomponents.util.SystemException) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Aggregations

AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)8 UIContext (com.github.bordertech.wcomponents.UIContext)6 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)4 Test (org.junit.Test)4 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)3 WText (com.github.bordertech.wcomponents.WText)3 WComponent (com.github.bordertech.wcomponents.WComponent)2 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)2 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 Action (com.github.bordertech.wcomponents.Action)1 ActionEvent (com.github.bordertech.wcomponents.ActionEvent)1 AjaxInternalTrigger (com.github.bordertech.wcomponents.AjaxInternalTrigger)1 WFigure (com.github.bordertech.wcomponents.WFigure)1 WPanel (com.github.bordertech.wcomponents.WPanel)1 WSection (com.github.bordertech.wcomponents.WSection)1 WSuggestions (com.github.bordertech.wcomponents.WSuggestions)1