GUI size is now saved and loaded, to prevent hassle for the user

This commit is contained in:
Kristian Knarvik 2018-02-17 23:05:12 +01:00
parent 1ec620f87a
commit 2fac5f403d
5 changed files with 68 additions and 47 deletions

View File

@ -27,7 +27,6 @@ import static net.knarcraft.serverlauncher.Shared.stringBetween;
*/ */
public class Main { public class Main {
private static GUI gui;
@SuppressWarnings("CanBeFinal") @SuppressWarnings("CanBeFinal")
public static String appDir; public static String appDir;
@ -44,7 +43,6 @@ public class Main {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
try { try {
setup(); setup();
gui = new GUI();
new ServerConsoles(); new ServerConsoles();
Profile.load(); Profile.load();
@ -56,10 +54,6 @@ public class Main {
}); });
} }
public static GUI gui() {
return gui;
}
private static void setup() { private static void setup() {
try { try {
ServerType.loadServerTypes(); ServerType.loadServerTypes();

View File

@ -3,6 +3,7 @@ package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.AdvancedServerType; import net.knarcraft.serverlauncher.server.AdvancedServerType;
import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI;
import net.knarcraft.serverlauncher.userinterface.ServerConsoles; import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
import net.knarcraft.serverlauncher.userinterface.ServerTab; import net.knarcraft.serverlauncher.userinterface.ServerTab;
import net.knarcraft.serverlauncher.Main; import net.knarcraft.serverlauncher.Main;
@ -30,6 +31,7 @@ import static net.knarcraft.serverlauncher.Shared.stringBetween;
public class Profile { public class Profile {
private static final ArrayList<Profile> profiles = new ArrayList<>(); private static final ArrayList<Profile> profiles = new ArrayList<>();
private static Profile current; private static Profile current;
private static GUI gui;
private static final String profilesDir = Main.appDir + File.separator + "files"; private static final String profilesDir = Main.appDir + File.separator + "files";
private static final String profilesFile = Main.appDir + File.separator + "files" + File.separator + "Profiles.txt"; private static final String profilesFile = Main.appDir + File.separator + "files" + File.separator + "Profiles.txt";
private static final String jarDir = Main.appDir + File.separator + "files" + File.separator + "Jars" + File.separator; private static final String jarDir = Main.appDir + File.separator + "files" + File.separator + "Jars" + File.separator;
@ -67,6 +69,10 @@ public class Profile {
} }
} }
public static GUI getGUI() {
return gui;
}
public boolean getRunInBackground() { public boolean getRunInBackground() {
return this.runInBackground; return this.runInBackground;
} }
@ -206,7 +212,7 @@ public class Profile {
for (int i = 0; i < collections.size(); i++) { for (int i = 0; i < collections.size(); i++) {
if (collections.get(i).getName().equals(name)) { if (collections.get(i).getName().equals(name)) {
this.collections.remove(i); this.collections.remove(i);
Main.gui().update(); gui.update();
break; break;
} }
} }
@ -307,7 +313,15 @@ public class Profile {
throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir); throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir);
} }
try (PrintWriter file = new PrintWriter(profilesFile)) { try (PrintWriter file = new PrintWriter(profilesFile)) {
file.println(String.format("%s;%s;%s;%s", current.name, vanillaVersion, snapshotVersion, bungeeVersion)); file.println(String.format(
"%s;%s;%s;%s;%d;%d",
current.name,
vanillaVersion,
snapshotVersion,
bungeeVersion,
gui.getSize().width,
gui.getSize().height
));
file.close(); file.close();
for (Profile profile : profiles) { for (Profile profile : profiles) {
StringBuilder saveString = new StringBuilder(String.format( StringBuilder saveString = new StringBuilder(String.format(
@ -341,7 +355,7 @@ public class Profile {
))) { ))) {
fileAppend.println(saveString); fileAppend.println(saveString);
} catch (IOException e) { } catch (IOException e) {
if (Main.gui() != null) { if (gui != null) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
"Unable to save to file. Try running the software as an administrator.", "Unable to save to file. Try running the software as an administrator.",
@ -353,7 +367,7 @@ public class Profile {
} }
} }
} catch (IOException e) { } catch (IOException e) {
if (Main.gui() != null) { if (gui != null) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
"Unable to save to file. Try running the software as an administrator.", "Unable to save to file. Try running the software as an administrator.",
@ -376,6 +390,9 @@ public class Profile {
vanillaVersion = staticData[1]; vanillaVersion = staticData[1];
snapshotVersion = staticData[2]; snapshotVersion = staticData[2];
bungeeVersion = staticData[3]; bungeeVersion = staticData[3];
int guiWidth = Integer.parseInt(staticData[4]);
int guiHeight = Integer.parseInt(staticData[5]);
gui = new GUI(guiWidth, guiHeight);
while (in.hasNextLine()) { while (in.hasNextLine()) {
String line = in.nextLine(); String line = in.nextLine();
if (line.contains("?")) { if (line.contains("?")) {
@ -418,13 +435,14 @@ public class Profile {
"Info", "Info",
JOptionPane.INFORMATION_MESSAGE JOptionPane.INFORMATION_MESSAGE
); );
gui = new GUI();
addProfile("Default"); addProfile("Default");
} }
Main.gui().update(); gui.update();
Main.gui().updateProfiles(); gui.updateProfiles();
current.updateConsoles(); current.updateConsoles();
if (current.runInBackground) { if (current.runInBackground) {
Main.gui().hide(); gui.hide();
Executors.newSingleThreadExecutor().execute(Server::startServers); Executors.newSingleThreadExecutor().execute(Server::startServers);
} }
if (current.downloadJars) { if (current.downloadJars) {
@ -479,7 +497,7 @@ public class Profile {
downloadMixed("Snapshot"); downloadMixed("Snapshot");
downloadSponge(); downloadSponge();
downloadBungee(); downloadBungee();
Main.gui().setStatus("Finished downloading jars"); gui.setStatus("Finished downloading jars");
} }
private static void downloadSimple(String typeName) throws FileNotFoundException { private static void downloadSimple(String typeName) throws FileNotFoundException {
@ -491,13 +509,13 @@ public class Profile {
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + type.getName() + version + ".jar");
if (!file.isFile()) { if (!file.isFile()) {
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Downloading: " + name + version + ".jar"); gui.setStatus("Downloading: " + name + version + ".jar");
} }
success = downloadFile(url + name + version + ".jar", filePath); success = downloadFile(url + name + version + ".jar", filePath);
if (!success) { if (!success) {
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar"); gui.setStatus("Error downloading: " + name + version + ".jar");
} }
throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
@ -515,8 +533,8 @@ public class Profile {
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + type.getName() + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Downloading: " + name + version + ".jar"); gui.setStatus("Downloading: " + name + version + ".jar");
} }
if (version.equals("Latest")) { if (version.equals("Latest")) {
try { try {
@ -532,8 +550,8 @@ public class Profile {
); );
setVersion(name, newestVersion); setVersion(name, newestVersion);
if (!success) { if (!success) {
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar"); gui.setStatus("Error downloading: " + name + version + ".jar");
} }
throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
@ -542,8 +560,8 @@ public class Profile {
if (!file.isFile()) { if (!file.isFile()) {
success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath); success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath);
if (!success) { if (!success) {
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar"); gui.setStatus("Error downloading: " + name + version + ".jar");
} }
throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
@ -562,8 +580,8 @@ public class Profile {
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
File file = new File(jarDir + name + version + ".jar"); File file = new File(jarDir + name + version + ".jar");
Path filePath = Paths.get(jarDir + name + version + ".jar"); Path filePath = Paths.get(jarDir + name + version + ".jar");
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Downloading: " + name + version + ".jar"); gui.setStatus("Downloading: " + name + version + ".jar");
} }
try { try {
versionText = readFile(type.getVersionURL() + version); versionText = readFile(type.getVersionURL() + version);
@ -577,8 +595,8 @@ public class Profile {
filePath filePath
); );
if (!success) { if (!success) {
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar"); gui.setStatus("Error downloading: " + name + version + ".jar");
} }
throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
@ -595,8 +613,8 @@ public class Profile {
Boolean success; Boolean success;
File file = new File(jarDir + type.getName() + ".jar"); File file = new File(jarDir + type.getName() + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + ".jar");
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Downloading: " + name + ".jar"); gui.setStatus("Downloading: " + name + ".jar");
} }
try { try {
versionText = readFile(type.getVersionURL()); versionText = readFile(type.getVersionURL());
@ -608,8 +626,8 @@ public class Profile {
success = downloadFile(url, filePath); success = downloadFile(url, filePath);
setVersion(name, newestVersion); setVersion(name, newestVersion);
if (!success) { if (!success) {
if (Main.gui() != null) { if (gui != null) {
Main.gui().setStatus("Error downloading: " + name + ".jar"); gui.setStatus("Error downloading: " + name + ".jar");
} }
throw new FileNotFoundException("Error downloading: " + name + ".jar"); throw new FileNotFoundException("Error downloading: " + name + ".jar");
} }

View File

@ -161,7 +161,7 @@ public class Server {
public void addPlayer(String name) { public void addPlayer(String name) {
this.playerList.add(name); this.playerList.add(name);
Main.gui().addPlayer(name); Profile.getGUI().addPlayer(name);
} }
public void removePlayer(String name) { public void removePlayer(String name) {
@ -170,7 +170,7 @@ public class Server {
playerList.remove(i); playerList.remove(i);
} }
} }
Main.gui().removePlayer(name); Profile.getGUI().removePlayer(name);
} }
public void setPath(String path) { public void setPath(String path) {
@ -229,10 +229,10 @@ public class Server {
* Runs all enabled servers with their settings. * Runs all enabled servers with their settings.
*/ */
public static void startServers() { public static void startServers() {
Main.gui().setStatus("Starting servers"); Profile.getGUI().setStatus("Starting servers");
for (Collection collection : Profile.getCurrent().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().run()) { if (!collection.getServer().run()) {
Main.gui().setStatus("An error occurred. Start aborted"); Profile.getGUI().setStatus("An error occurred. Start aborted");
} }
} }
} }
@ -244,11 +244,11 @@ public class Server {
if (this.enabled) { if (this.enabled) {
if (!Profile.getCurrent().getDownloadJars()) { if (!Profile.getCurrent().getDownloadJars()) {
try { try {
Main.gui().setStatus("Downloading jar..."); Profile.getGUI().setStatus("Downloading jar...");
this.downloadJar(); this.downloadJar();
Main.gui().setStatus("File downloaded"); Profile.getGUI().setStatus("File downloaded");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Main.gui().setStatus("Error: Jar file not found"); Profile.getGUI().setStatus("Error: Jar file not found");
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
@ -285,10 +285,10 @@ public class Server {
this.writer = new BufferedWriter(new OutputStreamWriter(stdin)); this.writer = new BufferedWriter(new OutputStreamWriter(stdin));
InputStream stdout = this.process.getInputStream(); InputStream stdout = this.process.getInputStream();
this.reader = new BufferedReader (new InputStreamReader(stdout)); this.reader = new BufferedReader (new InputStreamReader(stdout));
Main.gui().setStatus("Servers are running"); Profile.getGUI().setStatus("Servers are running");
return true; return true;
} catch (IOException e) { } catch (IOException e) {
Main.gui().setStatus("Could not start server"); Profile.getGUI().setStatus("Could not start server");
return false; return false;
} }
} else { } else {

View File

@ -62,7 +62,13 @@ public class GUI implements ActionListener {
* Create the application window. * Create the application window.
*/ */
public GUI() { public GUI() {
initialize(); initialize(440, 170);
loadMessages();
this.globalPlayers = new ArrayList<>();
}
public GUI(int width, int height) {
initialize(width, height);
loadMessages(); loadMessages();
this.globalPlayers = new ArrayList<>(); this.globalPlayers = new ArrayList<>();
} }
@ -96,6 +102,10 @@ public class GUI implements ActionListener {
} }
} }
public Dimension getSize() {
return frame.getContentPane().getPreferredSize();
}
/** /**
* Updates GUI according to current settings. * Updates GUI according to current settings.
*/ */
@ -117,7 +127,7 @@ public class GUI implements ActionListener {
/** /**
* Creates the GUI, * Creates the GUI,
*/ */
private void initialize() { private void initialize(int width, int height) {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | } catch (ClassNotFoundException |
@ -130,7 +140,7 @@ public class GUI implements ActionListener {
frame = new JFrame("Minecraft server launcher"); frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//frame.setResizable(false); frame.getContentPane().setPreferredSize(new Dimension(width, height));
ImageIcon img; ImageIcon img;
try { try {
img = new ImageIcon(ImageIO.read(GUI.class.getResourceAsStream("/files/GUIIcon.png"))); img = new ImageIcon(ImageIO.read(GUI.class.getResourceAsStream("/files/GUIIcon.png")));
@ -381,7 +391,6 @@ public class GUI implements ActionListener {
this.serversPane = tabbedPane_1; this.serversPane = tabbedPane_1;
tabbedPane_1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); tabbedPane_1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
frame.getContentPane().setPreferredSize(new Dimension(440, 170));
frame.validate(); frame.validate();
frame.pack(); frame.pack();
frame.setVisible(true); frame.setVisible(true);

View File

@ -37,7 +37,7 @@ public class ServerTab implements ActionListener {
public ServerTab(String name) { public ServerTab(String name) {
this.name = name; this.name = name;
panel = new JPanel(); panel = new JPanel();
Main.gui().getPane().addTab(name, null, panel, null); Profile.getGUI().getPane().addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout(); SpringLayout sl_panel_3 = new SpringLayout();
panel.setLayout(sl_panel_3); panel.setLayout(sl_panel_3);
@ -171,7 +171,7 @@ public class ServerTab implements ActionListener {
private void remove() { private void remove() {
Profile.getCurrent().removeCollection(name); Profile.getCurrent().removeCollection(name);
Main.gui().update(); Profile.getGUI().update();
Profile.getCurrent().updateConsoles(); Profile.getCurrent().updateConsoles();
} }