Profile switching and cleanup

It is now possible to switch between profiles, and the server tabs
will update.
This commit is contained in:
Kristian Knarvik 2018-02-02 22:02:57 +01:00
parent 062f57736b
commit 75d44f6d1d
8 changed files with 202 additions and 127 deletions

View File

@ -1,3 +1,3 @@
Manifest-Version: 1.0 Manifest-Version: 1.0
Main-Class: Main Main-Class: net.knarcraft.serverlauncher.Main

View File

@ -1,5 +1,8 @@
package net.knarcraft.serverlauncher;
import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI; import net.knarcraft.serverlauncher.userinterface.GUI;
import net.knarcraft.serverlauncher.userinterface.ServerConsoles; import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
@ -20,38 +23,30 @@ import java.util.concurrent.TimeUnit;
* @since 0.0.0.1 * @since 0.0.0.1
*/ */
class Main { public class Main {
private static GUI gui;
public static void main(String[] args) { public static void main(String[] args) {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
try { try {
setup(); setup();
new GUI(); gui = new GUI();
new ServerConsoles(); new ServerConsoles();
Profile.addProfile("Default"); Profile.addProfile("Default");
//TODO: replace with profiles loading generating a default profile if empty. //TODO: replace with profiles loading generating a default profile if empty.
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(() -> { exec.scheduleAtFixedRate(Main::updateConsoles, 10, 250, TimeUnit.MILLISECONDS);
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
if (collection.getServer().isEnabled() && collection.getServer().getProcess() != null) {
try {
String readText = collection.getServer().read();
if (!readText.equals("")) {
collection.getServerConsole().output(readText);
updatePlayerList(readText, collection);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}, 10, 250, TimeUnit.MILLISECONDS);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}); });
} }
public static GUI gui() {
return gui;
}
private static void setup() { private static void setup() {
try { try {
ServerType.loadServerTypes(); ServerType.loadServerTypes();
@ -61,32 +56,59 @@ class Main {
} }
} }
/**
* Reads from server processes, and writes the output to our custom consoles.
*/
private static void updateConsoles() {
for (Collection collection : Profile.getCurrent().getCollections()) {
Server server = collection.getServer();
if (server.isEnabled() && server.getProcess() != null) {
try {
String readText = server.read();
if (!readText.equals("")) {
collection.getServerConsole().output(readText);
updatePlayerList(readText, collection);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Looks for strings implying a player has joined or left, and updates the appropriate lists.
*
* @param text The text to search.
* @param collection The server which sent the text.
*/
private static void updatePlayerList(String text, Collection collection) { private static void updatePlayerList(String text, Collection collection) {
if (!collection.getServer().typeName().equals("Bungee")) { //Bungee servers are not to be treated as normal servers. Server server = collection.getServer();
if (!server.typeName().equals("Bungee")) {
String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game"); String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
if (!joinedPlayer.equals("")) { if (!joinedPlayer.equals("")) {
if (!collection.getServer().hasPlayer(joinedPlayer)) { if (!server.hasPlayer(joinedPlayer)) {
collection.getServer().addPlayer(joinedPlayer); server.addPlayer(joinedPlayer);
} }
} else { } else {
joinedPlayer = stringBetween(text, "UUID of player ", " is "); joinedPlayer = stringBetween(text, "UUID of player ", " is ");
if (!joinedPlayer.equals("")) { if (!joinedPlayer.equals("")) {
if (!collection.getServer().hasPlayer(joinedPlayer)) { if (!server.hasPlayer(joinedPlayer)) {
collection.getServer().addPlayer(joinedPlayer); server.addPlayer(joinedPlayer);
} }
} }
} }
if (joinedPlayer.equals("")) { if (joinedPlayer.equals("")) {
String leftPlayer = stringBetween(text, "INFO]: ", " lost connection"); String leftPlayer = stringBetween(text, "INFO]: ", " lost connection");
if (!leftPlayer.equals("")) { if (!leftPlayer.equals("")) {
if (collection.getServer().hasPlayer(leftPlayer)) { if (server.hasPlayer(leftPlayer)) {
collection.getServer().removePlayer(leftPlayer); server.removePlayer(leftPlayer);
} }
} else { } else {
leftPlayer = stringBetween(text, "[Server thread/INFO]: ", " left the game"); leftPlayer = stringBetween(text, "[Server thread/INFO]: ", " left the game");
if (!leftPlayer.equals("")) { if (!leftPlayer.equals("")) {
if (collection.getServer().hasPlayer(leftPlayer)) { if (server.hasPlayer(leftPlayer)) {
collection.getServer().removePlayer(leftPlayer); server.removePlayer(leftPlayer);
} }
} }
} }

View File

@ -2,8 +2,8 @@ package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI;
import net.knarcraft.serverlauncher.userinterface.ServerTab; import net.knarcraft.serverlauncher.userinterface.ServerTab;
import net.knarcraft.serverlauncher.Main;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
@ -18,6 +18,7 @@ import java.util.ArrayList;
*/ */
public class Profile { public class Profile {
private static final ArrayList<Profile> profiles = new ArrayList<>(); private static final ArrayList<Profile> profiles = new ArrayList<>();
private static Profile current;
private final ArrayList<Collection> collections; private final ArrayList<Collection> collections;
private final String name; private final String name;
@ -36,25 +37,30 @@ public class Profile {
this.delayStartup = 0; this.delayStartup = 0;
this.downloadJars = false; this.downloadJars = false;
profiles.add(this); profiles.add(this);
if (current == null) {
current = this;
}
} }
public void addCollection(String name) { public void addCollection(String name) {
if (name == null) { //If a user cancels or crosses out window
return;
}
if (!collectionExists(name) && !name.equals("") && !name.equals("All")) { if (!collectionExists(name) && !name.equals("") && !name.equals("All")) {
collections.add(new Collection(name)); collections.add(new Collection(name));
GUI.getGUI().updateSelectServers();
} else { } else {
JOptionPane.showMessageDialog(null, "A server name must my unique and not empty or \"All\".", "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "A server name must my unique and not empty or \"All\".", "Error", JOptionPane.ERROR_MESSAGE);
} }
} }
public void removeCollection(ServerTab serverTab) { public void removeCollection(String name) {
for (int i = 0; i < collections.size(); i++) { for (int i = 0; i < collections.size(); i++) {
if (collections.get(i).getServerTab() == serverTab) { if (collections.get(i).getName().equals(name)) {
this.collections.remove(i); this.collections.remove(i);
GUI.getGUI().updateSelectServers(); Main.gui().updateSelectServers();
break;
} }
} }
} }
public Collection getCollection(String name) { public Collection getCollection(String name) {
@ -94,7 +100,7 @@ public class Profile {
} }
} }
new Profile(name); new Profile(name);
GUI.getGUI().addProfile(name); Main.gui().addProfile(name);
} }
public static Profile getProfile(String name) { public static Profile getProfile(String name) {
@ -111,12 +117,25 @@ public class Profile {
for (int i = 0; i < profiles.size(); i++) { for (int i = 0; i < profiles.size(); i++) {
if (profiles.get(i).name.equals((name))) { if (profiles.get(i).name.equals((name))) {
profiles.remove(i); profiles.remove(i);
GUI.getGUI().removeProfile(i); Main.gui().removeProfile(i);
} }
} }
} }
} }
public static Profile getCurrent() {
return current;
}
public static void setCurrent(String name) {
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
current = profile;
break;
}
}
}
public void setRunInBackground(boolean value) { public void setRunInBackground(boolean value) {
this.runInBackground = value; this.runInBackground = value;
} }
@ -131,13 +150,19 @@ public class Profile {
this.downloadJars = value; this.downloadJars = value;
} }
/**
* Sends a command to a server, or all servers
*
* @param serverName The target server
* @param command The command to send.
*/
public void sendCommand(String serverName, String command) { public void sendCommand(String serverName, String command) {
if (serverName.equals("All")) { if (serverName.equals("All")) {
for (Collection collection : this.collections) { for (Collection collection : this.collections) {
try { try {
collection.getServer().sendCommand(command); collection.getServer().sendCommand(command);
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog(null, "Server " + collection.getServer().getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "Server " + collection.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
} }
} }
} else { } else {
@ -155,6 +180,12 @@ public class Profile {
} }
} }
/**
* Sets a server type's last downloaded version.
*
* @param type The version type
* @param version The version string
*/
public void setVersion(String type, String version) { public void setVersion(String type, String version) {
if (!version.equals("")) { if (!version.equals("")) {
switch (type) { switch (type) {
@ -173,6 +204,12 @@ public class Profile {
} }
} }
/**
* Returns the current version of a type
*
* @param type The version type
* @return The version string
*/
public String getVersion(String type) { public String getVersion(String type) {
switch (type) { switch (type) {
case "Vanilla": case "Vanilla":
@ -188,6 +225,9 @@ public class Profile {
} }
} }
/**
* Reads all server tabs, and saves it to the variables of the corresponding servers.
*/
public void save() { public void save() {
for (Collection collection : this.collections) { for (Collection collection : this.collections) {
Server server = collection.getServer(); Server server = collection.getServer();

View File

@ -1,9 +1,8 @@
package net.knarcraft.serverlauncher.server; package net.knarcraft.serverlauncher.server;
import net.knarcraft.serverlauncher.Main;
import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.userinterface.GUI;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.Scanner; import java.util.Scanner;
@ -48,7 +47,7 @@ public class Server {
public void addPlayer(String name) { public void addPlayer(String name) {
this.playerList.add(name); this.playerList.add(name);
GUI.getGUI().addPlayer(name); Main.gui().addPlayer(name);
} }
public void removePlayer(String name) { public void removePlayer(String name) {
@ -57,7 +56,7 @@ public class Server {
playerList.remove(i); playerList.remove(i);
} }
} }
GUI.getGUI().removePlayer(name); Main.gui().removePlayer(name);
} }
public ArrayList<String> getPlayers() { public ArrayList<String> getPlayers() {
@ -121,7 +120,7 @@ public class Server {
} }
public static void stop() throws IOException { public static void stop() throws IOException {
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
if (collection.getServer().writer != null) { if (collection.getServer().writer != null) {
if (collection.getServer().type.getName().equals("Bungee")) { if (collection.getServer().type.getName().equals("Bungee")) {
collection.getServer().writer.write("end\n"); collection.getServer().writer.write("end\n");
@ -158,10 +157,10 @@ public class Server {
* Runs all enabled servers with their settings. * Runs all enabled servers with their settings.
*/ */
public static void startServers() { public static void startServers() {
GUI.getGUI().setStatus("Starting servers"); Main.gui().setStatus("Starting servers");
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().run()) { if (!collection.getServer().run()) {
GUI.getGUI().setStatus("An error occurred. Start aborted"); Main.gui().setStatus("An error occurred. Start aborted");
} }
} }
} }
@ -172,11 +171,11 @@ public class Server {
private boolean run() { private boolean run() {
if (this.enabled) { if (this.enabled) {
try { try {
GUI.getGUI().setStatus("Downloading jar..."); Main.gui().setStatus("Downloading jar...");
this.downloadJar(); this.downloadJar();
GUI.getGUI().setStatus("File downloaded"); Main.gui().setStatus("File downloaded");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
GUI.getGUI().setStatus("Error: Jar file not found"); Main.gui().setStatus("Error: Jar file not found");
return false; return false;
} }
try { try {
@ -197,10 +196,10 @@ public class Server {
this.writer = new BufferedWriter(new OutputStreamWriter(stdin)); this.writer = new BufferedWriter(new OutputStreamWriter(stdin));
InputStream stdout = this.process.getInputStream(); InputStream stdout = this.process.getInputStream();
this.reader = new BufferedReader (new InputStreamReader(stdout)); this.reader = new BufferedReader (new InputStreamReader(stdout));
GUI.getGUI().setStatus("Servers are running"); Main.gui().setStatus("Servers are running");
return true; return true;
} catch (IOException e) { } catch (IOException e) {
GUI.getGUI().setStatus("Could not start server"); Main.gui().setStatus("Could not start server");
return false; return false;
} }
} else { } else {
@ -227,7 +226,7 @@ public class Server {
AdvancedServerType type; AdvancedServerType type;
File file = new File(this.path + "\\" + this.getType()); File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType()); Path filePath = Paths.get(this.path + "\\" + this.getType());
Profile currentProfile = GUI.getGUI().currentProfile(); Profile currentProfile = Profile.getCurrent();
String versionText; String versionText;
String newestVersion; String newestVersion;
String url = this.type.getDownloadURL(); String url = this.type.getDownloadURL();
@ -263,7 +262,7 @@ public class Server {
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) { if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath); success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
GUI.getGUI().currentProfile().setVersion(this.type.getName(), newestVersion); currentProfile.setVersion(this.type.getName(), newestVersion);
if (!success) { if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded."); throw new FileNotFoundException("Jar file could not be downloaded.");
} }

View File

@ -29,12 +29,10 @@ public class Console implements ActionListener {
panel = new JPanel(); panel = new JPanel();
tab.addTab(name, null, panel, null); tab.addTab(name, null, panel, null);
panel.setLayout(new BorderLayout(0, 0)); panel.setLayout(new BorderLayout(0, 0));
textInput = new JTextField(); textInput = new JTextField();
panel.add(textInput, BorderLayout.SOUTH); panel.add(textInput, BorderLayout.SOUTH);
textInput.setColumns(10); textInput.setColumns(10);
textInput.addActionListener(this); textInput.addActionListener(this);
textOutput = new JTextArea(); textOutput = new JTextArea();
JScrollPane scroll = new JScrollPane(textOutput); JScrollPane scroll = new JScrollPane(textOutput);
panel.add(scroll, BorderLayout.CENTER); panel.add(scroll, BorderLayout.CENTER);
@ -54,10 +52,9 @@ public class Console implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) { if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name.
String text = textInput.getText(); String text = textInput.getText();
Profile profile = GUI.getGUI().currentProfile(); Profile.getCurrent().sendCommand(this.name, text);
profile.sendCommand(this.name, text);
textInput.setText(""); textInput.setText("");
} }
} }

View File

@ -28,10 +28,7 @@ import java.util.concurrent.Executors;
*/ */
public class GUI implements ActionListener { public class GUI implements ActionListener {
private static GUI gui;
private JFrame frame; private JFrame frame;
//Menu //Menu
private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options
private JMenuItem mntmErrors, mntmSetup, mntmManualUpdate; //Help private JMenuItem mntmErrors, mntmSetup, mntmManualUpdate; //Help
@ -55,7 +52,6 @@ public class GUI implements ActionListener {
private final ArrayList<String> globalPlayers; private final ArrayList<String> globalPlayers;
private JTabbedPane serversPane; private JTabbedPane serversPane;
/** /**
@ -64,7 +60,6 @@ public class GUI implements ActionListener {
public GUI() { public GUI() {
initialize(); initialize();
loadMessages(); loadMessages();
gui = this;
this.globalPlayers = new ArrayList<>(); this.globalPlayers = new ArrayList<>();
} }
@ -82,6 +77,9 @@ public class GUI implements ActionListener {
this.updatePlayers(); this.updatePlayers();
} }
/**
* Updates the list of players currently online on the selected server,
*/
private void updatePlayers() { private void updatePlayers() {
String selectedServerValue; String selectedServerValue;
Object selectedServer = targetServer.getSelectedItem(); Object selectedServer = targetServer.getSelectedItem();
@ -91,7 +89,7 @@ public class GUI implements ActionListener {
if (selectedServerValue.equals("All")) { if (selectedServerValue.equals("All")) {
for (String player : this.globalPlayers) targetPlayer.addItem(player); for (String player : this.globalPlayers) targetPlayer.addItem(player);
} else { } else {
for (String player : this.currentProfile().getCollection(selectedServerValue).getServer().getPlayers()) for (String player : Profile.getCurrent().getCollection(selectedServerValue).getServer().getPlayers())
targetPlayer.addItem(player); targetPlayer.addItem(player);
} }
} }
@ -99,7 +97,7 @@ public class GUI implements ActionListener {
public void update() { public void update() {
serversPane.removeAll(); serversPane.removeAll();
for (Collection collection : currentProfile().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
serversPane.addTab(collection.getName(), collection.getServerTab().getPanel()); serversPane.addTab(collection.getName(), collection.getServerTab().getPanel());
} }
} }
@ -120,18 +118,14 @@ public class GUI implements ActionListener {
this.profiles.removeItemAt(index); this.profiles.removeItemAt(index);
} }
public Profile currentProfile() { /*public Profile currentProfile() {
Object selected = profiles.getSelectedItem(); Object selected = profiles.getSelectedItem();
if (selected != null) { if (selected != null) {
return Profile.getProfile(selected.toString()); return Profile.getProfile(selected.toString());
} else { } else {
return null; return null;
} }
} }*/
public static GUI getGUI() {
return gui;
}
private void initialize() { private void initialize() {
try { try {
@ -409,7 +403,7 @@ public class GUI implements ActionListener {
public void updateSelectServers() { public void updateSelectServers() {
this.targetServer.removeAllItems(); this.targetServer.removeAllItems();
this.targetServer.addItem("All"); this.targetServer.addItem("All");
for (Collection collection : currentProfile().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
this.targetServer.addItem(collection.getName()); this.targetServer.addItem(collection.getName());
} }
} }
@ -454,9 +448,9 @@ public class GUI implements ActionListener {
stop(); stop();
} else if (e.getSource() == addServer) { } else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: "); String serverName = JOptionPane.showInputDialog("Name of server: ");
this.currentProfile().addCollection(serverName); Profile.getCurrent().addCollection(serverName);
this.update(); this.update();
ServerConsoles.update(currentProfile().getCollections()); ServerConsoles.update();
} else if (e.getSource() == backup) { } else if (e.getSource() == backup) {
backup(); backup();
} else if (e.getSource() == addProfile) { } else if (e.getSource() == addProfile) {
@ -466,45 +460,57 @@ public class GUI implements ActionListener {
if (selected != null) { if (selected != null) {
Profile.deleteProfile(selected.toString()); Profile.deleteProfile(selected.toString());
} }
} else if (e.getSource() == profiles) {
changeProfile();
} else if (e.getSource() == btnKick) { } else if (e.getSource() == btnKick) {
if (selectedServerValue != null && selectedPlayerValue != null) { if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "kick " + selectedPlayerValue); Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
} }
} else if (e.getSource() == btnBan) { } else if (e.getSource() == btnBan) {
if (selectedServerValue != null && selectedPlayerValue != null) { if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "ban " + selectedPlayerValue); Profile.getCurrent().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
} }
} else if (e.getSource() == btnOp) { } else if (e.getSource() == btnOp) {
if (selectedServerValue != null && selectedPlayerValue != null) { if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "op " + selectedPlayerValue); Profile.getCurrent().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
} }
} else if (e.getSource() == btnDeop) { } else if (e.getSource() == btnDeop) {
if (selectedServerValue != null && selectedPlayerValue != null) { if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "deop " + selectedPlayerValue); Profile.getCurrent().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
} }
} else if (e.getSource() == btnCustomCommand) { } else if (e.getSource() == btnCustomCommand) {
if (selectedServerValue != null) { if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, customCommand.getText()); Profile.getCurrent().sendCommand(selectedServerValue, customCommand.getText());
customCommand.setText(""); customCommand.setText("");
} }
} else if (e.getSource() == btnSaveserver) { } else if (e.getSource() == btnSaveserver) {
if (selectedServerValue != null) { if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, "save-all"); Profile.getCurrent().sendCommand(selectedServerValue, "save-all");
} }
} else if (e.getSource() == btnReload) { } else if (e.getSource() == btnReload) {
if (selectedServerValue != null) { if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, "reload"); Profile.getCurrent().sendCommand(selectedServerValue, "reload");
} }
} else if (e.getSource() == btnServerConsoles) { } else if (e.getSource() == btnServerConsoles) {
ServerConsoles.show(); ServerConsoles.show();
} }
} }
private void changeProfile() {
save();
Object current = profiles.getSelectedItem();
if (current != null) {
Profile.setCurrent(current.toString());
}
update();
ServerConsoles.update();
}
/** /**
* Reads all combo boxes, updates variables (and saves to disk). * Reads all combo boxes, updates variables (and saves to disk).
*/ */
private void save() { private void save() {
currentProfile().save(); Profile.getCurrent().save();
//TODO: Save to a text file. //TODO: Save to a text file.
} }
@ -581,7 +587,7 @@ public class GUI implements ActionListener {
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File path = chooser.getSelectedFile(); File path = chooser.getSelectedFile();
for (Collection collection : currentProfile().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().getPath().equals("") && collection.getServer().isEnabled()) { if (!collection.getServer().getPath().equals("") && collection.getServer().isEnabled()) {
String name = collection.getServer().getName(); String name = collection.getServer().getName();
File srcFolder = new File(collection.getServer().getPath()); File srcFolder = new File(collection.getServer().getPath());
@ -608,7 +614,7 @@ public class GUI implements ActionListener {
* *
* @param url URL to open * @param url URL to open
*/ */
private static void goToURL(String url) { private void goToURL(String url) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try { try {
desktop.browse(new URI(url)); desktop.browse(new URI(url));

View File

@ -1,10 +1,11 @@
package net.knarcraft.serverlauncher.userinterface; package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.util.ArrayList;
/** /**
* A parent window for server consoles. * A parent window for server consoles.
@ -16,7 +17,6 @@ import java.util.ArrayList;
* @since 0.0.0.1 * @since 0.0.0.1
*/ */
public class ServerConsoles { public class ServerConsoles {
private static ServerConsoles serverConsoles;
private static JFrame frame; private static JFrame frame;
private static JTabbedPane consolesTab; private static JTabbedPane consolesTab;
@ -27,20 +27,15 @@ public class ServerConsoles {
consolesTab = new JTabbedPane(JTabbedPane.TOP); consolesTab = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(consolesTab, BorderLayout.CENTER); frame.getContentPane().add(consolesTab, BorderLayout.CENTER);
serverConsoles = this;
}
public static ServerConsoles getGUI() {
return serverConsoles;
} }
public static void show() { public static void show() {
frame.setVisible(true); frame.setVisible(true);
} }
public static void update(ArrayList<Collection> collections) { public static void update() {
consolesTab.removeAll(); consolesTab.removeAll();
for (Collection collection : collections) { for (Collection collection : Profile.getCurrent().getCollections()) {
consolesTab.add(collection.getName(), collection.getServerConsole().getPanel()); consolesTab.add(collection.getName(), collection.getServerConsole().getPanel());
} }
} }

View File

@ -1,5 +1,7 @@
package net.knarcraft.serverlauncher.userinterface; package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.Main;
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
@ -21,10 +23,12 @@ public class ServerTab implements ActionListener {
private final JButton btnRemoveServer, btnBrowse; private final JButton btnRemoveServer, btnBrowse;
private final JTextField directory; private final JTextField directory;
private final JPanel panel; private final JPanel panel;
private final String name;
public ServerTab(String name) { public ServerTab(String name) {
this.name = name;
panel = new JPanel(); panel = new JPanel();
GUI.getGUI().getPane().addTab(name, null, panel, null); Main.gui().getPane().addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout(); SpringLayout sl_panel_3 = new SpringLayout();
panel.setLayout(sl_panel_3); panel.setLayout(sl_panel_3);
@ -137,7 +141,7 @@ public class ServerTab implements ActionListener {
if (selected != null) { if (selected != null) {
return selected.toString(); return selected.toString();
} else { } else {
return "Vanilla"; return "Latest";
} }
} }
@ -148,19 +152,32 @@ public class ServerTab implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRemoveServer) { if (e.getSource() == btnRemoveServer) {
GUI.getGUI().currentProfile().removeCollection(this); remove();
GUI.getGUI().update();
ServerConsoles.update(GUI.getGUI().currentProfile().getCollections());
} else if (e.getSource() == btnBrowse) { } else if (e.getSource() == btnBrowse) {
browse();
} else if (e.getSource() == serverTypes) {
serverTypes();
}
}
private void remove() {
Profile.getCurrent().removeCollection(name);
Main.gui().update();
ServerConsoles.update();
}
private void browse() {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("/")); chooser.setCurrentDirectory(new java.io.File("/"));
chooser.setDialogTitle("Backup folder"); chooser.setDialogTitle("Server folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false); chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
directory.setText(chooser.getSelectedFile().toString()); directory.setText(chooser.getSelectedFile().toString());
} }
} else if (e.getSource() == serverTypes) { }
private void serverTypes() {
serverVersions.removeAllItems(); serverVersions.removeAllItems();
String selectedserverTypes = null; String selectedserverTypes = null;
Object selectedType = serverTypes.getSelectedItem(); Object selectedType = serverTypes.getSelectedItem();
@ -186,5 +203,4 @@ public class ServerTab implements ActionListener {
} }
} }
} }
}
} }