Skip to content

Commit a5360e3

Browse files
authored
Merge pull request #9 from uobteachingtechnologist/grid-fixes
Grid fixes + saving the window position
2 parents 414c8fa + 2700cb6 commit a5360e3

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

src/com/modsim/gui/GUI.java

Lines changed: 57 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,42 @@ 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 windowX, windowY, windowWidth, windowHeight;
79+
windowX = prefs.getInt("window_x", 0);
80+
windowY = prefs.getInt("window_y", 0);
81+
windowHeight = prefs.getInt("window_height", 600);
82+
windowWidth = prefs.getInt("window_width", 800);
83+
// Check the window is within the boundaries of the active display(s)
84+
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
85+
boolean onDesktop = false;
86+
for (int i = 0; i < devices.length; i++) {
87+
Rectangle displayBounds = devices[i].getDefaultConfiguration().getBounds();
88+
if (displayBounds.intersects(windowX, windowY, windowWidth, windowHeight)) {
89+
onDesktop = true;
90+
}
91+
}
92+
if (onDesktop) {
93+
frame.setBounds(windowX, windowY, windowWidth, windowHeight);
94+
}
95+
else {
96+
frame.pack();
97+
frame.setLocationRelativeTo(null);
98+
}
99+
// Restores the maximise state
100+
if (prefs.getBoolean("window_maximised", false)) {
101+
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
102+
}
103+
}
104+
else {
105+
// Pack & center the window
106+
frame.pack();
107+
frame.setLocationRelativeTo(null);
108+
}
109+
}
76110
frame.setVisible(arg);
77111
}
78112

@@ -117,7 +151,26 @@ private void createFrame() {
117151
frame.addWindowListener(new WindowAdapter() {
118152
@Override
119153
public void windowClosing(WindowEvent e) {
154+
// Prevents closing without saving first
120155
if (checkSave()) {
156+
// Store window size/maximise state
157+
Preferences prefs = Preferences.userNodeForPackage(GUI.class);
158+
if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != 0) {
159+
prefs.putBoolean("window_maximised", true);
160+
Rectangle windowBounds = e.getWindow().getBounds();
161+
prefs.putInt("window_x", windowBounds.x + 50);
162+
prefs.putInt("window_y", windowBounds.y + 50);
163+
}
164+
else {
165+
prefs.putBoolean("window_maximised", false);
166+
Rectangle windowBounds = e.getWindow().getBounds();
167+
prefs.putInt("window_x", windowBounds.x);
168+
prefs.putInt("window_y", windowBounds.y);
169+
prefs.putInt("window_width", windowBounds.width);
170+
prefs.putInt("window_height", windowBounds.height);
171+
}
172+
prefs.putBoolean("window_stored", true);
173+
// Close the window
121174
e.getWindow().dispose();
122175
}
123176
}
@@ -181,6 +234,7 @@ private void createCompPane() {
181234
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
182235
sp.getVerticalScrollBar().setUnitIncrement(10);
183236
sp.setPreferredSize(new Dimension(210,0));
237+
sp.setMinimumSize(new Dimension(210, 60));
184238

185239
hSplit.add(sp, JSplitPane.LEFT);
186240
}

0 commit comments

Comments
 (0)