mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-16 18:58:00 +00:00
Add Enter key handlers to username and password fields
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
package forge.screens.home.online;
|
package forge.screens.home.online;
|
||||||
|
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
import net.miginfocom.swing.MigLayout;
|
import net.miginfocom.swing.MigLayout;
|
||||||
@@ -16,6 +17,7 @@ import forge.toolbox.FLabel;
|
|||||||
import forge.toolbox.FOptionPane;
|
import forge.toolbox.FOptionPane;
|
||||||
import forge.toolbox.FPasswordField;
|
import forge.toolbox.FPasswordField;
|
||||||
import forge.toolbox.FTextField;
|
import forge.toolbox.FTextField;
|
||||||
|
import forge.util.Base64Coder;
|
||||||
import forge.view.FDialog;
|
import forge.view.FDialog;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
@@ -57,36 +59,29 @@ public class LoginDialog extends FDialog {
|
|||||||
|
|
||||||
txtUsername.setText(username);
|
txtUsername.setText(username);
|
||||||
|
|
||||||
|
txtUsername.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (!txtUsername.isEmpty()) {
|
||||||
|
txtPassword.selectAll();
|
||||||
|
txtPassword.requestFocusInWindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
txtPassword.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (!txtPassword.isEmpty()) {
|
||||||
|
tryLogin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
btnLogin.setCommand(new UiCommand() {
|
btnLogin.setCommand(new UiCommand() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
String username = txtUsername.getText();
|
tryLogin();
|
||||||
if (username.isEmpty()) {
|
|
||||||
FOptionPane.showErrorDialog("You must enter a username", "Login Failed");
|
|
||||||
txtUsername.requestFocusInWindow();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
char[] password = txtPassword.getPassword();
|
|
||||||
if (password == null || password.length == 0) {
|
|
||||||
FOptionPane.showErrorDialog("You must enter a password", "Login Failed");
|
|
||||||
txtPassword.requestFocusInWindow();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
String passwordStr = String.valueOf(password);
|
|
||||||
if (ServerUtil.login(username, passwordStr)) {
|
|
||||||
FModel.getPreferences().setPref(FPref.ONLINE_USERNAME, username);
|
|
||||||
FModel.getPreferences().setPref(FPref.ONLINE_PASSWORD, cbRememberMe.isSelected() ? passwordStr : "");
|
|
||||||
FModel.getPreferences().save();
|
|
||||||
setVisible(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
FOptionPane.showErrorDialog("Could not login with entered username and password.", "Login Failed");
|
|
||||||
txtUsername.requestFocusInWindow();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
BugReporter.reportException(e, "Login Failed");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
btnCancel.setCommand(new UiCommand() {
|
btnCancel.setCommand(new UiCommand() {
|
||||||
@@ -117,4 +112,34 @@ public class LoginDialog extends FDialog {
|
|||||||
setDefaultFocus(txtPassword);
|
setDefaultFocus(txtPassword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void tryLogin() {
|
||||||
|
String username = txtUsername.getText();
|
||||||
|
if (username.isEmpty()) {
|
||||||
|
FOptionPane.showErrorDialog("You must enter a username", "Login Failed");
|
||||||
|
txtUsername.requestFocusInWindow();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
char[] password = txtPassword.getPassword();
|
||||||
|
if (password == null || password.length == 0) {
|
||||||
|
FOptionPane.showErrorDialog("You must enter a password", "Login Failed");
|
||||||
|
txtPassword.requestFocusInWindow();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String encryptedPassword = Base64Coder.encrypt(String.valueOf(password));
|
||||||
|
if (ServerUtil.login(username, encryptedPassword)) {
|
||||||
|
FModel.getPreferences().setPref(FPref.ONLINE_USERNAME, username);
|
||||||
|
FModel.getPreferences().setPref(FPref.ONLINE_PASSWORD, cbRememberMe.isSelected() ? encryptedPassword : "");
|
||||||
|
FModel.getPreferences().save();
|
||||||
|
setVisible(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FOptionPane.showErrorDialog("Could not login with entered username and password.", "Login Failed");
|
||||||
|
txtUsername.requestFocusInWindow();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
BugReporter.reportException(e, "Login Failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user