Search in sources :

Example 1 with ActionEvent

use of jakarta.faces.event.ActionEvent in project myfaces by apache.

the class UIViewAction method broadcast.

@Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
    super.broadcast(event);
    FacesContext context = getFacesContext();
    if (context.getResponseComplete()) {
        return;
    }
    UIComponent c = event.getComponent();
    UIViewRoot sourceViewRoot = null;
    do {
        if (c instanceof UIViewRoot) {
            sourceViewRoot = (UIViewRoot) c;
            break;
        } else {
            c = c.getParent();
        }
    } while (c != null);
    if (!context.getViewRoot().equals(sourceViewRoot)) {
        return;
    }
    if (event instanceof ActionEvent) {
        ActionListener defaultActionListener = context.getApplication().getActionListener();
        if (defaultActionListener != null) {
            String viewIdBeforeAction = context.getViewRoot().getViewId();
            Boolean oldBroadcastProcessing = (Boolean) context.getAttributes().get(BROADCAST_PROCESSING_KEY);
            try {
                context.getAttributes().put(BROADCAST_PROCESSING_KEY, Boolean.TRUE);
                ViewActionFacesContextWrapper wrappedFacesContext = new ViewActionFacesContextWrapper(context);
                try {
                    wrappedFacesContext.setWrapperAsCurrentFacesContext();
                    if (event instanceof ViewActionEvent) {
                        ((ViewActionEvent) event).setFacesContext(wrappedFacesContext);
                    }
                    defaultActionListener.processAction((ActionEvent) event);
                    // Decrement count
                    Integer count = (Integer) context.getAttributes().get(EVENT_COUNT_KEY);
                    count = (count == null) ? 0 : count - 1;
                    context.getAttributes().put(EVENT_COUNT_KEY, count);
                } finally {
                    wrappedFacesContext.restoreCurrentFacesContext();
                }
            } finally {
                context.getAttributes().put(BROADCAST_PROCESSING_KEY, oldBroadcastProcessing == null ? Boolean.FALSE : oldBroadcastProcessing);
            }
            if (context.getResponseComplete()) {
                return;
            } else {
                Integer count = (Integer) context.getAttributes().get(EVENT_COUNT_KEY);
                count = (count == null) ? 0 : count;
                String viewIdAfterAction = context.getViewRoot().getViewId();
                if (viewIdBeforeAction.equals(viewIdAfterAction) && count == 0) {
                    context.renderResponse();
                }
            // "... Otherwise, execute the lifecycle on the new UIViewRoot ..."
            // Note these words are implemented in the NavigationHandler, but
            // the original proposal from seam s:viewAction did a trick here
            // to restart the JSF lifecycle.
            }
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ActionListener(jakarta.faces.event.ActionListener) ActionEvent(jakarta.faces.event.ActionEvent)

Example 2 with ActionEvent

use of jakarta.faces.event.ActionEvent in project myfaces by apache.

the class UIViewAction method decode.

@Override
public void decode(FacesContext context) {
    super.decode(context);
    if (context.isPostback() && !isOnPostback()) {
        return;
    }
    if (!isRendered()) {
        return;
    }
    ActionEvent evt = new ViewActionEvent(this);
    String phase = getPhase();
    PhaseId phaseId = phase != null ? PhaseId.phaseIdValueOf(phase) : isImmediate() ? PhaseId.APPLY_REQUEST_VALUES : PhaseId.INVOKE_APPLICATION;
    evt.setPhaseId(phaseId);
    this.queueEvent(evt);
    // "... Keep track of the number of events that are queued in this way
    // on this run through the lifecycle. ...". The are two options:
    // 1. Use an attribute over FacesContext attribute map
    // 2. Use an attribute over the component
    // If the view is recreated again with the same viewId, the component
    // state get lost, so the option 1 is preferred.
    Integer count = (Integer) context.getAttributes().get(EVENT_COUNT_KEY);
    count = (count == null) ? 1 : count + 1;
    context.getAttributes().put(EVENT_COUNT_KEY, count);
}
Also used : PhaseId(jakarta.faces.event.PhaseId) ActionEvent(jakarta.faces.event.ActionEvent)

Example 3 with ActionEvent

use of jakarta.faces.event.ActionEvent in project myfaces by apache.

the class UICommand method broadcast.

@Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
    super.broadcast(event);
    if (event instanceof ActionEvent) {
        FacesContext context = getFacesContext();
        ActionListener defaultActionListener = context.getApplication().getActionListener();
        if (defaultActionListener != null) {
            defaultActionListener.processAction((ActionEvent) event);
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ActionListener(jakarta.faces.event.ActionListener) ActionEvent(jakarta.faces.event.ActionEvent)

Example 4 with ActionEvent

use of jakarta.faces.event.ActionEvent in project myfaces by apache.

the class HtmlLinkRendererBase method decode.

@Override
public void decode(FacesContext facesContext, UIComponent component) {
    // check for NP
    super.decode(facesContext, component);
    if (component instanceof UICommand) {
        String clientId = component.getClientId(facesContext);
        UIForm form = ComponentUtils.findClosest(UIForm.class, component);
        boolean disabled = HtmlRendererUtils.isDisabled(component);
        // MYFACES-3960 Decode, decode client behavior and queue action event at the end
        boolean activateActionEvent = false;
        if (form != null && !disabled) {
            String reqValue = (String) facesContext.getExternalContext().getRequestParameterMap().get(HtmlRendererUtils.getHiddenCommandLinkFieldName(form, facesContext));
            activateActionEvent = reqValue != null && reqValue.equals(clientId) || HtmlRendererUtils.isPartialOrBehaviorSubmit(facesContext, clientId);
        }
        if (component instanceof ClientBehaviorHolder && !disabled) {
            ClientBehaviorRendererUtils.decodeClientBehaviors(facesContext, component);
        }
        if (activateActionEvent) {
            component.queueEvent(new ActionEvent(component));
        }
    } else if (component instanceof UIOutput) {
        // do nothing
        if (component instanceof ClientBehaviorHolder && !HtmlRendererUtils.isDisabled(component)) {
            ClientBehaviorRendererUtils.decodeClientBehaviors(facesContext, component);
        }
    } else {
        throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
    }
}
Also used : UIOutput(jakarta.faces.component.UIOutput) ActionEvent(jakarta.faces.event.ActionEvent) UICommand(jakarta.faces.component.UICommand) UIForm(jakarta.faces.component.UIForm) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Example 5 with ActionEvent

use of jakarta.faces.event.ActionEvent in project myfaces by apache.

the class HtmlButtonRendererBase method decode.

@Override
public void decode(FacesContext facesContext, UIComponent uiComponent) {
    RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
    // super.decode must not be called, because value is handled here
    boolean disabled = isDisabled(facesContext, uiComponent);
    // MYFACES-3960 Decode, decode client behavior and queue action event at the end
    boolean activateActionEvent = !isReset(uiComponent) && isSubmitted(facesContext, uiComponent) && !disabled;
    if (uiComponent instanceof ClientBehaviorHolder && !disabled) {
        ClientBehaviorRendererUtils.decodeClientBehaviors(facesContext, uiComponent);
    }
    if (activateActionEvent) {
        uiComponent.queueEvent(new ActionEvent(uiComponent));
    }
}
Also used : ActionEvent(jakarta.faces.event.ActionEvent) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Aggregations

ActionEvent (jakarta.faces.event.ActionEvent)39 UICommand (jakarta.faces.component.UICommand)21 UIComponent (jakarta.faces.component.UIComponent)14 UIViewRoot (jakarta.faces.component.UIViewRoot)14 Test (org.junit.Test)12 UINamingContainer (jakarta.faces.component.UINamingContainer)10 PrintWriter (java.io.PrintWriter)10 FacesContext (jakarta.faces.context.FacesContext)8 ActionListener (jakarta.faces.event.ActionListener)6 MethodExpression (jakarta.el.MethodExpression)4 UIForm (jakarta.faces.component.UIForm)3 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)3 StringWriter (java.io.StringWriter)3 MockResponseWriter (org.apache.myfaces.test.mock.MockResponseWriter)3 UIInput (jakarta.faces.component.UIInput)2 HtmlCommandButton (jakarta.faces.component.html.HtmlCommandButton)2 FacesListener (jakarta.faces.event.FacesListener)2 MethodExpressionActionListener (jakarta.faces.event.MethodExpressionActionListener)2 PhaseId (jakarta.faces.event.PhaseId)2 ServletException (jakarta.servlet.ServletException)2