Skip to content

Commit 1738458

Browse files
committed
Store window bounds between uses
1 parent 7730a58 commit 1738458

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

src/com/modsim/gui/GUI.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.ArrayList;
1212
import java.awt.*;
1313
import java.net.URL;
14+
import java.util.prefs.Preferences;
1415

1516
import javax.swing.*;
1617
import javax.swing.UIManager.LookAndFeelInfo;
@@ -70,9 +71,27 @@ public void generateUI() {
7071
* @param arg Whether or not to display
7172
*/
7273
public void showUI(boolean arg) {
73-
// Pack, centre and show
74-
frame.pack();
75-
frame.setLocationRelativeTo(null);
74+
if (arg) {
75+
Preferences prefs = Preferences.userNodeForPackage(GUI.class);
76+
if (prefs.getBoolean("window_stored", false)) {
77+
// Window border has been stored
78+
int window_x, window_y, window_width, window_height;
79+
window_x = prefs.getInt("window_x", 0);
80+
window_y = prefs.getInt("window_y", 0);
81+
window_height = prefs.getInt("window_height", 600);
82+
window_width = prefs.getInt("window_width", 800);
83+
frame.setBounds(window_x, window_y, window_width, window_height);
84+
// Restore maximise state
85+
if (prefs.getBoolean("window_maximised", false)) {
86+
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
87+
}
88+
}
89+
else {
90+
// Pack & center the window
91+
frame.pack();
92+
frame.setLocationRelativeTo(null);
93+
}
94+
}
7695
frame.setVisible(arg);
7796
}
7897

@@ -117,7 +136,26 @@ private void createFrame() {
117136
frame.addWindowListener(new WindowAdapter() {
118137
@Override
119138
public void windowClosing(WindowEvent e) {
139+
// Prevents closing without saving first
120140
if (checkSave()) {
141+
// Store window size/maximise state
142+
Preferences prefs = Preferences.userNodeForPackage(GUI.class);
143+
if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != 0) {
144+
prefs.putBoolean("window_maximised", true);
145+
Rectangle windowBounds = e.getWindow().getBounds();
146+
prefs.putInt("window_x", windowBounds.x + 50);
147+
prefs.putInt("window_y", windowBounds.y + 50);
148+
}
149+
else {
150+
prefs.putBoolean("window_maximised", false);
151+
Rectangle windowBounds = e.getWindow().getBounds();
152+
prefs.putInt("window_x", windowBounds.x);
153+
prefs.putInt("window_y", windowBounds.y);
154+
prefs.putInt("window_width", windowBounds.width);
155+
prefs.putInt("window_height", windowBounds.height);
156+
}
157+
prefs.putBoolean("window_stored", true);
158+
// Close the window
121159
e.getWindow().dispose();
122160
}
123161
}
@@ -181,6 +219,7 @@ private void createCompPane() {
181219
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
182220
sp.getVerticalScrollBar().setUnitIncrement(10);
183221
sp.setPreferredSize(new Dimension(210,0));
222+
sp.setMinimumSize(new Dimension(210, 60));
184223

185224
hSplit.add(sp, JSplitPane.LEFT);
186225
}

0 commit comments

Comments
 (0)