package realcix20.guis.views;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import javax.crypto.SealedObject;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.Item;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
import realcix20.utils.PasswordManager;
import realcix20.utils.Resources;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
public class LoginFrame extends JFrame implements ActionListener {
private JLabel loginLabel;
private JLabel languageLabel;
private JLabel userIdLabel;
private JLabel passwordLabel;
private JLabel alert_userIdLabel;
private JLabel alert_passwordLabel;
private JComboBox languageChooser;
private JTextField userIdField;
private JPasswordField passwordField;
private JCheckBox saveLoginInfoCheckBox;
private JButton loginButton;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Language Choose")) {
Resources.LANGUAGE = ((Item)languageChooser.getSelectedItem()).getFactValue().toString();
updateView();
} else if (e.getActionCommand().equals("Login")) {
login();
} else if (e.getActionCommand().equals("Exit")) {
exitApplication();
}
}
private void exitApplication() {
int n = DialogManager.showYesNoDialog(this, TxtManager.getTxt("alert.exit"));
if (n == 0) {//exit application
DAO dao = DAO.getInstance();
dao.commit();
dao.deleteDAO();
System.exit(0);
}
}
private void login() {
String userId = userIdField.getText().trim();
String password = new String(passwordField.getPassword());
boolean result = validate(userId, password);
try {
//Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
if (result) {
String lang = ((Item)languageChooser.getSelectedItem()).getFactValue().toString();
GlobalValueManager.setApplicationLang(lang);
if (saveLoginInfoCheckBox.isSelected()) {
saveUserInfo(userId, password, lang);
} else
disposeUserInfo();
new MainView();
dispose();
}
}
private void saveUserInfo(String userId, String password, String lang) {
GlobalValueManager.setValue("APPLICATION.USERID", userId);
GlobalValueManager.setValue("APPLICATION.PASSWORD", password);
//Resources.LANGUAGE
GlobalValueManager.setValue("SESSION.LOGINUSER", userId);
GlobalValueManager.setValue("loginuser", userId);//will be modified
}
private void disposeUserInfo() {
saveUserInfo(null, null, null);
}
private void setComponentsStat(boolean stat) {
languageChooser.setEnabled(stat);
userIdField.setEnabled(stat);
passwordField.setEnabled(stat);
saveLoginInfoCheckBox.setEnabled(stat);
loginButton.setEnabled(stat);
}
private boolean validate(String userId, String password) {
boolean result = true;
alert_userIdLabel.setVisible(false);
alert_passwordLabel.setVisible(false);
if (userId.length() == 0) {
alert_userIdLabel.setText(TxtManager.getTxt("ALERT.USERIDCANTBEEMPTY"));
alert_userIdLabel.setVisible(true);
result = false;
}
if (password.length() == 0) {
alert_passwordLabel.setText(TxtManager.getTxt("ALERT.PASSWORDCANTBEEMPTY"));
alert_passwordLabel.setVisible(true);
result = false;
}
if (result) {
result = validatePassword(userId, password);
if (!result) {
alert_passwordLabel.setText(TxtManager.getTxt("ALERT.USERIDORPASSWORDERROR"));
alert_passwordLabel.setVisible(true);
}
}
return result;
}
private boolean validatePassword(String userId, String password) {
boolean result = true;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_PASSWORD_FROM_RUSER);
dao.setString(1, userId);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
SealedObject so = (SealedObject)rs.getObject(1);
try {
String oldPassword = PasswordManager.getPassword(so);
if (!oldPassword.equals(password))
result = false;
} catch (Exception e) {
result = false;
}
} else {
result = false;
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
result = false;
}
return result;
}
private void updateView() {
setTitle(TxtManager.getTxt("VIEW.APPLICATION.TITLE"));
languageLabel.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.LOGINFRAME.LANGUAGELABEL"), KeyEvent.VK_L));
userIdLabel.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.LOGINFRAME.USERIDLABEL"), KeyEvent.VK_U));
passwordLabel.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.LOGINFRAME.PASSWORDLABEL"), KeyEvent.VK_P));
saveLoginInfoCheckBox.setText(TxtManager.getTxt("VIEW.LOGINFRAME.SAVELOGININFOCHECKBOX"));
loginButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.LOGINFRAME.LOGINBUTTON"), KeyEvent.VK_O));
alert_userIdLabel.setVisible(false);
alert_passwordLabel.setVisible(false);
}
public LoginFrame() {
initComponents();
initMenuBar();
}
private void initLanguageChooserItems() {
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_ACTIVE_LANGUAGE_SQL);
ResultSet rs = dao.executeQuery();
try {
while (rs.next()) {
String lang = rs.getString("LANG");
String txt = rs.getString("TXT");
Item item = new Item(lang, txt);
languageChooser.addItem(item);
if (GlobalValueManager.getApplicationLang().equals(lang))
languageChooser.setSelectedItem(item);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < languageChooser.getItemCount(); i++) {
Item item = (Item)languageChooser.getItemAt(i);
if (item.getFactValue().equals(GlobalValueManager.getValue("APPLICATION.DEFLANG"))) {
languageChooser.setSelectedItem(item);
Resources.LANGUAGE = GlobalValueManager.getValue("APPLICATION.DEFLANG");
}
}
}
private void initUserInfo() {
boolean result = false;
String userId = GlobalValueManager.getValue("APPLICATION.USERID");
if (userId != null)
result = true;
if (result) {
String password = GlobalValueManager.getValue("APPLICATION.PASSWORD");
userIdField.setText(userId);
userIdField.setSelectionStart(0);
passwordField.setText(password);
passwordField.setSelectionStart(0);
saveLoginInfoCheckBox.setSelected(true);
for (int i = 0; i < languageChooser.getItemCount(); i++) {
Item item = (Item)languageChooser.getItemAt(i);
if (item.getFactValue().equals(GlobalValueManager.getApplicationLang()))
languageChooser.setSelectedItem(item);
}
}
}
private void initMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu notVisibleMenu = new JMenu();
notVisibleMenu.setVisible(false);
menuBar.add(notVisibleMenu);
JMenuItem menuItem = new JMenuItem();
menuItem.setAccelerator(KeyStroke.getKeyStroke("ESCAPE"));
menuItem.setActionCommand("Exit");
menuItem.addActionListener(this);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
setJMenuBar(menuBar);
}
private void initComponents() {
//======== this ========
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
((LoginFrame)e.getSource()).exitApplication();
}
}
);
CellConstraints cc = new CellConstraints();
//======== this ========
setBackground(Color.white);
setIconImage(ImageManager.getImage(ImageManager.REALCIX_APP_IAMGE).getImage());
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};
//======== mainPanel ========
JPanel mainPanel = new JPanel();
{
mainPanel.setBackground(Color.white);
mainPanel.setLayout(new GridBagLayout());
((GridBagLayout)mainPanel.getLayout()).columnWidths = new int[] {0, 0};
((GridBagLayout)mainPanel.getLayout()).rowHeights = new int[] {0, 0, 0};
((GridBagLayout)mainPanel.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
((GridBagLayout)mainPanel.getLayout()).rowWeights = new double[] {0.0, 1.0, 1.0E-4};
//---- loginLabel ----
loginLabel = new JLabel();
loginLabel.setIcon(ImageManager.getImage(ImageManager.LOGIN_IMAGE));
mainPanel.add(loginLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.VERTICAL,
new Insets(0, 0, 0, 0), 0, 0));
//======== loginPanel ========
JPanel loginPanel = new JPanel();
{
loginPanel.setBackground(Color.white);
loginPanel.setLayout(new FormLayout(
new ColumnSpec[] {
new ColumnSpec("100px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("100px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("150px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("200px")
},
new RowSpec[] {
new RowSpec("30px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("30px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("30px"),
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("200px")
}));
//---- languageLabel ----
languageLabel = new JLabel();
languageLabel.setDisplayedMnemonic(KeyEvent.VK_L);
languageLabel.setRequestFocusEnabled(false);
loginPanel.add(languageLabel, cc.xywh(3, 1, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
//---- languageChooser ----
languageChooser = new JComboBox();
languageLabel.setLabelFor(languageChooser);
languageChooser.setBackground(Color.white);
initLanguageChooserItems();
languageChooser.addActionListener(this);
languageChooser.setActionCommand("Language Choose");
loginPanel.add(languageChooser, cc.xy(5, 1));
//---- userIdLabel ----
userIdLabel = new JLabel();
userIdLabel.setDisplayedMnemonic(KeyEvent.VK_U);
userIdLabel.setRequestFocusEnabled(false);
loginPanel.add(userIdLabel, cc.xywh(3, 3, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
//---- userIdField ----
userIdField = new JTextField();
userIdLabel.setLabelFor(userIdField);
loginPanel.add(userIdField, cc.xy(5, 3));
//---- alert_userIdLabel ----CustomTextFormator
alert_userIdLabel = new JLabel();
alert_userIdLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alert_userIdLabel.setVisible(false);
loginPanel.add(alert_userIdLabel, cc.xy(7, 3));
//---- passwordLabel ----
passwordLabel = new JLabel();
passwordLabel.setDisplayedMnemonic(KeyEvent.VK_P);
passwordLabel.setRequestFocusEnabled(false);
loginPanel.add(passwordLabel, cc.xywh(3, 5, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
//---- passwordField ----
passwordField = new JPasswordField();
passwordField.setColumns(12);
passwordLabel.setLabelFor(passwordField);
loginPanel.add(passwordField, cc.xy(5, 5));
//---- alert_passwordLabel ----
alert_passwordLabel = new JLabel();
alert_passwordLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alert_passwordLabel.setVisible(false);
loginPanel.add(alert_passwordLabel, cc.xy(7, 5));
//---- saveLoginInfoCheckBox ----
saveLoginInfoCheckBox = new JCheckBox();
saveLoginInfoCheckBox.setBackground(Color.white);
saveLoginInfoCheckBox.setSelected(false);
loginPanel.add(saveLoginInfoCheckBox, cc.xy(5, 7));
//======== loginButtonPanel ========
JPanel loginButtonPanel = new JPanel();
{
loginButtonPanel.setBackground(Color.white);
loginButtonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
//---- loginButton ----
loginButton = new JButton();
loginButton.setMnemonic(KeyEvent.VK_O);
getRootPane().setDefaultButton(loginButton);
loginButton.setBackground(Color.white);
loginButton.addActionListener(this);
loginButton.setActionCommand("Login");
loginButton.setPreferredSize(new Dimension(100, 20));
loginButtonPanel.add(loginButton);
}
loginPanel.add(loginButtonPanel, cc.xy(5, 9));
}
mainPanel.add(loginPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.VERTICAL,
new Insets(0, 0, 0, 0), 0, 0));
}
contentPane.add(mainPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setSize(new Dimension(800,600));
//setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(true);
setVisible(true);
initUserInfo();
updateView();
//KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(languageChooser);
}
}