From 062f57736b87730ff309c0423c75ef002545c967 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Fri, 2 Feb 2018 20:09:54 +0100 Subject: [PATCH] Lets GUI and ServerConsoles update their dynamic tabs List of server settings and server consoles are now updated on change. This will be helpful when we load profiles. --- .../serverlauncher/profile/Collection.java | 5 ++- .../serverlauncher/profile/Profile.java | 11 +++++-- .../serverlauncher/server/Server.java | 8 ++--- .../serverlauncher/userinterface/Console.java | 7 +++- .../serverlauncher/userinterface/GUI.java | 32 ++++++------------- .../userinterface/ServerConsoles.java | 11 +++++-- .../userinterface/ServerTab.java | 11 +++++-- 7 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/net/knarcraft/serverlauncher/profile/Collection.java b/src/net/knarcraft/serverlauncher/profile/Collection.java index c1732b1..ddb8bf8 100644 --- a/src/net/knarcraft/serverlauncher/profile/Collection.java +++ b/src/net/knarcraft/serverlauncher/profile/Collection.java @@ -1,7 +1,6 @@ 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; @@ -18,8 +17,8 @@ public class Collection { Collection(String name) { this.serverTab = new ServerTab(name); - this.server = new Server(name, GUI.getGUI(), this.serverTab); - this.serverConsole = ServerConsoles.getGUI().addTab(name); + this.server = new Server(name); + this.serverConsole = ServerConsoles.addTab(name); this.name = name; } diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index b71cbb1..3481d31 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -47,9 +47,14 @@ public class Profile { } } - public void removeCollection(int i) { - this.collections.remove(i); - GUI.getGUI().updateSelectServers(); + public void removeCollection(ServerTab serverTab) { + for (int i = 0; i < collections.size(); i++) { + if (collections.get(i).getServerTab() == serverTab) { + this.collections.remove(i); + GUI.getGUI().updateSelectServers(); + } + } + } public Collection getCollection(String name) { diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index 39aaa65..7b71b60 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -3,7 +3,6 @@ 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; @@ -36,7 +35,7 @@ public class Server { private BufferedWriter writer; private BufferedReader reader; - public Server(String name, GUI window, ServerTab serverTab) { + public Server(String name) { this.name = name; this.path = ""; this.enabled = false; @@ -45,7 +44,6 @@ public class Server { this.serverVersion = null; this.maxRam = ramList[0]; this.process = null; - window.addServer(serverTab); } public void addPlayer(String name) { @@ -214,7 +212,9 @@ public class Server { String line; if ((line = reader.readLine()) != null) { return line; - } else {return "";} + } else { + return ""; + } } /** diff --git a/src/net/knarcraft/serverlauncher/userinterface/Console.java b/src/net/knarcraft/serverlauncher/userinterface/Console.java index b519897..7bd2c58 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/Console.java +++ b/src/net/knarcraft/serverlauncher/userinterface/Console.java @@ -22,10 +22,11 @@ public class Console implements ActionListener { private final JTextField textInput; private final JTextArea textOutput; private final String name; + private final JPanel panel; Console(JTabbedPane tab, String name) { this.name = name; - JPanel panel = new JPanel(); + panel = new JPanel(); tab.addTab(name, null, panel, null); panel.setLayout(new BorderLayout(0, 0)); @@ -47,6 +48,10 @@ public class Console implements ActionListener { this.textOutput.setText(this.textOutput.getText() + "\n" + text); } + public JPanel getPanel() { + return this.panel; + } + @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == textInput) { diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index ec20756..3c73532 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -58,8 +58,6 @@ public class GUI implements ActionListener { private JTabbedPane serversPane; - private final ArrayList serverTabs = new ArrayList<>(); - /** * Create the application window. */ @@ -99,6 +97,13 @@ public class GUI implements ActionListener { } } + public void update() { + serversPane.removeAll(); + for (Collection collection : currentProfile().getCollections()) { + serversPane.addTab(collection.getName(), collection.getServerTab().getPanel()); + } + } + public void setStatus(String text) { this.lblStatuslabel.setText(text); } @@ -128,23 +133,6 @@ public class GUI implements ActionListener { return gui; } - /** - * Removes a server from the profile's collection, the list of tabs, and the actual tabs. - * - * @param tab The tab object where remove was clicked - */ - public void removeServer(ServerTab tab) { - for (int i = 0; i < this.serverTabs.size(); i++) { - if(this.serverTabs.get(i) == tab) { - this.serversPane.remove(i); - this.currentProfile().removeCollection(i); - ServerConsoles.getGUI().removeTab(i); - this.serverTabs.remove(i); - i = this.serverTabs.size(); - } - } - } - private void initialize() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); @@ -418,10 +406,6 @@ public class GUI implements ActionListener { frame.setVisible(true); } - public void addServer(ServerTab serverTab) { - serverTabs.add(serverTab); - } - public void updateSelectServers() { this.targetServer.removeAllItems(); this.targetServer.addItem("All"); @@ -471,6 +455,8 @@ public class GUI implements ActionListener { } else if (e.getSource() == addServer) { String serverName = JOptionPane.showInputDialog("Name of server: "); this.currentProfile().addCollection(serverName); + this.update(); + ServerConsoles.update(currentProfile().getCollections()); } else if (e.getSource() == backup) { backup(); } else if (e.getSource() == addProfile) { diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java b/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java index d826441..b28ff25 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java @@ -1,8 +1,10 @@ package net.knarcraft.serverlauncher.userinterface; +import net.knarcraft.serverlauncher.profile.Collection; import javax.swing.JFrame; import javax.swing.JTabbedPane; import java.awt.BorderLayout; +import java.util.ArrayList; /** * A parent window for server consoles. @@ -36,11 +38,14 @@ public class ServerConsoles { frame.setVisible(true); } - public void removeTab(int i) { - consolesTab.remove(i); + public static void update(ArrayList collections) { + consolesTab.removeAll(); + for (Collection collection : collections) { + consolesTab.add(collection.getName(), collection.getServerConsole().getPanel()); + } } - public Console addTab(String name) { + public static Console addTab(String name) { return new Console(consolesTab, name); } diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java index 298d2ff..b4da617 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java @@ -20,9 +20,10 @@ public class ServerTab implements ActionListener { private final JCheckBox chckbxEnabled; private final JButton btnRemoveServer, btnBrowse; private final JTextField directory; + private final JPanel panel; public ServerTab(String name) { - JPanel panel = new JPanel(); + panel = new JPanel(); GUI.getGUI().getPane().addTab(name, null, panel, null); SpringLayout sl_panel_3 = new SpringLayout(); panel.setLayout(sl_panel_3); @@ -105,6 +106,10 @@ public class ServerTab implements ActionListener { btnBrowse.addActionListener(this); } + public JPanel getPanel() { + return this.panel; + } + public String getPath() { return this.directory.getText(); } @@ -143,7 +148,9 @@ public class ServerTab implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btnRemoveServer) { - GUI.getGUI().removeServer(this); + GUI.getGUI().currentProfile().removeCollection(this); + GUI.getGUI().update(); + ServerConsoles.update(GUI.getGUI().currentProfile().getCollections()); } else if (e.getSource() == btnBrowse) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File("/"));