Added LoginDialog
This commit is contained in:
parent
7e2956ca11
commit
96066863ca
118
src/main/java/envoy/client/ui/LoginDialog.java
Normal file
118
src/main/java/envoy/client/ui/LoginDialog.java
Normal file
@ -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: <strong>envoy-client</strong><br>
|
||||||
|
* File: <strong>LoginDialog.java</strong><br>
|
||||||
|
* Created: <strong>01.01.2020</strong><br>
|
||||||
|
*
|
||||||
|
* @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; }
|
||||||
|
}
|
@ -71,15 +71,13 @@ public class Startup {
|
|||||||
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
|
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
|
||||||
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
|
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
|
||||||
|
|
||||||
// Ask the user for their user name
|
// Ask the user for their user name and password
|
||||||
String userName = JOptionPane.showInputDialog("Please enter your username");
|
LoginCredentials credentials = new LoginDialog().getCredentials();
|
||||||
if (userName == null || userName.isEmpty()) {
|
|
||||||
logger.severe("User name is not set or empty. Exiting...");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: create dialog
|
if (credentials == null) {
|
||||||
String pass = JOptionPane.showInputDialog("Enter password");
|
logger.info("The login process has been aborted by the user. Exiting...");
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the local database
|
// Initialize the local database
|
||||||
LocalDB localDB;
|
LocalDB localDB;
|
||||||
@ -99,18 +97,18 @@ public class Startup {
|
|||||||
Client client = new Client();
|
Client client = new Client();
|
||||||
try {
|
try {
|
||||||
// Try entering online mode first
|
// Try entering online mode first
|
||||||
client.onlineInit(new LoginCredentials(userName, pass.toCharArray()));
|
client.onlineInit(credentials);
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
logger.warning("Could not connect to server. Trying offline mode...");
|
logger.warning("Could not connect to server. Trying offline mode...");
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
try {
|
try {
|
||||||
// Try entering offline mode
|
// Try entering offline mode
|
||||||
localDB.loadUsers();
|
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");
|
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
|
||||||
client.setSender(clientUser);
|
client.setSender(clientUser);
|
||||||
JOptionPane.showMessageDialog(null,
|
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",
|
"Connection error",
|
||||||
JOptionPane.WARNING_MESSAGE);
|
JOptionPane.WARNING_MESSAGE);
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
@ -132,7 +130,7 @@ public class Startup {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
JOptionPane.showMessageDialog(null,
|
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",
|
"Local DB error",
|
||||||
JOptionPane.WARNING_MESSAGE);
|
JOptionPane.WARNING_MESSAGE);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user