From 30263d7753803e2446883aed9e093a9f58b325b1 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Wed, 31 Jan 2018 17:40:28 +0100 Subject: [PATCH] Using pack() for scaling --- src/Main.java | 2 +- .../serverlauncher/profile/Collection.java | 37 ++++++++++++ .../serverlauncher/profile/Profile.java | 50 +++++++++------- .../serverlauncher/server/Server.java | 25 ++++---- .../serverlauncher/userinterface/Console.java | 4 +- .../serverlauncher/userinterface/GUI.java | 60 +++++++++---------- .../userinterface/ServerConsoles.java | 6 +- .../userinterface/ServerTab.java | 1 - test/ServerTest.java | 4 +- 9 files changed, 114 insertions(+), 75 deletions(-) create mode 100644 src/net/knarcraft/serverlauncher/profile/Collection.java diff --git a/src/Main.java b/src/Main.java index 1dd809b..cd3e237 100644 --- a/src/Main.java +++ b/src/Main.java @@ -24,7 +24,7 @@ class Main { try { setup(); new GUI(); - //new ServerConsoles(); + new ServerConsoles(); Profile.addProfile("Default"); //TODO: replace with profiles loading generating a default profile if empty. diff --git a/src/net/knarcraft/serverlauncher/profile/Collection.java b/src/net/knarcraft/serverlauncher/profile/Collection.java new file mode 100644 index 0000000..002a107 --- /dev/null +++ b/src/net/knarcraft/serverlauncher/profile/Collection.java @@ -0,0 +1,37 @@ +package net.knarcraft.serverlauncher.profile; + +import net.knarcraft.serverlauncher.server.Server; +import net.knarcraft.serverlauncher.userinterface.GUI; +import net.knarcraft.serverlauncher.userinterface.ServerConsoles; +import net.knarcraft.serverlauncher.userinterface.ServerTab; +import net.knarcraft.serverlauncher.userinterface.Console; + + +public class Collection { + private Server server; + private ServerTab serverTab; + private Console serverConsole; + private String name; + + public Collection(String name) { + this.serverTab = new ServerTab(name); + this.server = new Server(name, GUI.getGUI(), this.serverTab); + this.serverConsole = ServerConsoles.getGUI().addTab(name); + } + + public String getName() { + return this.name; + } + + public Server getServer() { + return this.server; + } + + public ServerTab getServerTab() { + return this.serverTab; + } + + public Console getServerConsole() { + return this.serverConsole; + } +} diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index 07f58e7..2bef30a 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -19,7 +19,7 @@ import java.util.ArrayList; public class Profile { private static final ArrayList profiles = new ArrayList<>(); - private final ArrayList servers; + private final ArrayList collections; private final String name; private boolean runInBackground; private int delayStartup; @@ -30,7 +30,7 @@ public class Profile { private String bungeeVersion; private Profile(String name) { - this.servers = new ArrayList<>(); + this.collections = new ArrayList<>(); this.name = name; this.runInBackground = false; this.delayStartup = 0; @@ -38,34 +38,38 @@ public class Profile { profiles.add(this); } - public void addServer(Server server) { - this.servers.add(server); + public void addCollection(String name) { + if (!collectionExists(name)) { + collections.add(new Collection(name)); + } else { + JOptionPane.showMessageDialog(null, "A server name must my unique and not empty.", "Error", JOptionPane.ERROR_MESSAGE); + } } - public void removeServer(int i) { - this.servers.remove(i); + public void removeCollection(int i) { + this.collections.remove(i); } - private Server getServer(String name) { - for (Server server : this.servers) { - if (server.getName().equals(name)) { - return server; + private Collection getCollection(String name) { + for (Collection collection : this.collections) { + if (collection.getName().equals(name)) { + return collection; } } return null; } - public boolean serverExists(String name) { - for (Server server : this.servers) { - if (server.getName().equals(name)) { + public boolean collectionExists(String name) { + for (Collection collection : this.collections) { + if (collection.getName().equals(name)) { return true; } } return false; } - public ArrayList getServers() { - return this.servers; + public ArrayList getCollections() { + return this.collections; } public static void addProfile(String name) { @@ -122,15 +126,15 @@ public class Profile { public void sendCommand(String serverName, String command) { if (serverName.equals("All")) { - for (Server server : this.servers) { + for (Collection collection : this.collections) { try { - server.sendCommand(command); + collection.getServer().sendCommand(command); } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Server " + server.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "Server " + collection.getServer().getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE); } } } else { - Server target = getServer(serverName); + Server target = getCollection(serverName).getServer(); if (target != null) { try { target.sendCommand(command); @@ -176,10 +180,10 @@ public class Profile { } } - public void save(ArrayList serverTabs) { - for (int i = 0; i < this.servers.size(); i++) { - Server server = servers.get(i); - ServerTab serverTab = serverTabs.get(i); + public void save() { + for (int i = 0; i < this.collections.size(); i++) { + Server server = collections.get(i).getServer(); + ServerTab serverTab = collections.get(i).getServerTab(); server.setPath(serverTab.getPath()); server.setMaxRam(serverTab.getMaxRam()); server.setType(ServerType.getByName(serverTab.getType())); diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index 2fb7eae..33a9191 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -1,7 +1,9 @@ package net.knarcraft.serverlauncher.server; +import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.userinterface.GUI; +import net.knarcraft.serverlauncher.userinterface.ServerTab; import java.io.*; import java.net.URL; @@ -33,7 +35,7 @@ public class Server { private Process process; private BufferedWriter writer; - public Server(String name, GUI window) { + public Server(String name, GUI window, ServerTab serverTab) { this.name = name; this.path = ""; this.enabled = false; @@ -42,8 +44,7 @@ public class Server { this.serverVersion = null; this.maxRam = ramList[0]; this.process = null; - GUI.getGUI().currentProfile().addServer(this); - window.addServer(name); + window.addServer(serverTab); } public void addPlayer(String name) { @@ -110,15 +111,15 @@ public class Server { } public static void stop() throws IOException { - for (Server server : GUI.getGUI().currentProfile().getServers()) { - if (server.writer != null) { - if (server.type.getName().equals("Bungee")) { - server.writer.write("end\n"); + for (Collection collection : GUI.getGUI().currentProfile().getCollections()) { + if (collection.getServer().writer != null) { + if (collection.getServer().type.getName().equals("Bungee")) { + collection.getServer().writer.write("end\n"); } else { - server.writer.write("stop\n"); + collection.getServer().writer.write("stop\n"); } - server.writer.flush(); - server.writer = null; + collection.getServer().writer.flush(); + collection.getServer().writer = null; } } } @@ -144,8 +145,8 @@ public class Server { */ public static void startServers() { GUI.getGUI().setStatus("Starting servers"); - for (Server server : GUI.getGUI().currentProfile().getServers()) { - if (!server.run()) { + for (Collection collection : GUI.getGUI().currentProfile().getCollections()) { + if (!collection.getServer().run()) { GUI.getGUI().setStatus("An error occurred. Start aborted"); } } diff --git a/src/net/knarcraft/serverlauncher/userinterface/Console.java b/src/net/knarcraft/serverlauncher/userinterface/Console.java index 65dfaf4..2efd00b 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/Console.java +++ b/src/net/knarcraft/serverlauncher/userinterface/Console.java @@ -11,7 +11,7 @@ import java.awt.*; * @version 0.0.0.1 * @since 0.0.0.1 */ -class Console { +public class Console { private final JTextField textInput; private final JTextArea textOutput; @@ -19,7 +19,7 @@ class Console { this.textOutput.setText(text); } - Console(JTabbedPane tab, String name) { + public Console(JTabbedPane tab, String name) { JPanel panel = new JPanel(); tab.addTab(name, null, panel, null); panel.setLayout(new BorderLayout(0, 0)); diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index 925aca9..8ab117b 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -1,10 +1,12 @@ package net.knarcraft.serverlauncher.userinterface; +import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.profile.Profile; import javax.imageio.ImageIO; import javax.swing.*; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; @@ -107,7 +109,7 @@ public class GUI implements ActionListener { for (int i = 0; i < this.serverTabs.size(); i++) { if(this.serverTabs.get(i) == tab) { serversPane.remove(i); - currentProfile().removeServer(i); + currentProfile().removeCollection(i); this.serverTabs.remove(i); i = serverTabs.size(); } @@ -119,14 +121,13 @@ public class GUI implements ActionListener { * Initialize the contents of the frame. */ private void initialize() { - try { + /*try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); - } + }*/ frame = new JFrame("Minecraft server launcher"); - frame.setBounds(100, 100, 470, 219); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setResizable(false); ImageIcon img; @@ -204,7 +205,6 @@ public class GUI implements ActionListener { mntmStory = new JMenuItem("Story"); mnAbout.add(mntmStory); - frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS)); mntmStory.addActionListener(this); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); @@ -221,8 +221,7 @@ public class GUI implements ActionListener { btnStartServer = new JButton("Start servers"); sl_panel.putConstraint(SpringLayout.WEST, lblBasicControls, 0, SpringLayout.WEST, btnStartServer); - sl_panel.putConstraint(SpringLayout.SOUTH, lblBasicControls, -6, SpringLayout.NORTH, btnStartServer); - sl_panel.putConstraint(SpringLayout.NORTH, btnStartServer, 30, SpringLayout.NORTH, panelBasic); + sl_panel.putConstraint(SpringLayout.NORTH, btnStartServer, 6, SpringLayout.SOUTH, lblBasicControls); sl_panel.putConstraint(SpringLayout.WEST, btnStartServer, 10, SpringLayout.WEST, panelBasic); panelBasic.add(btnStartServer); btnStartServer.addActionListener(this); @@ -251,15 +250,16 @@ public class GUI implements ActionListener { delProfile.addActionListener(this); profiles = new JComboBox<>(); - sl_panel.putConstraint(SpringLayout.NORTH, profiles, 1, SpringLayout.NORTH, addProfile); + sl_panel.putConstraint(SpringLayout.NORTH, profiles, 0, SpringLayout.NORTH, addProfile); sl_panel.putConstraint(SpringLayout.WEST, profiles, 6, SpringLayout.EAST, delProfile); sl_panel.putConstraint(SpringLayout.EAST, profiles, 124, SpringLayout.EAST, delProfile); panelBasic.add(profiles); profiles.addActionListener(this); - sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 8, SpringLayout.SOUTH, addProfile); + sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 6, SpringLayout.SOUTH, addProfile); + sl_panel.putConstraint(SpringLayout.SOUTH, lblStatuslabel, -10, SpringLayout.SOUTH, panelBasic); sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic); - sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, 386, SpringLayout.WEST, panelBasic); + sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, -10, SpringLayout.EAST, panelBasic); panelBasic.add(lblStatuslabel); addServer = new JButton("Add server"); @@ -291,15 +291,15 @@ public class GUI implements ActionListener { btnKick = new JButton("Kick"); sl_panel_1.putConstraint(SpringLayout.NORTH, btnKick, 10, SpringLayout.NORTH, controlServers); sl_panel_1.putConstraint(SpringLayout.WEST, btnKick, 6, SpringLayout.EAST, targetServer); - sl_panel_1.putConstraint(SpringLayout.EAST, btnKick, 124, SpringLayout.WEST, btnKick); + sl_panel_1.putConstraint(SpringLayout.EAST, btnKick, 104, SpringLayout.WEST, btnKick); sl_panel_1.putConstraint(SpringLayout.SOUTH, targetServer, 0, SpringLayout.SOUTH, btnKick); controlServers.add(btnKick); btnKick.addActionListener(this); btnBan = new JButton("Ban"); - sl_panel_1.putConstraint(SpringLayout.NORTH, btnBan, 5, SpringLayout.SOUTH, btnKick); + sl_panel_1.putConstraint(SpringLayout.NORTH, btnBan, 6, SpringLayout.SOUTH, btnKick); sl_panel_1.putConstraint(SpringLayout.WEST, btnBan, 6, SpringLayout.EAST, targetPlayer); - sl_panel_1.putConstraint(SpringLayout.EAST, btnBan, 124, SpringLayout.WEST, btnBan); + sl_panel_1.putConstraint(SpringLayout.EAST, btnBan, 104, SpringLayout.WEST, btnBan); sl_panel_1.putConstraint(SpringLayout.SOUTH, targetPlayer, 0, SpringLayout.SOUTH, btnBan); controlServers.add(btnBan); btnBan.addActionListener(this); @@ -307,7 +307,6 @@ public class GUI implements ActionListener { btnOp = new JButton("OP"); sl_panel_1.putConstraint(SpringLayout.NORTH, btnOp, 10, SpringLayout.NORTH, controlServers); sl_panel_1.putConstraint(SpringLayout.WEST, btnOp, 6, SpringLayout.EAST, btnKick); - //sl_panel_1.putConstraint(SpringLayout.SOUTH, btnOp, 32, SpringLayout.NORTH, controlServers); sl_panel_1.putConstraint(SpringLayout.EAST, btnOp, -10, SpringLayout.EAST, controlServers); controlServers.add(btnOp); btnOp.addActionListener(this); @@ -353,7 +352,8 @@ public class GUI implements ActionListener { sl_panel_1.putConstraint(SpringLayout.NORTH, customCommand, 6, SpringLayout.SOUTH, btnSaveserver); sl_panel_1.putConstraint(SpringLayout.NORTH, btnSaveserver, 6, SpringLayout.SOUTH, btnBan); sl_panel_1.putConstraint(SpringLayout.WEST, btnSaveserver, 0, SpringLayout.WEST, btnKick); - sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 120, SpringLayout.WEST, btnKick); + sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 104, SpringLayout.WEST, btnKick); + sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 104, SpringLayout.WEST, btnKick); controlServers.add(btnSaveserver); btnSaveserver.addActionListener(this); @@ -386,13 +386,14 @@ public class GUI implements ActionListener { this.serversPane = tabbedPane_1; tabbedPane_1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); - frame.setPreferredSize(frame.getPreferredSize()); + frame.getContentPane().setPreferredSize(new Dimension(440, 170)); frame.validate(); + frame.pack(); frame.setVisible(true); } - public void addServer(String name) { - serverTabs.add(new ServerTab(name)); + public void addServer(ServerTab serverTab) { + serverTabs.add(serverTab); } @Override @@ -421,11 +422,11 @@ public class GUI implements ActionListener { } else if (e.getSource() == mntmRunInBackground) { JOptionPane.showMessageDialog(null, runInBackgroundText, "Run in background", JOptionPane.INFORMATION_MESSAGE); } else if (e.getSource() == mntmDelayStartup) { - JOptionPane.showMessageDialog(null, delayStartupText, "Run in background", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(null, delayStartupText, "Delay startup", JOptionPane.INFORMATION_MESSAGE); } else if (e.getSource() == mntmDownloadJars) { - JOptionPane.showMessageDialog(null, downloadJarsText, "Run in background", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(null, downloadJarsText, "Download jars", JOptionPane.INFORMATION_MESSAGE); } else if (e.getSource() == mntmAbout) { - JOptionPane.showMessageDialog(null, aboutText, "Run in background", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(null, aboutText, "About", JOptionPane.INFORMATION_MESSAGE); } else if (e.getSource() == mntmStory) { goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/"); } else if (e.getSource() == btnStartServer) { @@ -435,11 +436,7 @@ public class GUI implements ActionListener { stop(); } else if (e.getSource() == addServer) { String serverName = JOptionPane.showInputDialog("Name of server: "); - if (!serverName.equals("") && !currentProfile().serverExists(serverName)) { - new Server(serverName, this); - } else { - JOptionPane.showMessageDialog(null, "A server name must my unique and not empty.", "Error", JOptionPane.ERROR_MESSAGE); - } + this.currentProfile().addCollection(serverName); } else if (e.getSource() == backup) { backup(); } else if (e.getSource() == addProfile) { @@ -479,6 +476,7 @@ public class GUI implements ActionListener { } } else if (e.getSource() == btnServerConsoles) { //TODO: Make server consoles window, and toggle visibility on this action. + ServerConsoles.getGUI().show(); } } @@ -486,7 +484,7 @@ public class GUI implements ActionListener { * Reads all combo boxes, updates variables and saves to disk */ private void save() { - currentProfile().save(serverTabs); + currentProfile().save(); } /** @@ -562,10 +560,10 @@ public class GUI implements ActionListener { if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File path = chooser.getSelectedFile(); - for (Server server : currentProfile().getServers()) { - if (!server.getPath().equals("") && server.isEnabled()) { - String name = server.getName(); - File srcFolder = new File(server.getPath()); + for (Collection collection : currentProfile().getCollections()) { + if (!collection.getServer().getPath().equals("") && collection.getServer().isEnabled()) { + String name = collection.getServer().getName(); + File srcFolder = new File(collection.getServer().getPath()); File destFolder = new File(path, name); if (!destFolder.exists()) { if (destFolder.mkdirs()) { diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java b/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java index 697ba6c..9d0e640 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java @@ -16,7 +16,6 @@ import java.util.ArrayList; */ public class ServerConsoles { private static ServerConsoles serverConsoles; - private static ArrayList consoles = new ArrayList<>(); private final JFrame frame; private final JTabbedPane consolesTab; @@ -28,7 +27,6 @@ public class ServerConsoles { consolesTab = new JTabbedPane(JTabbedPane.TOP); frame.getContentPane().add(consolesTab, BorderLayout.CENTER); serverConsoles = this; - frame.setVisible(true); } public static ServerConsoles getGUI() { @@ -39,8 +37,8 @@ public class ServerConsoles { frame.setVisible(true); } - public void addTab(String name) { - new Console(consolesTab, name); + public Console addTab(String name) { + return new Console(consolesTab, name); } } \ No newline at end of file diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java index e16f0ab..ecf9e28 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java @@ -58,7 +58,6 @@ public class ServerTab implements ActionListener { JLabel lblMaxRam = new JLabel("Max ram"); sl_panel_3.putConstraint(SpringLayout.NORTH, lblMaxRam, 0, SpringLayout.NORTH, serverTypes); sl_panel_3.putConstraint(SpringLayout.SOUTH, lblMaxRam, 0, SpringLayout.SOUTH, serverTypes); - sl_panel_3.putConstraint(SpringLayout.EAST, lblMaxRam, 50, SpringLayout.WEST, lblMaxRam); sl_panel_3.putConstraint(SpringLayout.WEST, lblMaxRam, 6, SpringLayout.EAST, serverTypes); panel.add(lblMaxRam); diff --git a/test/ServerTest.java b/test/ServerTest.java index 099067d..5538edd 100644 --- a/test/ServerTest.java +++ b/test/ServerTest.java @@ -2,6 +2,7 @@ import javax.naming.ConfigurationException; import net.knarcraft.serverlauncher.server.*; import net.knarcraft.serverlauncher.userinterface.GUI; +import net.knarcraft.serverlauncher.userinterface.ServerTab; import java.awt.*; import java.io.*; @@ -22,7 +23,8 @@ class ServerTest { window.getFrame().setVisible(true); - Server server1 = new Server("Server1", window); + + Server server1 = new Server("Server1", window, new ServerTab("Server1")); server1.toggle(); server1.setPath("C:\\Users\\Kristian\\Desktop\\Test"); server1.setType(ServerType.getServerTypes().get(4));