Class SwingUtilities

java.lang.Object
vaadinx.swing.SwingUtilities
All Implemented Interfaces:
SwingConstants

public final class SwingUtilities extends Object implements SwingConstants
Emulator for SwingUtilities. Splits the JDK surface three ways:
  • EDT primitives — invokeLater(Runnable), invokeAndWait(Runnable), isEventDispatchThread() bottom out in UI.getCurrent(), the Vaadin UI-thread proxy.
  • Tree walks — ancestor / root-pane / window / class-named lookups walk our emulator parent chain (Component.getParent()) and resolve cleanly. The Rectangle math overloads (computeIntersection / Union / Difference, isRectangleContainingRectangle) and the mouse-button bit checks (isLeftMouseButton et al.) are pure value-math and behave exactly as the JDK implementation.
  • Pixel / paint / accessibility / L&F surface — coordinate conversions, deepest-component-at, paint, layoutCompoundLabel, FontMetrics-based string width, accessibility queries, processKeyBindings / notifyAction key dispatch, UI input/action map plumbing, viewport wrapper helpers, focus-owner walk, and updateComponentTreeUI all WARN via EHelper.onUnimplemented(String, String, Object...) and return safe defaults. These pieces of the JDK surface depend on machinery we don't have (a Graphics pipeline, a FontMetrics implementation, an L&F dispatcher, the AWT focus-traversal engine), so we surface the gap in the migration log instead of fabricating answers.

"EDT" maps onto "Vaadin UI thread context with the session lock." The proxy is UI.getCurrent() != null: every legitimate Swing-side codepath enters with a current UI (peer event listeners, timer fires, worker process/done callbacks all funnel through EHelper.callSwing(Runnable) which sets the UI). A background thread the user spawned themselves has no current UI and so reports "not on EDT" — matching Swing's model.

