Class UIUtilities


  • public final class UIUtilities
    extends java.lang.Object
    Miscellaneous UI related utility methods used from within the IJC code-base.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void centerDialog​(java.awt.Dialog dialog)
      Center given dialog in the main IJC application window.
      static void checkEDT()
      Ensures that current thread is the event-dispatching thread.
      static void checkNotEDT()
      Ensures that current thread is not the event-dispatching thread.
      static java.awt.Color colorFromHex​(java.lang.String color)  
      static java.lang.String convertForPaste​(java.lang.String input)
      Convert all non-space white chars to ',' inside input
      static java.awt.Color decorateTextPaneAsHyperlink​(javax.swing.JTextPane textPane, java.awt.Color inactiveColor, boolean asActive)
      Decorates (or undecorates) content of given textPane as traditional hypertext link.
      static <T extends org.openide.windows.TopComponent>
      T
      findTopComponent​(java.lang.Class<T> component, java.lang.String componentId, java.util.function.Supplier<T> defaultInstanceSupplier)
      Helper method for finding top component instance.
      static java.lang.Double getDoubleOrNull​(javax.swing.text.JTextComponent field)
      If the text in the given field contains valid real number it is returned as Double.
      static int getIntegerOrDefault​(javax.swing.text.JTextComponent field, java.lang.Integer def)
      If the text in the given field contains valid integer it is returned as Integer.
      static java.lang.Integer getIntegerOrNull​(javax.swing.text.JTextComponent field)
      If the text in the given field contains valid integer it is returned as Integer.
      static boolean hasAltModifier​(java.awt.event.MouseEvent e)  
      static boolean hasCtrlModifier​(java.awt.event.MouseEvent e)  
      static boolean hasMetaModifier​(java.awt.event.MouseEvent e)  
      static boolean hasShiftModifier​(java.awt.event.MouseEvent e)  
      static <T> T invokeAndWait​(java.util.concurrent.Callable<T> call)
      Behaves the same as SwingUtilities.invokeAndWait(java.lang.Runnable) but returns a value.
      static <T> T invokeAndWaitCheckAwt​(java.util.concurrent.Callable<T> call)
      Behaves the same as SwingUtilities.invokeAndWait(java.lang.Runnable) but returns a value.
      static void invokeInAwtOrLater​(java.lang.Runnable runnable)
      Just invokes given task immediately if the current thread is AWT event dispatching thread.
      static boolean isEmptyOrValidInteger​(javax.swing.text.JTextComponent textField)  
      static boolean isEmptyOrValidNumber​(javax.swing.text.JTextComponent textField)  
      static boolean isValidNumber​(javax.swing.text.JTextComponent textField)  
      static boolean isValidPositiveInteger​(javax.swing.text.JTextComponent textField)  
      static boolean isValidPositiveNumber​(javax.swing.text.JTextComponent textField)  
      static java.awt.Color mixColors​(java.awt.Color a, java.awt.Color b, float balance)
      Computes the colour between the given two colours according to balance (0.0-1.0).
      static void setEnabled​(boolean enabled, javax.swing.JComponent... components)
      Sets whether or not the given components are enabled.
      static void setVisible​(boolean visible, javax.swing.JComponent... components)
      Makes the components visible or invisible.
      static java.lang.Double toDoubleOrNull​(java.lang.String val)  
      static java.lang.Integer toIntegerOrNull​(java.lang.String val)  
      static void tryToSetOpacity​(java.awt.Window window, float opacity)
      Tries to set opacity for the given windows iff the underlying JRE supports it.
      • Methods inherited from class java.lang.Object

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

      • checkEDT

        public static void checkEDT()
        Ensures that current thread is the event-dispatching thread.
        Throws:
        java.lang.IllegalStateException - if the current thread is other then the EDT.
      • checkNotEDT

        public static void checkNotEDT()
        Ensures that current thread is not the event-dispatching thread.
        Throws:
        java.lang.IllegalStateException - if the current thread is the EDT.
      • invokeAndWaitCheckAwt

        public static <T> T invokeAndWaitCheckAwt​(java.util.concurrent.Callable<T> call)
                                           throws java.lang.Exception
        Behaves the same as SwingUtilities.invokeAndWait(java.lang.Runnable) but returns a value. The method checks if already in AWT and does not call it recursively.
        Type Parameters:
        T - a type of the returned value
        Parameters:
        call - a task to invoke
        Returns:
        returns a value of a task
        Throws:
        java.lang.InterruptedException
        java.lang.reflect.InvocationTargetException
        java.util.concurrent.ExecutionException - when execution of callable fails
        java.lang.Exception
      • invokeAndWait

        public static <T> T invokeAndWait​(java.util.concurrent.Callable<T> call)
                                   throws java.lang.InterruptedException,
                                          java.lang.reflect.InvocationTargetException,
                                          java.util.concurrent.ExecutionException
        Behaves the same as SwingUtilities.invokeAndWait(java.lang.Runnable) but returns a value.
        Type Parameters:
        T - a type of the returned value
        Parameters:
        call - a task to invoke
        Returns:
        returns a value of a task
        Throws:
        java.lang.InterruptedException - SwingUtilities.invokeAndWait(java.lang.Runnable)
        java.lang.reflect.InvocationTargetException - SwingUtilities.invokeAndWait(java.lang.Runnable)
        java.util.concurrent.ExecutionException - SwingUtilities.invokeAndWait(java.lang.Runnable)
      • invokeInAwtOrLater

        public static void invokeInAwtOrLater​(java.lang.Runnable runnable)
        Just invokes given task immediately if the current thread is AWT event dispatching thread. If not the task is rescheduled into the EDT with SwingUtilities.invokeLater(java.lang.Runnable).
        Parameters:
        runnable - task to be performed in EDT.
      • getDoubleOrNull

        public static java.lang.Double getDoubleOrNull​(javax.swing.text.JTextComponent field)
        If the text in the given field contains valid real number it is returned as Double. null otherwise.
        Parameters:
        field - JTextComponent containing text
        Returns:
        possibly double
      • toDoubleOrNull

        public static java.lang.Double toDoubleOrNull​(java.lang.String val)
        Since:
        6.2
      • getIntegerOrDefault

        public static int getIntegerOrDefault​(javax.swing.text.JTextComponent field,
                                              java.lang.Integer def)
        If the text in the given field contains valid integer it is returned as Integer. Default value otherwise.
        Parameters:
        field - JTextComponent containing text
        def - default value. Must be non-null.
        Returns:
        the integer or default value
      • getIntegerOrNull

        public static java.lang.Integer getIntegerOrNull​(javax.swing.text.JTextComponent field)
        If the text in the given field contains valid integer it is returned as Integer. null otherwise.
        Parameters:
        field - JTextComponent containing text
        Returns:
        possibly integer
      • toIntegerOrNull

        public static java.lang.Integer toIntegerOrNull​(java.lang.String val)
        Since:
        6.2
      • isEmptyOrValidInteger

        public static boolean isEmptyOrValidInteger​(javax.swing.text.JTextComponent textField)
      • isEmptyOrValidNumber

        public static boolean isEmptyOrValidNumber​(javax.swing.text.JTextComponent textField)
      • isValidNumber

        public static boolean isValidNumber​(javax.swing.text.JTextComponent textField)
      • isValidPositiveNumber

        public static boolean isValidPositiveNumber​(javax.swing.text.JTextComponent textField)
      • isValidPositiveInteger

        public static boolean isValidPositiveInteger​(javax.swing.text.JTextComponent textField)
      • hasCtrlModifier

        public static boolean hasCtrlModifier​(java.awt.event.MouseEvent e)
      • hasAltModifier

        public static boolean hasAltModifier​(java.awt.event.MouseEvent e)
      • hasShiftModifier

        public static boolean hasShiftModifier​(java.awt.event.MouseEvent e)
      • hasMetaModifier

        public static boolean hasMetaModifier​(java.awt.event.MouseEvent e)
      • setEnabled

        public static void setEnabled​(boolean enabled,
                                      javax.swing.JComponent... components)
        Sets whether or not the given components are enabled.
        Parameters:
        enabled - true if this component should be enabled, false otherwise
        components - the components
        See Also:
        JComponent.setEnabled(boolean)
      • setVisible

        public static void setVisible​(boolean visible,
                                      javax.swing.JComponent... components)
        Makes the components visible or invisible.
        Parameters:
        visible - true to make the component visible; false to make it invisible
        components - the components
        See Also:
        JComponent.setVisible(boolean)
      • centerDialog

        public static void centerDialog​(java.awt.Dialog dialog)
        Center given dialog in the main IJC application window.
        Parameters:
        dialog - centered dialog.
      • colorFromHex

        public static java.awt.Color colorFromHex​(java.lang.String color)
      • mixColors

        public static java.awt.Color mixColors​(java.awt.Color a,
                                               java.awt.Color b,
                                               float balance)
        Computes the colour between the given two colours according to balance (0.0-1.0). Luminosity contrast check is used for resulting color and given color 'b'. If contrast is too small, color is adjusted in a direction where's more color space to go.

        The implementation uses well known algorithm. Search the Internet for luminosity contrast ratio algorithm to get more detailed information.

      • tryToSetOpacity

        public static void tryToSetOpacity​(java.awt.Window window,
                                           float opacity)
        Tries to set opacity for the given windows iff the underlying JRE supports it. This is OS and JRE version dependent. Translucency support was added in JRE 6.0u10 as a non-public API, available via AWTUtilities#setWindowOpacity method, which is accessed by this method via reflection.

        This method will be removed when IJC switches to Java 7 where translucency is available via official public APIs.

        Parameters:
        window - target windows
        opacity - the opacity level to set for the window
      • convertForPaste

        public static java.lang.String convertForPaste​(java.lang.String input)
        Convert all non-space white chars to ',' inside input
        Parameters:
        input - The s tring to paste
        Returns:
        The comma-separated list of values
        Since:
        6.2
      • findTopComponent

        public static <T extends org.openide.windows.TopComponent> T findTopComponent​(java.lang.Class<T> component,
                                                                                      java.lang.String componentId,
                                                                                      java.util.function.Supplier<T> defaultInstanceSupplier)
        Helper method for finding top component instance.
        Type Parameters:
        T - Type of the component
        Parameters:
        component - Class of the component
        componentId - Component id
        defaultInstanceSupplier - Supplier of a new component instance
        Returns:
        component instance, either found or new
        Throws:
        java.lang.RuntimeException - error occurred while searching top component in EDT thread
      • decorateTextPaneAsHyperlink

        public static java.awt.Color decorateTextPaneAsHyperlink​(javax.swing.JTextPane textPane,
                                                                 java.awt.Color inactiveColor,
                                                                 boolean asActive)
        Decorates (or undecorates) content of given textPane as traditional hypertext link.
        Parameters:
        textPane - component to decorate
        inactiveColor - default font color when hyperlink should be inactive
        asActive - true if display hyperlink as active
        Returns:
        original font color of given textPane. Can be null.
        Since:
        20.14.0