More gui buttons work in theory

This commit is contained in:
Kristian Knarvik 2018-01-30 00:44:03 +01:00
parent 0267961eff
commit 1c00ae9bab
5 changed files with 158 additions and 45 deletions

View File

@ -4,6 +4,9 @@ import net.knarcraft.serverlauncher.userinterface.GUI;
import javax.naming.ConfigurationException;
import java.awt.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
//Java 8 required.
/**
@ -21,6 +24,12 @@ class Main {
setup();
new GUI();
Profile.addProfile("Default");
//TODO: replace with profiles loading generating a default profile if empty.
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(() -> {
//TODO: Read from consoles and insert into gui not added yet.
}, 0, 5, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -3,6 +3,8 @@ package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.userinterface.GUI;
import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
/**
@ -13,13 +15,17 @@ import java.util.ArrayList;
* @since 0.0.0.1
*/
public class Profile {
private static final GUI gui = Server.getGUI();
private static final ArrayList<Profile> profiles = new ArrayList<>();
private final ArrayList<Server> servers;
private final String name;
private boolean runInBackground;
private int delayStartup;
private boolean downloadJars;
private String vanillaVersion;
private String snapshotVersion;
private String spongeVanillaVersion;
private String bungeeVersion;
private Profile(String name) {
this.servers = new ArrayList<>();
@ -35,7 +41,25 @@ public class Profile {
}
public void removeServer(int i) {
servers.remove(i);
this.servers.remove(i);
}
private Server getServer(String name) {
for (Server server : this.servers) {
if (server.getName().equals(name)) {
return server;
}
}
return null;
}
public boolean serverExists(String name) {
for (Server server : this.servers) {
if (server.getName().equals(name)) {
return true;
}
}
return false;
}
public ArrayList<Server> getServers() {
@ -43,16 +67,21 @@ public class Profile {
}
public static void addProfile(String name) {
if (name == null) { //If a user cancels or crosses out window
return;
}
if (name.equals("")) {
JOptionPane.showMessageDialog(null, "Profile name can't be blank.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
JOptionPane.showMessageDialog(null, "There is already a profile with this name.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
}
new Profile(name);
gui.addProfile(name);
GUI.getGUI().addProfile(name);
}
public static Profile getProfile(String name) {
@ -69,7 +98,7 @@ public class Profile {
for (int i = 0; i < profiles.size(); i++) {
if (profiles.get(i).name.equals((name))) {
profiles.remove(i);
gui.removeProfile(i);
GUI.getGUI().removeProfile(i);
}
}
}
@ -88,4 +117,27 @@ public class Profile {
public void setDownloadJars(boolean value) {
this.downloadJars = value;
}
public void sendCommand(String serverName, String command) {
if (serverName.equals("All")) {
for (Server server : this.servers) {
try {
server.sendCommand(command);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Server " + server.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} else {
Server target = getServer(serverName);
if (target != null) {
try {
target.sendCommand(command);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Server " + target.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "Server " + serverName + " is invalid.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}

View File

@ -19,7 +19,6 @@ import java.util.ArrayList;
public class Server {
/** Available ram sizes. For GUI dropdown */
private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
private static GUI gui;
private final String name;
private String path;
@ -40,7 +39,7 @@ public class Server {
this.serverVersion = null;
this.maxRam = ramList[0];
this.process = null;
gui.currentProfile().addServer(this);
GUI.getGUI().currentProfile().addServer(this);
window.addServer(name);
}
@ -57,14 +56,6 @@ public class Server {
}
}
public static void setGui(GUI _gui) {
gui = _gui;
}
public static GUI getGUI() {
return gui;
}
/**
* @return A representation of the name of a jarfile.
*/
@ -104,8 +95,12 @@ public class Server {
return this.enabled;
}
public void setMaxRam(String ram) {
this.maxRam = ram;
}
public static void stop() throws IOException {
for (Server server : gui.currentProfile().getServers()) {
for (Server server : GUI.getGUI().currentProfile().getServers()) {
if (server.writer != null) {
if (server.type.getName().equals("Bungee")) {
server.writer.write("end\n");
@ -134,18 +129,14 @@ public class Server {
throw new IllegalArgumentException("Invalid server version.");
}
public void setMaxRam(String ram) {
this.maxRam = ram;
}
/**
* Runs all enabled servers with their settings.
*/
public static void startServers() {
System.out.println("Starting servers.");
for (Server server : gui.currentProfile().getServers()) {
for (Server server : GUI.getGUI().currentProfile().getServers()) {
if (!server.run()) {
gui.setStatus("An error occurred. Start aborted");
GUI.getGUI().setStatus("An error occurred. Start aborted");
}
}
}
@ -156,11 +147,11 @@ public class Server {
private boolean run() {
if (this.enabled) {
try {
gui.setStatus("Downloading jar...");
GUI.getGUI().setStatus("Downloading jar...");
this.downloadJar();
gui.setStatus("File downloaded");
GUI.getGUI().setStatus("File downloaded");
} catch (FileNotFoundException e) {
gui.setStatus("Error: Jar file not found");
GUI.getGUI().setStatus("Error: Jar file not found");
return false;
}
try {
@ -170,10 +161,10 @@ public class Server {
this.process = builder.start();
OutputStream stdin = this.process.getOutputStream ();
this.writer = new BufferedWriter(new OutputStreamWriter(stdin));
gui.setStatus("Servers are running");
GUI.getGUI().setStatus("Servers are running");
return true;
} catch (IOException e) {
gui.setStatus("Could not start server");
GUI.getGUI().setStatus("Could not start server");
return false;
}
} else {
@ -308,4 +299,12 @@ public class Server {
int startPos = string.indexOf(start) + start.length();
return string.substring(startPos, string.indexOf(end, startPos));
}
public void sendCommand(String command) throws IOException {
if (this.writer != null) {
this.writer.write(command);
this.writer.flush();
this.writer = null;
}
}
}

View File

@ -1,7 +1,6 @@
package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.*;
@ -25,6 +24,8 @@ import java.util.Scanner;
*/
public class GUI implements ActionListener {
private static GUI gui;
private JFrame frame;
//Menu
@ -50,7 +51,7 @@ public class GUI implements ActionListener {
private JTabbedPane serversPane;
private ArrayList<ServerTab> serverTabs = new ArrayList<>();
private final ArrayList<ServerTab> serverTabs = new ArrayList<>();
/**
* Create the application.
@ -58,7 +59,7 @@ public class GUI implements ActionListener {
public GUI() {
initialize();
loadMessages();
Server.setGui(this);
gui = this;
}
public JFrame getFrame() {
@ -89,6 +90,10 @@ public class GUI implements ActionListener {
return null;
}
public static GUI getGUI() {
return gui;
}
/**
* Removes a server's tab, removes it from the list of tabs, and removes the server from Profile.java
*
@ -377,6 +382,16 @@ public class GUI implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String selectedServerValue = null, selectedPlayerValue = null;
Object selectedServer = targetServer.getSelectedItem();
if (selectedServer != null) {
selectedServerValue = selectedServer.toString();
}
Object selectedPlayer = targetPlayer.getSelectedItem();
if (selectedPlayer != null) {
selectedPlayerValue = selectedPlayer.toString();
}
if (e.getSource() == chckbxmntmRunInBackground) {
background();
} else if (e.getSource() == chckbxmntmDelayStartup) {
@ -406,7 +421,11 @@ 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 servername must my unique and not empty.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == backup) {
backup();
} else if (e.getSource() == addProfile) {
@ -416,13 +435,37 @@ public class GUI implements ActionListener {
if (selected != null) {
Profile.deleteProfile(selected.toString());
}
} else if (e.getSource() == btnKick) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
}
} else if (e.getSource() == btnBan) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
}
} else if (e.getSource() == btnOp) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
}
} else if (e.getSource() == btnDeop) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
}
} else if (e.getSource() == btnCustomCommand) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, customCommand.getSelectedText());
}
} else if (e.getSource() == btnSaveserver) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, "save-all");
}
} else if (e.getSource() == btnReload) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
//TODO: Make server consoles window, and toggle visibility on this action.
}
/*
//Server controls
private JComboBox targetServer, targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;*/
}
/**
@ -441,7 +484,7 @@ public class GUI implements ActionListener {
Server.stop();
setStatus("Servers are stopped");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Could not stop server", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Could not stop server.", "Error", JOptionPane.ERROR_MESSAGE);
}
}

View File

@ -7,16 +7,19 @@ 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;
class ServerTab implements ActionListener {
private final JComboBox<String> serverTypes;
private final JComboBox<String> serverVersions;
private final JComboBox<String> maxRam;
private final JCheckBox chckbxEnabled;
private final JButton btnRemoveServer;
private final JButton btnBrowse;
private final JTextField directory;
public ServerTab(String name) {
JPanel panel = new JPanel();
Server.getGUI().getPane().addTab(name, null, panel, null);
GUI.getGUI().getPane().addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout();
panel.setLayout(sl_panel_3);
@ -100,9 +103,16 @@ public class ServerTab implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRemoveServer) {
Server.getGUI().removeServer(this);
GUI.getGUI().removeServer(this);
} else if (e.getSource() == btnBrowse) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("/"));
chooser.setDialogTitle("Backup folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
directory.setText(chooser.getSelectedFile().toString());
}
}
}
}