From a background thread, invokeLater(Runnable) / invokeAndWait(Runnable) reach the EDT through the thread's EmulatorContext: the runnable is delivered to the session's live UI the way a Timer fire or a SwingWorker.done() is (EHelper.onSessionLiveUI(VaadinSession, Runnable)). The context carries the session but never makes a UI current, so these calls cannot resolve the EDT from UI.getCurrent() there. A thread with no context has no session to deliver to and throws IllegalStateException (D_invoke_from_background).

  • Method Details

    • isEventDispatchThread

      public static boolean isEventDispatchThread()
      Returns:
      true when called from a Vaadin UI thread context.
    • invokeLater

      public static void invokeLater(Runnable runnable)
      Posts the runnable to the Vaadin UI thread asynchronously. Runs inside a EHelper.callSwing(Runnable) virtual-thread continuation so the runnable can park on blocking dialogs (R_callswing_envelope envelope around every Swing-side event handler).

      From a background thread the runnable is dropped, silently, when the session has no live UI left — a closed tab has no EDT to run it on.

      Throws:
      IllegalStateException - on a thread with neither a current UI nor an EmulatorContext — see class doc.
    • invokeAndWait

      public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException
      Posts the runnable to the Vaadin UI thread and blocks the calling thread until it has run. Throws IllegalStateException when called from the EDT itself — under Swing this would deadlock (D_gap_severity_triage case 2: loom rescue trigger). Migrators surfaced by this throw should either move the call off the EDT or use invokeLater(Runnable) for fire-and-forget dispatch.

      "Has run" includes a modal dialog the runnable opens: the call returns once the user has answered it, as on the desktop.

      Throws:
      IllegalStateException - when called from the EDT, or on a thread with no EmulatorContext — see class doc.
      InvocationTargetException - if the runnable throws.
      InterruptedException - if the wait is interrupted.
      BrowserSessionClosedError - if the session has no live UI to run the runnable on, or loses it while the runnable waits on a modal dialog — nothing would ever end the wait (R_match_swing_errors case (8))
    • getWindowAncestor

      public static Window getWindowAncestor(Component c)
      Walks c's emulator parent chain and returns the first Window ancestor (excluding c itself), or null if none. Mirrors SwingUtilities.getWindowAncestor(Component).
    • windowForComponent

      public static Window windowForComponent(Component c)
      Alias for getWindowAncestor(Component) — same JDK contract.
    • getRootPane

      public static JRootPane getRootPane(Component c)
      Returns the JRootPane that contains c, or null if none — the JDK's own body, reachable because RootPaneContainer is ported (D_hierarchy_parity). The container test comes first and covers c itself, so getRootPane(frame) answers the frame's root pane rather than walking past it.
    • getRoot

      public static Component getRoot(Component c)
      Returns the topmost ancestor of c: either the first Window ancestor or, if none, the topmost ancestor with no parent. c itself is returned when it has no parent.
    • isDescendingFrom

      public static boolean isDescendingFrom(Component a, Component b)
      Returns:
      true if a is b or a descendant of b in the emulator parent chain.
    • getAncestorOfClass

      public static Container getAncestorOfClass(Class<?> clazz, Component c)
      Walks c's parent chain (starting at c.getParent()) and returns the first ancestor whose runtime class is assignable to clazz, or null if none.
    • getAncestorNamed

      public static Container getAncestorNamed(String name, Component c)
      Walks c's parent chain (starting at c.getParent()) and returns the first ancestor whose name matches name, or null if none.
    • getUnwrappedParent

      public static Container getUnwrappedParent(Component c)
      JDK contract: returns parent unwrapped through a JViewport. We don't emulate JViewport (Vaadin scrolling doesn't go through a viewport peer), so this just returns Component.getParent().
    • isLeftMouseButton

      public static boolean isLeftMouseButton(MouseEvent e)
    • isMiddleMouseButton

      public static boolean isMiddleMouseButton(MouseEvent e)
    • isRightMouseButton

      public static boolean isRightMouseButton(MouseEvent e)
    • isRectangleContainingRectangle

      public static boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)
    • computeIntersection

      public static Rectangle computeIntersection(int x, int y, int width, int height, Rectangle dest)
    • computeUnion

      public static Rectangle computeUnion(int x, int y, int width, int height, Rectangle dest)
    • computeDifference

      public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)
    • getLocalBounds

      public static Rectangle getLocalBounds(Component c)
    • convertPoint

      public static Point convertPoint(Component source, Point aPoint, Component destination)
    • convertPoint

      public static Point convertPoint(Component source, int x, int y, Component destination)
    • convertRectangle

      public static Rectangle convertRectangle(Component source, Rectangle aRectangle, Component destination)
    • convertMouseEvent

      public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
    • convertPointToScreen

      public static void convertPointToScreen(Point p, Component c)
    • convertPointFromScreen

      public static void convertPointFromScreen(Point p, Component c)
    • getDeepestComponentAt

      public static Component getDeepestComponentAt(Component parent, int x, int y)
    • calculateInnerArea

      public static Rectangle calculateInnerArea(JComponent c, Rectangle r)
    • findFocusOwner

      public static Component findFocusOwner(Component c)
    • computeStringWidth

      public static int computeStringWidth(FontMetrics fm, String str)
    • layoutCompoundLabel

      public static String layoutCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)
    • layoutCompoundLabel

      public static String layoutCompoundLabel(FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)
    • paintComponent

      public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)
    • paintComponent

      public static void paintComponent(Graphics g, Component c, Container p, Rectangle r)
    • updateComponentTreeUI

      public static void updateComponentTreeUI(Component c)
      L&F dispatch is permanently deferred per R_match_swing_errors sub-bucket (b) — the styling story lives in a later Vaadin+CSS pass. We WARN so migration logs surface the call site, but make no peer changes.
    • processKeyBindings

      public static boolean processKeyBindings(KeyEvent event)
    • notifyAction

      public static boolean notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)
    • replaceUIInputMap

      public static void replaceUIInputMap(JComponent c, int condition, InputMap map)
    • replaceUIActionMap

      public static void replaceUIActionMap(JComponent c, ActionMap map)
    • getUIInputMap

      public static InputMap getUIInputMap(JComponent c, int condition)
    • getUIActionMap

      public static ActionMap getUIActionMap(JComponent c)
    • getAccessibleIndexInParent

      public static int getAccessibleIndexInParent(Component c)
    • getAccessibleAt

      public static Accessible getAccessibleAt(Component c, Point p)
    • getAccessibleStateSet

      public static AccessibleStateSet getAccessibleStateSet(Component c)
    • getAccessibleChildrenCount

      public static int getAccessibleChildrenCount(Component c)
    • getAccessibleChild

      public static Accessible getAccessibleChild(Component c, int i)