From 6582e88a0206619d38b94283a452dd46cf833a9d Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 9 Feb 2020 20:43:12 +0100 Subject: [PATCH 1/7] 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, From 0257db8449f1e35767f0839c4fbb679d0d9673c4 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 9 Feb 2020 20:48:08 +0100 Subject: [PATCH 2/7] fixed Javadoc error. Current implementation is NOT working! --- src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java b/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java index 3322ce4..7c20760 100644 --- a/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java +++ b/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java @@ -3,7 +3,7 @@ package envoy.client.event; import envoy.event.Event; /** - * This {@link Event} indicates that a handshake completed successful + * This {@link Event} indicates that a handshake was completed successfully. * * Project: envoy-client
* File: HandshakeSuccessfulEvent.java
From 511146c98eeb037c858cebc544777206d54becb6 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 07:12:22 +0100 Subject: [PATCH 3/7] Clean-up, moved Settings and SettingsItem into the data package --- .../envoy/client/{ => data}/Settings.java | 3 +- .../envoy/client/{ => data}/SettingsItem.java | 2 +- .../event/HandshakeSuccessfulEvent.java | 2 +- src/main/java/envoy/client/ui/ChatWindow.java | 2 +- .../client/ui/ContactsSearchRenderer.java | 2 +- .../java/envoy/client/ui/LoginDialog.java | 29 ++++++++++--------- .../envoy/client/ui/MessageListRenderer.java | 2 +- .../envoy/client/ui/PrimaryScrollBar.java | 2 +- .../envoy/client/ui/PrimaryToggleSwitch.java | 4 +-- src/main/java/envoy/client/ui/Startup.java | 3 +- .../envoy/client/ui/UserListRenderer.java | 2 +- .../ui/settings/GeneralSettingsPanel.java | 4 +-- .../client/ui/settings/NewThemeScreen.java | 2 +- .../client/ui/settings/SettingsScreen.java | 2 +- .../ui/settings/ThemeCustomizationPanel.java | 2 +- 15 files changed, 31 insertions(+), 32 deletions(-) rename src/main/java/envoy/client/{ => data}/Settings.java (99%) rename src/main/java/envoy/client/{ => data}/SettingsItem.java (99%) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/data/Settings.java similarity index 99% rename from src/main/java/envoy/client/Settings.java rename to src/main/java/envoy/client/data/Settings.java index c921605..8cf0283 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/data/Settings.java @@ -1,4 +1,4 @@ -package envoy.client; +package envoy.client.data; import java.io.File; import java.io.IOException; @@ -6,7 +6,6 @@ import java.util.HashMap; import java.util.Map; import java.util.prefs.Preferences; -import envoy.client.data.Config; import envoy.client.ui.Color; import envoy.client.ui.Theme; import envoy.util.SerializationUtils; diff --git a/src/main/java/envoy/client/SettingsItem.java b/src/main/java/envoy/client/data/SettingsItem.java similarity index 99% rename from src/main/java/envoy/client/SettingsItem.java rename to src/main/java/envoy/client/data/SettingsItem.java index 4de2a47..57ba600 100644 --- a/src/main/java/envoy/client/SettingsItem.java +++ b/src/main/java/envoy/client/data/SettingsItem.java @@ -1,4 +1,4 @@ -package envoy.client; +package envoy.client.data; import java.io.Serializable; import java.util.HashMap; diff --git a/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java b/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java index 7c20760..9cafa48 100644 --- a/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java +++ b/src/main/java/envoy/client/event/HandshakeSuccessfulEvent.java @@ -12,7 +12,7 @@ import envoy.event.Event; * @author Leon Hofmeister * @since Envoy v0.3-alpha */ -public class HandshakeSuccessfulEvent implements Event { +public class HandshakeSuccessfulEvent extends Event.Valueless { private static final long serialVersionUID = -157972384126278855L; } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 5aa18c5..045bf9c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -12,9 +12,9 @@ import javax.swing.border.EmptyBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; -import envoy.client.Settings; import envoy.client.data.Chat; import envoy.client.data.LocalDb; +import envoy.client.data.Settings; import envoy.client.event.MessageCreationEvent; import envoy.client.event.ThemeChangeEvent; import envoy.client.net.Client; diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index f206c54..92f5204 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -6,7 +6,7 @@ import java.awt.Font; import javax.swing.*; -import envoy.client.Settings; +import envoy.client.data.Settings; import envoy.client.event.SendEvent; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index 6e7f9a1..d9e4750 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -8,8 +8,8 @@ import java.util.Arrays; import javax.swing.*; import javax.swing.border.EmptyBorder; +import envoy.client.data.Settings; import envoy.client.event.HandshakeSuccessfulEvent; -import envoy.client.Settings; import envoy.data.LoginCredentials; import envoy.event.EventBus; import envoy.event.HandshakeRejectionEvent; @@ -26,20 +26,20 @@ import envoy.event.HandshakeRejectionEvent; public class LoginDialog extends JDialog { private final JPanel contentPanel; - private JTextField textField; - private JPasswordField passwordField; - private JPasswordField repeatPasswordField; + private JTextField textField; + private JPasswordField passwordField; + private JPasswordField repeatPasswordField; private JLabel lblUserName; private JLabel lblPassword; private JLabel lblRepeatPassword; - private JLabel errorMessage; - + private JLabel errorMessage; + private GridBagConstraints gbc_lblRepeatPassword; private GridBagConstraints gbc_repeatPasswordField; - private GridBagConstraints gbc_errorMessage; + private GridBagConstraints gbc_errorMessage; - private JPanel buttonPane; + private JPanel buttonPane; private JTextPane registerText; private JCheckBox registerCheckBox; private PrimaryButton okButton; @@ -48,6 +48,7 @@ public class LoginDialog extends JDialog { private LoginCredentials credentials; private static final long serialVersionUID = 352021600833907468L; + /** * Displays a dialog enabling the user to enter their user name and password. * @@ -68,7 +69,7 @@ public class LoginDialog extends JDialog { contentPanel.setLayout(gbl_contentPanel); { lblUserName = new JLabel("Username:"); - GridBagConstraints gbc_lblUserName = new GridBagConstraints(); + GridBagConstraints gbc_lblUserName = new GridBagConstraints(); gbc_lblUserName.anchor = GridBagConstraints.EAST; gbc_lblUserName.insets = new Insets(0, 0, 5, 5); gbc_lblUserName.gridx = 0; @@ -88,7 +89,7 @@ public class LoginDialog extends JDialog { } { lblPassword = new JLabel("Password:"); - GridBagConstraints gbc_lblPassword = new GridBagConstraints(); + GridBagConstraints gbc_lblPassword = new GridBagConstraints(); gbc_lblPassword.anchor = GridBagConstraints.EAST; gbc_lblPassword.insets = new Insets(0, 0, 0, 5); gbc_lblPassword.gridx = 0; @@ -124,8 +125,8 @@ public class LoginDialog extends JDialog { 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()); + System.out.println("Caught HandshakeRejectionEvent with reason" + evt.get()); + errorMessage = new JLabel(evt.get()); gbc_errorMessage = new GridBagConstraints(); gbc_errorMessage.gridx = 2; gbc_errorMessage.gridy = 0; @@ -201,11 +202,11 @@ public class LoginDialog extends JDialog { setModal(true); setVisible(true); - EventBus.getInstance().register(HandshakeSuccessfulEvent.class, evt -> this.dispose()); + EventBus.getInstance().register(HandshakeSuccessfulEvent.class, evt -> dispose()); } /** - * Resets the text stored by this + * Resets the text stored in the passwort fields. * * @since Envoy v0.3-alpha */ diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 65ee63a..a069721 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -5,7 +5,7 @@ import java.text.SimpleDateFormat; import javax.swing.*; -import envoy.client.Settings; +import envoy.client.data.Settings; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.Message; diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index a7cb8fc..8e8131c 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -12,7 +12,7 @@ import javax.swing.JComponent; import javax.swing.JScrollBar; import javax.swing.plaf.basic.BasicScrollBarUI; -import envoy.client.Settings; +import envoy.client.data.Settings; /** * Project: envoy-client
diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java index 2882a05..3f58e2e 100644 --- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java +++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java @@ -5,8 +5,8 @@ import java.awt.Graphics; import javax.swing.JButton; -import envoy.client.Settings; -import envoy.client.SettingsItem; +import envoy.client.data.Settings; +import envoy.client.data.SettingsItem; /** * This component can be used to toggle between two options. This will change diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 24f6a9f..db55c53 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -11,7 +11,6 @@ import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; -import envoy.client.Settings; import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.net.WriteProxy; @@ -79,7 +78,7 @@ public class Startup { LoginCredentials credentials = config.hasLoginCredentials() ? config.getLoginCredentials() : new LoginDialog().getCredentials(); if (credentials == null) { - logger.info("The login process has been aborted by the user. Exiting..."); + logger.info("The login process has been cancelled. Exiting..."); System.exit(0); } diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index 8c63ad7..c43eeb6 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -7,7 +7,7 @@ import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; -import envoy.client.Settings; +import envoy.client.data.Settings; import envoy.data.User; import envoy.data.User.UserStatus; diff --git a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index adafc2e..a1b5966 100644 --- a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -10,8 +10,8 @@ import java.util.logging.Logger; import javax.swing.JComponent; import javax.swing.JTextPane; -import envoy.client.Settings; -import envoy.client.SettingsItem; +import envoy.client.data.Settings; +import envoy.client.data.SettingsItem; import envoy.client.ui.Theme; import envoy.client.util.EnvoyLog; diff --git a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java index 3e259e4..02dd42a 100644 --- a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java +++ b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java @@ -7,7 +7,7 @@ import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JTextPane; -import envoy.client.Settings; +import envoy.client.data.Settings; import envoy.client.ui.PrimaryButton; import envoy.client.ui.PrimaryTextArea; import envoy.client.ui.Theme; diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index af68e8e..df2fa2c 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -9,7 +9,7 @@ import java.util.logging.Logger; import javax.swing.*; -import envoy.client.Settings; +import envoy.client.data.Settings; import envoy.client.event.ThemeChangeEvent; import envoy.client.ui.PrimaryButton; import envoy.client.ui.Theme; diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 8df3491..ed4be4f 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -9,7 +9,7 @@ import java.util.logging.Logger; import javax.swing.*; -import envoy.client.Settings; +import envoy.client.data.Settings; import envoy.client.event.ThemeChangeEvent; import envoy.client.ui.Color; import envoy.client.ui.Theme; From 9498bf216d98046fe7822b124813376d4e098fb9 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 07:53:24 +0100 Subject: [PATCH 4/7] Split Client#onlineInit method up into performHandshake and initReceiver --- pom.xml | 2 +- src/main/java/envoy/client/net/Client.java | 46 ++++++++++++++++++---- src/main/java/envoy/client/ui/Startup.java | 5 ++- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index ec25577..6106227 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~advanced_login-SNAPSHOT diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 9f84364..b404807 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -42,6 +42,7 @@ public class Client implements Closeable { // Asynchronously initialized during handshake private volatile User sender; private volatile Contacts contacts; + private volatile boolean rejected; // Configuration and logging private static final Config config = Config.getInstance(); @@ -54,16 +55,19 @@ public class Client implements Closeable { * an exception is thrown. * * @param credentials the login credentials of the user - * @param localDb the local database used to persist the current - * {@link IdGenerator} * @param receivedMessageCache a message cache containing all unread messages * from the server that can be relayed after * initialization - * @throws Exception if the online mode could not be entered or the request - * failed for some other reason - * @since Envoy v0.2-alpha + * @throws TimeLimitExceededException if the server could not be reached + * @throws IOException if the login credentials could not be + * written + * @throws InterruptedException if the current thread is interrupted while + * waiting for the handshake response */ - public void onlineInit(LoginCredentials credentials, LocalDb localDb, Cache receivedMessageCache) throws Exception { + public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache) + throws TimeLimitExceededException, IOException, InterruptedException { + if (online) throw new IllegalStateException("Handshake has already been performed successfully"); + // Establish TCP connection logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); socket = new Socket(config.getServer(), config.getPort()); @@ -76,7 +80,9 @@ 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); + receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; EventBus.getInstance().dispatch(evt); }); + + rejected = false; // Start receiver new Thread(receiver).start(); @@ -88,6 +94,15 @@ public class Client implements Closeable { // Wait for a maximum of five seconds to acquire the sender object long start = System.currentTimeMillis(); while (sender == null || contacts == null) { + + // Quit immediately after handshake rejection + // This method can then be called again + if (rejected) { + socket.close(); + receiver.removeAllProcessors(); + return; + } + if (System.currentTimeMillis() - start > 5000) throw new TimeLimitExceededException("Did not log in after 5 seconds"); Thread.sleep(500); } @@ -97,8 +112,23 @@ public class Client implements Closeable { // Remove user creation processor receiver.removeAllProcessors(); + } - // Register processors for message and status handling + /** + * Initializes the {@link Receiver} used to process data sent from the server to + * this client. + * + * @param localDb the local database used to persist the current + * {@link IdGenerator} + * @param receivedMessageCache a message cache containing all unread messages + * from the server that can be relayed after + * initialization + * @throws IOException if no {@link IdGenerator} is present and none could be + * requested from the server + * @since Envoy v0.2-alpha + */ + public void initReceiver(LocalDb localDb, Cache receivedMessageCache) throws IOException { + checkOnline(); // Process incoming messages final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index db55c53..f31cfa3 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -62,7 +62,7 @@ public class Startup { if (args.length > 0) config.load(args); // Check if all mandatory configuration values have been initialized - if (!config.isInitialized()) throw new EnvoyException("Server or port are not defined"); + if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized"); } catch (Exception e) { JOptionPane .showMessageDialog(null, "Error loading configuration values:\n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE); @@ -109,7 +109,8 @@ public class Startup { try { // Try entering online mode first localDb.loadIdGenerator(); - client.onlineInit(credentials, localDb, cache); + client.performHandshake(credentials, cache); + client.initReceiver(localDb, cache); } catch (Exception e1) { logger.warning("Could not connect to server. Trying offline mode..."); e1.printStackTrace(); From b3101e2418dd92d3c6b1314e6401354285d6c41a Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 17:31:20 +0100 Subject: [PATCH 5/7] Performing handshake and online init in LoginDialog --- src/main/java/envoy/client/net/Receiver.java | 2 +- .../java/envoy/client/ui/LoginDialog.java | 337 ++++++++++-------- src/main/java/envoy/client/ui/Startup.java | 46 +-- 3 files changed, 205 insertions(+), 180 deletions(-) diff --git a/src/main/java/envoy/client/net/Receiver.java b/src/main/java/envoy/client/net/Receiver.java index 25e7153..d8a0d45 100644 --- a/src/main/java/envoy/client/net/Receiver.java +++ b/src/main/java/envoy/client/net/Receiver.java @@ -51,7 +51,7 @@ public class Receiver implements Runnable { try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) { Object obj = oin.readObject(); - logger.finest("Received object " + obj); + logger.info("Received object " + obj); // Get appropriate processor @SuppressWarnings("rawtypes") diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index d9e4750..bdac73a 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -2,17 +2,25 @@ package envoy.client.ui; import java.awt.*; import java.awt.event.ItemEvent; +import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.util.Arrays; +import java.util.logging.Logger; +import javax.naming.TimeLimitExceededException; import javax.swing.*; import javax.swing.border.EmptyBorder; -import envoy.client.data.Settings; +import envoy.client.data.*; import envoy.client.event.HandshakeSuccessfulEvent; +import envoy.client.net.Client; +import envoy.client.util.EnvoyLog; import envoy.data.LoginCredentials; +import envoy.data.Message; +import envoy.data.User; import envoy.event.EventBus; import envoy.event.HandshakeRejectionEvent; +import envoy.exception.EnvoyException; /** * Project: envoy-client
@@ -25,7 +33,7 @@ import envoy.event.HandshakeRejectionEvent; */ public class LoginDialog extends JDialog { - private final JPanel contentPanel; + private JPanel contentPanel; private JTextField textField; private JPasswordField passwordField; private JPasswordField repeatPasswordField; @@ -47,14 +55,111 @@ public class LoginDialog extends JDialog { private LoginCredentials credentials; - private static final long serialVersionUID = 352021600833907468L; + private final Client client; + private final LocalDb localDb; + private final Cache receivedMessageCache; + + private static final Config config = Config.getInstance(); + private static final Logger logger = EnvoyLog.getLogger(LoginDialog.class.getSimpleName()); + private static final long serialVersionUID = 352021600833907468L; /** * Displays a dialog enabling the user to enter their user name and password. * + * @param client the client used to perform the handshake + * @param localDb the local database in which data is persisted + * @param receivedMessageCache the cache that stored messages received during + * the handshake * @since Envoy v0.3-alpha */ - public LoginDialog() { + public LoginDialog(Client client, LocalDb localDb, Cache receivedMessageCache) { + this.client = client; + this.localDb = localDb; + this.receivedMessageCache = receivedMessageCache; + + // Prepare handshake + localDb.loadIdGenerator(); + + initUi(); + + okButton.addActionListener((evt) -> { + try { + if (registerCheckBox.isSelected()) { + // Check password equality + if (Arrays.equals(passwordField.getPassword(), repeatPasswordField.getPassword())) + credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), true); + else { + JOptionPane.showMessageDialog(this, "The repeated password is not the origional password!"); + clearPasswordFields(); + } + } else credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false); + performHandshake(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + }); + + // Listen to handshake rejections + EventBus.getInstance().register(HandshakeRejectionEvent.class, evt -> { + for (Component c : contentPanel.getComponents()) + if (c == errorMessage) contentPanel.remove(errorMessage); + clearPasswordFields(); + errorMessage = new JLabel(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); + contentPanel.revalidate(); + contentPanel.repaint(); + }); + + // Exit the application when the dialog is cancelled + cancelButton.addActionListener(evt -> { logger.info("The login process has been cancelled. Exiting..."); System.exit(0); }); + + // Log in directly if configured + if (config.hasLoginCredentials()) { + credentials = config.getLoginCredentials(); + performHandshake(); + return; + } + + setVisible(true); + } + + private void performHandshake() { + try { + client.performHandshake(credentials, receivedMessageCache); + if (client.isOnline()) { + client.initReceiver(localDb, receivedMessageCache); + dispose(); + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } catch (TimeLimitExceededException e) { + logger.warning("Could not connect to server. Trying offline mode..."); + e.printStackTrace(); + try { + // Try entering offline mode + localDb.loadUsers(); + 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, + "A connection to the server could not be established. Starting in offline mode.\n" + e, + "Connection error", + JOptionPane.WARNING_MESSAGE); + dispose(); + } catch (Exception e1) { + JOptionPane.showMessageDialog(null, e1, "Client error", JOptionPane.ERROR_MESSAGE); + System.exit(1); + return; + } + } + } + + private void initUi() { setSize(338, 123); setLocationRelativeTo(null); getContentPane().setLayout(new BorderLayout()); @@ -67,150 +172,109 @@ public class LoginDialog extends JDialog { gbl_contentPanel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE }; gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE }; contentPanel.setLayout(gbl_contentPanel); - { - lblUserName = new JLabel("Username:"); - GridBagConstraints gbc_lblUserName = new GridBagConstraints(); - gbc_lblUserName.anchor = GridBagConstraints.EAST; - gbc_lblUserName.insets = new Insets(0, 0, 5, 5); - gbc_lblUserName.gridx = 0; - gbc_lblUserName.gridy = 0; - contentPanel.add(lblUserName, gbc_lblUserName); - } - { - textField = new JTextField(); - textField.setBorder(null); - GridBagConstraints gbc_textField = new GridBagConstraints(); - gbc_textField.insets = new Insets(0, 0, 5, 0); - gbc_textField.fill = GridBagConstraints.HORIZONTAL; - gbc_textField.gridx = 1; - gbc_textField.gridy = 0; - contentPanel.add(textField, gbc_textField); - textField.setColumns(10); - } - { - lblPassword = new JLabel("Password:"); - GridBagConstraints gbc_lblPassword = new GridBagConstraints(); - gbc_lblPassword.anchor = GridBagConstraints.EAST; - gbc_lblPassword.insets = new Insets(0, 0, 0, 5); - gbc_lblPassword.gridx = 0; - gbc_lblPassword.gridy = 1; - contentPanel.add(lblPassword, gbc_lblPassword); - } - { - passwordField = new JPasswordField(); - passwordField.setBorder(null); - GridBagConstraints gbc_passwordField = new GridBagConstraints(); - gbc_passwordField.fill = GridBagConstraints.HORIZONTAL; - gbc_passwordField.gridx = 1; - gbc_passwordField.gridy = 1; - contentPanel.add(passwordField, gbc_passwordField); - } - { - lblRepeatPassword = new JLabel("Repeat Password:"); - gbc_lblRepeatPassword = new GridBagConstraints(); - gbc_lblRepeatPassword.anchor = GridBagConstraints.EAST; - gbc_lblRepeatPassword.insets = new Insets(0, 0, 0, 5); - gbc_lblRepeatPassword.gridx = 0; - gbc_lblRepeatPassword.gridy = 2; - } - { - 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" + evt.get()); - errorMessage = new JLabel(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); - }); - } - { - buttonPane = new JPanel(); - registerText = new JTextPane(); - registerText.setEditable(false); - registerText.setText("Register?"); - registerText.setFont(new Font("Arial", Font.BOLD, 12)); - registerText.setAlignmentX(LEFT_ALIGNMENT); - buttonPane.add(registerText); + lblUserName = new JLabel("Username:"); + GridBagConstraints gbc_lblUserName = new GridBagConstraints(); + gbc_lblUserName.anchor = GridBagConstraints.EAST; + gbc_lblUserName.insets = new Insets(0, 0, 5, 5); + gbc_lblUserName.gridx = 0; + gbc_lblUserName.gridy = 0; + contentPanel.add(lblUserName, gbc_lblUserName); - registerCheckBox = new JCheckBox(); - registerCheckBox.setAlignmentX(LEFT_ALIGNMENT); - registerCheckBox.addItemListener(e -> { - switch (e.getStateChange()) { - case ItemEvent.SELECTED: - contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); - contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); - setSize(338, 148); - break; + textField = new JTextField(); + textField.setBorder(null); + GridBagConstraints gbc_textField = new GridBagConstraints(); + gbc_textField.insets = new Insets(0, 0, 5, 0); + gbc_textField.fill = GridBagConstraints.HORIZONTAL; + gbc_textField.gridx = 1; + gbc_textField.gridy = 0; + contentPanel.add(textField, gbc_textField); + textField.setColumns(10); - case ItemEvent.DESELECTED: - if (repeatPasswordField.getParent() == contentPanel) { - contentPanel.remove(lblRepeatPassword); - contentPanel.remove(repeatPasswordField); - setSize(338, 123); - } - break; - } - contentPanel.revalidate(); - contentPanel.repaint(); - }); - buttonPane.add(registerCheckBox); + lblPassword = new JLabel("Password:"); + GridBagConstraints gbc_lblPassword = new GridBagConstraints(); + gbc_lblPassword.anchor = GridBagConstraints.EAST; + gbc_lblPassword.insets = new Insets(0, 0, 0, 5); + gbc_lblPassword.gridx = 0; + gbc_lblPassword.gridy = 1; + contentPanel.add(lblPassword, gbc_lblPassword); - buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); - getContentPane().add(buttonPane, BorderLayout.SOUTH); - { - okButton = new PrimaryButton("OK"); - okButton.addActionListener((evt) -> { - try { - if (registerCheckBox.isSelected()) { - // password checking - if (Arrays.equals(passwordField.getPassword(), repeatPasswordField.getPassword())) - credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), true); - else { - JOptionPane.showMessageDialog(this, "The repeated password is not the origional password!"); - clearPasswordFields(); - } - } else credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + passwordField = new JPasswordField(); + passwordField.setBorder(null); + GridBagConstraints gbc_passwordField = new GridBagConstraints(); + gbc_passwordField.fill = GridBagConstraints.HORIZONTAL; + gbc_passwordField.gridx = 1; + gbc_passwordField.gridy = 1; + contentPanel.add(passwordField, gbc_passwordField); + + lblRepeatPassword = new JLabel("Repeat Password:"); + gbc_lblRepeatPassword = new GridBagConstraints(); + gbc_lblRepeatPassword.anchor = GridBagConstraints.EAST; + gbc_lblRepeatPassword.insets = new Insets(0, 0, 0, 5); + gbc_lblRepeatPassword.gridx = 0; + gbc_lblRepeatPassword.gridy = 2; + + repeatPasswordField = new JPasswordField(); + gbc_repeatPasswordField = new GridBagConstraints(); + gbc_repeatPasswordField.fill = GridBagConstraints.HORIZONTAL; + gbc_repeatPasswordField.gridx = 1; + gbc_repeatPasswordField.gridy = 2; + + buttonPane = new JPanel(); + + registerText = new JTextPane(); + registerText.setEditable(false); + registerText.setText("Register?"); + registerText.setFont(new Font("Arial", Font.BOLD, 12)); + registerText.setAlignmentX(LEFT_ALIGNMENT); + buttonPane.add(registerText); + + registerCheckBox = new JCheckBox(); + registerCheckBox.setAlignmentX(LEFT_ALIGNMENT); + registerCheckBox.addItemListener(e -> { + switch (e.getStateChange()) { + case ItemEvent.SELECTED: + contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); + contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); + setSize(338, 148); + break; + + case ItemEvent.DESELECTED: + if (repeatPasswordField.getParent() == contentPanel) { + contentPanel.remove(lblRepeatPassword); + contentPanel.remove(repeatPasswordField); + setSize(338, 123); } - }); - okButton.setActionCommand("OK"); - buttonPane.add(okButton); - getRootPane().setDefaultButton(okButton); + break; } - { - cancelButton = new PrimaryButton("Cancel"); - cancelButton.addActionListener((evt) -> dispose()); - cancelButton.setActionCommand("Cancel"); - buttonPane.add(cancelButton); - } - } + contentPanel.revalidate(); + contentPanel.repaint(); + }); + buttonPane.add(registerCheckBox); + + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + okButton = new PrimaryButton("OK"); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + + cancelButton = new PrimaryButton("Cancel"); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); setTheme(); - setModal(true); - setVisible(true); + setModalityType(Dialog.DEFAULT_MODALITY_TYPE); + EventBus.getInstance().register(HandshakeSuccessfulEvent.class, evt -> dispose()); } /** - * Resets the text stored in the passwort fields. + * Resets the text stored in the password fields. * * @since Envoy v0.3-alpha */ - public void clearPasswordFields() { + private void clearPasswordFields() { passwordField.setText(null); repeatPasswordField.setText(null); } @@ -258,11 +322,4 @@ public class LoginDialog extends JDialog { cancelButton.setBackground(theme.getInteractableBackgroundColor()); cancelButton.setForeground(theme.getInteractableForegroundColor()); } - - /** - * @return the {@link LoginCredentials} entered by the user, or {@code null} if - * the dialog has been cancelled - * @since Envoy v0.3-alpha - */ - public LoginCredentials getCredentials() { return credentials; } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index f31cfa3..a684326 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -15,9 +15,7 @@ import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.client.util.EnvoyLog; -import envoy.data.LoginCredentials; import envoy.data.Message; -import envoy.data.User; import envoy.exception.EnvoyException; /** @@ -74,14 +72,6 @@ public class Startup { EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); - // Acquire login credentials - LoginCredentials credentials = config.hasLoginCredentials() ? config.getLoginCredentials() : new LoginDialog().getCredentials(); - - if (credentials == null) { - logger.info("The login process has been cancelled. Exiting..."); - System.exit(0); - } - // Initialize the local database LocalDb localDb; if (config.isIgnoreLocalDB()) { @@ -95,45 +85,23 @@ public class Startup { } catch (IOException e3) { logger.log(Level.SEVERE, "Could not initialize local database", e3); JOptionPane - .showMessageDialog(null, "Could not initialize local database!\n" + e3.toString(), "Local database error", JOptionPane.ERROR_MESSAGE); + .showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE); System.exit(1); return; } - SwingUtilities.invokeLater(() -> chatWindow.setVisible(true)); - - // Acquire the client user (with ID) either from the server or from the local - // database, which triggers offline mode + // Initialize client and unread message cache Client client = new Client(); Cache cache = new Cache<>(); - try { - // Try entering online mode first - localDb.loadIdGenerator(); - client.performHandshake(credentials, cache); - client.initReceiver(localDb, cache); - } catch (Exception e1) { - logger.warning("Could not connect to server. Trying offline mode..."); - e1.printStackTrace(); - try { - // Try entering offline mode - localDb.loadUsers(); - 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, - "A connection to the server could not be established. Starting in offline mode.\n" + e1, - "Connection error", - JOptionPane.WARNING_MESSAGE); - } catch (Exception e2) { - JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE); - System.exit(1); - return; - } - } + + // Try to connect to the server + new LoginDialog(client, localDb, cache); + SwingUtilities.invokeLater(() -> chatWindow.setVisible(true)); // Set client user in local database localDb.setUser(client.getSender()); + // Initialize chats in local database try { localDb.initializeUserStorage(); From 84fd2229565437d3a7d1184cf024e30e0b71f501 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 22:22:29 +0100 Subject: [PATCH 6/7] Fixed entering offline mode --- src/main/java/envoy/client/ui/LoginDialog.java | 7 ++----- src/main/java/envoy/client/ui/Startup.java | 1 - 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index bdac73a..40991c6 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -101,8 +101,7 @@ public class LoginDialog extends JDialog { // Listen to handshake rejections EventBus.getInstance().register(HandshakeRejectionEvent.class, evt -> { - for (Component c : contentPanel.getComponents()) - if (c == errorMessage) contentPanel.remove(errorMessage); + if (errorMessage != null) contentPanel.remove(errorMessage); clearPasswordFields(); errorMessage = new JLabel(evt.get()); gbc_errorMessage = new GridBagConstraints(); @@ -135,9 +134,7 @@ public class LoginDialog extends JDialog { client.initReceiver(localDb, receivedMessageCache); dispose(); } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - } catch (TimeLimitExceededException e) { + } catch (IOException | InterruptedException | TimeLimitExceededException e) { logger.warning("Could not connect to server. Trying offline mode..."); e.printStackTrace(); try { diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a684326..5427fe1 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -101,7 +101,6 @@ public class Startup { // Set client user in local database localDb.setUser(client.getSender()); - // Initialize chats in local database try { localDb.initializeUserStorage(); From 6e211a01a1297934dc7268db4b924df5b9d39d88 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 13 Feb 2020 10:11:25 +0100 Subject: [PATCH 7/7] Fixed error message layout in LoginDialog Updated envoy-common dependency to v0.2-alpha in pom.xml --- pom.xml | 2 +- .../java/envoy/client/ui/LoginDialog.java | 31 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 6106227..e670832 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - f~advanced_login-SNAPSHOT + v0.2-alpha diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index 40991c6..09fd551 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -100,19 +100,9 @@ public class LoginDialog extends JDialog { }); // Listen to handshake rejections - EventBus.getInstance().register(HandshakeRejectionEvent.class, evt -> { - if (errorMessage != null) contentPanel.remove(errorMessage); - clearPasswordFields(); - errorMessage = new JLabel(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); - contentPanel.revalidate(); - contentPanel.repaint(); - }); + EventBus.getInstance() + .register(HandshakeRejectionEvent.class, + evt -> { clearPasswordFields(); errorMessage.setVisible(true); errorMessage.setText(evt.get()); }); // Exit the application when the dialog is cancelled cancelButton.addActionListener(evt -> { logger.info("The login process has been cancelled. Exiting..."); System.exit(0); }); @@ -159,6 +149,7 @@ public class LoginDialog extends JDialog { private void initUi() { setSize(338, 123); setLocationRelativeTo(null); + setResizable(false); getContentPane().setLayout(new BorderLayout()); contentPanel = new JPanel(); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -217,6 +208,16 @@ public class LoginDialog extends JDialog { gbc_repeatPasswordField.gridx = 1; gbc_repeatPasswordField.gridy = 2; + errorMessage = new JLabel(); + gbc_errorMessage = new GridBagConstraints(); + gbc_errorMessage.gridx = 1; + gbc_errorMessage.gridy = 3; + gbc_errorMessage.fill = GridBagConstraints.HORIZONTAL; + gbc_errorMessage.insets = new Insets(5, 5, 5, 5); + errorMessage.setForeground(Color.RED); + errorMessage.setVisible(false); + contentPanel.add(errorMessage, gbc_errorMessage); + buttonPane = new JPanel(); registerText = new JTextPane(); @@ -233,14 +234,14 @@ public class LoginDialog extends JDialog { case ItemEvent.SELECTED: contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); - setSize(338, 148); + setSize(338, 173); break; case ItemEvent.DESELECTED: if (repeatPasswordField.getParent() == contentPanel) { contentPanel.remove(lblRepeatPassword); contentPanel.remove(repeatPasswordField); - setSize(338, 123); + setSize(338, 148); } break; }