From 2fac5f403dcea0333dfb8ffbbfd3b3fb5faf2123 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Sat, 17 Feb 2018 23:05:12 +0100 Subject: [PATCH] GUI size is now saved and loaded, to prevent hassle for the user --- src/net/knarcraft/serverlauncher/Main.java | 6 -- .../serverlauncher/profile/Profile.java | 70 ++++++++++++------- .../serverlauncher/server/Server.java | 18 ++--- .../serverlauncher/userinterface/GUI.java | 17 +++-- .../userinterface/ServerTab.java | 4 +- 5 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/net/knarcraft/serverlauncher/Main.java b/src/net/knarcraft/serverlauncher/Main.java index 0967a4c..9ebf14b 100644 --- a/src/net/knarcraft/serverlauncher/Main.java +++ b/src/net/knarcraft/serverlauncher/Main.java @@ -27,7 +27,6 @@ import static net.knarcraft.serverlauncher.Shared.stringBetween; */ public class Main { - private static GUI gui; @SuppressWarnings("CanBeFinal") public static String appDir; @@ -44,7 +43,6 @@ public class Main { EventQueue.invokeLater(() -> { try { setup(); - gui = new GUI(); new ServerConsoles(); Profile.load(); @@ -56,10 +54,6 @@ public class Main { }); } - public static GUI gui() { - return gui; - } - private static void setup() { try { ServerType.loadServerTypes(); diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index e59e65d..e266cf1 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -3,6 +3,7 @@ package net.knarcraft.serverlauncher.profile; import net.knarcraft.serverlauncher.server.AdvancedServerType; import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.ServerType; +import net.knarcraft.serverlauncher.userinterface.GUI; import net.knarcraft.serverlauncher.userinterface.ServerConsoles; import net.knarcraft.serverlauncher.userinterface.ServerTab; import net.knarcraft.serverlauncher.Main; @@ -30,6 +31,7 @@ import static net.knarcraft.serverlauncher.Shared.stringBetween; public class Profile { private static final ArrayList profiles = new ArrayList<>(); private static Profile current; + private static GUI gui; 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 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() { return this.runInBackground; } @@ -206,7 +212,7 @@ public class Profile { for (int i = 0; i < collections.size(); i++) { if (collections.get(i).getName().equals(name)) { this.collections.remove(i); - Main.gui().update(); + gui.update(); break; } } @@ -307,7 +313,15 @@ public class Profile { throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir); } 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(); for (Profile profile : profiles) { StringBuilder saveString = new StringBuilder(String.format( @@ -341,7 +355,7 @@ public class Profile { ))) { fileAppend.println(saveString); } catch (IOException e) { - if (Main.gui() != null) { + if (gui != null) { JOptionPane.showMessageDialog( null, "Unable to save to file. Try running the software as an administrator.", @@ -353,7 +367,7 @@ public class Profile { } } } catch (IOException e) { - if (Main.gui() != null) { + if (gui != null) { JOptionPane.showMessageDialog( null, "Unable to save to file. Try running the software as an administrator.", @@ -376,6 +390,9 @@ public class Profile { vanillaVersion = staticData[1]; snapshotVersion = staticData[2]; bungeeVersion = staticData[3]; + int guiWidth = Integer.parseInt(staticData[4]); + int guiHeight = Integer.parseInt(staticData[5]); + gui = new GUI(guiWidth, guiHeight); while (in.hasNextLine()) { String line = in.nextLine(); if (line.contains("?")) { @@ -418,13 +435,14 @@ public class Profile { "Info", JOptionPane.INFORMATION_MESSAGE ); + gui = new GUI(); addProfile("Default"); } - Main.gui().update(); - Main.gui().updateProfiles(); + gui.update(); + gui.updateProfiles(); current.updateConsoles(); if (current.runInBackground) { - Main.gui().hide(); + gui.hide(); Executors.newSingleThreadExecutor().execute(Server::startServers); } if (current.downloadJars) { @@ -479,7 +497,7 @@ public class Profile { downloadMixed("Snapshot"); downloadSponge(); downloadBungee(); - Main.gui().setStatus("Finished downloading jars"); + gui.setStatus("Finished downloading jars"); } private static void downloadSimple(String typeName) throws FileNotFoundException { @@ -491,13 +509,13 @@ public class Profile { File file = new File(jarDir + type.getName() + version + ".jar"); if (!file.isFile()) { Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); - if (Main.gui() != null) { - Main.gui().setStatus("Downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("Downloading: " + name + version + ".jar"); } success = downloadFile(url + name + version + ".jar", filePath); if (!success) { - if (Main.gui() != null) { - Main.gui().setStatus("Error downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("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()) { File file = new File(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); - if (Main.gui() != null) { - Main.gui().setStatus("Downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("Downloading: " + name + version + ".jar"); } if (version.equals("Latest")) { try { @@ -532,8 +550,8 @@ public class Profile { ); setVersion(name, newestVersion); if (!success) { - if (Main.gui() != null) { - Main.gui().setStatus("Error downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("Error downloading: " + name + version + ".jar"); } throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); } @@ -542,8 +560,8 @@ public class Profile { if (!file.isFile()) { success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath); if (!success) { - if (Main.gui() != null) { - Main.gui().setStatus("Error downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("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()) { File file = new File(jarDir + name + version + ".jar"); Path filePath = Paths.get(jarDir + name + version + ".jar"); - if (Main.gui() != null) { - Main.gui().setStatus("Downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("Downloading: " + name + version + ".jar"); } try { versionText = readFile(type.getVersionURL() + version); @@ -577,8 +595,8 @@ public class Profile { filePath ); if (!success) { - if (Main.gui() != null) { - Main.gui().setStatus("Error downloading: " + name + version + ".jar"); + if (gui != null) { + gui.setStatus("Error downloading: " + name + version + ".jar"); } throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); } @@ -595,8 +613,8 @@ public class Profile { Boolean success; File file = new File(jarDir + type.getName() + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + ".jar"); - if (Main.gui() != null) { - Main.gui().setStatus("Downloading: " + name + ".jar"); + if (gui != null) { + gui.setStatus("Downloading: " + name + ".jar"); } try { versionText = readFile(type.getVersionURL()); @@ -608,8 +626,8 @@ public class Profile { success = downloadFile(url, filePath); setVersion(name, newestVersion); if (!success) { - if (Main.gui() != null) { - Main.gui().setStatus("Error downloading: " + name + ".jar"); + if (gui != null) { + gui.setStatus("Error downloading: " + name + ".jar"); } throw new FileNotFoundException("Error downloading: " + name + ".jar"); } diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index d77c993..62ce791 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -161,7 +161,7 @@ public class Server { public void addPlayer(String name) { this.playerList.add(name); - Main.gui().addPlayer(name); + Profile.getGUI().addPlayer(name); } public void removePlayer(String name) { @@ -170,7 +170,7 @@ public class Server { playerList.remove(i); } } - Main.gui().removePlayer(name); + Profile.getGUI().removePlayer(name); } public void setPath(String path) { @@ -229,10 +229,10 @@ public class Server { * Runs all enabled servers with their settings. */ public static void startServers() { - Main.gui().setStatus("Starting servers"); + Profile.getGUI().setStatus("Starting servers"); for (Collection collection : Profile.getCurrent().getCollections()) { 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 (!Profile.getCurrent().getDownloadJars()) { try { - Main.gui().setStatus("Downloading jar..."); + Profile.getGUI().setStatus("Downloading jar..."); this.downloadJar(); - Main.gui().setStatus("File downloaded"); + Profile.getGUI().setStatus("File downloaded"); } catch (FileNotFoundException e) { - Main.gui().setStatus("Error: Jar file not found"); + Profile.getGUI().setStatus("Error: Jar file not found"); e.printStackTrace(); return false; } @@ -285,10 +285,10 @@ public class Server { this.writer = new BufferedWriter(new OutputStreamWriter(stdin)); InputStream stdout = this.process.getInputStream(); this.reader = new BufferedReader (new InputStreamReader(stdout)); - Main.gui().setStatus("Servers are running"); + Profile.getGUI().setStatus("Servers are running"); return true; } catch (IOException e) { - Main.gui().setStatus("Could not start server"); + Profile.getGUI().setStatus("Could not start server"); return false; } } else { diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index 7d06508..f82dd13 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -62,7 +62,13 @@ public class GUI implements ActionListener { * Create the application window. */ public GUI() { - initialize(); + initialize(440, 170); + loadMessages(); + this.globalPlayers = new ArrayList<>(); + } + + public GUI(int width, int height) { + initialize(width, height); loadMessages(); 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. */ @@ -117,7 +127,7 @@ public class GUI implements ActionListener { /** * Creates the GUI, */ - private void initialize() { + private void initialize(int width, int height) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | @@ -130,7 +140,7 @@ public class GUI implements ActionListener { frame = new JFrame("Minecraft server launcher"); frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); - //frame.setResizable(false); + frame.getContentPane().setPreferredSize(new Dimension(width, height)); ImageIcon img; try { img = new ImageIcon(ImageIO.read(GUI.class.getResourceAsStream("/files/GUIIcon.png"))); @@ -381,7 +391,6 @@ public class GUI implements ActionListener { this.serversPane = tabbedPane_1; tabbedPane_1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); - frame.getContentPane().setPreferredSize(new Dimension(440, 170)); frame.validate(); frame.pack(); frame.setVisible(true); diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java index 540c014..3723c3e 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java @@ -37,7 +37,7 @@ public class ServerTab implements ActionListener { public ServerTab(String name) { this.name = name; 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(); panel.setLayout(sl_panel_3); @@ -171,7 +171,7 @@ public class ServerTab implements ActionListener { private void remove() { Profile.getCurrent().removeCollection(name); - Main.gui().update(); + Profile.getGUI().update(); Profile.getCurrent().updateConsoles(); }