feat: add getUI() to ComponentEvent#24205
Conversation
`ComponentEvent` exposes the source component but offers no direct way to reach its UI. Listeners that need the UI must dereference an `Optional` via `event.getSource().getUI().ifPresent(...)`, which is verbose and silently swallows the case where the source is detached. Add `ComponentEvent.getUI()` returning `UI` directly. When the source is itself a `UI`, it is returned as-is; otherwise the method returns the resolved UI or throws `IllegalStateException` with a message pointing to `getSource().getUI()` for code that must handle a detached source. This matches the convention already used by `AbstractAttachDetachEvent`, `UIInitEvent`, `BeforeEvent` and similar event types. Fixes #18818
|
Adding It is source-incompatible in one narrow case: a subclass of |
|
|
I wonder if the implementation is now a similar trap as the Component.getUI currently is? So if I start some call sequence that completes in a separate thread, calling getUI may be unsafe again IIRC🤔 Not 100% sure, but this is the reason why I didn't proceed with a PR. My guess originally was that UI instance should be saved when creating an event instance. |
|
Saving the UI instance in the event is the best approach, indeed. But this needs a refactoring of all code that creates event instances, since there's not a single point in the framework where you can do it. |



ComponentEventexposes the source component but offers no direct way to reach its UI. Listeners that need the UI must dereference anOptionalviaevent.getSource().getUI().ifPresent(...), which is verbose and silently swallows the case where the source is detached.Add
ComponentEvent.getUI()returningUIdirectly. When the source is itself aUI, it is returned as-is; otherwise the method returns the resolved UI or throwsIllegalStateExceptionwith a message pointing togetSource().getUI()for code that must handle a detached source. This matches the convention already used byAbstractAttachDetachEvent,UIInitEvent,BeforeEventand similar event types.Fixes #18818