public class GUIServices
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.awt.Insets |
ICON_INSETS
The insets to use for icon-only button margins.
|
static boolean |
IS_AQUA
The Aqua boolean, true if we are running on a Mac with Aqua look.
|
static boolean |
IS_GTK
The GTK boolean, true if we are running the GTK look.
|
static boolean |
IS_MAC
The Mac boolean, true if we are running on a Mac.
|
static boolean |
IS_WIN
The Windows boolean, true if we are running on Windows.
|
static java.lang.String |
RECENT_FILES_KEY
The preferences key for recently opened files.
|
static java.lang.String |
WINDOW_HEIGHT_KEY
The preferences key for applicationwindow height.
|
static java.lang.String |
WINDOW_WIDTH_KEY
The preferences key for application window width.
|
Modifier and Type | Method and Description |
---|---|
static void |
addFileToRecentlyOpened(java.lang.String file,
java.lang.Class targetClass,
int maxFiles)
Adds the specified file name to the list of recently opened files for the
target class.
|
static void |
addMacOpenFileListener(java.awt.event.ActionListener listener)
Adds a listener that executes when a file is opened on a Mac via
the Finder (for example by double-clicking).
|
static javax.swing.Action |
createAction(java.lang.String name,
java.lang.Runnable runnable)
Creates an action with the specified name and listener.
|
static javax.swing.JDialog |
createDialog(java.awt.Component parent,
java.lang.String title,
boolean modal,
java.awt.Component component,
java.awt.Component[] controls,
javax.swing.Action[] actions,
boolean[] hideAction,
boolean doDispose)
Creates a dialog with similar layout to dialogs produced by
JOptionPane . |
static javax.swing.JDialog |
createErrorDialog(java.awt.Component parent,
java.lang.String title,
java.lang.String userText)
Creates a dialog that displays standard error messages to the
user in a graphical window.
|
static javax.swing.JDialog |
createMessageDialog(java.awt.Component parent,
java.lang.String title,
java.lang.String message)
Creates a modal message dialog with no user input options.
|
static java.lang.String |
ellipsisString(java.lang.String input,
int maxLength)
Truncates a string to a maximum length by embedding an ellipsis in the
middle.
|
static javax.swing.JFileChooser |
getFileChooser()
Gets a file chooser that tracks the user's selected directory.
|
static javax.swing.JButton |
getHelpButton(java.lang.Class helpClass)
Gets a help button for the specified class.
|
static javax.swing.ImageIcon |
getIcon(java.lang.String purpose)
Gets an icon for the specified purpose.
|
static javax.swing.JButton |
getIconButton(java.lang.String purpose)
Gets a button with icon.
|
static javax.swing.JToggleButton |
getIconToggle(java.lang.String purpose)
Gets a toggle button with icon.
|
static java.io.File |
getPlatformDefaultDirectory()
Gets the platform default directory for opening new files.
|
static java.util.List<java.lang.String> |
getRecentlyOpenedFiles(java.lang.Class targetClass)
Get the list of recently opened files for the target class.
|
static javax.swing.JButton |
getTextButton(javax.swing.Action action)
Gets a button with text label using an action.
|
static javax.swing.JButton |
getTextButton(java.lang.String text)
Gets a button with text label.
|
static void |
invokeAndWait(java.lang.Runnable doRun)
Invokes the runnable using the
SwingUtilities.invokeAndWait() method but discards
any exceptions thrown. |
static java.lang.String |
lineWrap(java.lang.String input,
int columns)
Creates a line-wrapped version of a string.
|
static void |
main(java.lang.String[] argv)
Tests this class.
|
static boolean |
recallBooleanSettingForClass(boolean def,
java.lang.String key,
java.lang.Class targetClass)
Recalls a boolean value for the target class.
|
static java.awt.Dimension |
recallWindowSizeForClass(java.lang.Class targetClass)
Recalls the specified window size for the target class.
|
static void |
setConstraints(java.awt.GridBagConstraints c,
int gridx,
int gridy,
int gridwidth,
int gridheight,
int fill,
double weightx,
double weighty)
Sets the values in a
GridBagConstraints object. |
static void |
setContainerEnabled(java.awt.Container container,
boolean isEnabled)
Sets the enabled flag on all components in a container.
|
static void |
setHelpIndex(java.net.URL url)
Sets the active help index file for subsequent calls to
getHelpButton(java.lang.Class) . |
static void |
setRecentlyOpenedFiles(java.util.List<java.lang.String> recentFilesList,
java.lang.Class targetClass)
Sets the list of recently opened files for the target class.
|
static void |
setSameSize(java.util.List componentList)
Sets a list of
JComponent objects to be the
same size as that of the maximum sized component. |
static void |
setSquare(javax.swing.JComponent comp)
Sets a component's preferred, min, and max size to be square.
|
static void |
storeBooleanSettingForClass(boolean value,
java.lang.String key,
java.lang.Class targetClass)
Stores a boolean value for the target class.
|
static void |
storeWindowSizeForClass(java.awt.Dimension windowSize,
java.lang.Class targetClass)
Stores the specified window size for the target class.
|
public static final boolean IS_MAC
public static final boolean IS_WIN
public static final boolean IS_AQUA
public static final boolean IS_GTK
public static final java.awt.Insets ICON_INSETS
public static final java.lang.String WINDOW_WIDTH_KEY
public static final java.lang.String WINDOW_HEIGHT_KEY
public static final java.lang.String RECENT_FILES_KEY
public static java.io.File getPlatformDefaultDirectory()
public static void setConstraints(java.awt.GridBagConstraints c, int gridx, int gridy, int gridwidth, int gridheight, int fill, double weightx, double weighty)
GridBagConstraints
object.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.public static javax.swing.JButton getTextButton(javax.swing.Action action)
action
- the action for the button.public static javax.swing.JButton getTextButton(java.lang.String text)
text
- the text label.public static javax.swing.JButton getIconButton(java.lang.String purpose)
purpose
- the icon purpose.java.lang.IllegalArgumentException
- if the icon purpose has no known
icon, or the icon resource cannot be found.public static javax.swing.JToggleButton getIconToggle(java.lang.String purpose)
purpose
- the icon purpose.java.lang.IllegalArgumentException
- if the icon purpose has no known
icon, or the icon resource cannot be found.public static void setSquare(javax.swing.JComponent comp)
comp
- the component to modify.public static void setContainerEnabled(java.awt.Container container, boolean isEnabled)
container
- the container to modify.isEnabled
- the enabled flag, true to enable or false to
disable.public static void invokeAndWait(java.lang.Runnable doRun)
SwingUtilities.invokeAndWait()
method but discards
any exceptions thrown.doRun
- the object to invoke.public static javax.swing.JDialog createDialog(java.awt.Component parent, java.lang.String title, boolean modal, java.awt.Component component, java.awt.Component[] controls, javax.swing.Action[] actions, boolean[] hideAction, boolean doDispose)
JOptionPane
. The dialog has a main message area,
and a set of buttons.parent
- the dialog parent component.title
- the dialog title string.modal
- true if the dialog should be model, 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.public static java.lang.String lineWrap(java.lang.String input, int columns)
input
- the string to line-wrap.columns
- the number of columns to wrap at.public static java.lang.String ellipsisString(java.lang.String input, int maxLength)
input
- the input string to truncate.maxLength
- the maximum length of the output string.public static javax.swing.JDialog createErrorDialog(java.awt.Component parent, java.lang.String title, java.lang.String userText)
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.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.java.lang.IllegalStateException
- if the error dialog has already
been created.public static javax.swing.Action createAction(java.lang.String name, java.lang.Runnable runnable)
name
- the action name.runnable
- the runnable to invoke when the action is
performed, or null for no operation.public static javax.swing.JDialog createMessageDialog(java.awt.Component parent, java.lang.String title, java.lang.String message)
parent
- the parent component.title
- the dialog title.message
- the message to display.public static javax.swing.ImageIcon getIcon(java.lang.String purpose)
purpose
- the icon purpose.java.lang.IllegalArgumentException
- if the icon purpose has no known
icon, or the icon resource cannot be found.public static javax.swing.JButton getHelpButton(java.lang.Class helpClass)
helpClass
- the class to get a help button for.java.lang.IllegalArgumentException
- if the class has no known
help resource, or the help resource cannot be found.public static void setHelpIndex(java.net.URL url)
getHelpButton(java.lang.Class)
. HTML panels will go to the
specified index when the Home button is clicked.url
- the help index URL to use for the help system.public static javax.swing.JFileChooser getFileChooser()
public static void addMacOpenFileListener(java.awt.event.ActionListener listener)
MacGUIServices
class via reflection so
that it will run correctly no matter the platform.listener
- the listener to call.java.lang.RuntimeException
- if called on a non-Mac platform.MacGUIServices
public static void setSameSize(java.util.List componentList)
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.componentList
- the list of components.public static java.util.List<java.lang.String> getRecentlyOpenedFiles(java.lang.Class targetClass)
targetClass
- the class for the recent files.public static void addFileToRecentlyOpened(java.lang.String file, java.lang.Class targetClass, int maxFiles)
file
- the file name to add.targetClass
- the class for the recent files.maxFiles
- the maximum number of files to store.public static void setRecentlyOpenedFiles(java.util.List<java.lang.String> recentFilesList, java.lang.Class targetClass)
recentFilesList
- the new list of recently opened files, possibly
empty.targetClass
- the class for the recent files.public static void storeWindowSizeForClass(java.awt.Dimension windowSize, java.lang.Class targetClass)
windowSize
- the window size to store.targetClass
- the class to associate with the window size.recallWindowSizeForClass(java.lang.Class)
public static java.awt.Dimension recallWindowSizeForClass(java.lang.Class targetClass)
targetClass
- the class to recall the window size.storeWindowSizeForClass(java.awt.Dimension, java.lang.Class)
public static void storeBooleanSettingForClass(boolean value, java.lang.String key, java.lang.Class targetClass)
value
- the value to store.key
- the key to use for storing the boolen value.targetClass
- the class to associate with the value.public static boolean recallBooleanSettingForClass(boolean def, java.lang.String key, java.lang.Class targetClass)
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.public static void main(java.lang.String[] argv) throws java.lang.Exception
argv
- the array of command line parameters.java.lang.Exception
- if an error occurred.