Class Utilities


  • public final class Utilities
    extends Object
    A utility class for UI related things.
    • Method Detail

      • updateUI

        public static void updateUI​(JComponent... components)
        Calls JComponent.updateUI() for the given components, ignores null components.
        Parameters:
        components - the components to update the UI for
      • updateUI

        public static void updateUI​(Collection<? extends JComponent> components)
        Calls JComponent.updateUI() for the given components, ignores null components.
        Parameters:
        components - the components to update the UI for
      • linkToEnabledState

        public static Action linkToEnabledState​(StateObserver enabledState,
                                                Action action)
        Links the given action to the given StateObserver, so that the action is enabled only when the observed state is active
        Parameters:
        enabledState - the StateObserver with which to link the action
        action - the action
        Returns:
        the linked action
      • linkToEnabledState

        public static void linkToEnabledState​(StateObserver enabledState,
                                              JComponent... components)
        Links the given components to the given StateObserver, so that each component is enabled only when the observed state is active
        Parameters:
        enabledState - the StateObserver with which to link the components
        components - the components
      • propertyChangeObserver

        public static <T> EventObserver<T> propertyChangeObserver​(JComponent component,
                                                                  String property)
        Returns a EventObserver notified each time the value of the given property changes in the given component.
        Type Parameters:
        T - the property data type
        Parameters:
        component - the component
        property - the property to listen to changes for
        Returns:
        a EventObserver notified each time the value of the given property changes
      • linkBoundedRangeModels

        public static void linkBoundedRangeModels​(BoundedRangeModel main,
                                                  BoundedRangeModel linked)
        Links the given BoundedRangeModels so that changes in main are reflected in linked
        Parameters:
        main - the main model
        linked - the model to link with main
      • expandAll

        public static void expandAll​(JTree tree,
                                     TreePath parent)
        Expands all the paths from a parent in the given tree
        Parameters:
        tree - the tree
        parent - the parent from which to exapand
      • collapseAll

        public static void collapseAll​(JTree tree,
                                       TreePath parent)
        Collapses all the paths from a parent in the given tree
        Parameters:
        tree - the tree
        parent - the parent from which to collapse
      • addInitialFocusHack

        public static void addInitialFocusHack​(JComponent component,
                                               Action onFocusAction)
        http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5018574
        Parameters:
        component - the component
        onFocusAction - the action to run when the focus has been requested
      • loadIcon

        public static ImageIcon loadIcon​(Class<?> resourceClass,
                                         String resourceName)
        Loads an icon as a resource
        Parameters:
        resourceClass - the class owning the resource
        resourceName - the resource name
        Returns:
        an icon
      • setFontSize

        public static void setFontSize​(float multiplier)
        Sets a global font size multiplier.
        Parameters:
        multiplier - the font size multiplier
      • darker

        public static Color darker​(Color color)
        Returns a darker version of the given color, using 0.8 as the mulitiplication factor.
        Parameters:
        color - the color to darken
        Returns:
        a darker version of the given color
        See Also:
        Color.darker()
      • darker

        public static Color darker​(Color color,
                                   double factor)
        Returns a darker version of the given color, using the given factor.
        Parameters:
        color - the color to darken
        factor - a number between 0 and 1, non-inclusive
        Returns:
        a darker version of the given color
        See Also:
        Color.darker()
      • setClipboard

        public static void setClipboard​(String string)
        Sets the given string as clipboard contents
        Parameters:
        string - the string to put on the clipboard
      • getParentOfType

        public static <T> Optional<T> getParentOfType​(Class<T> clazz,
                                                      Component component)
        Searches the parent component hierarchy of the given component for an ancestor of the given type
        Type Parameters:
        T - the type of parent to find
        Parameters:
        clazz - the class of the parent to find
        component - the component
        Returns:
        the parent of the given component of the given type, an empty Optional if none is found
      • getParentWindow

        public static Optional<Window> getParentWindow​(Component component)
        Finds the first component of type Window in the parent hierarchy of component. Note that if component is of type Window, it is returned.
        Parameters:
        component - the component
        Returns:
        the parent Window of the given component, an empty Optional if none exists
      • getParentFrame

        public static Optional<JFrame> getParentFrame​(Component component)
        Finds the first component of type JFrame in the parent hierarchy of component. Note that if component is of type JFrame, it is returned.
        Parameters:
        component - the component
        Returns:
        the parent JFrame of the given component, an empty Optional if none exists
      • getParentDialog

        public static Optional<JDialog> getParentDialog​(Component component)
        Finds the first component of type JDialog in the parent hierarchy of component. Note that if component is of type JDialog, it is returned.
        Parameters:
        component - the component
        Returns:
        the parent JDialog of the given component, an empty Optional if none exists
      • systemLookAndFeelClassName

        public static String systemLookAndFeelClassName()
        Note that GTKLookAndFeel is overridden with MetalLookAndFeel, since JTabbedPane does not respect the 'TabbedPane.contentBorderInsets' setting, making hierachical tabbed panes look bad
        Returns:
        the default look and feel for the platform we're running on
      • isSystemOrCrossPlatformLookAndFeelEnabled

        public static boolean isSystemOrCrossPlatformLookAndFeelEnabled()
        Returns:
        true if the system or cross-platform look and feel is enabled
        See Also:
        systemLookAndFeelClassName()