/** * @param trigger the AJAX trigger to check * @return true if this is the current AJAX trigger */ public static boolean isCurrentAjaxTrigger(final WComponent trigger) { if (trigger == null) { return false; } AjaxOperation operation = AjaxHelper.getCurrentOperation(); return operation != null && operation.getTriggerId().equals(trigger.getId()); }
/** * The Ajax servlet needs access to the AjaxOperation Store the operation in the user context using the trigger Id, * as this will be present in the Servlet HttpRequest. agreed key. The ajax id is passed in the url to the servlet * so it can then access the context. * * @param operation the operation to register. */ private static void registerAjaxOperation(final AjaxOperation operation) { UIContext uic = UIContextHolder.getCurrentPrimaryUIContext(); if (uic == null) { throw new SystemException("No User Context Available to Register AJAX Operations."); } Map<String, AjaxOperation> operations = (Map<String, AjaxOperation>) uic.getFwkAttribute(AJAX_OPERATIONS_SESSION_KEY); if (operations == null) { operations = new HashMap<>(); uic.setFwkAttribute(AJAX_OPERATIONS_SESSION_KEY, operations); } operations.put(operation.getTriggerId(), operation); } }
/** * Override handleRequest in order to perform processing for this component. This implementation checks whether the * link has been pressed via the current ajax operation. * * @param request the request being responded to. */ @Override public void handleRequest(final Request request) { // Check if this link was the AJAX Trigger AjaxOperation operation = AjaxHelper.getCurrentOperation(); boolean pressed = (operation != null && getId().equals(operation.getTriggerId())); // Protect against client-side tampering of disabled/read-only fields. if (isDisabled() && pressed) { LOG.warn("A disabled link has been triggered. " + getText() + ". " + getId()); return; } // If an action has been supplied then execute it, but only after // handle request has been performed on the entire component tree. final Action action = getAction(); if (pressed && action != null) { final ActionEvent event = new ActionEvent(this, getActionCommand(), getActionObject()); Runnable later = new Runnable() { @Override public void run() { action.execute(event); } }; invokeLater(later); } }
throw new SystemException("No context available for trigger " + operation.getTriggerId()); if (targetId.equals(operation.getTriggerId())) { target = trigger; } else {
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 {
@Test public void testPreparePaintComponent() { WApplication app = new WApplication(); AjaxTrigger trigger = new WButton(); AjaxTarget target = new WPanel(); WAjaxControl control = new WAjaxControl(trigger, target); app.add(control); app.add(trigger); app.add(target); app.setLocked(true); UIContext uic = createUIContext(); uic.setUI(app); setActiveContext(uic); MockRequest req = new MockRequest(); control.handleRequest(req); control.preparePaint(req); AjaxOperation ajaxOperation = AjaxHelper.getAjaxOperation(trigger.getId()); Assert.assertNotNull("Ajax operation should have been registered", ajaxOperation); Assert.assertEquals("Incorrect ajax operation trigger id", trigger.getId(), ajaxOperation. getTriggerId()); Assert.assertEquals("Incorrect ajax operation targets", target.getId(), ajaxOperation. getTargets().get(0)); }