Skip to content

Commit

Permalink
Warn user when creating/editing server if a server with that name alr…
Browse files Browse the repository at this point in the history
…eady exists

Closes #8
  • Loading branch information
Andrew committed Jul 18, 2020
1 parent 8f3a0c1 commit 5adf058
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
27 changes: 21 additions & 6 deletions src/main/java/org/cyberpwn/react/AddConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class AddConnection extends JDialog {
private static final long serialVersionUID = 801014377635942783L;
private final JPanel contentPanel = new JPanel();
private final JLabel lblExists;
private final JTextField txtLocalhost;
private final JTextField textField_1;
private final JTextField txtCyberpwn;
Expand All @@ -23,6 +24,7 @@ public AddConnection() {
setTitle("Add a Connection");
setBounds(100, 100, 450, 397);
getContentPane().setLayout(new BorderLayout());
JPanel contentPanel = new JPanel();
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

getContentPane().add(contentPanel, BorderLayout.CENTER);
Expand Down Expand Up @@ -88,6 +90,13 @@ public AddConnection() {

contentPanel.add(txtReactisawesome, "cell 0 7,growx");
}
{
lblExists = new JLabel("Server name already exists");
lblExists.setFont(new Font("Segoe UI Light", Font.PLAIN, 18));
lblExists.setForeground(Color.RED);
lblExists.setVisible(false);
contentPanel.add(lblExists, "cell 0 8");
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
Expand All @@ -98,10 +107,8 @@ public AddConnection() {
okButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) {
if (SwingUtilities.isLeftMouseButton(e)) {
addServer(txtFancyServer.getText(), txtLocalhost.getText(), Integer.parseInt(textField_1.getText()), txtCyberpwn.getText(), txtReactisawesome.getText());
setVisible(false);
dispose();
}
}
});
Expand All @@ -115,7 +122,7 @@ public void mouseReleased(MouseEvent e) {
cancelButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) {
if (SwingUtilities.isLeftMouseButton(e)) {
setVisible(false);
dispose();
}
Expand All @@ -138,6 +145,14 @@ public static void addConnection() {
}

public void addServer(String name, String address, int port, String username, String password) {
ReactClient.getInstance().validateConnectionAdd(name, address, port, username, password);
ArrayList<String> networkedServerNames = new ArrayList<>();
ReactClient.getInstance().getNetwork().getServers().forEach((networkedServer -> networkedServerNames.add(networkedServer.getName())));
if (networkedServerNames.contains(name)) {
lblExists.setVisible(true);
} else {
ReactClient.getInstance().validateConnectionAdd(name, address, port, username, password);
setVisible(false);
dispose();
}
}
}
28 changes: 24 additions & 4 deletions src/main/java/org/cyberpwn/react/EditConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class EditConnection extends JDialog {
private static final long serialVersionUID = 801014377635942783L;
private final JLabel lblExists;
private final JTextField txtLocalhost;
private final JTextField textField_1;
private final JTextField txtCyberpwn;
Expand Down Expand Up @@ -89,6 +91,13 @@ public EditConnection(final NetworkedServer ns) {

contentPanel.add(txtReactisawesome, "cell 0 7,growx");
}
{
lblExists = new JLabel("Server name already exists");
lblExists.setFont(new Font("Segoe UI Light", Font.PLAIN, 18));
lblExists.setForeground(Color.RED);
lblExists.setVisible(false);
contentPanel.add(lblExists, "cell 0 8");
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
Expand All @@ -102,9 +111,6 @@ public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) {
L.l("Connection edited");
editServer(txtFancyServer.getText(), txtLocalhost.getText(), Integer.parseInt(textField_1.getText()), txtCyberpwn.getText(), txtReactisawesome.getText(), ns);
setVisible(false);
dispose();
ReactClient.getInstance().releaseConnection(ns);
}
}
});
Expand Down Expand Up @@ -142,6 +148,20 @@ public static void editConnection(NetworkedServer ns) {
}

public void editServer(String name, String address, int port, String username, String password, NetworkedServer ns) {
ReactClient.getInstance().validateConnectionEdit(name, address, port, username, password, ns);
ArrayList<String> networkedServerNames = new ArrayList<>();
ReactClient.getInstance().getNetwork().getServers().forEach((networkedServer -> {
networkedServerNames.add(networkedServer.getName());
L.l("Added " + networkedServer.getName());
}));
networkedServerNames.remove(ns.getName());
L.l("Removed " + name);
if (networkedServerNames.contains(name)) {
lblExists.setVisible(true);
} else {
ReactClient.getInstance().validateConnectionEdit(name, address, port, username, password, ns);
setVisible(false);
dispose();
ReactClient.getInstance().releaseConnection(ns);
}
}
}

0 comments on commit 5adf058

Please sign in to comment.