jAER project on SourceForge

net.sf.jaer.util
Class WindowSaver

java.lang.Object
  extended by net.sf.jaer.util.WindowSaver
All Implemented Interfaces:
java.awt.event.AWTEventListener, java.util.EventListener

public class WindowSaver
extends java.lang.Object
implements java.awt.event.AWTEventListener

adapted from O'Reilly book Swing Hacks by Marinacci and Adamson ISBN 0-596-00907-0. Used to save and restore window positions. Static methods allow explicit saving and restoring, or the user can do the following in their main class:
Toolkit.getDefaultToolkit().addAWTEventListener(windowSaver,AWTEvent.WINDOW_EVENT_MASK); .
Then (magically) global window opening events will result in callbacks to eventDispatched which loads saved settings, keyed on the frame. A class could add a ShutdownHook to save the last window settings:

Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
if(windowSaver!=null){
try{
windowSaver.saveSettings();
}catch(IOException e){
e.printStackTrace();
}
}
}
});

Unexpected behavior can result if the user application resizes its own windows after the window settings are loaded.


Nested Class Summary
static interface WindowSaver.DontResize
          This marker interface can be implemented to avoid resizing the window
 
Field Summary
 int DEFAULT_HEIGHT
          Default width and height values.
 int DEFAULT_WIDTH
          Default width and height values.
 int OFFSET_FROM_SAME
          Offset from last window with same name.
 int WINDOWS_TASK_BAR_HEIGHT
           
 
Constructor Summary
WindowSaver(java.lang.Object o, java.util.prefs.Preferences preferences)
          Creates a new instance of WindowSaver.
 
Method Summary
 void eventDispatched(java.awt.AWTEvent evt)
          Called when event is dispatched.
 void loadSettings(javax.swing.JFrame frame)
          The preferred settings are loaded based on window name.
static void restoreWindowLocation(java.awt.Window window, java.util.prefs.Preferences prefs)
          This static method can be used to restore the window x,y, position (but not size) of a window based on the Window class name.
 void saveSettings()
          Used to explicity save settings.
static void saveWindowLocation(java.awt.Window window, java.util.prefs.Preferences prefs)
          This static method can be used to save the window x,y, position (but not size).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WINDOWS_TASK_BAR_HEIGHT

public final int WINDOWS_TASK_BAR_HEIGHT
See Also:
Constant Field Values

OFFSET_FROM_SAME

public final int OFFSET_FROM_SAME
Offset from last window with same name.

See Also:
Constant Field Values

DEFAULT_WIDTH

public final int DEFAULT_WIDTH
Default width and height values. Width and height are not set for a window unless preferences are saved

See Also:
Constant Field Values

DEFAULT_HEIGHT

public final int DEFAULT_HEIGHT
Default width and height values. Width and height are not set for a window unless preferences are saved

See Also:
Constant Field Values
Constructor Detail

WindowSaver

public WindowSaver(java.lang.Object o,
                   java.util.prefs.Preferences preferences)
Creates a new instance of WindowSaver.

Parameters:
o - the object for which to save
preferences - the user preferences to save to
Method Detail

eventDispatched

public void eventDispatched(java.awt.AWTEvent evt)
Called when event is dispatched. WindowEvent.WINDOW_OPENED events for JFrames are processed here to loadSettings.

Specified by:
eventDispatched in interface java.awt.event.AWTEventListener
Parameters:
evt - the AWTEvent. Only WINDOW_OPENED events are processed to loadSettings
See Also:
loadSettings(javax.swing.JFrame)

loadSettings

public void loadSettings(javax.swing.JFrame frame)
                  throws java.io.IOException
The preferred settings are loaded based on window name. A windows which would be displayed partly off-screen is moved to originate at 0,0. A window which would be too tall or wide is resized to screen size.

Parameters:
frame - JFrame to load settings for
Throws:
java.io.IOException

saveSettings

public void saveSettings()
                  throws java.io.IOException
Used to explicity save settings. Saves the x,y and width, height settings of window in preferences.

Throws:
java.io.IOException

restoreWindowLocation

public static void restoreWindowLocation(java.awt.Window window,
                                         java.util.prefs.Preferences prefs)
This static method can be used to restore the window x,y, position (but not size) of a window based on the Window class name. This is a separate mechanism than the instance methods saveSettings and loadSettings.

Parameters:
window - the window to restore
prefs - user preferences node
See Also:
saveWindowLocation(java.awt.Window, java.util.prefs.Preferences)

saveWindowLocation

public static void saveWindowLocation(java.awt.Window window,
                                      java.util.prefs.Preferences prefs)
This static method can be used to save the window x,y, position (but not size). This statis method saves the window origin but not the size, based on a classname-based key in the supplied preferences node.

Parameters:
window - the window to save for
prefs - user preferences node
See Also:
restoreWindowLocation(java.awt.Window, java.util.prefs.Preferences)

jAER project on SourceForge