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.
This commit is contained in:
Kristian Knarvik 2018-02-02 20:09:54 +01:00
parent f0618d6338
commit 062f57736b
7 changed files with 46 additions and 39 deletions

View File

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

View File

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

View File

@ -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 "";
}
}
/**

View File

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

View File

@ -58,8 +58,6 @@ public class GUI implements ActionListener {
private JTabbedPane serversPane;
private final ArrayList<ServerTab> 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) {

View File

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

View File

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