Created
November 1, 2013 18:36
-
-
Save PhilippThaler/7269800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.philippthaler.emaillistener.gui; | |
import java.awt.Color; | |
import java.awt.event.ActionEvent; | |
import java.security.GeneralSecurityException; | |
import javax.swing.AbstractAction; | |
import javax.swing.Action; | |
import javax.swing.JButton; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JPasswordField; | |
import javax.swing.JTextField; | |
import com.philippthaler.emaillistener.Main; | |
import com.philippthaler.emaillistener.email.EmailListener; | |
public class Login extends JPanel { | |
private static final long serialVersionUID = -8807244985892120772L; | |
private JLabel lblUsername; | |
private static JTextField textFieldUsername; | |
private JLabel lblPassword; | |
private static JPasswordField passwordField; | |
private JButton btnLogin; | |
private JLabel lblgmailcom; | |
private static JLabel lblError; | |
public Login() { | |
setLayout(null); | |
lblUsername = new JLabel("Username:"); | |
lblUsername.setBounds(10, 26, 67, 14); | |
add(lblUsername); | |
textFieldUsername = new JTextField(); | |
textFieldUsername.setBounds(87, 23, 115, 20); | |
add(textFieldUsername); | |
textFieldUsername.setColumns(10); | |
lblPassword = new JLabel("Password:"); | |
lblPassword.setBounds(10, 70, 67, 14); | |
add(lblPassword); | |
Action loginAction = new AbstractAction("Login") { | |
private static final long serialVersionUID = 3761026640275057670L; | |
public void actionPerformed(ActionEvent e) { | |
login(); | |
} | |
}; | |
passwordField = new JPasswordField(); | |
passwordField.addActionListener(loginAction); | |
passwordField.setBounds(87, 67, 193, 20); | |
add(passwordField); | |
btnLogin = new JButton("Login"); | |
btnLogin.addActionListener(loginAction); | |
btnLogin.setBounds(213, 98, 67, 23); | |
add(btnLogin); | |
lblgmailcom = new JLabel("@gmail.com"); | |
lblgmailcom.setBounds(206, 23, 74, 20); | |
add(lblgmailcom); | |
lblError = new JLabel(""); | |
lblError.setForeground(Color.RED); | |
lblError.setBounds(10, 102, 192, 14); | |
add(lblError); | |
} | |
@SuppressWarnings("deprecation") | |
public static void login() { | |
String username = (textFieldUsername.getText() + "@gmail.com").trim(); | |
String password = passwordField.getText(); | |
try { | |
if (EmailListener.tryConnection(username, password)) { | |
Login.lblError.setForeground(Color.GREEN); | |
Login.lblError.setText("Login successful."); | |
Main.setMainVisible(false); | |
Main.el = new EmailListener(username, password); | |
Main.el.run(); | |
} else { | |
Login.lblError.setForeground(Color.RED); | |
Login.lblError.setText("Invalid username or password."); | |
} | |
} catch (GeneralSecurityException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment