Package org.omnifaces.util
Class Events
- java.lang.Object
-
- org.omnifaces.util.Events
-
public final class Events extends Object
Collection of utility methods for the Faces API with respect to working with system and phase events.
Usage
Some examples:
// Add a callback to the current view which should run during every after phase of the render response on same view. Events.subscribeToViewAfterPhase(PhaseId.RENDER_RESPONSE, () -> { // ... });
// Add a callback to the current request which should run during before phase of the render response on current request. Events.subscribeToRequestBeforePhase(PhaseId.RENDER_RESPONSE, () -> { // ... });
// Add a callback to the current view which should run during the pre render view event. Events.subscribeToViewEvent(PreRenderViewEvent.class, () -> { // ... });
Note that you can specify any phase ID or system event to your choice.
- Author:
- Arjan Tijms, Bauke Scholtz
- See Also:
CallbackPhaseListener
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addRequestPhaseListener(PhaseListener listener)
Adds the given phase listener to the current request.static void
addViewPhaseListener(PhaseListener listener)
Adds the given phase listener to the current view.static void
subscribeToApplicationEvent(Class<? extends SystemEvent> type, SystemEventListener listener)
Subscribe the given system event listener to the current application that get invoked every time when the given system event type is published in the current application.static void
subscribeToApplicationEvent(Class<? extends SystemEvent> type, Callback.SerializableVoid callback)
Subscribe the given callback to the current application that get invoked every time when the given system event type is published in the current application.static void
subscribeToApplicationEvent(Class<? extends SystemEvent> type, Callback.SerializableWithArgument<SystemEvent> callback)
Subscribe the given callback to the current application that get invoked every time when the given system event type is published in the current application.static void
subscribeToRequestAfterPhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current request that get invoked after given phase ID.static void
subscribeToRequestAfterPhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current request that get invoked after given phase ID.static void
subscribeToRequestBeforePhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current request that get invoked before given phase ID.static void
subscribeToRequestBeforePhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current request that get invoked before given phase ID.static void
subscribeToRequestComponentEvent(UIComponent component, Class<? extends ComponentSystemEvent> type, Consumer<ComponentSystemEvent> callback)
Subscribe the given callback instance to the given component that get invoked only in the current request when the given component system event type is published on the given component.static void
subscribeToViewAfterPhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current view that get invoked every time after given phase ID.static void
subscribeToViewAfterPhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current view that get invoked every time after given phase ID.static void
subscribeToViewBeforePhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current view that get invoked every time before given phase ID.static void
subscribeToViewBeforePhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current view that get invoked every time before given phase ID.static void
subscribeToViewEvent(Class<? extends SystemEvent> type, SystemEventListener listener)
Subscribe the given system event listener to the current view that get invoked every time when the given system event type is published on the current view.static void
subscribeToViewEvent(Class<? extends SystemEvent> type, Callback.SerializableVoid callback)
Subscribe the given callback to the current view that get invoked every time when the given system event type is published on the current view.static void
subscribeToViewEvent(Class<? extends SystemEvent> type, Callback.SerializableWithArgument<SystemEvent> callback)
Subscribe the given callback to the current view that get invoked every time when the given system event type is published on the current view.static void
unsubscribeFromComponentEvent(UIComponent component, Class<? extends SystemEvent> event, ComponentSystemEventListener listener)
Unsubscribe the given event listener on the given event from the given component.
-
-
-
Method Detail
-
subscribeToApplicationEvent
public static void subscribeToApplicationEvent(Class<? extends SystemEvent> type, SystemEventListener listener)
Subscribe the given system event listener to the current application that get invoked every time when the given system event type is published in the current application.- Parameters:
type
- The system event type to be observed.listener
- The system event listener to be subscribed.- Since:
- 2.0
- See Also:
Application.subscribeToEvent(Class, SystemEventListener)
-
subscribeToApplicationEvent
public static void subscribeToApplicationEvent(Class<? extends SystemEvent> type, Callback.SerializableVoid callback)
Subscribe the given callback to the current application that get invoked every time when the given system event type is published in the current application.- Parameters:
type
- The system event type to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
subscribeToApplicationEvent(Class, SystemEventListener)
-
subscribeToApplicationEvent
public static void subscribeToApplicationEvent(Class<? extends SystemEvent> type, Callback.SerializableWithArgument<SystemEvent> callback)
Subscribe the given callback to the current application that get invoked every time when the given system event type is published in the current application.- Parameters:
type
- The system event type to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
subscribeToApplicationEvent(Class, SystemEventListener)
-
subscribeToViewEvent
public static void subscribeToViewEvent(Class<? extends SystemEvent> type, SystemEventListener listener)
Subscribe the given system event listener to the current view that get invoked every time when the given system event type is published on the current view.- Parameters:
type
- The system event type to be observed.listener
- The system event listener to be subscribed.- Since:
- 1.2
- See Also:
UIViewRoot.subscribeToViewEvent(Class, SystemEventListener)
-
subscribeToViewEvent
public static void subscribeToViewEvent(Class<? extends SystemEvent> type, Callback.SerializableVoid callback)
Subscribe the given callback to the current view that get invoked every time when the given system event type is published on the current view.- Parameters:
type
- The system event type to be observed.callback
- The callback to be invoked.- Since:
- 1.2
- See Also:
subscribeToViewEvent(Class, SystemEventListener)
-
subscribeToViewEvent
public static void subscribeToViewEvent(Class<? extends SystemEvent> type, Callback.SerializableWithArgument<SystemEvent> callback)
Subscribe the given callback to the current view that get invoked every time when the given system event type is published on the current view.- Parameters:
type
- The system event type to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
subscribeToViewEvent(Class, SystemEventListener)
-
addViewPhaseListener
public static void addViewPhaseListener(PhaseListener listener)
Adds the given phase listener to the current view. The difference withaddRequestPhaseListener(PhaseListener)
is that the given phase listener is invoked during every (postback) request on the same view instead of only during the current request.- Parameters:
listener
- The phase listener to be added to the current view.- Since:
- 2.0
- See Also:
UIViewRoot.addPhaseListener(PhaseListener)
-
subscribeToViewBeforePhase
public static void subscribeToViewBeforePhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current view that get invoked every time before given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addViewPhaseListener(PhaseListener)
-
subscribeToViewBeforePhase
public static void subscribeToViewBeforePhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current view that get invoked every time before given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addViewPhaseListener(PhaseListener)
-
subscribeToViewAfterPhase
public static void subscribeToViewAfterPhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current view that get invoked every time after given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addViewPhaseListener(PhaseListener)
-
subscribeToViewAfterPhase
public static void subscribeToViewAfterPhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current view that get invoked every time after given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addViewPhaseListener(PhaseListener)
-
subscribeToRequestComponentEvent
public static void subscribeToRequestComponentEvent(UIComponent component, Class<? extends ComponentSystemEvent> type, Consumer<ComponentSystemEvent> callback)
Subscribe the given callback instance to the given component that get invoked only in the current request when the given component system event type is published on the given component. The difference withUIComponent.subscribeToEvent(Class, ComponentSystemEventListener)
is that this listener is request scoped instead of view scoped as component system event listeners are by default saved in Faces state and thus inherently view scoped.- Parameters:
component
- The component to subscribe the given callback instance to.type
- The system event type to be observed.callback
- The callback to be invoked.- Since:
- 2.1
- See Also:
UIComponent.subscribeToEvent(Class, ComponentSystemEventListener)
,unsubscribeFromComponentEvent(UIComponent, Class, ComponentSystemEventListener)
-
addRequestPhaseListener
public static void addRequestPhaseListener(PhaseListener listener)
Adds the given phase listener to the current request. The difference withaddViewPhaseListener(PhaseListener)
is that the given phase listener is invoked only during the current request instead of during every (postback) request on the same view.- Parameters:
listener
- The phase listener to be added to the current request.- Since:
- 2.0
- See Also:
CallbackPhaseListener
-
subscribeToRequestBeforePhase
public static void subscribeToRequestBeforePhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current request that get invoked before given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addRequestPhaseListener(PhaseListener)
-
subscribeToRequestBeforePhase
public static void subscribeToRequestBeforePhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current request that get invoked before given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addRequestPhaseListener(PhaseListener)
-
subscribeToRequestAfterPhase
public static void subscribeToRequestAfterPhase(PhaseId phaseId, Runnable callback)
Subscribe the given callback instance to the current request that get invoked after given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addRequestPhaseListener(PhaseListener)
-
subscribeToRequestAfterPhase
public static void subscribeToRequestAfterPhase(PhaseId phaseId, Consumer<PhaseEvent> callback)
Subscribe the given callback instance to the current request that get invoked after given phase ID.- Parameters:
phaseId
- The phase ID to be observed.callback
- The callback to be invoked.- Since:
- 2.0
- See Also:
addRequestPhaseListener(PhaseListener)
-
unsubscribeFromComponentEvent
public static void unsubscribeFromComponentEvent(UIComponent component, Class<? extends SystemEvent> event, ComponentSystemEventListener listener)
Unsubscribe the given event listener on the given event from the given component. Normally, you would useUIComponent.unsubscribeFromEvent(Class, ComponentSystemEventListener)
for this, but this wouldn't work when executed insideComponentSystemEventListener.processEvent(jakarta.faces.event.ComponentSystemEvent)
, as it would otherwise end up in aConcurrentModificationException
while Faces is iterating over all system event listeners. The trick is to perform the unsubscribe during the after phase of the current request phasesubscribeToRequestAfterPhase(PhaseId, Runnable)
.- Parameters:
component
- The component to unsubscribe the given event listener from.event
- The event associated with the given event listener.listener
- The event listener to be unsubscribed from the given component.- Throws:
IllegalStateException
- When this method is invoked during render response phase, because it would be too late to remove it from the view state.- Since:
- 2.1
- See Also:
subscribeToRequestAfterPhase(PhaseId, Runnable)
,UIComponent.unsubscribeFromEvent(Class, ComponentSystemEventListener)
-
-