From 5adf0584d28bc281fabc0961520aeffeb095bb80 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 18 Jul 2020 16:17:16 -0700 Subject: [PATCH] Warn user when creating/editing server if a server with that name already exists Closes #8 --- .../org/cyberpwn/react/AddConnection.java | 27 ++++++++++++++---- .../org/cyberpwn/react/EditConnection.java | 28 ++++++++++++++++--- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/cyberpwn/react/AddConnection.java b/src/main/java/org/cyberpwn/react/AddConnection.java index fc2bc12..49bbca1 100644 --- a/src/main/java/org/cyberpwn/react/AddConnection.java +++ b/src/main/java/org/cyberpwn/react/AddConnection.java @@ -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; @@ -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); @@ -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)); @@ -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(); } } }); @@ -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(); } @@ -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 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(); + } } } diff --git a/src/main/java/org/cyberpwn/react/EditConnection.java b/src/main/java/org/cyberpwn/react/EditConnection.java index 5ef2db4..6f974ab 100644 --- a/src/main/java/org/cyberpwn/react/EditConnection.java +++ b/src/main/java/org/cyberpwn/react/EditConnection.java @@ -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; @@ -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)); @@ -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); } } }); @@ -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 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); + } } }