Servers can now be removed

This commit is contained in:
Kristian Knarvik 2018-01-29 20:14:17 +01:00
parent b0979c6e0f
commit 0267961eff
5 changed files with 203 additions and 79 deletions

View File

@ -1,10 +1,10 @@
import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.*; import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI; import net.knarcraft.serverlauncher.userinterface.GUI;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import java.awt.*; import java.awt.*;
//Java 9 required. //Java 8 required.
/** /**
* A software for managing Minecraft servers. * A software for managing Minecraft servers.
@ -35,5 +35,4 @@ class Main {
System.exit(1); System.exit(1);
} }
} }
// TODO: Add gui functionality
} }

View File

@ -5,6 +5,13 @@ import net.knarcraft.serverlauncher.userinterface.GUI;
import java.util.ArrayList; import java.util.ArrayList;
/**
* Contains all user settings
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class Profile { public class Profile {
private static final GUI gui = Server.getGUI(); private static final GUI gui = Server.getGUI();
private static final ArrayList<Profile> profiles = new ArrayList<>(); private static final ArrayList<Profile> profiles = new ArrayList<>();
@ -27,6 +34,10 @@ public class Profile {
this.servers.add(server); this.servers.add(server);
} }
public void removeServer(int i) {
servers.remove(i);
}
public ArrayList<Server> getServers() { public ArrayList<Server> getServers() {
return this.servers; return this.servers;
} }

View File

@ -41,7 +41,7 @@ public class Server {
this.maxRam = ramList[0]; this.maxRam = ramList[0];
this.process = null; this.process = null;
gui.currentProfile().addServer(this); gui.currentProfile().addServer(this);
window.addServer(name, this); window.addServer(name);
} }
public void addPlayer(String name) { public void addPlayer(String name) {

View File

@ -7,12 +7,22 @@ import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
/**
* Generates a GUI
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class GUI implements ActionListener { public class GUI implements ActionListener {
private JFrame frame; private JFrame frame;
@ -40,6 +50,8 @@ public class GUI implements ActionListener {
private JTabbedPane serversPane; private JTabbedPane serversPane;
private ArrayList<ServerTab> serverTabs = new ArrayList<>();
/** /**
* Create the application. * Create the application.
*/ */
@ -77,6 +89,24 @@ public class GUI implements ActionListener {
return null; return null;
} }
/**
* Removes a server's tab, removes it from the list of tabs, and removes the server from Profile.java
*
* @param tab The current tab object
*/
public void removeServer(ServerTab tab) {
for (int i = 0; i < this.serverTabs.size(); i++) {
System.out.println(this.serverTabs.get(i));
if(this.serverTabs.get(i) == tab) {
serversPane.remove(i);
currentProfile().removeServer(i);
this.serverTabs.remove(i);
i = serverTabs.size();
}
}
}
/** /**
* Initialize the contents of the frame. * Initialize the contents of the frame.
*/ */
@ -89,9 +119,16 @@ public class GUI implements ActionListener {
frame = new JFrame(); frame = new JFrame();
frame.setBounds(100, 100, 398, 219); frame.setBounds(100, 100, 398, 219);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false); frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
save();
stop();
}
});
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); frame.setJMenuBar(menuBar);
@ -334,80 +371,8 @@ public class GUI implements ActionListener {
frame.setVisible(true); frame.setVisible(true);
} }
public void addServer(String name, Server server) { public void addServer(String name) {
JPanel panel = new JPanel(); serverTabs.add(new ServerTab(name));
this.serversPane.addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout();
panel.setLayout(sl_panel_3);
JLabel lblServerType = new JLabel("Server type");
sl_panel_3.putConstraint(SpringLayout.NORTH, lblServerType, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, lblServerType, 10, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblServerType, 30, SpringLayout.NORTH, panel);
panel.add(lblServerType);
JComboBox<String> comboBox_3 = new JComboBox<>(ServerType.getTypeNames());
sl_panel_3.putConstraint(SpringLayout.NORTH, comboBox_3, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, comboBox_3, 16, SpringLayout.EAST, lblServerType);
panel.add(comboBox_3);
JLabel lblServerVersion = new JLabel("Server version");
sl_panel_3.putConstraint(SpringLayout.NORTH, lblServerVersion, 6, SpringLayout.SOUTH, lblServerType);
sl_panel_3.putConstraint(SpringLayout.WEST, lblServerVersion, 10, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblServerVersion, 26, SpringLayout.SOUTH, lblServerType);
panel.add(lblServerVersion);
JComboBox<String> comboBox_4 = new JComboBox<>(ServerType.getServerTypes().get(0).getVersions());
sl_panel_3.putConstraint(SpringLayout.NORTH, comboBox_4, 6, SpringLayout.SOUTH, comboBox_3);
sl_panel_3.putConstraint(SpringLayout.WEST, comboBox_4, 0, SpringLayout.WEST, comboBox_3);
panel.add(comboBox_4);
JLabel lblMaxRam = new JLabel("Max ram");
sl_panel_3.putConstraint(SpringLayout.EAST, comboBox_3, -13, SpringLayout.WEST, lblMaxRam);
sl_panel_3.putConstraint(SpringLayout.NORTH, lblMaxRam, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblMaxRam, -92, SpringLayout.SOUTH, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, lblMaxRam, -111, SpringLayout.EAST, panel);
panel.add(lblMaxRam);
JComboBox<String> comboBox_5 = new JComboBox<>(Server.getRamList());
sl_panel_3.putConstraint(SpringLayout.NORTH, comboBox_5, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, comboBox_5, 6, SpringLayout.EAST, lblMaxRam);
sl_panel_3.putConstraint(SpringLayout.EAST, comboBox_5, 86, SpringLayout.EAST, lblMaxRam);
panel.add(comboBox_5);
JCheckBox chckbxEnabled = new JCheckBox("Enabled");
sl_panel_3.putConstraint(SpringLayout.NORTH, chckbxEnabled, 6, SpringLayout.SOUTH, lblServerVersion);
sl_panel_3.putConstraint(SpringLayout.EAST, chckbxEnabled, 0, SpringLayout.EAST, lblServerType);
panel.add(chckbxEnabled);
JButton btnRemoveServer = new JButton("Remove server");
sl_panel_3.putConstraint(SpringLayout.NORTH, btnRemoveServer, 5, SpringLayout.SOUTH, lblMaxRam);
sl_panel_3.putConstraint(SpringLayout.SOUTH, btnRemoveServer, -64, SpringLayout.SOUTH, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, comboBox_5, -5, SpringLayout.NORTH, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.EAST, comboBox_4, -13, SpringLayout.WEST, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.WEST, btnRemoveServer, 239, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, btnRemoveServer, 0, SpringLayout.EAST, comboBox_5);
panel.add(btnRemoveServer);
JLabel lblDirectory = new JLabel("Directory");
sl_panel_3.putConstraint(SpringLayout.NORTH, lblDirectory, 1, SpringLayout.NORTH, chckbxEnabled);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblDirectory, 0, SpringLayout.SOUTH, chckbxEnabled);
panel.add(lblDirectory);
JTextField textField_1 = new JTextField();
sl_panel_3.putConstraint(SpringLayout.NORTH, textField_1, 4, SpringLayout.SOUTH, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.SOUTH, textField_1, -37, SpringLayout.SOUTH, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, lblDirectory, -6, SpringLayout.WEST, textField_1);
sl_panel_3.putConstraint(SpringLayout.WEST, textField_1, 130, SpringLayout.WEST, panel);
panel.add(textField_1);
textField_1.setColumns(10);
JButton btnBrowse = new JButton("Browse");
sl_panel_3.putConstraint(SpringLayout.EAST, textField_1, -6, SpringLayout.WEST, btnBrowse);
sl_panel_3.putConstraint(SpringLayout.NORTH, btnBrowse, 3, SpringLayout.SOUTH, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.WEST, btnBrowse, 293, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, btnBrowse, 0, SpringLayout.EAST, comboBox_5);
panel.add(btnBrowse);
} }
@Override @Override
@ -435,6 +400,7 @@ public class GUI implements ActionListener {
} else if (e.getSource() == mntmStory) { } else if (e.getSource() == mntmStory) {
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/"); goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/");
} else if (e.getSource() == btnStartServer) { } else if (e.getSource() == btnStartServer) {
save();
Server.startServers(); Server.startServers();
} else if (e.getSource() == btnStopServer) { } else if (e.getSource() == btnStopServer) {
stop(); stop();
@ -456,8 +422,19 @@ public class GUI implements ActionListener {
private JComboBox targetServer, targetPlayer; private JComboBox targetServer, targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles; private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;*/ private JTextField customCommand;*/
} }
/**
* Reads all combo boxes, updates variables and saves to disk
*/
private void save() {
//TODO: Finish save
}
/**
* Stops all servers
*/
private void stop() { private void stop() {
try { try {
setStatus("Servers are stopping"); setStatus("Servers are stopping");
@ -485,6 +462,9 @@ public class GUI implements ActionListener {
} }
} }
/**
* Saves the runInBackground setting to the current profile
*/
private void background() { private void background() {
Object selected = profiles.getSelectedItem(); Object selected = profiles.getSelectedItem();
if (selected != null) { if (selected != null) {
@ -493,8 +473,16 @@ public class GUI implements ActionListener {
} else { } else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
} }
if (chckbxmntmRunInBackground.isSelected()) {
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
} else {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
} }
/**
* Saves the downloadJars setting to the current profile
*/
private void downloadJars() { private void downloadJars() {
Object selected = profiles.getSelectedItem(); Object selected = profiles.getSelectedItem();
if (selected != null) { if (selected != null) {
@ -505,6 +493,9 @@ public class GUI implements ActionListener {
} }
} }
/**
* Copies all server directories to a folder specified by the user
*/
private void backup() { private void backup() {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File(".")); chooser.setCurrentDirectory(new java.io.File("."));
@ -536,6 +527,11 @@ public class GUI implements ActionListener {
} }
} }
/**
* Opens an url in the user's default application
*
* @param url URL to open
*/
private static void goToURL(String url) { private static void goToURL(String url) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try { try {
@ -545,6 +541,9 @@ public class GUI implements ActionListener {
} }
} }
/**
* Loads popup messages from a text file
*/
private void loadMessages() { private void loadMessages() {
try (Scanner in = new Scanner(new File("config/menumsg.csv"))) { try (Scanner in = new Scanner(new File("config/menumsg.csv"))) {
while (in.hasNextLine()) { while (in.hasNextLine()) {
@ -572,6 +571,13 @@ public class GUI implements ActionListener {
} }
} }
/**
* Recursivly copies a folder to another location
*
* @param src The folder to copy
* @param dest Target destination
* @throws IOException If we can't start a file stream
*/
private void copyFolder(File src, File dest) throws IOException{ private void copyFolder(File src, File dest) throws IOException{
if (!src.isDirectory()) { if (!src.isDirectory()) {
InputStream in = new FileInputStream(src); InputStream in = new FileInputStream(src);

View File

@ -0,0 +1,108 @@
package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ServerTab implements ActionListener {
private JComboBox<String> serverTypes, serverVersions, maxRam;
private JCheckBox chckbxEnabled;
private JButton btnRemoveServer, btnBrowse;
private JTextField directory;
public ServerTab(String name) {
JPanel panel = new JPanel();
Server.getGUI().getPane().addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout();
panel.setLayout(sl_panel_3);
JLabel lblServerType = new JLabel("Server type");
sl_panel_3.putConstraint(SpringLayout.NORTH, lblServerType, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, lblServerType, 10, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblServerType, 30, SpringLayout.NORTH, panel);
panel.add(lblServerType);
serverTypes = new JComboBox<>(ServerType.getTypeNames());
sl_panel_3.putConstraint(SpringLayout.NORTH, serverTypes, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, serverTypes, 16, SpringLayout.EAST, lblServerType);
panel.add(serverTypes);
serverTypes.addActionListener(this);
JLabel lblServerVersion = new JLabel("Server version");
sl_panel_3.putConstraint(SpringLayout.NORTH, lblServerVersion, 6, SpringLayout.SOUTH, lblServerType);
sl_panel_3.putConstraint(SpringLayout.WEST, lblServerVersion, 10, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblServerVersion, 26, SpringLayout.SOUTH, lblServerType);
panel.add(lblServerVersion);
serverVersions = new JComboBox<>(ServerType.getServerTypes().get(0).getVersions());
sl_panel_3.putConstraint(SpringLayout.NORTH, serverVersions, 6, SpringLayout.SOUTH, serverTypes);
sl_panel_3.putConstraint(SpringLayout.WEST, serverVersions, 0, SpringLayout.WEST, serverTypes);
panel.add(serverVersions);
serverVersions.addActionListener(this);
JLabel lblMaxRam = new JLabel("Max ram");
sl_panel_3.putConstraint(SpringLayout.EAST, serverTypes, -13, SpringLayout.WEST, lblMaxRam);
sl_panel_3.putConstraint(SpringLayout.NORTH, lblMaxRam, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblMaxRam, -92, SpringLayout.SOUTH, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, lblMaxRam, -111, SpringLayout.EAST, panel);
panel.add(lblMaxRam);
maxRam = new JComboBox<>(Server.getRamList());
sl_panel_3.putConstraint(SpringLayout.NORTH, maxRam, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, maxRam, 6, SpringLayout.EAST, lblMaxRam);
sl_panel_3.putConstraint(SpringLayout.EAST, maxRam, 86, SpringLayout.EAST, lblMaxRam);
panel.add(maxRam);
maxRam.addActionListener(this);
chckbxEnabled = new JCheckBox("Enabled");
sl_panel_3.putConstraint(SpringLayout.NORTH, chckbxEnabled, 6, SpringLayout.SOUTH, lblServerVersion);
sl_panel_3.putConstraint(SpringLayout.EAST, chckbxEnabled, 0, SpringLayout.EAST, lblServerType);
panel.add(chckbxEnabled);
chckbxEnabled.addActionListener(this);
btnRemoveServer = new JButton("Remove server");
sl_panel_3.putConstraint(SpringLayout.NORTH, btnRemoveServer, 5, SpringLayout.SOUTH, lblMaxRam);
sl_panel_3.putConstraint(SpringLayout.SOUTH, btnRemoveServer, -64, SpringLayout.SOUTH, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, maxRam, -5, SpringLayout.NORTH, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.EAST, serverVersions, -13, SpringLayout.WEST, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.WEST, btnRemoveServer, 239, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, btnRemoveServer, 0, SpringLayout.EAST, maxRam);
panel.add(btnRemoveServer);
btnRemoveServer.addActionListener(this);
JLabel lblDirectory = new JLabel("Directory");
sl_panel_3.putConstraint(SpringLayout.NORTH, lblDirectory, 1, SpringLayout.NORTH, chckbxEnabled);
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblDirectory, 0, SpringLayout.SOUTH, chckbxEnabled);
panel.add(lblDirectory);
directory = new JTextField();
sl_panel_3.putConstraint(SpringLayout.NORTH, directory, 4, SpringLayout.SOUTH, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.SOUTH, directory, -37, SpringLayout.SOUTH, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, lblDirectory, -6, SpringLayout.WEST, directory);
sl_panel_3.putConstraint(SpringLayout.WEST, directory, 130, SpringLayout.WEST, panel);
panel.add(directory);
directory.setColumns(10);
directory.addActionListener(this);
btnBrowse = new JButton("Browse");
sl_panel_3.putConstraint(SpringLayout.EAST, directory, -6, SpringLayout.WEST, btnBrowse);
sl_panel_3.putConstraint(SpringLayout.NORTH, btnBrowse, 3, SpringLayout.SOUTH, btnRemoveServer);
sl_panel_3.putConstraint(SpringLayout.WEST, btnBrowse, 293, SpringLayout.WEST, panel);
sl_panel_3.putConstraint(SpringLayout.EAST, btnBrowse, 0, SpringLayout.EAST, maxRam);
panel.add(btnBrowse);
btnBrowse.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRemoveServer) {
Server.getGUI().removeServer(this);
} else if (e.getSource() == btnBrowse) {
}
}
}