|
11 | 11 | import java.util.ArrayList;
|
12 | 12 | import java.awt.*;
|
13 | 13 | import java.net.URL;
|
| 14 | +import java.util.prefs.Preferences; |
14 | 15 |
|
15 | 16 | import javax.swing.*;
|
16 | 17 | import javax.swing.UIManager.LookAndFeelInfo;
|
@@ -70,9 +71,42 @@ public void generateUI() {
|
70 | 71 | * @param arg Whether or not to display
|
71 | 72 | */
|
72 | 73 | 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 | + } |
76 | 110 | frame.setVisible(arg);
|
77 | 111 | }
|
78 | 112 |
|
@@ -117,7 +151,26 @@ private void createFrame() {
|
117 | 151 | frame.addWindowListener(new WindowAdapter() {
|
118 | 152 | @Override
|
119 | 153 | public void windowClosing(WindowEvent e) {
|
| 154 | + // Prevents closing without saving first |
120 | 155 | 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 |
121 | 174 | e.getWindow().dispose();
|
122 | 175 | }
|
123 | 176 | }
|
@@ -181,6 +234,7 @@ private void createCompPane() {
|
181 | 234 | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
182 | 235 | sp.getVerticalScrollBar().setUnitIncrement(10);
|
183 | 236 | sp.setPreferredSize(new Dimension(210,0));
|
| 237 | + sp.setMinimumSize(new Dimension(210, 60)); |
184 | 238 |
|
185 | 239 | hSplit.add(sp, JSplitPane.LEFT);
|
186 | 240 | }
|
|
0 commit comments