Profile switching and cleanup

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

View File

@ -29,12 +29,10 @@ public class Console implements ActionListener {
panel = new JPanel();
tab.addTab(name, null, panel, null);
panel.setLayout(new BorderLayout(0, 0));
textInput = new JTextField();
panel.add(textInput, BorderLayout.SOUTH);
textInput.setColumns(10);
textInput.addActionListener(this);
textOutput = new JTextArea();
JScrollPane scroll = new JScrollPane(textOutput);
panel.add(scroll, BorderLayout.CENTER);
@ -54,10 +52,9 @@ public class Console implements ActionListener {
@Override
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();
Profile profile = GUI.getGUI().currentProfile();
profile.sendCommand(this.name, text);
Profile.getCurrent().sendCommand(this.name, text);
textInput.setText("");
}
}

View File

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

View File

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

View File

@ -1,5 +1,7 @@
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.ServerType;
@ -21,10 +23,12 @@ public class ServerTab implements ActionListener {
private final JButton btnRemoveServer, btnBrowse;
private final JTextField directory;
private final JPanel panel;
private final String name;
public ServerTab(String name) {
this.name = name;
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();
panel.setLayout(sl_panel_3);
@ -137,7 +141,7 @@ public class ServerTab implements ActionListener {
if (selected != null) {
return selected.toString();
} else {
return "Vanilla";
return "Latest";
}
}
@ -148,40 +152,52 @@ public class ServerTab implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRemoveServer) {
GUI.getGUI().currentProfile().removeCollection(this);
GUI.getGUI().update();
ServerConsoles.update(GUI.getGUI().currentProfile().getCollections());
remove();
} 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());
}
browse();
} else if (e.getSource() == serverTypes) {
serverVersions.removeAllItems();
String selectedserverTypes = null;
Object selectedType = serverTypes.getSelectedItem();
if (selectedType != null) {
selectedserverTypes = selectedType.toString();
}
if (selectedserverTypes != null) {
if (selectedserverTypes.equals("Custom")) {
serverVersions.setEditable(true);
} else {
serverVersions.setEditable(false);
ServerType current = null;
for (ServerType servertype : ServerType.getServerTypes()) {
if (servertype.getName().equals(selectedserverTypes)) {
current = servertype;
}
serverTypes();
}
}
private void remove() {
Profile.getCurrent().removeCollection(name);
Main.gui().update();
ServerConsoles.update();
}
private void browse() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("/"));
chooser.setDialogTitle("Server folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
directory.setText(chooser.getSelectedFile().toString());
}
}
private void serverTypes() {
serverVersions.removeAllItems();
String selectedserverTypes = null;
Object selectedType = serverTypes.getSelectedItem();
if (selectedType != null) {
selectedserverTypes = selectedType.toString();
}
if (selectedserverTypes != null) {
if (selectedserverTypes.equals("Custom")) {
serverVersions.setEditable(true);
} else {
serverVersions.setEditable(false);
ServerType current = null;
for (ServerType servertype : ServerType.getServerTypes()) {
if (servertype.getName().equals(selectedserverTypes)) {
current = servertype;
}
if (current != null) {
for (String version : current.getVersions()) {
serverVersions.addItem(version);
}
}
if (current != null) {
for (String version : current.getVersions()) {
serverVersions.addItem(version);
}
}
}