Search in sources :

Example 1 with ComponentSystemEvent

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

the class AnnotationConfigurator method handleNamedEvent.

private void handleNamedEvent(FacesConfigImpl facesConfig, Set<Class<?>> classes) {
    for (Class<?> clazz : classes) {
        NamedEvent namedEvent = (NamedEvent) clazz.getAnnotation(NamedEvent.class);
        if (namedEvent != null) {
            // Can only apply @NamedEvent to ComponentSystemEvent subclasses.
            if (!ComponentSystemEvent.class.isAssignableFrom(clazz)) {
                // Just log this.  We'll catch it later in the runtime.
                if (log.isLoggable(Level.WARNING)) {
                    log.warning(clazz.getName() + " is annotated with @jakarta.faces.event.NamedEvent, but does " + "not extend jakarta.faces.event.ComponentSystemEvent");
                }
            }
            // Have to register @NamedEvent annotations with the NamedEventManager class since
            // we need to get access to this info later and can't from the dispenser (it's not a
            // singleton).
            org.apache.myfaces.config.impl.element.NamedEventImpl namedEventConfig = new org.apache.myfaces.config.impl.element.NamedEventImpl();
            namedEventConfig.setEventClass(clazz.getName());
            namedEventConfig.setShortName(namedEvent.shortName());
            facesConfig.addNamedEvent(namedEventConfig);
        }
    }
}
Also used : ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) NamedEvent(jakarta.faces.event.NamedEvent)

Example 2 with ComponentSystemEvent

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

the class EventHandler method getEventClass.

/**
 * Gets the event class defined by the tag (either in the "name" or "type" attribute).
 *
 * @param context the Facelet context
 * @return a Class containing the event class defined by the tag.
 */
@SuppressWarnings("unchecked")
private Class<? extends ComponentSystemEvent> getEventClass(FaceletContext context) {
    Class<?> eventClass = null;
    String value = null;
    if (type.isLiteral() && eventClassLiteral != null) {
        // if type is literal it does not change, avoid Reflection and use cached value
        return (Class<? extends ComponentSystemEvent>) eventClassLiteral;
    }
    if (type.isLiteral()) {
        value = type.getValue();
    } else {
        value = (String) type.getValueExpression(context, String.class).getValue(context.getFacesContext().getELContext());
    }
    Collection<Class<? extends ComponentSystemEvent>> events;
    // We can look up the event class by name in the NamedEventManager.
    events = RuntimeConfig.getCurrentInstance(context.getFacesContext().getExternalContext()).getNamedEventManager().getNamedEvent(value);
    if (events == null) {
        try {
            eventClass = ClassUtils.forName(value);
            if (type.isLiteral()) {
                eventClassLiteral = eventClass;
            }
        } catch (Throwable e) {
            throw new TagAttributeException(type, "Couldn't create event class", e);
        }
    } else if (events.size() > 1) {
        StringBuilder classNames = new StringBuilder("[");
        Iterator<Class<? extends ComponentSystemEvent>> eventIterator = events.iterator();
        while (eventIterator.hasNext()) {
            classNames.append(eventIterator.next().getName());
            if (eventIterator.hasNext()) {
                classNames.append(", ");
            } else {
                classNames.append(']');
            }
        }
        throw new FacesException("The event name '" + value + "' is mapped to more than one " + " event class: " + classNames.toString());
    } else {
        eventClass = events.iterator().next();
    }
    if (!ComponentSystemEvent.class.isAssignableFrom(eventClass)) {
        throw new TagAttributeException(type, "Event class " + eventClass.getName() + " is not of type jakarta.faces.event.ComponentSystemEvent");
    }
    return (Class<? extends ComponentSystemEvent>) eventClass;
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) Iterator(java.util.Iterator) FacesException(jakarta.faces.FacesException)

Example 3 with ComponentSystemEvent

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

the class PostAddToViewEventTestCase method testPostAddToViewOnViewRoot.

/**
 * Test that UIViewRoot receive a PostAddToViewEvent after the view is
 * populated.
 *
 * @throws Exception
 */
@Test
public void testPostAddToViewOnViewRoot() throws Exception {
    startViewRequest("/postAddToViewEvent_1.xhtml");
    PostAddToViewEventBean bean = EasyMock.createMock(PostAddToViewEventBean.class);
    bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
    EasyMock.expectLastCall().andAnswer(() -> {
        ComponentSystemEvent e = (ComponentSystemEvent) EasyMock.getCurrentArguments()[0];
        Assert.assertTrue(e.getComponent() instanceof UIViewRoot);
        Assert.assertEquals(PhaseId.RENDER_RESPONSE, facesContext.getCurrentPhaseId());
        return null;
    }).once();
    EasyMock.replay(bean);
    // Put on request map
    request.setAttribute("postAddToViewEventBean", bean);
    processLifecycleExecuteAndRender();
    EasyMock.verify(bean);
    UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
    client.submit(button);
    bean = EasyMock.createMock(PostAddToViewEventBean.class);
    bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
    // but when building initial state.
    if (WebConfigParamUtils.getBooleanInitParameter(externalContext, StateManager.PARTIAL_STATE_SAVING_PARAM_NAME)) {
        EasyMock.expectLastCall().andAnswer(() -> {
            ComponentSystemEvent e = (ComponentSystemEvent) EasyMock.getCurrentArguments()[0];
            Assert.assertTrue(e.getComponent() instanceof UIViewRoot);
            Assert.assertEquals(PhaseId.RESTORE_VIEW, facesContext.getCurrentPhaseId());
            Assert.assertTrue(facesContext.getAttributes().containsKey("jakarta.faces.IS_BUILDING_INITIAL_STATE"));
            Assert.assertFalse(FaceletViewDeclarationLanguage.isRefreshingTransientBuild(facesContext));
            return null;
        }).once();
    } else {
        // Without PSS the listener should not be called, because it was already
        // called on the first request
        EasyMock.expectLastCall().andAnswer(() -> {
            Assert.fail();
            return null;
        }).anyTimes();
    }
    EasyMock.replay(bean);
    // Put on request map
    request.setAttribute("postAddToViewEventBean", bean);
    processLifecycleExecute();
    EasyMock.verify(bean);
}
Also used : ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) UICommand(jakarta.faces.component.UICommand) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Example 4 with ComponentSystemEvent

