Class PlatformSupport
- java.lang.Object
-
- ca.cgjennings.platform.PlatformSupport
-
public class PlatformSupport extends java.lang.Object
This utility class provides methods that help integrate an application into the native operating system more cleanly. It is pure Java code.- Author:
- Chris Jennings
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
PLATFORM_IS_MAC
True if the JVM is running on an Apple macOS operating system.static boolean
PLATFORM_IS_OSX
Deprecated.Alias forPLATFORM_IS_MAC
.static boolean
PLATFORM_IS_OTHER
True if running on a non-Windows, non-maxOS operating system.static boolean
PLATFORM_IS_WINDOWS
True if the JVM is running on a Windows operating system.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static javax.swing.JButton
getAgnosticCancel(boolean isInOKCancelOrder, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
Return the button that will be (or is) the Cancel button in an agnostic dialog.static javax.swing.JButton
getAgnosticOK(boolean isInOKCancelOrder, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
Return the button that will be (or is) the OK button in an agnostic dialog.static javax.swing.KeyStroke
getKeyStroke(java.lang.String stroke)
Parse a string to create aKeyStroke
appropriate as an accelerator for the native OS.static void
installNativeLookAndFeel()
Call this method before opening any Swing windows to install the native look and feel.static boolean
isAgnosticOKInFirstPosition()
Returnstrue
if the OK button should be left (ahead) of the Cancel button on this platform.static javax.swing.JButton
makeAgnosticDialog(AgnosticDialog dialog, boolean isInOKCancelOrder, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
Initialize anAgnosticDialog
by swapping the OK and Cancel buttons, if appropriate, and attaching action listeners to theAgnosticDialog.handleOKAction(java.awt.event.ActionEvent)
andAgnosticDialog.handleCancelAction(java.awt.event.ActionEvent)
methods.static javax.swing.JButton
makeAgnosticDialog(AgnosticDialog dialog, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
A convenience method formakeAgnosticDialog( dialog, true, designedOK, designedCancel )
, which assumes that the dialog buttons are in "Windows order" (OK on the left, Cancel on the right).
-
-
-
Field Detail
-
PLATFORM_IS_MAC
public static final boolean PLATFORM_IS_MAC
True if the JVM is running on an Apple macOS operating system.
-
PLATFORM_IS_OSX
@Deprecated public static final boolean PLATFORM_IS_OSX
Deprecated.Alias forPLATFORM_IS_MAC
.
-
PLATFORM_IS_WINDOWS
public static final boolean PLATFORM_IS_WINDOWS
True if the JVM is running on a Windows operating system.
-
PLATFORM_IS_OTHER
public static final boolean PLATFORM_IS_OTHER
True if running on a non-Windows, non-maxOS operating system. Typically this means a Unix-like operating system such as Linux.
-
-
Method Detail
-
makeAgnosticDialog
public static javax.swing.JButton makeAgnosticDialog(AgnosticDialog dialog, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
A convenience method formakeAgnosticDialog( dialog, true, designedOK, designedCancel )
, which assumes that the dialog buttons are in "Windows order" (OK on the left, Cancel on the right).- Parameters:
dialog
- the dialog or other control to be reordereddesignedOK
- the button used as the OK button in the designdesignedCancel
- the button used as the Cancel button in the design- Returns:
- the button that will be used as the OK button
-
makeAgnosticDialog
public static javax.swing.JButton makeAgnosticDialog(AgnosticDialog dialog, boolean isInOKCancelOrder, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
Initialize anAgnosticDialog
by swapping the OK and Cancel buttons, if appropriate, and attaching action listeners to theAgnosticDialog.handleOKAction(java.awt.event.ActionEvent)
andAgnosticDialog.handleCancelAction(java.awt.event.ActionEvent)
methods.This method should generally be applied during the window's construction, before it is first made visible.
By default, this method will automatically swap the text, icon, mnemonic, displayed mnemonic index, and default button status of the two buttons if they need to be swapped. No other properties are changed. For complete control over the actions taken when a swap occurs, implement
QueriedAgnosticDialog
instead. This is a subinterface ofAgnosticDialog
.Notes:
- You must ensure that this method is only called once for a given
AgnosticDialog
during its lifetime, or the button order will be inconsistent and the action events will be called multiple times. - Although referred to in the general sense as the "OK" button, the text of this button should name the specific action it will perform.
- Parameters:
dialog
- the dialog or other control to be reorderedisInOKCancelOrder
-true
is the dialog is designed withdesignedOK
on the leftdesignedOK
- the button that was designed to be the "OK" (accept, commit) buttondesignedCancel
- the button that was designed to be the "Cancel" button- Returns:
- the button that will be used as the OK button
- Throws:
java.lang.IllegalArgumentException
- if either button isnull
or if they refer to the same object
- You must ensure that this method is only called once for a given
-
getAgnosticOK
public static javax.swing.JButton getAgnosticOK(boolean isInOKCancelOrder, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
Return the button that will be (or is) the OK button in an agnostic dialog. This can be called before or aftermakeAgnosticDialog(ca.cgjennings.platform.AgnosticDialog, javax.swing.JButton, javax.swing.JButton)
.- Parameters:
isInOKCancelOrder
-true
is the dialog is designed withdesignedOK
on the leftdesignedOK
- the button that was designed as the "OK" (accept, commit) buttondesignedCancel
- the button that was designed as the "Cancel" button- Returns:
- the button that will represent the OK button in an agnostic dialog
-
getAgnosticCancel
public static javax.swing.JButton getAgnosticCancel(boolean isInOKCancelOrder, javax.swing.JButton designedOK, javax.swing.JButton designedCancel)
Return the button that will be (or is) the Cancel button in an agnostic dialog. This can be called before or aftermakeAgnosticDialog(ca.cgjennings.platform.AgnosticDialog, javax.swing.JButton, javax.swing.JButton)
.- Parameters:
isInOKCancelOrder
-true
is the dialog is designed withdesignedOK
on the leftdesignedOK
- the button that was designed as the "OK" (accept, commit) buttondesignedCancel
- the button that was designed as the "Cancel" button- Returns:
- the button that will represent the OK button in an agnostic dialog
-
isAgnosticOKInFirstPosition
public static boolean isAgnosticOKInFirstPosition()
Returnstrue
if the OK button should be left (ahead) of the Cancel button on this platform.- Returns:
true
if OK comes before Cancel;false
if it comes after
-
installNativeLookAndFeel
public static void installNativeLookAndFeel()
Call this method before opening any Swing windows to install the native look and feel. If they are available in the classpath, various OS-specific optimizations and patches may also be installed.
-
getKeyStroke
public static javax.swing.KeyStroke getKeyStroke(java.lang.String stroke)
Parse a string to create aKeyStroke
appropriate as an accelerator for the native OS. The string must have the following syntax:<modifiers>* (<typedID> | <pressedReleasedID>) modifiers := menu | shift | control | ctrl | meta | alt | altGraph typedID := typed <typedKey> typedKey := string of length 1 giving Unicode character. pressedReleasedID := (pressed | released) key key := KeyEvent key code name, without the "VK_" prefix.
If typed, pressed or released is not specified, pressed is assumed.The special pseudo-modifier "menu" will be converted into the correct menu accelerator key for the native platform. For example, "menu X" will be treated as "ctrl X" on the Windows platform, but as "meta X" (which is Command key + X) on Max OS X platform. Note that there is no way to determine from the returned
KeyStroke
instance whether the "menu" modifier was used or not.- Parameters:
stroke
- a string formatted as above- Returns:
- a
KeyStroke
object representing the specified key event
-
-