Class GuiHelper


  • public final class GuiHelper
    extends java.lang.Object
    basic gui utils
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GuiHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertCallFromEdt()
      This function fails if it was not called from the EDT thread.
      static void destroyComponents​(java.awt.Component component, boolean destroyItself)
      Destroys recursively all Destroyable components of a given container, and optionnally the container itself.
      static javax.swing.JScrollPane embedInVerticalScrollPane​(java.awt.Component panel)
      Embeds the given component into a new vertical-only scrollable JScrollPane.
      static void executeByMainWorkerInEDT​(java.lang.Runnable task)
      Add a task to the main worker that will block the worker and run in the GUI thread.
      static void extendTooltipDelay​(java.awt.Component c)
      Extends tooltip dismiss delay to a default value of 1 minute for the given component.
      static void extendTooltipDelay​(java.awt.Component c, int delay)
      Extends tooltip dismiss delay to the specified value for the given component.
      static java.awt.Stroke getCustomizedStroke​(java.lang.String code)
      Return s new BasicStroke object with given thickness and style
      static javax.swing.ImageIcon getDisabledIcon​(javax.swing.ImageIcon icon)
      Replies the disabled (grayed) version of the specified icon.
      static java.awt.Image getDisabledImage​(java.awt.Image image)
      Replies the disabled (grayed) version of the specified image.
      static java.awt.Frame getFrameForComponent​(java.awt.Component parentComponent)
      Returns the specified component's Frame without throwing exception in headless mode.
      static java.awt.Dimension getMaximumScreenSize()
      Gets the size of the screen.
      static java.awt.Font getMonospacedFont​(javax.swing.JComponent component)
      Gets the font used to display monospaced text in a component, if possible.
      static java.awt.Dimension getScreenSize()
      Gets the size of the screen.
      static java.awt.Font getTitleFont()
      Gets the font used to display JOSM title in about dialog and splash screen.
      static java.awt.Window getWindowAncestorFor​(java.util.EventObject e)
      Returns the first Window ancestor of event source, or null if event source is not a component contained inside a Window.
      (package private) static void handleEDTException​(java.lang.Throwable t)
      Handle exceptions in the EDT.
      static void notifyUserHtmlError​(java.awt.Component parent, java.lang.String title, java.lang.String message, java.lang.String html)
      Notifies user about an error received from an external source as an HTML page.
      static java.awt.Component prepareResizeableOptionPane​(java.awt.Component pane, java.awt.Dimension minDimension)
      Attaches a HierarchyListener to the specified Component that will set its parent dialog resizeable.
      static void runInEDT​(java.lang.Runnable task)
      Executes asynchronously a runnable in Event Dispatch Thread, except if we're already in the EDT: in this case the runnable is executed synchronously.
      static void runInEDTAndWait​(java.lang.Runnable task)
      Executes synchronously a runnable in Event Dispatch Thread.
      static <V> V runInEDTAndWaitAndReturn​(java.util.concurrent.Callable<V> callable)
      Executes synchronously a callable in Event Dispatch Thread and return a value.
      static void runInEDTAndWaitWithException​(java.lang.Runnable task)
      Executes synchronously a runnable in Event Dispatch Thread.
      static javax.swing.Timer scheduleTimer​(int initialDelay, java.awt.event.ActionListener actionListener, boolean repeats)
      Schedules a new Timer to be run in the future (once or several times).
      static void setBackgroundReadable​(javax.swing.JComponent c, java.awt.Color background)
      Sets the background color for this component, and adjust the foreground color so the text remains readable.
      static javax.swing.JScrollPane setDefaultIncrement​(javax.swing.JScrollPane sp)
      Set the default unit increment for a JScrollPane.
      static void setEnabledRec​(java.awt.Container root, boolean enabled)
      disable / enable a component and all its child components
      static void setUIFont​(java.lang.String name)
      Sets a global font for all UI, replacing default font of current look and feel.
      static void setupLanguageFonts()
      Setup special font for Khmer script, as the default Java fonts do not display these characters.
      static void translateJavaInternalMessages()
      Localizations for file chooser dialog.
      static boolean warnUser​(java.lang.String title, java.lang.String content, javax.swing.ImageIcon baseActionIcon, java.lang.String continueToolTip)
      Warns user about a dangerous action requiring confirmation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • setEnabledRec

        public static void setEnabledRec​(java.awt.Container root,
                                         boolean enabled)
        disable / enable a component and all its child components
        Parameters:
        root - component
        enabled - enabled state
      • executeByMainWorkerInEDT

        public static void executeByMainWorkerInEDT​(java.lang.Runnable task)
        Add a task to the main worker that will block the worker and run in the GUI thread.
        Parameters:
        task - The task to run
      • runInEDT

        public static void runInEDT​(java.lang.Runnable task)
        Executes asynchronously a runnable in Event Dispatch Thread, except if we're already in the EDT: in this case the runnable is executed synchronously.
        Parameters:
        task - The runnable to execute
        See Also:
        SwingUtilities.invokeLater(java.lang.Runnable)
      • handleEDTException

        static void handleEDTException​(java.lang.Throwable t)
        Handle exceptions in the EDT. This should only be used in GuiHelper and org.openstreetmap.josm.testutils.mockers.EDTAssertionMocker.
        Parameters:
        t - The throwable to handle
      • runInEDTAndWait

        public static void runInEDTAndWait​(java.lang.Runnable task)
        Executes synchronously a runnable in Event Dispatch Thread.
        Parameters:
        task - The runnable to execute
        See Also:
        SwingUtilities.invokeAndWait(java.lang.Runnable)
      • runInEDTAndWaitWithException

        public static void runInEDTAndWaitWithException​(java.lang.Runnable task)
        Executes synchronously a runnable in Event Dispatch Thread.

        Passes on the exception that was thrown to the thread calling this. The exception is wrapped using a ReportedException.

        Parameters:
        task - The runnable to execute
        Since:
        10271
        See Also:
        SwingUtilities.invokeAndWait(java.lang.Runnable)
      • runInEDTAndWaitAndReturn

        public static <V> V runInEDTAndWaitAndReturn​(java.util.concurrent.Callable<V> callable)
        Executes synchronously a callable in Event Dispatch Thread and return a value.
        Type Parameters:
        V - the result type of method call
        Parameters:
        callable - The callable to execute
        Returns:
        The computed result
        Since:
        7204
      • assertCallFromEdt

        public static void assertCallFromEdt()
        This function fails if it was not called from the EDT thread.
        Throws:
        java.lang.IllegalStateException - if called from wrong thread.
        Since:
        10271
      • warnUser

        public static boolean warnUser​(java.lang.String title,
                                       java.lang.String content,
                                       javax.swing.ImageIcon baseActionIcon,
                                       java.lang.String continueToolTip)
        Warns user about a dangerous action requiring confirmation.
        Parameters:
        title - Title of dialog
        content - Content of dialog
        baseActionIcon - Unused? FIXME why is this parameter unused?
        continueToolTip - Tooltip to display for "continue" button
        Returns:
        true if the user wants to cancel, false if they want to continue
      • notifyUserHtmlError

        public static void notifyUserHtmlError​(java.awt.Component parent,
                                               java.lang.String title,
                                               java.lang.String message,
                                               java.lang.String html)
        Notifies user about an error received from an external source as an HTML page.
        Parameters:
        parent - Parent component
        title - Title of dialog
        message - Message displayed at the top of the dialog
        html - HTML content to display (real error message)
        Since:
        7312
      • getDisabledImage

        public static java.awt.Image getDisabledImage​(java.awt.Image image)
        Replies the disabled (grayed) version of the specified image.
        Parameters:
        image - The image to disable
        Returns:
        The disabled (grayed) version of the specified image, brightened by 20%.
        Since:
        5484
      • getDisabledIcon

        public static javax.swing.ImageIcon getDisabledIcon​(javax.swing.ImageIcon icon)
        Replies the disabled (grayed) version of the specified icon.
        Parameters:
        icon - The icon to disable
        Returns:
        The disabled (grayed) version of the specified icon, brightened by 20%.
        Since:
        5484
      • prepareResizeableOptionPane

        public static java.awt.Component prepareResizeableOptionPane​(java.awt.Component pane,
                                                                     java.awt.Dimension minDimension)
        Attaches a HierarchyListener to the specified Component that will set its parent dialog resizeable. Use it before a call to JOptionPane#showXXXXDialog to make it resizeable.
        Parameters:
        pane - The component that will be displayed
        minDimension - The minimum dimension that will be set for the dialog. Ignored if null
        Returns:
        pane
        Since:
        5493
      • scheduleTimer

        public static javax.swing.Timer scheduleTimer​(int initialDelay,
                                                      java.awt.event.ActionListener actionListener,
                                                      boolean repeats)
        Schedules a new Timer to be run in the future (once or several times).
        Parameters:
        initialDelay - milliseconds for the initial and between-event delay if repeatable
        actionListener - an initial listener; can be null
        repeats - specify false to make the timer stop after sending its first action event
        Returns:
        The (started) timer.
        Since:
        5735
      • getCustomizedStroke

        public static java.awt.Stroke getCustomizedStroke​(java.lang.String code)
        Return s new BasicStroke object with given thickness and style
        Parameters:
        code - = 3.5 → thickness=3.5px; 3.5 10 5 → thickness=3.5px, dashed: 10px filled + 5px empty
        Returns:
        stroke for drawing
        See Also:
        StrokeProperty
      • getMonospacedFont

        public static java.awt.Font getMonospacedFont​(javax.swing.JComponent component)
        Gets the font used to display monospaced text in a component, if possible.
        Parameters:
        component - The component
        Returns:
        the font used to display monospaced text in a component, if possible
        Since:
        7896
      • getTitleFont

        public static java.awt.Font getTitleFont()
        Gets the font used to display JOSM title in about dialog and splash screen.
        Returns:
        title font
        Since:
        5797
      • embedInVerticalScrollPane

        public static javax.swing.JScrollPane embedInVerticalScrollPane​(java.awt.Component panel)
        Embeds the given component into a new vertical-only scrollable JScrollPane.
        Parameters:
        panel - The component to embed
        Returns:
        the vertical scrollable JScrollPane
        Since:
        6666
      • setDefaultIncrement

        public static javax.swing.JScrollPane setDefaultIncrement​(javax.swing.JScrollPane sp)
        Set the default unit increment for a JScrollPane. This fixes slow mouse wheel scrolling when the content of the JScrollPane is a JPanel or other component that does not implement the Scrollable interface. The default unit increment is 1 pixel. Multiplied by the number of unit increments per mouse wheel "click" (platform dependent, usually 3), this makes a very sluggish mouse wheel experience. This methods sets the unit increment to a larger, more reasonable value.
        Parameters:
        sp - the scroll pane
        Returns:
        the scroll pane (same object) with fixed unit increment
        Throws:
        java.lang.IllegalArgumentException - if the component inside of the scroll pane implements the Scrollable interface (JTree, JLayer, JList, JTextComponent and JTable)
      • setUIFont

        public static void setUIFont​(java.lang.String name)
        Sets a global font for all UI, replacing default font of current look and feel.
        Parameters:
        name - Font name. It is up to the caller to make sure the font exists
        Throws:
        java.lang.IllegalArgumentException - if name is null
        Since:
        7896
      • setBackgroundReadable

        public static void setBackgroundReadable​(javax.swing.JComponent c,
                                                 java.awt.Color background)
        Sets the background color for this component, and adjust the foreground color so the text remains readable.
        Parameters:
        c - component
        background - background color
        Since:
        9223
      • getScreenSize

        public static java.awt.Dimension getScreenSize()
        Gets the size of the screen. On systems with multiple displays, the primary display is used. This method returns always 800x600 in headless mode (useful for unit tests).
        Returns:
        the size of this toolkit's screen, in pixels, or 800x600
        Since:
        9576
        See Also:
        Toolkit.getScreenSize()
      • getMaximumScreenSize

        public static java.awt.Dimension getMaximumScreenSize()
        Gets the size of the screen. On systems with multiple displays, contrary to getScreenSize(), the biggest display is used. This method returns always 800x600 in headless mode (useful for unit tests).
        Returns:
        the size of maximum screen, in pixels, or 800x600
        Since:
        10470
        See Also:
        Toolkit.getScreenSize()
      • getWindowAncestorFor

        public static java.awt.Window getWindowAncestorFor​(java.util.EventObject e)
        Returns the first Window ancestor of event source, or null if event source is not a component contained inside a Window.
        Parameters:
        e - event object
        Returns:
        a Window, or null
        Since:
        9916
      • extendTooltipDelay

        public static void extendTooltipDelay​(java.awt.Component c)
        Extends tooltip dismiss delay to a default value of 1 minute for the given component.
        Parameters:
        c - component
        Since:
        10024
      • extendTooltipDelay

        public static void extendTooltipDelay​(java.awt.Component c,
                                              int delay)
        Extends tooltip dismiss delay to the specified value for the given component.
        Parameters:
        c - component
        delay - tooltip dismiss delay in milliseconds
        Since:
        10024
        See Also:
        http://stackoverflow.com/a/6517902/2257172
      • getFrameForComponent

        public static java.awt.Frame getFrameForComponent​(java.awt.Component parentComponent)
        Returns the specified component's Frame without throwing exception in headless mode.
        Parameters:
        parentComponent - the Component to check for a Frame
        Returns:
        the Frame that contains the component, or getRootFrame if the component is null, or does not have a valid Frame parent
        Since:
        10035
        See Also:
        JOptionPane.getFrameForComponent(java.awt.Component), GraphicsEnvironment.isHeadless()
      • translateJavaInternalMessages

        public static void translateJavaInternalMessages()
        Localizations for file chooser dialog. For some locales (e.g. de, fr) translations are provided by Java, but not for others (e.g. ru, uk).
        Since:
        12644 (moved from I18n)
      • setupLanguageFonts

        public static void setupLanguageFonts()
        Setup special font for Khmer script, as the default Java fonts do not display these characters.
        Since:
        12644 (moved from I18n), 8282
      • destroyComponents

        public static void destroyComponents​(java.awt.Component component,
                                             boolean destroyItself)
        Destroys recursively all Destroyable components of a given container, and optionnally the container itself.
        Parameters:
        component - the component to destroy
        destroyItself - whether to destroy the component itself
        Since:
        14463