use of jakarta.faces.event.ComponentSystemEvent in project mojarra by eclipse-ee4j.

the class WebsocketFacesListener method processEvent.

/**
 * If the websocket has just switched its <code>rendered</code> or <code>connected</code> attribute, then render either
 * the <code>open()</code> script or the <code>close()</code> script. During an ajax request with partial rendering,
 * it's added as <code>&lt;eval&gt;</code> by partial response writer, else it's just added as a script component with
 * <code>target="body"</code>.
 */
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
    if (!(event instanceof PreRenderViewEvent)) {
        return;
    }
    FacesContext context = ((ComponentSystemEvent) event).getFacesContext();
    Map<String, Boolean> initializedWebsockets = getInitializedWebsockets(context);
    if (!context.getPartialViewContext().isAjaxRequest()) {
        initializedWebsockets.clear();
    }
    for (Entry<String, Boolean> initializedWebsocket : initializedWebsockets.entrySet()) {
        String clientId = initializedWebsocket.getKey();
        UIWebsocket websocket = (UIWebsocket) context.getViewRoot().findComponent(clientId);
        boolean connected = websocket.isRendered() && websocket.isConnected();
        boolean previouslyConnected = initializedWebsocket.setValue(connected);
        if (previouslyConnected != connected) {
            String script = String.format(connected ? SCRIPT_OPEN : SCRIPT_CLOSE, clientId);
            PartialViewContext pvc = context.getPartialViewContext();
            if (pvc.isAjaxRequest() && !pvc.isRenderAll()) {
                context.getPartialViewContext().getEvalScripts().add(script);
            } else {
                UIOutput outputScript = new UIOutput();
                outputScript.setRendererType("jakarta.faces.resource.Script");
                UIOutput content = new UIOutput();
                content.setValue(script);
                outputScript.getChildren().add(content);
                context.getViewRoot().addComponentResource(context, outputScript, "body");
            }
        }
    }
}
Also used : UIWebsocket(jakarta.faces.component.UIWebsocket) FacesContext(jakarta.faces.context.FacesContext) UIOutput(jakarta.faces.component.UIOutput) ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) PartialViewContext(jakarta.faces.context.PartialViewContext) PreRenderViewEvent(jakarta.faces.event.PreRenderViewEvent)

Example 5 with ComponentSystemEvent

use of jakarta.faces.event.ComponentSystemEvent in project faces by jakartaee.

the class TestServlet method preRemoveFromViewEventIsApproiateListenerPostiveTest.

// ------------------------------------------------------------
// PreRemoveFromViewEvent Tests
public void preRemoveFromViewEventIsApproiateListenerPostiveTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter pw = response.getWriter();
    SystemEventListener testListener = new TestSystemEventListener();
    ComponentSystemEvent cse = createEvent(getTestComponent());
    String eventName = cse.getClass().getName();
    if (cse.isAppropriateListener(testListener)) {
        pw.println(JSFTestUtil.PASS);
    } else {
        pw.println("Test FAILED. " + eventName + ".isAppropriateListener " + "did not return true when SystemEventListener was passed in " + "as a parameter");
    }
}
Also used : TestSystemEventListener(com.sun.ts.tests.jsf.api.jakarta_faces.event.common.TestSystemEventListener) SystemEventListener(jakarta.faces.event.SystemEventListener) ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) PrintWriter(java.io.PrintWriter) TestSystemEventListener(com.sun.ts.tests.jsf.api.jakarta_faces.event.common.TestSystemEventListener)

Aggregations

ComponentSystemEvent (jakarta.faces.event.ComponentSystemEvent)9 UICommand (jakarta.faces.component.UICommand)4 Test (org.junit.Test)4 TestSystemEventListener (com.sun.ts.tests.jsf.api.jakarta_faces.event.common.TestSystemEventListener)2 UIOutput (jakarta.faces.component.UIOutput)2 SystemEventListener (jakarta.faces.event.SystemEventListener)2 PrintWriter (java.io.PrintWriter)2 FacesException (jakarta.faces.FacesException)1 UIForm (jakarta.faces.component.UIForm)1 UIPanel (jakarta.faces.component.UIPanel)1 UIViewRoot (jakarta.faces.component.UIViewRoot)1 UIWebsocket (jakarta.faces.component.UIWebsocket)1 FacesContext (jakarta.faces.context.FacesContext)1 PartialViewContext (jakarta.faces.context.PartialViewContext)1 NamedEvent (jakarta.faces.event.NamedEvent)1 PreRenderViewEvent (jakarta.faces.event.PreRenderViewEvent)1 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)1 Iterator (java.util.Iterator)1 IAnswer (org.easymock.IAnswer)1