Skip to content

Commit

Permalink
Merge pull request #9 from uobteachingtechnologist/grid-fixes
Browse files Browse the repository at this point in the history
Grid fixes + saving the window position
  • Loading branch information
jackoson authored Oct 24, 2016
2 parents 414c8fa + 2700cb6 commit a5360e3
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions src/com/modsim/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.awt.*;
import java.net.URL;
import java.util.prefs.Preferences;

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
Expand Down Expand Up @@ -70,9 +71,42 @@ public void generateUI() {
* @param arg Whether or not to display
*/
public void showUI(boolean arg) {
// Pack, centre and show
frame.pack();
frame.setLocationRelativeTo(null);
if (arg) {
Preferences prefs = Preferences.userNodeForPackage(GUI.class);
if (prefs.getBoolean("window_stored", false)) {
// Window border has been stored
int windowX, windowY, windowWidth, windowHeight;
windowX = prefs.getInt("window_x", 0);
windowY = prefs.getInt("window_y", 0);
windowHeight = prefs.getInt("window_height", 600);
windowWidth = prefs.getInt("window_width", 800);
// Check the window is within the boundaries of the active display(s)
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
boolean onDesktop = false;
for (int i = 0; i < devices.length; i++) {
Rectangle displayBounds = devices[i].getDefaultConfiguration().getBounds();
if (displayBounds.intersects(windowX, windowY, windowWidth, windowHeight)) {
onDesktop = true;
}
}
if (onDesktop) {
frame.setBounds(windowX, windowY, windowWidth, windowHeight);
}
else {
frame.pack();
frame.setLocationRelativeTo(null);
}
// Restores the maximise state
if (prefs.getBoolean("window_maximised", false)) {
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
}
else {
// Pack & center the window
frame.pack();
frame.setLocationRelativeTo(null);
}
}
frame.setVisible(arg);
}

Expand Down Expand Up @@ -117,7 +151,26 @@ private void createFrame() {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Prevents closing without saving first
if (checkSave()) {
// Store window size/maximise state
Preferences prefs = Preferences.userNodeForPackage(GUI.class);
if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != 0) {
prefs.putBoolean("window_maximised", true);
Rectangle windowBounds = e.getWindow().getBounds();
prefs.putInt("window_x", windowBounds.x + 50);
prefs.putInt("window_y", windowBounds.y + 50);
}
else {
prefs.putBoolean("window_maximised", false);
Rectangle windowBounds = e.getWindow().getBounds();
prefs.putInt("window_x", windowBounds.x);
prefs.putInt("window_y", windowBounds.y);
prefs.putInt("window_width", windowBounds.width);
prefs.putInt("window_height", windowBounds.height);
}
prefs.putBoolean("window_stored", true);
// Close the window
e.getWindow().dispose();
}
}
Expand Down Expand Up @@ -181,6 +234,7 @@ private void createCompPane() {
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.getVerticalScrollBar().setUnitIncrement(10);
sp.setPreferredSize(new Dimension(210,0));
sp.setMinimumSize(new Dimension(210, 60));

hSplit.add(sp, JSplitPane.LEFT);
}
Expand Down

0 comments on commit a5360e3

Please sign in to comment.