More gui added, and profiles half implemented

This commit is contained in:
Kristian Knarvik 2018-01-28 19:06:50 +01:00
parent bcaf8640d4
commit b0979c6e0f
5 changed files with 249 additions and 39 deletions

View File

@ -1,3 +1,4 @@
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.*;
import net.knarcraft.serverlauncher.userinterface.GUI;
@ -18,8 +19,8 @@ class Main {
EventQueue.invokeLater(() -> {
try {
setup();
GUI window = new GUI();
window.getFrame().setVisible(true);
new GUI();
Profile.addProfile("Default");
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -0,0 +1,80 @@
package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.userinterface.GUI;
import java.util.ArrayList;
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 Profile(String name) {
this.servers = new ArrayList<>();
this.name = name;
this.runInBackground = false;
this.delayStartup = 0;
this.downloadJars = false;
profiles.add(this);
}
public void addServer(Server server) {
this.servers.add(server);
}
public ArrayList<Server> getServers() {
return this.servers;
}
public static void addProfile(String name) {
if (name.equals("")) {
return;
}
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
return;
}
}
new Profile(name);
gui.addProfile(name);
}
public static Profile getProfile(String name) {
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
return profile;
}
}
return null;
}
public static void deleteProfile(String name) {
if (profiles.size() > 1) {
for (int i = 0; i < profiles.size(); i++) {
if (profiles.get(i).name.equals((name))) {
profiles.remove(i);
gui.removeProfile(i);
}
}
}
}
public void setRunInBackground(boolean value) {
this.runInBackground = value;
}
public void setDelayStartup(int value) {
if (value >= 0) {
this.delayStartup = value;
}
}
public void setDownloadJars(boolean value) {
this.downloadJars = value;
}
}

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 final ArrayList<Server> servers = new ArrayList<>();
private static GUI gui;
private final String name;
@ -41,7 +40,7 @@ public class Server {
this.serverVersion = null;
this.maxRam = ramList[0];
this.process = null;
servers.add(this);
gui.currentProfile().addServer(this);
window.addServer(name, this);
}
@ -62,6 +61,10 @@ public class Server {
gui = _gui;
}
public static GUI getGUI() {
return gui;
}
/**
* @return A representation of the name of a jarfile.
*/
@ -69,8 +72,12 @@ public class Server {
return this.type.getName() + this.serverVersion + ".jar";
}
public static ArrayList<Server> getServers() {
return servers;
public String getName() {
return this.name;
}
public String getPath() {
return this.path;
}
public Process getProcess() {
@ -93,8 +100,12 @@ public class Server {
return ramList;
}
public boolean isEnabled() {
return this.enabled;
}
public static void stop() throws IOException {
for (Server server : servers) {
for (Server server : gui.currentProfile().getServers()) {
if (server.writer != null) {
if (server.type.getName().equals("Bungee")) {
server.writer.write("end\n");
@ -132,7 +143,7 @@ public class Server {
*/
public static void startServers() {
System.out.println("Starting servers.");
for (Server server : servers) {
for (Server server : gui.currentProfile().getServers()) {
if (!server.run()) {
gui.setStatus("An error occurred. Start aborted");
}

View File

@ -2,24 +2,20 @@ 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.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Scanner;
public class GUI implements ActionListener {
private JFrame frame;
//Settings
private boolean runInBackground;
private int delayStartup;
private boolean downloadJars;
//Menu
private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options
@ -28,7 +24,7 @@ public class GUI implements ActionListener {
private JMenuItem mntmAbout, mntmStory; //Info/about
//Basic controls
private JButton btnStartServer, btnStopServer, addServer, backup, addProfile, delProfile;
private JComboBox profiles;
private JComboBox<String> profiles;
private final JLabel lblStatuslabel = new JLabel("Servers are stopped");
//Server controls
private JComboBox targetServer, targetPlayer;
@ -61,6 +57,26 @@ public class GUI implements ActionListener {
this.lblStatuslabel.setText(text);
}
public JTabbedPane getPane() {
return this.serversPane;
}
public void addProfile(String name) {
this.profiles.addItem(name);
}
public void removeProfile(int index) {
this.profiles.removeItemAt(index);
}
public Profile currentProfile() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
return Profile.getProfile(selected.toString());
}
return null;
}
/**
* Initialize the contents of the frame.
*/
@ -182,14 +198,14 @@ public class GUI implements ActionListener {
panelBasic.add(delProfile);
delProfile.addActionListener(this);
profiles = new JComboBox();
profiles = new JComboBox<>();
sl_panel.putConstraint(SpringLayout.NORTH, profiles, 1, 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, 15, SpringLayout.SOUTH, addProfile);
sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 8, SpringLayout.SOUTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic);
sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, 386, SpringLayout.WEST, panelBasic);
panelBasic.add(lblStatuslabel);
@ -314,6 +330,8 @@ public class GUI implements ActionListener {
panel_2.add(tabbedPane_1);
this.serversPane = tabbedPane_1;
frame.setVisible(true);
}
public void addServer(String name, Server server) {
@ -395,15 +413,11 @@ public class GUI implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == chckbxmntmRunInBackground) {
this.runInBackground = chckbxmntmRunInBackground.isSelected();
background();
} else if (e.getSource() == chckbxmntmDelayStartup) {
if (chckbxmntmDelayStartup.isSelected()) {
this.delayStartup = Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: "));
} else {
this.delayStartup = 0;
}
delay();
} else if (e.getSource() == chckbxmntmDownloadJars) {
this.downloadJars = chckbxmntmDownloadJars.isSelected();
downloadJars();
} else if (e.getSource() == mntmErrors) {
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Info/");
} else if (e.getSource() == mntmSetup) {
@ -423,6 +437,28 @@ public class GUI implements ActionListener {
} else if (e.getSource() == btnStartServer) {
Server.startServers();
} else if (e.getSource() == btnStopServer) {
stop();
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
new Server(serverName, this);
} else if (e.getSource() == backup) {
backup();
} else if (e.getSource() == addProfile) {
Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
} else if (e.getSource() == delProfile) {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile.deleteProfile(selected.toString());
}
}
/*
//Server controls
private JComboBox targetServer, targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;*/
}
private void stop() {
try {
setStatus("Servers are stopping");
Server.stop();
@ -430,21 +466,74 @@ public class GUI implements ActionListener {
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Could not stop server", "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
new Server(serverName, this);
} else if (e.getSource() == backup) {
}
/*
//Basic controls
backup, addProfile, delProfile;
private JComboBox profiles;
private JLabel lblStatuslabel;
//Server controls
private JComboBox targetServer, targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;*/
/**
* Asks the user for a delay if checked, and sets the value to the current profile.
*/
private void delay() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
if (chckbxmntmDelayStartup.isSelected()) {
Objects.requireNonNull(profile).setDelayStartup(Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: ")));
} else {
Objects.requireNonNull(profile).setDelayStartup(0);
}
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void background() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void downloadJars() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected());
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void backup() {
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) {
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());
File destFolder = new File(path, name);
if (!destFolder.exists()) {
if (destFolder.mkdirs()) {
try {
copyFolder(srcFolder, destFolder);
} catch (IOException e) {
e.printStackTrace();
return;
}
} else {
return;
}
}
}
}
}
}
private static void goToURL(String url) {
@ -481,6 +570,36 @@ public class GUI implements ActionListener {
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Messages file could not be read", "Setup", JOptionPane.ERROR_MESSAGE);
}
}
private void copyFolder(File src, File dest) throws IOException{
if (!src.isDirectory()) {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
this.setStatus("File copied from " + src + " to " + dest);
} else {
if(!dest.exists()){
if (dest.mkdir()) {
this.setStatus("Directory copied from " + src + " to " + dest);
} else {
return;
}
}
String files[] = src.list();
if (files != null) {
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(srcFile, destFile);
}
}
}
}
}

View File

@ -22,8 +22,7 @@ class ServerTest {
window.getFrame().setVisible(true);
new Server("Server1", window);
Server server1 = Server.getServers().get(0);
Server server1 = new Server("Server1", window);
server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType(ServerType.getServerTypes().get(4));