Skip to content

Commit 5adf058

Browse files
author
Andrew
committed
Warn user when creating/editing server if a server with that name already exists
Closes #8
1 parent 8f3a0c1 commit 5adf058

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/main/java/org/cyberpwn/react/AddConnection.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import java.awt.*;
99
import java.awt.event.MouseAdapter;
1010
import java.awt.event.MouseEvent;
11+
import java.util.ArrayList;
1112

1213
public class AddConnection extends JDialog {
1314
private static final long serialVersionUID = 801014377635942783L;
14-
private final JPanel contentPanel = new JPanel();
15+
private final JLabel lblExists;
1516
private final JTextField txtLocalhost;
1617
private final JTextField textField_1;
1718
private final JTextField txtCyberpwn;
@@ -23,6 +24,7 @@ public AddConnection() {
2324
setTitle("Add a Connection");
2425
setBounds(100, 100, 450, 397);
2526
getContentPane().setLayout(new BorderLayout());
27+
JPanel contentPanel = new JPanel();
2628
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
2729

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

8991
contentPanel.add(txtReactisawesome, "cell 0 7,growx");
9092
}
93+
{
94+
lblExists = new JLabel("Server name already exists");
95+
lblExists.setFont(new Font("Segoe UI Light", Font.PLAIN, 18));
96+
lblExists.setForeground(Color.RED);
97+
lblExists.setVisible(false);
98+
contentPanel.add(lblExists, "cell 0 8");
99+
}
91100
{
92101
JPanel buttonPane = new JPanel();
93102
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -98,10 +107,8 @@ public AddConnection() {
98107
okButton.addMouseListener(new MouseAdapter() {
99108
@Override
100109
public void mouseReleased(MouseEvent e) {
101-
if(SwingUtilities.isLeftMouseButton(e)) {
110+
if (SwingUtilities.isLeftMouseButton(e)) {
102111
addServer(txtFancyServer.getText(), txtLocalhost.getText(), Integer.parseInt(textField_1.getText()), txtCyberpwn.getText(), txtReactisawesome.getText());
103-
setVisible(false);
104-
dispose();
105112
}
106113
}
107114
});
@@ -115,7 +122,7 @@ public void mouseReleased(MouseEvent e) {
115122
cancelButton.addMouseListener(new MouseAdapter() {
116123
@Override
117124
public void mouseReleased(MouseEvent e) {
118-
if(SwingUtilities.isLeftMouseButton(e)) {
125+
if (SwingUtilities.isLeftMouseButton(e)) {
119126
setVisible(false);
120127
dispose();
121128
}
@@ -138,6 +145,14 @@ public static void addConnection() {
138145
}
139146

140147
public void addServer(String name, String address, int port, String username, String password) {
141-
ReactClient.getInstance().validateConnectionAdd(name, address, port, username, password);
148+
ArrayList<String> networkedServerNames = new ArrayList<>();
149+
ReactClient.getInstance().getNetwork().getServers().forEach((networkedServer -> networkedServerNames.add(networkedServer.getName())));
150+
if (networkedServerNames.contains(name)) {
151+
lblExists.setVisible(true);
152+
} else {
153+
ReactClient.getInstance().validateConnectionAdd(name, address, port, username, password);
154+
setVisible(false);
155+
dispose();
156+
}
142157
}
143158
}

src/main/java/org/cyberpwn/react/EditConnection.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import java.awt.*;
1010
import java.awt.event.MouseAdapter;
1111
import java.awt.event.MouseEvent;
12+
import java.util.ArrayList;
1213

1314
public class EditConnection extends JDialog {
1415
private static final long serialVersionUID = 801014377635942783L;
16+
private final JLabel lblExists;
1517
private final JTextField txtLocalhost;
1618
private final JTextField textField_1;
1719
private final JTextField txtCyberpwn;
@@ -89,6 +91,13 @@ public EditConnection(final NetworkedServer ns) {
8991

9092
contentPanel.add(txtReactisawesome, "cell 0 7,growx");
9193
}
94+
{
95+
lblExists = new JLabel("Server name already exists");
96+
lblExists.setFont(new Font("Segoe UI Light", Font.PLAIN, 18));
97+
lblExists.setForeground(Color.RED);
98+
lblExists.setVisible(false);
99+
contentPanel.add(lblExists, "cell 0 8");
100+
}
92101
{
93102
JPanel buttonPane = new JPanel();
94103
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -102,9 +111,6 @@ public void mouseReleased(MouseEvent e) {
102111
if(SwingUtilities.isLeftMouseButton(e)) {
103112
L.l("Connection edited");
104113
editServer(txtFancyServer.getText(), txtLocalhost.getText(), Integer.parseInt(textField_1.getText()), txtCyberpwn.getText(), txtReactisawesome.getText(), ns);
105-
setVisible(false);
106-
dispose();
107-
ReactClient.getInstance().releaseConnection(ns);
108114
}
109115
}
110116
});
@@ -142,6 +148,20 @@ public static void editConnection(NetworkedServer ns) {
142148
}
143149

144150
public void editServer(String name, String address, int port, String username, String password, NetworkedServer ns) {
145-
ReactClient.getInstance().validateConnectionEdit(name, address, port, username, password, ns);
151+
ArrayList<String> networkedServerNames = new ArrayList<>();
152+
ReactClient.getInstance().getNetwork().getServers().forEach((networkedServer -> {
153+
networkedServerNames.add(networkedServer.getName());
154+
L.l("Added " + networkedServer.getName());
155+
}));
156+
networkedServerNames.remove(ns.getName());
157+
L.l("Removed " + name);
158+
if (networkedServerNames.contains(name)) {
159+
lblExists.setVisible(true);
160+
} else {
161+
ReactClient.getInstance().validateConnectionEdit(name, address, port, username, password, ns);
162+
setVisible(false);
163+
dispose();
164+
ReactClient.getInstance().releaseConnection(ns);
165+
}
146166
}
147167
}

0 commit comments

Comments
 (0)