dmolony-DiskBrowser/src/com/bytezone/diskbrowser/gui/WindowSaver.java

54 lines
1.3 KiB
Java
Raw Normal View History

2016-03-15 04:40:57 +00:00
package com.bytezone.diskbrowser.gui;
2016-08-02 10:37:27 +00:00
import java.awt.Dimension;
2016-03-15 04:40:57 +00:00
import java.util.prefs.Preferences;
import javax.swing.JFrame;
public class WindowSaver
{
private final Preferences prefs;
private final JFrame frame;
private final String key;
public WindowSaver (Preferences prefs, JFrame frame, String key)
{
this.prefs = prefs;
this.frame = frame;
this.key = key;
}
public void saveWindow ()
{
prefs.putInt (key + "X", frame.getX ());
prefs.putInt (key + "Y", frame.getY ());
prefs.putInt (key + "Height", frame.getHeight ());
prefs.putInt (key + "Width", frame.getWidth ());
}
public boolean restoreWindow ()
{
int x = prefs.getInt (key + "X", -1);
int y = prefs.getInt (key + "Y", -1);
int height = prefs.getInt (key + "Height", -1);
int width = prefs.getInt (key + "Width", -1);
2016-08-02 10:37:27 +00:00
Dimension screen = java.awt.Toolkit.getDefaultToolkit ().getScreenSize ();
2019-10-12 02:47:21 +00:00
if (width < 0) // nothing to restore
2016-03-15 04:40:57 +00:00
{
2016-08-02 10:37:27 +00:00
frame.setLocation (100, 100);
frame.setSize (1000, 600);
2016-03-15 04:40:57 +00:00
frame.setLocationRelativeTo (null); // centre
return false;
}
2016-08-02 10:37:27 +00:00
if (width > screen.getWidth () - 15)
width = (int) (screen.getWidth () - 15);
2016-03-15 04:40:57 +00:00
frame.setSize (width, height);
2016-08-02 10:37:27 +00:00
frame.setLocation (x, y);
2016-03-15 04:40:57 +00:00
return true;
}
}