Package noaa.coastwatch.gui
Class GUIServices
java.lang.Object
noaa.coastwatch.gui.GUIServices
The GUI services class defines various static methods relating
to graphical user interfaces.
- Since:
- 3.1.1
- Author:
- Peter Hollemans
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Insets
The insets to use for icon-only button margins.static final boolean
The Aqua boolean, true if we are running on a Mac with Aqua look.static final boolean
The GTK boolean, true if we are running the GTK look.static final boolean
The Linux boolean, true if we are running on Linux.static final boolean
The Mac boolean, true if we are running on a Mac.static final boolean
The Windows boolean, true if we are running on Windows.static final String
The preferences key for recently opened files.static final String
The preferences key for applicationwindow height.static final String
The preferences key for application window width. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addFileToRecentlyOpened
(String file, Class targetClass, int maxFiles) Adds the specified file name to the list of recently opened files for the target class.static void
addOpenFileListener
(ActionListener listener) Adds a listener that executes when a file is double-clicked on Mac or Windows.static void
centerOnScreen
(JFrame frame) static Action
createAction
(String name, Runnable runnable) Creates an action with the specified name and listener.static Action
static Composite
createComposite
(BiFunction<int[], int[], Boolean> function) Creates a composite that uses a function to specify the mapping from source to destination pixels.static BiFunction<int[],
int[], Boolean> createDarkenFunction
(double f) Creates a darkening function to be use with a composite.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 byJOptionPane
.static JDialog
createErrorDialog
(Component parent, String title, String userText) Creates a dialog that displays standard error messages to the user in a graphical window.static JLabel
createLinkLabel
(String text, String url) Creates a label with text and URL that opens a browser window.static JDialog
createMessageDialog
(Component parent, String title, String message) Creates a modal message dialog with no user input options.static Image
createModifiedImage
(Image image, BiConsumer<int[], int[]> operator) static JButton
createOnScreenStyleButton
(int size, IconFactory.Purpose purpose) Creates a button with an on screen display type of style.static TranslucentButton
createTranslucentButton
(Icon defaultIcon, Icon rolloverIcon, Icon pressedIcon) Creates a button with an on screen display type of style and translucent properties.static String
ellipsisString
(String input, int maxLength) Truncates a string to a maximum length by embedding an ellipsis in the middle.static void
enterFullScreen
(Window window) Enters full screen mode for a window.static void
exitFullScreen
(Window window) Exits full screen mode for a window.static Component
getAboutComponent
(String tool) Gets an about component appropriate for an about dialog box.static JFileChooser
Gets a file chooser that tracks the user's selected directory.static JButton
getHelpButton
(Class helpClass) Gets a help button for the specified class.static ImageIcon
Gets an icon for the specified purpose.static JButton
getIconButton
(String purpose) Gets a button with icon.static JToggleButton
getIconToggle
(String purpose) Gets a toggle button with icon.static int
Gets the height of a label.static int
getLabelWidth
(int len) Gets the width of a label.static File
Gets the platform default directory for opening new files.getRecentlyOpenedFiles
(Class targetClass) Get the list of recently opened files for the target class.static JButton
getTextButton
(String text) Gets a button with text label.static JButton
getTextButton
(Action action) Gets a button with text label using an action.static void
Initializes the environment prior to creating any GUI components.static void
invokeAndWait
(Runnable doRun) Invokes the runnable using theSwingUtilities.invokeAndWait()
method but discards any exceptions thrown.static boolean
isFullScreen
(Window window) Determines if a window is currently the full screen window.static String
Creates a line-wrapped version of a string.static void
Tests this class.static boolean
recallBooleanSettingForClass
(boolean def, String key, Class targetClass) Recalls a boolean value for the target class.static String
recallStringSettingForClass
(String def, String key, Class targetClass) Recalls a string value for the target class.static Dimension
recallWindowSizeForClass
(Class targetClass) Recalls the specified window size for the target class.static void
setConstraints
(GridBagConstraints c, int gridx, int gridy, int gridwidth, int gridheight, int fill, double weightx, double weighty) Sets the values in aGridBagConstraints
object.static void
setContainerEnabled
(Container container, boolean isEnabled) Sets the enabled flag on all components in a container.static void
setHelpIndex
(URL url) Sets the active help index file for subsequent calls togetHelpButton(java.lang.Class)
.static void
setRecentlyOpenedFiles
(List<String> recentFilesList, Class targetClass) Sets the list of recently opened files for the target class.static void
setSameSize
(List componentList) Sets a list ofJComponent
objects to be the same size as that of the maximum sized component.static void
setSquare
(JComponent comp) Sets a component's preferred, min, and max size to be square.static void
Shows the specified frame in the AWT event dispatching thread.static void
storeBooleanSettingForClass
(boolean value, String key, Class targetClass) Stores a boolean value for the target class.static void
storeStringSettingForClass
(String value, String key, Class targetClass) Stores a string value for the target class.static void
storeWindowSizeForClass
(Dimension windowSize, Class targetClass) Stores the specified window size for the target class.static void
toggleOSXFullscreen
(Window window)
-
Field Details
-
IS_MAC
public static final boolean IS_MACThe Mac boolean, true if we are running on a Mac. -
IS_WIN
public static final boolean IS_WINThe Windows boolean, true if we are running on Windows. -
IS_LINUX
public static final boolean IS_LINUXThe Linux boolean, true if we are running on Linux. -
IS_AQUA
public static final boolean IS_AQUAThe Aqua boolean, true if we are running on a Mac with Aqua look. -
IS_GTK
public static final boolean IS_GTKThe GTK boolean, true if we are running the GTK look. -
ICON_INSETS
The insets to use for icon-only button margins. -
WINDOW_WIDTH_KEY
The preferences key for application window width.- See Also:
-
WINDOW_HEIGHT_KEY
The preferences key for applicationwindow height.- See Also:
-
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
- 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
-
createOnScreenStyleButton
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
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
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
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
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
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 aGridBagConstraints
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
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
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
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
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
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
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
Invokes the runnable using theSwingUtilities.invokeAndWait()
method but discards any exceptions thrown.- Parameters:
doRun
- the object to invoke.
-
showFrame
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 byJOptionPane
. 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
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
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
Creates a dialog that displays standard error messages to the user in a graphical window. The dialog uses aPanelOutputStream
object to redirect standard error messages to a scrolling panel. TheSystem
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
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
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
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
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
Sets the active help index file for subsequent calls togetHelpButton(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
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
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
Sets a list ofJComponent
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
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
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
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
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
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
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
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
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
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
-
isFullScreen
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
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
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
Tests this class.- Parameters:
argv
- the array of command line parameters.- Throws:
Exception
- if an error occurred.
-