2
0

Java Client: Remember window size/position

This commit is contained in:
Michael Ziminsky (Z) 2018-10-29 13:01:44 -07:00
parent 2e5d3b32e8
commit e9ad781ebe
3 changed files with 114 additions and 23 deletions

View File

@ -22,20 +22,28 @@ import com.lyndir.lhunath.opal.system.util.ConversionUtils;
import com.lyndir.masterpassword.model.MPConfig;
import com.lyndir.masterpassword.model.MPModelConstants;
import java.awt.Dimension;
import java.awt.Point;
/**
* @author lhunath, 2014-08-31
*/
@SuppressWarnings("CallToSystemGetenv")
@SuppressWarnings({"CallToSystemGetenv", "WeakerAccess"})
public class MPGuiConfig extends MPConfig {
public static MPGuiConfig get() {
return get( MPGuiConfig.class );
}
Boolean checkForUpdates;
Boolean stayResident;
Boolean logoutOnClose;
// Visible for serialization
protected Boolean checkForUpdates;
protected Boolean stayResident;
protected Boolean logoutOnClose;
protected Dimension windowSize;
protected Point windowLocation;
protected Boolean windowMaximized;
public boolean checkForUpdates() {
return (checkForUpdates != null)? checkForUpdates:
@ -65,4 +73,31 @@ public class MPGuiConfig extends MPConfig {
this.logoutOnClose = logoutOnClose;
setChanged();
}
public Dimension getWindowSize() {
return windowSize;
}
public void setWindowSize(Dimension windowSize) {
this.windowSize = windowSize;
setChanged();
}
public Point getWindowLocation() {
return windowLocation;
}
public void setWindowLocation(Point windowPosition) {
this.windowLocation = windowPosition;
setChanged();
}
public Boolean getWindowMaximized() {
return (windowMaximized != null)? windowMaximized: false;
}
public void setWindowMaximized(Boolean windowMaximized) {
this.windowMaximized = windowMaximized;
setChanged();
}
}

View File

@ -38,15 +38,33 @@ public class MasterPasswordFrame extends JFrame {
userPanel.add( userContent.getUserToolbar(), BorderLayout.LINE_START );
userPanel.add( userContent.getSiteToolbar(), BorderLayout.LINE_END );
final MPGuiConfig config = MPGuiConfig.get();
if (config.getWindowSize() != null) {
setSize(config.getWindowSize());
}
else {
setPreferredSize(new Dimension(800, 560));
pack();
}
if (config.getWindowLocation() != null) {
setLocation(config.getWindowLocation());
}
else {
setLocationRelativeTo(null);
setLocationByPlatform(true);
}
if (config.getWindowMaximized())
setExtendedState(MAXIMIZED_BOTH);
final WindowHandler windowHandler = new WindowHandler();
addWindowListener(windowHandler);
addWindowStateListener(windowHandler);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(windowHandler);
setPreferredSize( new Dimension( 800, 560 ) );
setDefaultCloseOperation( DISPOSE_ON_CLOSE );
pack();
setLocationRelativeTo( null );
setLocationByPlatform( true );
addComponentListener(new ResizeHandler());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
@ -66,6 +84,14 @@ public class MasterPasswordFrame extends JFrame {
}
}
@Override
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == MAXIMIZED_BOTH)
MPGuiConfig.get().setWindowMaximized(true);
else if (e.getOldState() == MAXIMIZED_BOTH)
MPGuiConfig.get().setWindowMaximized(false);
}
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
@ -75,4 +101,18 @@ public class MasterPasswordFrame extends JFrame {
return false;
}
}
private static class ResizeHandler extends ComponentAdapter {
@Override
public void componentResized(ComponentEvent e) {
if ((((Frame) e.getComponent()).getExtendedState() & MAXIMIZED_BOTH) != MAXIMIZED_BOTH)
MPGuiConfig.get().setWindowSize(e.getComponent().getSize());
}
@Override
public void componentMoved(ComponentEvent e) {
if ((((Frame) e.getComponent()).getExtendedState() & MAXIMIZED_BOTH) != MAXIMIZED_BOTH)
MPGuiConfig.get().setWindowLocation(e.getComponent().getLocation());
}
}
}

View File

@ -1,6 +1,5 @@
package com.lyndir.masterpassword.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;
import com.lyndir.lhunath.opal.system.logging.Logger;
@ -8,6 +7,8 @@ import com.lyndir.masterpassword.model.impl.Changeable;
import com.lyndir.masterpassword.model.impl.MPJSONAnyObject;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
/**
@ -20,18 +21,7 @@ public class MPConfig extends MPJSONAnyObject {
private static final ClassToInstanceMap<MPConfig> instances = MutableClassToInstanceMap.create();
private static final File configFile = new File( rcDir(), "config.json" );
private final Changeable changeable = new Changeable() {
@Override
protected void onChanged() {
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue( configFile, MPConfig.this );
instances.clear();
}
catch (final IOException e) {
logger.err( e, "While saving config to: %s", configFile );
}
}
};
private final Changeable changeable = new ChangeHandler();
protected static synchronized <C extends MPConfig> C get(final Class<C> type) {
C instance = instances.getInstance( type );
@ -75,4 +65,30 @@ public class MPConfig extends MPJSONAnyObject {
return new File( home, ".mpw.d" );
}
private class ChangeHandler extends Changeable {
private Timer saveTimer = new Timer(false);
private TimerTask saveTask = new SaveTask();
@Override
protected void onChanged() {
saveTask.cancel();
saveTimer.schedule(saveTask = new SaveTask(), 1000);
}
private class SaveTask extends TimerTask {
@Override
public void run() {
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue( configFile, MPConfig.this );
instances.clear();
}
catch (final IOException e) {
logger.err( e, "While saving config to: %s", configFile );
}
saveTimer.purge(); // cleanup cancelled jobs
}
}
}
}