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 {
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();

View File

@ -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<Profile> 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");
}

View File

@ -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 {

View File

@ -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);

View File

@ -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();
}