Class McVGuiUtils

java.lang.Object
edu.wisc.ssec.mcidasv.util.McVGuiUtils
All Implemented Interfaces:
Constants, IdvConstants

public class McVGuiUtils extends Object implements Constants
McIDAS-V's collection of GUI utility methods
  • Field Details

  • Constructor Details

  • Method Details

    • makeLabelRight

      public static JLabel makeLabelRight(String title)
      Create a standard sized, right-justified label
      Parameters:
      title - Label text. Should not be null.
      Returns:
      A new label.
    • makeLabelRight

      public static JLabel makeLabelRight(String title, McVGuiUtils.Width width)
    • makeLabelLeft

      public static JLabel makeLabelLeft(String title)
      Create a standard sized, left-justified label.
      Parameters:
      title - Label text. Should not be null.
      Returns:
      A new label.
    • makeLabelLeft

      public static JLabel makeLabelLeft(String title, McVGuiUtils.Width width)
    • makeLabeledComponent

      public static JPanel makeLabeledComponent(String label, JComponent thing)
      Create a sized, labeled component.
      Parameters:
      label - Label for thing. Should not be null.
      thing - Component to label. Should not be null.
      Returns:
      A component with its label to the right.
    • makeLabeledComponent

      public static JPanel makeLabeledComponent(JLabel label, JComponent thing)
    • makeLabeledComponent

      public static JPanel makeLabeledComponent(String label, JComponent thing, McVGuiUtils.Position position)
    • makeLabeledComponent

      public static JPanel makeLabeledComponent(JLabel label, JComponent thing, McVGuiUtils.Position position)
    • makeComponentLabeled

      public static JPanel makeComponentLabeled(JComponent thing, String label)
      Create a sized, labeled component.
      Parameters:
      thing - Component to label. Should not be null.
      label - Label for thing. Should not be null.
      Returns:
      A labeled component.
    • makeComponentLabeled

      public static JPanel makeComponentLabeled(JComponent thing, String label, McVGuiUtils.Position position)
    • makeComponentLabeled

      public static JPanel makeComponentLabeled(JComponent thing, JLabel label)
    • makeComponentLabeled

      public static JPanel makeComponentLabeled(JComponent thing, JLabel label, McVGuiUtils.Position position)
    • setComponentWidth

      public static void setComponentWidth(JComponent existingComponent)
      Set the width of an existing component.
      Parameters:
      existingComponent - Component that will have its width set.
    • setComponentWidth

      public static void setComponentWidth(JComponent existingComponent, McVGuiUtils.Width width)
      Set the width of an existing component using standard McIDAS-V component widths.
      Parameters:
      existingComponent - Component that will have its width set.
      width - Width to use for existingComponent.
    • setComponentWidth

      public static void setComponentWidth(JComponent existingComponent, int width)
      Set the width of an existing component to a given integer width.
      Parameters:
      existingComponent - Component that will have its width set.
      width - Width to use for existingComponent.
    • setComponentWidth

      public static void setComponentWidth(JComponent setme, JComponent getme)
      Set the component width to that of another component.
    • setComponentWidth

      public static void setComponentWidth(JComponent setme, JComponent getme, int padding)
    • setComponentHeight

      public static void setComponentHeight(JComponent setme, JComponent getme)
      Set the component height to that of another component.
    • setComponentHeight

      public static void setComponentHeight(JComponent setme, JComponent getme, int padding)
    • setLabelPosition

      public static void setLabelPosition(JLabel existingLabel)
      Set the label position of an existing label
      Parameters:
      existingLabel -
    • setLabelPosition

      public static void setLabelPosition(JLabel existingLabel, McVGuiUtils.Position position)
    • setLabelBold

      public static void setLabelBold(JLabel existingLabel, boolean bold)
      Set the bold attribute of an existing label
      Parameters:
      existingLabel -
      bold -
    • setComponentColor

      public static void setComponentColor(JComponent existingComponent)
      Set the foreground color of an existing component
      Parameters:
      existingComponent -
    • setComponentColor

      public static void setComponentColor(JComponent existingComponent, McVGuiUtils.TextColor color)
    • makeImageButton

      public static JButton makeImageButton(String iconName, Object object, String methodName, Object arg, String tooltip)
      Custom makeImageButton to ensure proper sizing and mouseborder are set
    • makeImageButton

      public static JButton makeImageButton(String iconName, String tooltip)
      Custom makeImageButton to ensure proper sizing and mouseborder are set
    • makeImageTextButton

      public static JButton makeImageTextButton(String iconName, String label)
      Create a button with text and an icon
    • setButtonImage

      public static void setButtonImage(JButton existingButton, String iconName)
      Add an icon to a button... but only if the LookAndFeel supports it
    • setMenuImage

      public static void setMenuImage(JMenuItem existingMenuItem, String iconName)
      Add an icon to a menu item
    • makeComboBox

      public static <E> JComboBox<E> makeComboBox(E[] items, E selected)
    • makeComboBox

      public static <E> JComboBox<E> makeComboBox(E[] items, E selected, McVGuiUtils.Width width)
    • makeComboBox

      public static <E> JComboBox<E> makeComboBox(Collection<E> items, E selected)
    • makeComboBox

      public static <E> JComboBox<E> makeComboBox(Collection<E> items, E selected, McVGuiUtils.Width width)
    • setListData

      public static <E> void setListData(JComboBox<E> box, Collection<E> items, E selected)
    • getEditableBox

      public static <E> JComboBox<E> getEditableBox(Collection<E> items, E selected)
    • makeTextField

      public static JTextField makeTextField(String value)
      Create a standard sized text field.
      Parameters:
      value - Text to place within the text field. Should not be null.
      Returns:
      JTextField with initial text taken from value.
    • makeTextField

      public static JTextField makeTextField(String value, McVGuiUtils.Width width)
    • makeTextFieldLimit

      public static McVTextField makeTextFieldLimit(String defaultString, int limit)
      Create some custom text entry widgets
    • makeTextFieldUpper

      public static McVTextField makeTextFieldUpper(String defaultString, int limit)
    • makeTextFieldAllow

      public static McVTextField makeTextFieldAllow(String defaultString, int limit, boolean upper, String allow)
    • makeTextFieldDeny

      public static McVTextField makeTextFieldDeny(String defaultString, int limit, boolean upper, String deny)
    • makeTextFieldAllow

      public static McVTextField makeTextFieldAllow(String defaultString, int limit, boolean upper, char... allow)
    • makeTextFieldDeny

      public static McVTextField makeTextFieldDeny(String defaultString, int limit, boolean upper, char... deny)
    • makeTextFieldAllow

      public static McVTextField makeTextFieldAllow(String defaultString, int limit, boolean upper, Pattern allow)
    • makeTextFieldDeny

      public static McVTextField makeTextFieldDeny(String defaultString, int limit, boolean upper, Pattern deny)
    • topCenterBottom

      public static JPanel topCenterBottom(JComponent top, JComponent center, JComponent bottom)
      Use GroupLayout for stacking components vertically. Set center to resize vertically.
      Parameters:
      top - Component to place at the top of the newly created panel. Should not be null.
      center - Component to place in the center of the newly created panel. Should not be null.
      bottom - Component to place at the bottom of the newly created panel. Should not be null.
      Returns:
      New JPanel with the given components in the top, center, and bottom positions.
    • topBottom

      public static JPanel topBottom(JComponent top, JComponent bottom, McVGuiUtils.Prefer which)
      Use GroupLayout for stacking components vertically.
      Parameters:
      top - Component to place at the top of the newly created panel. Should not be null.
      bottom - Component to place at the bottom of the newly created panel. Should not be null.
      which - Which component's size to prefer. Should not be null.
      Returns:
      New JPanel with the given components.
    • sideBySide

      public static JPanel sideBySide(JComponent left, JComponent right)
      Use GroupLayout for wrapping components to stop vertical resizing.
      Parameters:
      left - Left component. Should not be null.
      right - Right component. Should not be null.
      Returns:
      New JPanel with the given components side-by-side.
    • sideBySide

      public static JPanel sideBySide(JComponent left, JComponent right, int gap)
      Use GroupLayout for wrapping components to stop vertical resizing.
      Parameters:
      left - Left component. Should not be null.
      right - Right component. Should not be null.
      gap - Gap between left and right.
      Returns:
      New JPanel with the given components side-by-side, separated by value from gap.
    • horizontal

      public static JPanel horizontal(Component... components)
      Use GroupLayout for wrapping a list of components horizontally.
      Parameters:
      components - Components to stack horizontally. Should not be null.
      Returns:
      JPanel with the given components.
    • vertical

      public static JPanel vertical(Component... components)
      Use GroupLayout for wrapping a list of components vertically.
      Parameters:
      components - Components to stack vertically. Should not be null.
      Returns:
      JPanel with the given components.
    • makePrettyButtons

      public static JPanel makePrettyButtons(JPanel idvButtonPanel)
      Hack apart an IDV button panel and do a few things: - Reorder the buttons based on OS preference Windows: OK on left Mac: OK on right - Add icons when we understand the button name
      Parameters:
      idvButtonPanel - JPanel to scan for understood button names. Should not be null.
      Returns:
      The given JPanel with pretty buttons (where possible).
    • makePrettyButtons

      public static List makePrettyButtons(List buttonList)
      Take a list of buttons and make them pretty.
      Parameters:
      buttonList - List of buttons. Should not be null.
      Returns:
      List of pretty buttons.
    • makePrettyButton

      public static JButton makePrettyButton(String name)
      Convenience method to make a button based solely on its name.
      Parameters:
      name - Button text. Should not be null.
      Returns:
      A "pretty" button.
    • makePrettyButton

      public static JButton makePrettyButton(JButton button)
      Add icons when we understand the button name.
      Parameters:
      button - Button to make pretty. Should not be null.
      Returns:
      button Either the given button with an icon, or just the given button (if the name was not understood).
    • printUIComponents

      public static void printUIComponents(JComponent parent)
      Print the hierarchy of components.
    • printUIComponents

      public static void printUIComponents(JComponent parent, int index, int depth)
    • runOnEDT

      public static void runOnEDT(Runnable r)
      Calls SwingUtilities.invokeLater(Runnable) if the current thread is not the event dispatch thread. If this thread is the EDT, then call Runnable.run() for r.

      Remember, you do not want to execute long-running tasks in the event dispatch thread--it'll lock up the GUI.

      Parameters:
      r - Code to run in the event dispatch thread. Cannot be null.
    • getFromEDT

      public static <T> T getFromEDT(Callable<T> callable)
      Executes the specified Callable on the EDT thread.

      If the calling thread is already the EDT thread, this invocation simply delegates to call(), otherwise the callable is placed in Swing's event dispatch queue and the method waits for the result.

      Type Parameters:
      T - Result type of the callable.
      Parameters:
      callable - Callable task. Cannot be null.
      Returns:
      Computed result
    • getVMCount

      private static int getVMCount()
    • getHolderCount

      private static int getHolderCount()
    • getGroupCount

      private static int getGroupCount()
    • getActiveViewManagers

    • getAllViewManagers

    • getWindowForViewManager

      Attempt to find the IdvWindow that contains the given ViewManager.
      Parameters:
      vm - ViewManager whose IdvWindow is needed. Cannot be null.
      Returns:
      Either the IdvWindow containing vm, or null.
    • getShareGroupsInWindow

      public static List<Object> getShareGroupsInWindow(IdvWindow window)
    • getAllShareGroups

      public static List<Object> getAllShareGroups()
    • getViewManagersInGroup

      public static List<ViewManager> getViewManagersInGroup(Object sharedGroup)
    • getViewManagers

      public static List<ViewManager> getViewManagers(WindowInfo info)
    • getViewManagers

      public static List<ViewManager> getViewManagers(IdvWindow window)
    • isUIHolder

      public static boolean isUIHolder(IdvComponentHolder h)
      Returns:
      Whether or not h contains some UI component like the dashboard of field selector. Yes, it can happen!
    • isDynamicSkin

      public static boolean isDynamicSkin(IdvComponentHolder h)
      Returns:
      Whether or not h is a dynamic skin.
    • hasDynamicSkins

      public static boolean hasDynamicSkins(List<WindowInfo> windows)
      Returns:
      Whether or not windows has at least one dynamic skin.
    • getComponentHolders

      Returns:
      The component holders within windowInfo.
      See Also:
    • getComponentHolders

      Returns:
      The component holders within idvWindow.
      See Also:
    • getComponentHolders

      Returns:
      Recursively searches group to find any component holders.
    • getComponentGroups

      Returns:
      Recursively searches group for any nested component groups.
    • getComponentGroups

      Returns:
      Component groups contained in window.
      See Also:
    • getComponentGroups

      Returns:
      Component groups contained in windows.
      See Also:
    • getComponentGroup

      Returns:
      The component group within window.
    • hasNestedGroups

      public static boolean hasNestedGroups(IdvComponentGroup group)
      Returns:
      Whether or not group contains any component groups.
    • getAllComponentHolders

      Returns:
      All active component holders in McIDAS-V.
    • getAllComponentGroups

      Returns:
      All active component groups in McIDAS-V.
    • getAllDisplayWindows

      Returns:
      All windows that contain at least one component group.
    • getAfterActiveHolder

      Returns:
      The component holder positioned after the active component holder.
    • getBeforeActiveHolder

      Returns:
      The component holder positioned before the active component holder.
    • getActiveComponentHolder

      Returns:
      The active component holder in the active window.
    • getAfterHolder

      Returns:
      The component holder positioned after current.
    • getBeforeHolder

      Returns:
      The component holder positioned before current.
    • idvGroupsToMcv

      Parameters:
      w - IdvWindow whose component groups you want (as McvComponentGroups).
      Returns:
      A List of McvComponentGroups or an empty list. If there were no McvComponentGroups in w, or if w is null, an empty List is returned.
    • compGroup

      public static void compGroup(IdvComponentGroup g)
    • compGroup

      public static void compGroup(IdvComponentGroup g, int level)
    • compHolder

      public static void compHolder(IdvComponentHolder h, int level)
    • findvms

      public static List<ViewManager> findvms(List<WindowInfo> windows)
    • vmType

      private static String vmType(ViewManager vm)
    • pad

      private static String pad(String str, int pad)
    • p

      private static void p(String str, int padding)
    • getDisplayBoundsFor

      public static Rectangle getDisplayBoundsFor(int index)
      Find the "bounds" for the physical display at index.
      Parameters:
      index - Zero-based index of the desired physical display.
      Returns:
      Either a Rectangle representing the display's bounds, or null if index is invalid.
    • findDisplayNumberForRectangle

      public static int findDisplayNumberForRectangle(Rectangle rect)
      Tries to determine the physical display that contains the given Rectangle. This method (currently) fails for Rectangles that span multiple displays!
      Parameters:
      rect - Rectangle to test. Should not be null.
      Returns:
      Either the (zero-based) index of the physical display, or -1 if there was no match.
    • findDisplayNumberForComponent

      public static int findDisplayNumberForComponent(Component comp)
      Tries to determine the physical display that contains the given Component. This method (currently) fails for Components that span multiple displays!
      Parameters:
      comp - Component to test. Should not be null.
      Returns:
      Either the (zero-based) index of the physical display, or -1 if there was no match.
    • findDisplayNumberForMultiFrame

      Tries to determine the physical display that contains the given MultiFrame. This method (currently) fails for MultiFrames that span multiple displays!
      Parameters:
      mf - MultiFrame to test. Should not be null.
      Returns:
      Either the (zero-based) index of the physical display, or -1 if there was no match.
    • findDisplayNumberForCoords

      public static int findDisplayNumberForCoords(int x, int y, int width, int height)
      Tries to determine the physical display that contains the rectangle defined by the specified coordinates. This method (currently) fails for coordinates that span multiple displays!
      Parameters:
      x - X coordinate of the upper-left corner.
      y - Y coordinate of the upper-left corner.
      width - Width of the rectangle.
      height - Height of the rectangle.
      Returns:
      Either the (zero-based) index of the physical display, or -1 if there was no match.
      See Also:
    • findDisplayNumberForEvent

      public static int findDisplayNumberForEvent(EventObject event)
      Tries to determine which physical display contains the Component or MultiFrame that fired the given event. This method (currently) fails for coordinates that span multiple displays!
      Parameters:
      event - EventObject to test. Should not be null.
      Returns:
      Either the (zero-based) index of the physical display, or -1 if there was no match.
    • getDescendantsOfType

      public static <T extends JComponent> List<T> getDescendantsOfType(Class<T> clazz, Container container)
      Convenience method for searching below container in the component hierarchy and return nested components that are instances of class clazz it finds. Returns an empty list if no such components exist in the container.

      Invoking this method with a class parameter of JComponent.class will return all nested components.

      This method invokes getDescendantsOfType(Class, java.awt.Container, boolean)

      Parameters:
      clazz - the class of components whose instances are to be found.
      container - the container at which to begin the search
      Returns:
      the List of components
    • getDescendantsOfType

      public static <T extends JComponent> List<T> getDescendantsOfType(Class<T> clazz, Container container, boolean nested)
      Convenience method for searching below container in the component hierarchy and return nested components that are instances of class clazz it finds. Returns an empty list if no such components exist in the container.

      Invoking this method with a class parameter of JComponent.class will return all nested components.

      Parameters:
      clazz - the class of components whose instances are to be found.
      container - the container at which to begin the search
      nested - true to list components nested within another listed component, false otherwise
      Returns:
      the List of components
    • getDescendantOfType

      public static <T extends JComponent> T getDescendantOfType(Class<T> clazz, Container container, String property, Object value) throws IllegalArgumentException
      Convenience method that searches below container in the component hierarchy and returns the first found component that is an instance of class clazz having the bound property value. Returns null if such component cannot be found.

      This method invokes getDescendantOfType(Class, java.awt.Container, String, Object, boolean)

      Parameters:
      clazz - the class of component whose instance is to be found.
      container - the container at which to begin the search
      property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue().
      value - the value of the bound property
      Returns:
      the component, or null if no such component exists in the container
      Throws:
      IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
    • getDescendantOfType

      public static <T extends JComponent> T getDescendantOfType(Class<T> clazz, Container container, String property, Object value, boolean nested) throws IllegalArgumentException
      Convenience method that searches below container in the component hierarchy and returns the first found component that is an instance of class clazz and has the bound property value. Returns null if such component cannot be found.
      Parameters:
      clazz - the class of component whose instance to be found.
      container - the container at which to begin the search
      property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue().
      value - the value of the bound property
      nested - true to list components nested within another component which is also an instance of clazz, false otherwise.
      Returns:
      the component, or null if no such component exists in the container.
      Throws:
      IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed.
    • getDescendantsOfClass

      public static <T extends JComponent> List<T> getDescendantsOfClass(Class<T> clazz, Container container)
      Convenience method for searching below container in the component hierarchy and return nested components of class clazz it finds. Returns an empty list if no such components exist in the container.

      This method invokes getDescendantsOfClass(Class, java.awt.Container, boolean).

      Parameters:
      clazz - the class of components to be found.
      container - the container at which to begin the search
      Returns:
      the List of components
    • getDescendantsOfClass

      public static <T extends JComponent> List<T> getDescendantsOfClass(Class<T> clazz, Container container, boolean nested)
      Convenience method for searching below container in the component hierarchy and return nested components of class clazz it finds. Returns an empty list if no such components exist in the container.
      Parameters:
      clazz - the class of components to be found.
      container - the container at which to begin the search
      nested - true to list components nested within another listed component, false otherwise
      Returns:
      the List of components
    • getDescendantOfClass

      public static <T extends JComponent> T getDescendantOfClass(Class<T> clazz, Container container, String property, Object value) throws IllegalArgumentException
      Convenience method that searches below container in the component hierarchy in a depth first manner and returns the first found component of class clazz having the bound property value.

      Returns null if such component cannot be found.

      This method invokes getDescendantOfClass(Class, java.awt.Container, String, Object, boolean)

      Parameters:
      clazz - the class of component to be found.
      container - the container at which to begin the search
      property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue(). This parameter is case sensitive.
      value - the value of the bound property
      Returns:
      the component, or null if no such component exists in the container's hierarchy.
      Throws:
      IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
    • getDescendantOfClass

      public static <T extends JComponent> T getDescendantOfClass(Class<T> clazz, Container container, String property, Object value, boolean nested) throws IllegalArgumentException
      Convenience method that searches below container in the component hierarchy in a depth first manner and returns the first found component of class clazz having the bound property value.

      Returns null if such component cannot be found.

      Parameters:
      clazz - the class of component to be found.
      container - the container at which to begin the search
      property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue(). This parameter is case sensitive.
      value - the value of the bound property
      nested - true to include components nested within another listed component, false otherwise.
      Returns:
      the component, or null if no such component exists in the container's hierarchy.
      Throws:
      IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
    • getComponentFromList

      private static <T extends JComponent> T getComponentFromList(Class<T> clazz, List<T> list, String property, Object value) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • equals

      public static boolean equals(Object obj1, Object obj2)
      Convenience method for determining whether two objects are either equal or both null.
      Parameters:
      obj1 - the first reference object to compare.
      obj2 - the second reference object to compare.
      Returns:
      true if obj1 and obj2 are equal or if both are null, false otherwise
    • getComponentMap

      public static Map<JComponent,List<JComponent>> getComponentMap(JComponent container, boolean nested)
      Convenience method for mapping a container in the hierarchy to its contained components. The keys are the containers, and the values are lists of contained components.

      Implementation note: The returned value is a HashMap and the values are of type ArrayList. This is subject to change, so callers should code against the interfaces Map and List.

      Parameters:
      container - The JComponent to be mapped
      nested - true to drill down to nested containers, false otherwise
      Returns:
      the Map of the UI
    • getUIDefaultsOfClass

      public static UIDefaults getUIDefaultsOfClass(Class<?> clazz)
      Convenience method for retrieving a subset of the UIDefaults pertaining to a particular class.
      Parameters:
      clazz - the class of interest
      Returns:
      the UIDefaults of the class
    • getUIDefaultsOfClass

      public static UIDefaults getUIDefaultsOfClass(String className)
      Convenience method for retrieving a subset of the UIDefaults pertaining to a particular class.
      Parameters:
      className - fully qualified name of the class of interest
      Returns:
      the UIDefaults of the class named
    • getUIDefaultOfClass

      public static Object getUIDefaultOfClass(Class<?> clazz, String property)
      Convenience method for retrieving the UIDefault for a single property of a particular class.
      Parameters:
      clazz - the class of interest
      property - the property to query
      Returns:
      the UIDefault property, or null if not found
    • getProperties

      public static Map<Object,Object> getProperties(JComponent component)
      Convenience method for obtaining most non-null human readable properties of a JComponent. Array properties are not included.

      Implementation note: The returned value is a HashMap. This is subject to change, so callers should code against the interface Map.

      Parameters:
      component - the component whose proerties are to be determined
      Returns:
      the class and value of the properties
    • getJClass

      public static <T extends JComponent> Class<?> getJClass(T component)
      Convenience method to obtain the Swing class from which this component was directly or indirectly derived.
      Parameters:
      component - The component whose Swing superclass is to be determined
      Returns:
      The nearest Swing class in the inheritance tree
    • safeGetText

      public static String safeGetText(JTextComponent textComponent)
      Gets the "text" contents of a JTextComponent without the possibility of a NullPointerException.
      Parameters:
      textComponent - JTextComponent whose contents should be extracted. null is allowed.
      Returns:
      Either the results of JTextComponent.getText() or an empty String if textComponent or getText() are null.