From d37ab53275401385eecfea1b3a7e4ebd5a349360 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 9 Feb 2020 20:43:12 +0100 Subject: [PATCH] Added handling of incorrect logins --- .../event/HandshakeSuccessfulEvent.java | 18 ++++ src/main/java/envoy/client/net/Client.java | 1 + .../java/envoy/client/ui/LoginDialog.java | 97 +++++++++++-------- src/main/java/envoy/client/ui/Startup.java | 2 +- 4 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java diff --git a/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java b/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java new file mode 100644 index 0000000..3322ce4 --- /dev/null +++ b/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java @@ -0,0 +1,18 @@ +package envoy.client.event; + +import envoy.event.Event; + +/** + * This {@link Event} indicates that a handshake completed successful + * + * Project: envoy-client
+ * File: HandshakeSuccessfulEvent.java
+ * Created: 8 Feb 2020
+ * + * @author Leon Hofmeister + * @since Envoy v0.3-alpha + */ +public class HandshakeSuccessfulEvent implements Event { + + private static final long serialVersionUID = -157972384126278855L; +} diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 0d6a9d3..f1432f6 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -74,6 +74,7 @@ public class Client implements Closeable { receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); receiver.registerProcessor(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; }); receiver.registerProcessor(Message.class, receivedMessageCache); + receiver.registerProcessor(HandshakeRejectionEvent.class, EventBus.getInstance()::dispatch); // Start receiver new Thread(receiver).start(); diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index d81ecc1..e58b0b1 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -2,20 +2,22 @@ package envoy.client.ui; import java.awt.*; import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.swing.*; import javax.swing.border.EmptyBorder; +import envoy.client.event.HandshakeSuccessfulEvent; import envoy.data.LoginCredentials; +import envoy.event.EventBus; +import envoy.event.HandshakeRejectionEvent; /** * Project: envoy-client
* File: LoginDialog.java
* Created: 01.01.2020
- * + * * @author Kai S. K. Engelbart * @author Maximilian Käfer * @since Envoy v0.3-alpha @@ -28,8 +30,11 @@ public class LoginDialog extends JDialog { private JTextField textField; private JPasswordField passwordField; - private JPasswordField repeatPasswordField; - private JLabel lblRepeatPassword; + private JPasswordField repeatPasswordField; + private JLabel lblRepeatPassword; + private JLabel errorMessage; + + private GridBagConstraints gbc_errorMessage; private GridBagConstraints gbc_lblRepeatPassword; private GridBagConstraints gbc_repeatPasswordField; @@ -37,7 +42,7 @@ public class LoginDialog extends JDialog { /** * Displays a dialog enabling the user to enter their user name and password. - * + * * @since Envoy v0.3-alpha */ public LoginDialog() { @@ -97,12 +102,27 @@ public class LoginDialog extends JDialog { gbc_lblRepeatPassword.gridy = 2; } { - repeatPasswordField = new JPasswordField(); + repeatPasswordField = new JPasswordField(); gbc_repeatPasswordField = new GridBagConstraints(); gbc_repeatPasswordField.fill = GridBagConstraints.HORIZONTAL; gbc_repeatPasswordField.gridx = 1; gbc_repeatPasswordField.gridy = 2; } + { + EventBus.getInstance().register(HandshakeRejectionEvent.class, evt -> { + contentPanel.remove(errorMessage); + clearPasswordFields(); + // TODO delete - only for testing purposes + System.out.println("Caught HandshakeRejectionEvent with reason" + ((HandshakeRejectionEvent) evt).get()); + errorMessage = new JLabel(((HandshakeRejectionEvent) evt).get()); + gbc_errorMessage = new GridBagConstraints(); + gbc_errorMessage.gridx = 2; + gbc_errorMessage.gridy = 0; + gbc_errorMessage.fill = GridBagConstraints.HORIZONTAL; + gbc_errorMessage.insets = new Insets(5, 5, 5, 5); + contentPanel.add(errorMessage, gbc_errorMessage); + }); + } { JPanel buttonPane = new JPanel(); @@ -114,30 +134,24 @@ public class LoginDialog extends JDialog { JCheckBox registerCheckBox = new JCheckBox(); registerCheckBox.setAlignmentX(LEFT_ALIGNMENT); - registerCheckBox.addItemListener(new ItemListener() { + registerCheckBox.addItemListener(e -> { + switch (e.getStateChange()) { + case ItemEvent.SELECTED: + contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); + contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); + setSize(338, 160); + break; - @Override - public void itemStateChanged(ItemEvent e) { - switch (e.getStateChange()) { - case ItemEvent.SELECTED: - contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); - contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); - setSize(338, 160); - contentPanel.revalidate(); - contentPanel.repaint(); - break; - - case ItemEvent.DESELECTED: - if (repeatPasswordField.getParent() == contentPanel) { - contentPanel.remove(lblRepeatPassword); - contentPanel.remove(repeatPasswordField); - setSize(338, 123); - contentPanel.revalidate(); - contentPanel.repaint(); - } - break; - } + case ItemEvent.DESELECTED: + if (repeatPasswordField.getParent() == contentPanel) { + contentPanel.remove(lblRepeatPassword); + contentPanel.remove(repeatPasswordField); + setSize(338, 123); + } + break; } + contentPanel.revalidate(); + contentPanel.repaint(); }); buttonPane.add(registerCheckBox); @@ -148,18 +162,14 @@ public class LoginDialog extends JDialog { okButton.addActionListener((evt) -> { try { if (registerCheckBox.isSelected()) { - if (Arrays.equals(passwordField.getPassword(), repeatPasswordField.getPassword())) { + // password checking + if (Arrays.equals(passwordField.getPassword(), repeatPasswordField.getPassword())) credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), true); - dispose(); - } else { - JOptionPane.showMessageDialog(this, "The repeated password is unequal to the origional password!"); - passwordField.setText(null); - repeatPasswordField.setText(null); + else { + JOptionPane.showMessageDialog(this, "The repeated password is not the origional password!"); + clearPasswordFields(); } - } else { - credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false); - dispose(); - } + } else credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } @@ -178,6 +188,17 @@ public class LoginDialog extends JDialog { setModal(true); setVisible(true); + EventBus.getInstance().register(HandshakeSuccessfulEvent.class, evt -> this.dispose()); + } + + /** + * Resets the text stored by this + * + * @since Envoy v0.3-alpha + */ + public void clearPasswordFields() { + passwordField.setText(null); + repeatPasswordField.setText(null); } /** diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a976824..7cd09a7 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -117,7 +117,7 @@ public class Startup { try { // Try entering offline mode localDb.loadUsers(); - User clientUser = localDb.getUsers().get(credentials.getName()); + User clientUser = localDb.getUsers().get(credentials.getIdentifier()); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); client.setSender(clientUser); JOptionPane.showMessageDialog(null,