com.jgoodies.uif.util
Class WindowUtils

java.lang.Object
  extended by com.jgoodies.uif.util.WindowUtils

public final class WindowUtils
extends Object

Provides static convenience behavior to store and restore window bounds in a Preferences node and to ensure a minimum size for Swing windows.

Window bounds can be stored by the #storeBounds methods. These require that the given JWindow, JDialog or JFrame provide a unique window ID that is requested by #getWindowID().

A second feature allows to ensure a window minimum size. The SizeChangeHandler listens to window resize events and sets a new size if the user has shrunken the window below a given dimension. This mechanism requires that the Swing window class implements a method #getWindowMinimumSize().

Both features have been specialized for instances of JFrame, to avoid storing the bounds of a maximized frame in the preferences. Therefore the SizeChangeHandler stores bounds (in a frame client property) but only if the frame is not maximized. Using a ComponentAdapter that stores bounds every time the window has been moved or resized fails, because sometimes expanding a frame is splitted in a move event plus resize event, where the first move does not yet indicate the expanded state - even if delayed using SwingUtilities#invokeLater.

Version:
$Revision: 1.16 $
Author:
Karsten Lentzsch

Nested Class Summary
static class WindowUtils.SizeChangeHandler
          A ComponentListener that handles size changes in Windows to ensure a minimum size.
 
Method Summary
static Window getParent(EventObject e)
          Returns the parent window for a given event.
static void restoreBounds(JDialog dialog, Preferences prefs)
          Restores the window's bounds from the given preferences.
static void restoreBounds(JFrame frame, Preferences prefs)
          Restores the frame's bounds from the given preferences.
static void restoreBounds(JWindow window, Preferences prefs)
          Restores the window's bounds from the given preferences.
static void restoreState(Frame frame, Preferences prefs)
          Restores the frame's state from the given preferences.
static void restoreState(Frame frame, Preferences prefs, boolean restoreIconified)
          Restores the frame's state from the given preferences.
static void setImageIcon(Frame frame, Image image12x12, Image image16x16)
          Chooses an appropriate image from the given image set and sets it as image icon in the given frame.
static void storeBounds(Preferences prefs, Frame frame)
          Stores the frame bounds in a child node of the given preferences, that reflects the current screen configuration.
static void storeBounds(Preferences prefs, JDialog dialog)
          Stores the dialog bounds in a child node of the given preferences, that reflects the current screen configuration.
static void storeBounds(Preferences prefs, JWindow window)
          Stores the window bounds in a child node of the given preferences, that reflects the current screen configuration.
static void storeState(Preferences prefs, Frame frame)
          Stores the frame's extended state in a child node of given preferences that reflects the current screen configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setImageIcon

public static void setImageIcon(Frame frame,
                                Image image12x12,
                                Image image16x16)
Chooses an appropriate image from the given image set and sets it as image icon in the given frame. Checks the platform and screen resolution to find the image size that fits best.

Parameters:
frame - the frame to set an icon image for
image12x12 - a 12x12 pixel image useful for Windows 2000
image16x16 - a 16x16 pixel image used for most platforms
Since:
1.3

storeBounds

public static void storeBounds(Preferences prefs,
                               JWindow window)
Stores the window bounds in a child node of the given preferences, that reflects the current screen configuration. The bounds Rectangle is encoded as a String.

The window must provide an ID via method #getWindowID.

Parameters:
prefs - the preferences to store the bounds in
window - the window that provides the bounds
Throws:
IllegalArgumentException - if the window provides no window ID

storeBounds

public static void storeBounds(Preferences prefs,
                               JDialog dialog)
Stores the dialog bounds in a child node of the given preferences, that reflects the current screen configuration. The bounds Rectangle is encoded as a String.

The dialog must provide an ID via method #getWindowID.

Parameters:
prefs - the preferences to store the bounds in
dialog - the dialog that provides the bounds
Throws:
IllegalArgumentException - if the dialog provides no window ID

storeBounds

public static void storeBounds(Preferences prefs,
                               Frame frame)
Stores the frame bounds in a child node of the given preferences, that reflects the current screen configuration. The bounds are stored, if and only if the frame is visible and not maximized in both directions. The bounds Rectangle is encoded as a String.

The frame must provide an ID via method #getWindowID.

Parameters:
prefs - the preferences to store the bounds in
frame - the frame that provides the bounds
Throws:
IllegalArgumentException - if the frame provides no window ID

storeState

public static void storeState(Preferences prefs,
                              Frame frame)
Stores the frame's extended state in a child node of given preferences that reflects the current screen configuration. The state is encoded as a String that represents the state's bit mask.

The frame must provide an ID via method #getWindowID.

Parameters:
prefs - the preferences to store the extended state in
frame - the frame that provides the window ID, state and toolkit
Throws:
IllegalArgumentException - if the frame provides no window ID
Since:
1.4

restoreBounds

public static void restoreBounds(JWindow window,
                                 Preferences prefs)
Restores the window's bounds from the given preferences. Decodes the String encoding of the bounds rectangle, and sets the bounds if the bounds are present and have a valid encoding.

The window must provide an ID via method #getWindowID.

Parameters:
window - the window to be restored
prefs - the preferences that provide the bounds
Throws:
IllegalArgumentException - if the window provides no window ID

restoreBounds

public static void restoreBounds(JDialog dialog,
                                 Preferences prefs)
Restores the window's bounds from the given preferences. Decodes the String encoding of the bounds rectangle, and sets the bounds if the bounds are present and have a valid encoding.

The dialog must provide an ID via method #getWindowID.

Parameters:
dialog - the dialog to be restored
prefs - the preferences that provide the bounds
Throws:
IllegalArgumentException - if the dialog provides no window ID

restoreBounds

public static void restoreBounds(JFrame frame,
                                 Preferences prefs)
Restores the frame's bounds from the given preferences. Decodes the String encoding of the bounds rectangle, and sets the bounds if the bounds are present and have a valid encoding.

The frame must provide an ID via method #getWindowID.

Parameters:
frame - the frame to be restored
prefs - the preferences that provide the bounds
Throws:
IllegalArgumentException - if the frame provides no window ID

restoreState

public static void restoreState(Frame frame,
                                Preferences prefs)
Restores the frame's state from the given preferences. Decodes the stored encoded state and sets it - even if invalid or not supported by the platform. In the latter two cases, the frame will do nothing.

The frame must provide an ID via method #getWindowID.

Parameters:
frame - the frame to be restored
prefs - the preferences that provide the state
Throws:
IllegalArgumentException - if the frame provides no window ID
Since:
1.4

restoreState

public static void restoreState(Frame frame,
                                Preferences prefs,
                                boolean restoreIconified)
Restores the frame's state from the given preferences. Decodes the stored encoded state and sets it - even if invalid or not supported by the platform. In the latter two cases, the frame will do nothing. The iconified state is restored iff the boolean parameter is set to true.

The frame must provide an ID via method #getWindowID.

Parameters:
frame - the frame to be restored
prefs - the preferences that provide the state
restoreIconified - true to restore the iconified state, false to not restore the iconified state
Throws:
IllegalArgumentException - if the frame provides no window ID
Since:
1.4

getParent

public static Window getParent(EventObject e)
Returns the parent window for a given event.

Parameters:
e - the event that may have a component as source.
Returns:
the parent window of the event's source
Since:
1.7


Copyright © 2000-2008 JGoodies Karsten Lentzsch. All Rights Reserved.