Playerlist and Serverlist combos are properly updated. It is possible to send commands to servers.

This commit is contained in:
2018-02-01 13:16:25 +01:00
parent 2350247975
commit 3a1e61c754
4 changed files with 144 additions and 12 deletions

View File

@ -39,18 +39,20 @@ public class Profile {
}
public void addCollection(String name) {
if (!collectionExists(name)) {
if (!collectionExists(name) && !name.equals("") && !name.equals("All")) {
collections.add(new Collection(name));
GUI.getGUI().updateSelectServers();
} else {
JOptionPane.showMessageDialog(null, "A server name must my unique and not empty.", "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(int i) {
this.collections.remove(i);
GUI.getGUI().updateSelectServers();
}
private Collection getCollection(String name) {
public Collection getCollection(String name) {
for (Collection collection : this.collections) {
if (collection.getName().equals(name)) {
return collection;

View File

@ -50,15 +50,29 @@ public class Server {
public void addPlayer(String name) {
this.playerList.add(name);
GUI.getGUI().addPlayer(name);
}
public void removePlayer(String name) {
for (int i = 0; i < playerList.size(); i++) {
if (name.equals(playerList.get(i))) {
playerList.remove(i);
return;
}
}
GUI.getGUI().removePlayer(name);
}
public ArrayList<String> getPlayers() {
return this.playerList;
}
public boolean hasPlayer(String name) {
for (String player : this.playerList) {
if (player.equals(name)) {
return true;
}
}
return false;
}
/**
@ -122,6 +136,10 @@ public class Server {
}
}
public String typeName() {
return this.type.getName();
}
/**
* Sets the server's server version to a valid version, or ignores the request.
*

View File

@ -42,7 +42,8 @@ public class GUI implements ActionListener {
private JComboBox<String> profiles;
private final JLabel lblStatuslabel = new JLabel("Servers are stopped");
//Server controls
private JComboBox targetServer, targetPlayer;
private JComboBox<String> targetServer;
private JComboBox<String> targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;
//Text
@ -52,13 +53,13 @@ public class GUI implements ActionListener {
private String downloadJarsText;
private String aboutText;
private final ArrayList<String> globalPlayers;
private JTabbedPane serversPane;
private final ArrayList<ServerTab> serverTabs = new ArrayList<>();
//TODO: Update target server list with the list of servers when adding or removing a server.
/**
* Create the application window.
*/
@ -66,6 +67,36 @@ public class GUI implements ActionListener {
initialize();
loadMessages();
gui = this;
this.globalPlayers = new ArrayList<>();
}
public void addPlayer(String name) {
this.globalPlayers.add(name);
this.updatePlayers();
}
public void removePlayer(String name) {
for (int i = 0; i < this.globalPlayers.size(); i++) {
if (this.globalPlayers.get(i).equals(name)) {
this.globalPlayers.remove(i);
}
}
this.updatePlayers();
}
private void updatePlayers() {
String selectedServerValue;
Object selectedServer = targetServer.getSelectedItem();
if (selectedServer != null) {
targetPlayer.removeAllItems();
selectedServerValue = selectedServer.toString();
if (selectedServerValue.equals("All")) {
for (String player : this.globalPlayers) targetPlayer.addItem(player);
} else {
for (String player : this.currentProfile().getCollection(selectedServerValue).getServer().getPlayers())
targetPlayer.addItem(player);
}
}
}
public void setStatus(String text) {
@ -112,7 +143,6 @@ public class GUI implements ActionListener {
i = this.serverTabs.size();
}
}
}
private void initialize() {
@ -274,11 +304,11 @@ public class GUI implements ActionListener {
SpringLayout sl_panel_1 = new SpringLayout();
controlServers.setLayout(sl_panel_1);
targetServer = new JComboBox();
targetServer = new JComboBox<>();
sl_panel_1.putConstraint(SpringLayout.NORTH, targetServer, 10, SpringLayout.NORTH, controlServers);
controlServers.add(targetServer);
targetPlayer = new JComboBox();
targetPlayer = new JComboBox<>();
sl_panel_1.putConstraint(SpringLayout.NORTH, targetPlayer, 6, SpringLayout.SOUTH, targetServer);
targetPlayer.setEditable(true);
controlServers.add(targetPlayer);
@ -334,6 +364,7 @@ public class GUI implements ActionListener {
sl_panel_1.putConstraint(SpringLayout.EAST, btnCustomCommand, 0, SpringLayout.EAST, btnOp);
controlServers.add(btnCustomCommand);
btnCustomCommand.addActionListener(this);
frame.getRootPane().setDefaultButton(btnCustomCommand);
customCommand = new JTextField();
sl_panel_1.putConstraint(SpringLayout.WEST, customCommand, 10, SpringLayout.WEST, controlServers);
@ -391,6 +422,14 @@ public class GUI implements ActionListener {
serverTabs.add(serverTab);
}
public void updateSelectServers() {
this.targetServer.removeAllItems();
this.targetServer.addItem("All");
for (Collection collection : currentProfile().getCollections()) {
this.targetServer.addItem(collection.getName());
}
}
@Override
public void actionPerformed(ActionEvent e) {
String selectedServerValue = null, selectedPlayerValue = null;
@ -459,7 +498,8 @@ public class GUI implements ActionListener {
}
} else if (e.getSource() == btnCustomCommand) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, customCommand.getSelectedText());
currentProfile().sendCommand(selectedServerValue, customCommand.getText());
customCommand.setText("");
}
} else if (e.getSource() == btnSaveserver) {
if (selectedServerValue != null) {