From 34785dc7f59b2e64a81d013320cd736805bee8aa Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 1 Jan 2020 18:18:18 +0200 Subject: [PATCH] Added LoginDialog --- .../java/envoy/client/ui/LoginDialog.java | 118 ++++++++++++++++++ src/main/java/envoy/client/ui/Startup.java | 22 ++-- 2 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 src/main/java/envoy/client/ui/LoginDialog.java diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java new file mode 100644 index 0000000..db88176 --- /dev/null +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -0,0 +1,118 @@ +package envoy.client.ui; + +import java.awt.*; +import java.security.NoSuchAlgorithmException; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; + +import envoy.data.LoginCredentials; + +/** + * Project: envoy-client
+ * File: LoginDialog.java
+ * Created: 01.01.2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy v0.3-alpha + */ +public class LoginDialog extends JDialog { + + private final JPanel contentPanel = new JPanel(); + + private static final long serialVersionUID = 352021600833907468L; + private JTextField textField; + private JPasswordField passwordField; + + private LoginCredentials credentials; + + /** + * Displays a dialog enabling the user to enter their user name and password. + * + * @since Envoy v0.3-alpha + */ + public LoginDialog() { + setSize(338, 123); + setLocationRelativeTo(null); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + GridBagLayout gbl_contentPanel = new GridBagLayout(); + gbl_contentPanel.columnWidths = new int[] { 0, 0, 0 }; + gbl_contentPanel.rowHeights = new int[] { 0, 0, 0 }; + 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); + { + JLabel lblUserName = new JLabel("User name:"); + 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(); + 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); + } + { + JLabel 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(); + GridBagConstraints gbc_passwordField = new GridBagConstraints(); + gbc_passwordField.fill = GridBagConstraints.HORIZONTAL; + gbc_passwordField.gridx = 1; + gbc_passwordField.gridy = 1; + contentPanel.add(passwordField, gbc_passwordField); + } + { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + JButton okButton = new JButton("OK"); + okButton.addActionListener((evt) -> { + try { + credentials = new LoginCredentials(textField.getText(), passwordField.getPassword()); + dispose(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + }); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + { + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener((evt) -> dispose()); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); + } + } + + setModal(true); + setVisible(true); + } + + /** + * @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 ae01e82..3e4692d 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -71,15 +71,13 @@ public class Startup { EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); - // Ask the user for their user name - String userName = JOptionPane.showInputDialog("Please enter your username"); - if (userName == null || userName.isEmpty()) { - logger.severe("User name is not set or empty. Exiting..."); - System.exit(1); - } + // Ask the user for their user name and password + LoginCredentials credentials = new LoginDialog().getCredentials(); - // TODO: create dialog - String pass = JOptionPane.showInputDialog("Enter password"); + if (credentials == null) { + logger.info("The login process has been aborted by the user. Exiting..."); + System.exit(0); + } // Initialize the local database LocalDB localDB; @@ -99,18 +97,18 @@ public class Startup { Client client = new Client(); try { // Try entering online mode first - client.onlineInit(new LoginCredentials(userName, pass.toCharArray())); + client.onlineInit(credentials); } 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(userName); + User clientUser = localDB.getUsers().get(credentials.getName()); 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.", + "A connection to the server could not be established. Starting in offline mode.\n" + e1, "Connection error", JOptionPane.WARNING_MESSAGE); } catch (Exception e2) { @@ -132,7 +130,7 @@ public class Startup { } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, - "Error while loading local database: " + e.toString() + "\nChats might not be stored locally.", + "Error while loading local database: " + e + "\nChats might not be stored locally.", "Local DB error", JOptionPane.WARNING_MESSAGE); }