More gui added, and profiles half implemented

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

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,30 +437,105 @@ public class GUI implements ActionListener {
} else if (e.getSource() == btnStartServer) {
Server.startServers();
} else if (e.getSource() == btnStopServer) {
try {
setStatus("Servers are stopping");
Server.stop();
setStatus("Servers are stopped");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Could not stop server", "Error", JOptionPane.ERROR_MESSAGE);
}
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());
}
}
/*
//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;*/
}
private void stop() {
try {
setStatus("Servers are stopping");
Server.stop();
setStatus("Servers are stopped");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Could not stop server", "Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* 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) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try {
@ -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);
}
}
}
}
}