Class GUIServices

java.lang.Object
noaa.coastwatch.gui.GUIServices

public class GUIServices extends Object
The GUI services class defines various static methods relating to graphical user interfaces.
Since:
3.1.1
Author:
Peter Hollemans
  • Field Details

    • IS_MAC

      public static final boolean IS_MAC
      The Mac boolean, true if we are running on a Mac.
    • IS_WIN

      public static final boolean IS_WIN
      The Windows boolean, true if we are running on Windows.
    • IS_LINUX

      public static final boolean IS_LINUX
      The Linux boolean, true if we are running on Linux.
    • IS_AQUA

      public static final boolean IS_AQUA
      The Aqua boolean, true if we are running on a Mac with Aqua look.
    • IS_GTK

      public static final boolean IS_GTK
      The GTK boolean, true if we are running the GTK look.
    • ICON_INSETS

      public static final Insets ICON_INSETS
      The insets to use for icon-only button margins.
    • WINDOW_WIDTH_KEY

      public static final String WINDOW_WIDTH_KEY
      The preferences key for application window width.
      See Also:
    • WINDOW_HEIGHT_KEY

      public static final String WINDOW_HEIGHT_KEY
      The preferences key for applicationwindow height.
      See Also:
    • RECENT_FILES_KEY

      public static final String RECENT_FILES_KEY
      The preferences key for recently opened files.
      See Also:
  • Method Details

    • createAction

      public static Action createAction(String className, String command, String text, String iconPurpose, String desc)
      Since:
      3.8.1
    • centerOnScreen

      public static void centerOnScreen(JFrame frame)
      Since:
      3.8.1
    • initializeLaf

      public static void initializeLaf()
      Initializes the environment prior to creating any GUI components.
      Since:
      3.7.1
    • createTranslucentButton

      public static TranslucentButton createTranslucentButton(Icon defaultIcon, Icon rolloverIcon, Icon pressedIcon)
      Creates a button with an on screen display type of style and translucent properties.
      Parameters:
      defaultIcon - the icon to use.
      rolloverIcon - the rollover icon to use or null.
      pressedIcon - the pressed icon to use or null.
      Returns:
      the button.
      Since:
      3.8.1
    • createModifiedImage

      public static Image createModifiedImage(Image image, BiConsumer<int[],int[]> operator)
    • createOnScreenStyleButton

      public static JButton createOnScreenStyleButton(int size, IconFactory.Purpose purpose)
      Creates a button with an on screen display type of style.
      Parameters:
      size - the width and height of the button.
      purpose - the button purpose.
      Returns:
      the button.
      Since:
      3.8.1
    • createDarkenFunction

      public static BiFunction<int[],int[],Boolean> createDarkenFunction(double f)
      Creates a darkening function to be use with a composite.
      Parameters:
      f - the darkening factor.
      Returns:
      the darkening function.
      Since:
      3.8.1
      See Also:
    • createComposite

      public static Composite createComposite(BiFunction<int[],int[],Boolean> function)
      Creates a composite that uses a function to specify the mapping from source to destination pixels.
      Parameters:
      function - a function that takes (srcPixels, destPixels) and overwrites destPixels with the output composite pixel values. The function returns true if successful and the destPixels values should be written to the output, or false if not. The source and destination pixels are stored as int[] arrays that contain [red, green, blue, alpha] in the range of 0-255.
      Returns:
      the composite object.
      Since:
      3.8.1
    • createLinkLabel

      public static JLabel createLinkLabel(String text, String url)
      Creates a label with text and URL that opens a browser window.
      Parameters:
      text - the text for the label.
      url - the URL to open when the label is clicked.
      Returns:
      the hyperlink label.
      Since:
      3.8.1
    • getAboutComponent

      public static Component getAboutComponent(String tool)
      Gets an about component appropriate for an about dialog box.
      Parameters:
      tool - the tool or program name.
      Returns:
      the about component.
      Since:
      3.8.1
    • getPlatformDefaultDirectory

      public static File getPlatformDefaultDirectory()
      Gets the platform default directory for opening new files.
      Returns:
      the platform default directory or null if not available.
    • setConstraints

      public static void setConstraints(GridBagConstraints c, int gridx, int gridy, int gridwidth, int gridheight, int fill, double weightx, double weighty)
      Sets the values in a GridBagConstraints object.
      Parameters:
      c - the contraints object.
      gridx - the grid x position.
      gridy - the grid y position.
      gridwidth - the number of grid cells wide.
      gridheight - the number of grid cells high.
      fill - the fill mode.
      weightx - the weight in the x direction.
      weighty - the weight in the y direction.
    • getTextButton

      public static JButton getTextButton(Action action)
      Gets a button with text label using an action. In some look and feels, this returns a slightly modified button that is more consistent with the operating system UI.
      Parameters:
      action - the action for the button.
      Returns:
      the button created using the action.
    • getTextButton

      public static JButton getTextButton(String text)
      Gets a button with text label. In some look and feels, this returns a slightly modified button that is more consistent with the operating system UI.
      Parameters:
      text - the text label.
      Returns:
      the button with text label.
    • getIconButton

      public static JButton getIconButton(String purpose)
      Gets a button with icon.
      Parameters:
      purpose - the icon purpose.
      Returns:
      the button with image icon appropriate for the specified purpose.
      Throws:
      IllegalArgumentException - if the icon purpose has no known icon, or the icon resource cannot be found.
    • getIconToggle

      public static JToggleButton getIconToggle(String purpose)
      Gets a toggle button with icon.
      Parameters:
      purpose - the icon purpose.
      Returns:
      the toggle button with image icon appropriate for the specified purpose.
      Throws:
      IllegalArgumentException - if the icon purpose has no known icon, or the icon resource cannot be found.
    • setSquare

      public static void setSquare(JComponent comp)
      Sets a component's preferred, min, and max size to be square. The current minimum preferred dimension is used for both dimensions.
      Parameters:
      comp - the component to modify.
    • setContainerEnabled

      public static void setContainerEnabled(Container container, boolean isEnabled)
      Sets the enabled flag on all components in a container. If the container contains othe containers, they are recursively set.
      Parameters:
      container - the container to modify.
      isEnabled - the enabled flag, true to enable or false to disable.
    • invokeAndWait

      public static void invokeAndWait(Runnable doRun)
      Invokes the runnable using the SwingUtilities.invokeAndWait() method but discards any exceptions thrown.
      Parameters:
      doRun - the object to invoke.
    • showFrame

      public static void showFrame(Frame frame)
      Shows the specified frame in the AWT event dispatching thread.
      Parameters:
      frame - the frame to show.
      Since:
      3.4.1
    • createDialog

      public static JDialog createDialog(Component parent, String title, boolean modal, Component component, Component[] controls, Action[] actions, boolean[] hideAction, boolean doDispose)
      Creates a dialog with similar layout to dialogs produced by JOptionPane. The dialog has a main message area, and a set of buttons.
      Parameters:
      parent - the dialog parent component.
      title - the dialog title string.
      modal - true if the dialog should be modal (ie: blocking input in all other windows), false if not.
      component - the main dialog component.
      controls - the list of custom controls to be placed to the left of the action buttons, or null for no custom controls.
      actions - the list of actions for dialog buttons. The first action is taken to be the default for the dialog.
      hideAction - the list of hide flags, true if the corresponding action should cause the dialog to be hidden, or null for all actions to hide the dialog.
      doDispose - the dispose flag, true to dispose rather than hide when a button is clicked.
      Returns:
      the dialog created.
    • lineWrap

      public static String lineWrap(String input, int columns)
      Creates a line-wrapped version of a string.
      Parameters:
      input - the string to line-wrap.
      columns - the number of columns to wrap at.
      Returns:
      the line-wrapped version of the string.
    • ellipsisString

      public static String ellipsisString(String input, int maxLength)
      Truncates a string to a maximum length by embedding an ellipsis in the middle. This is useful for long file names for example.
      Parameters:
      input - the input string to truncate.
      maxLength - the maximum length of the output string.
      Returns:
      the input string, truncated to the specified max length and ellipsis inserted if needed. If the input is less than or equal to the maximum length, no truncation is done an the input string is returned as-is.
    • createErrorDialog

      public static JDialog createErrorDialog(Component parent, String title, String userText)
      Creates a dialog that displays standard error messages to the user in a graphical window. The dialog uses a PanelOutputStream object to redirect standard error messages to a scrolling panel. The System standard error object is replaced with a new one that outputs to the dialog, so this method should only be called once in the lifecycle of a GUI program. The dialog is set up so that if any errors are caught, the dialog becomes visible and shows the error, and then allows the user to close the program.
      Parameters:
      parent - the dialog parent component.
      title - the dialog title string.
      userText - the instructions to give to the user concerning the error message, or null for none.
      Returns:
      the new standard error dialog.
      Throws:
      IllegalStateException - if the error dialog has already been created.
    • createAction

      public static Action createAction(String name, Runnable runnable)
      Creates an action with the specified name and listener.
      Parameters:
      name - the action name.
      runnable - the runnable to invoke when the action is performed, or null for no operation.
      Returns:
      the action created.
    • createMessageDialog

      public static JDialog createMessageDialog(Component parent, String title, String message)
      Creates a modal message dialog with no user input options. The dialog may not be closed by the user.
      Parameters:
      parent - the parent component.
      title - the dialog title.
      message - the message to display.
      Returns:
      the dialog created.
    • getIcon

      public static ImageIcon getIcon(String purpose)
      Gets an icon for the specified purpose.
      Parameters:
      purpose - the icon purpose.
      Returns:
      the image icon appropriate for the specified purpose.
      Throws:
      IllegalArgumentException - if the icon purpose has no known icon, or the icon resource cannot be found.
    • getHelpButton

      public static JButton getHelpButton(Class helpClass)
      Gets a help button for the specified class.
      Parameters:
      helpClass - the class to get a help button for.
      Returns:
      a help button, which when pressed will show help for the specified class.
      Throws:
      IllegalArgumentException - if the class has no known help resource, or the help resource cannot be found.
    • setHelpIndex

      public static void setHelpIndex(URL url)
      Sets the active help index file for subsequent calls to getHelpButton(java.lang.Class). HTML panels will go to the specified index when the Home button is clicked.
      Parameters:
      url - the help index URL to use for the help system.
    • getFileChooser

      public static JFileChooser getFileChooser()
      Gets a file chooser that tracks the user's selected directory. Objects should use this method if they are going to display a file chooser, so that the user always sees the same directory that they were last in.
      Returns:
      a file chooser to use for opening or saving files.
    • addOpenFileListener

      public static void addOpenFileListener(ActionListener listener)
      Adds a listener that executes when a file is double-clicked on Mac or Windows.
      Parameters:
      listener - the listener to call.
      Since:
      3.4.1
    • setSameSize

      public static void setSameSize(List componentList)
      Sets a list of JComponent objects to be the same size as that of the maximum sized component. This is useful for a row of buttons which may have different length labels and thus would be sized differently by a layout manager, but would look better all one size. Good for tool bars and button boxes.
      Parameters:
      componentList - the list of components.
    • getRecentlyOpenedFiles

      public static List<String> getRecentlyOpenedFiles(Class targetClass)
      Get the list of recently opened files for the target class.
      Parameters:
      targetClass - the class for the recent files.
      Returns:
      the list of recnetly opened file names, possibly empty.
      Since:
      3.4.0
    • addFileToRecentlyOpened

      public static void addFileToRecentlyOpened(String file, Class targetClass, int maxFiles)
      Adds the specified file name to the list of recently opened files for the target class.
      Parameters:
      file - the file name to add.
      targetClass - the class for the recent files.
      maxFiles - the maximum number of files to store.
      Since:
      3.4.0
    • setRecentlyOpenedFiles

      public static void setRecentlyOpenedFiles(List<String> recentFilesList, Class targetClass)
      Sets the list of recently opened files for the target class.
      Parameters:
      recentFilesList - the new list of recently opened files, possibly empty.
      targetClass - the class for the recent files.
      Since:
      3.4.0
    • storeWindowSizeForClass

      public static void storeWindowSizeForClass(Dimension windowSize, Class targetClass)
      Stores the specified window size for the target class. Storing a window size is useful for maintaining a window size across application invocations.
      Parameters:
      windowSize - the window size to store.
      targetClass - the class to associate with the window size.
      Since:
      3.3.1
      See Also:
    • recallWindowSizeForClass

      public static Dimension recallWindowSizeForClass(Class targetClass)
      Recalls the specified window size for the target class. Recalling a window size is useful for maintaining a window size across application invokations.
      Parameters:
      targetClass - the class to recall the window size.
      Returns:
      the windows size or null if one was not found for the target class.
      Since:
      3.3.1
      See Also:
    • storeBooleanSettingForClass

      public static void storeBooleanSettingForClass(boolean value, String key, Class targetClass)
      Stores a boolean value for the target class.
      Parameters:
      value - the value to store.
      key - the key to use for storing the boolen value.
      targetClass - the class to associate with the value.
      Since:
      3.4.0
    • recallBooleanSettingForClass

      public static boolean recallBooleanSettingForClass(boolean def, String key, Class targetClass)
      Recalls a boolean value for the target class.
      Parameters:
      def - the default value for the boolean if not found.
      key - the key to use for recalling the boolean value.
      targetClass - the class to recall the boolean value.
      Returns:
      the boolean value, or the default value if none was found.
      Since:
      3.4.0
    • storeStringSettingForClass

      public static void storeStringSettingForClass(String value, String key, Class targetClass)
      Stores a string value for the target class.
      Parameters:
      value - the value to store.
      key - the key to use for storing the string value, may not be null.
      targetClass - the class to associate with the value.
      Since:
      3.6.0
    • recallStringSettingForClass

      public static String recallStringSettingForClass(String def, String key, Class targetClass)
      Recalls a string value for the target class.
      Parameters:
      def - the default value for the string if not found.
      key - the key to use for recalling the string value.
      targetClass - the class to recall the string value.
      Returns:
      the string value, or the default value if none was found.
      Since:
      3.6.0
    • getLabelWidth

      public static int getLabelWidth(int len)
      Gets the width of a label.
      Parameters:
      len - the length of text in the label.
      Returns:
      the width of the label in pixels.
      Since:
      3.7.1
    • getLabelHeight

      public static int getLabelHeight()
      Gets the height of a label.
      Returns:
      the height of the label in pixels.
      Since:
      3.7.1
    • toggleOSXFullscreen

      public static void toggleOSXFullscreen(Window window)
    • isFullScreen

      public static boolean isFullScreen(Window window)
      Determines if a window is currently the full screen window.
      Parameters:
      window - the window to check for full screen.
      Returns:
      true if the windoew is full screen or false if not.
      Since:
      3.8.1
    • enterFullScreen

      public static void enterFullScreen(Window window)
      Enters full screen mode for a window. The window must not already be in full screen mode.
      Parameters:
      window - the window to enter full screen.
      Since:
      3.8.1
      See Also:
    • exitFullScreen

      public static void exitFullScreen(Window window)
      Exits full screen mode for a window. The window must be in full screen mode.
      Parameters:
      window - the window to exit full screen.
      Since:
      3.8.1
      See Also:
    • main

      public static void main(String[] argv) throws Exception
      Tests this class.
      Parameters:
      argv - the array of command line parameters.
      Throws:
      Exception - if an error occurred.