package realcix20.guis.components;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import realcix20.classes.UserClass;
import realcix20.classes.basic.Column;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.GlobalValueManager;
import realcix20.utils.PasswordManager;
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 PasswordEdit extends JPanel implements ActionListener {
private JPasswordField passwordField;
private JButton passwordButton;
private Column column;
private Object value;
public PasswordEdit(Column column) {
super();
this.column = column;
this.value = null;
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
passwordField = new JPasswordField();
passwordField.setPreferredSize(new Dimension(130, 20));
passwordField.setEditable(false);
passwordField.setBackground(Color.WHITE);
passwordButton = new JButton();
passwordButton.setPreferredSize(new Dimension(20,20));
passwordButton.setText(null);
passwordButton.setIcon(ImageManager.getImage(ImageManager.KEY_IMAGE));
passwordButton.setActionCommand("Show PasswordEdit Form");
passwordButton.addActionListener(this);
add(passwordField);
add(passwordButton);
}
public void showPasswordEditForm() {
JPanel panel = new JPanel();
CellConstraints cc = new CellConstraints();
panel.setLayout(new FormLayout(
new ColumnSpec[] {
new ColumnSpec("10px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("150px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("100px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("150px")
},
new RowSpec[] {
new RowSpec("10px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px")
}));
JLabel superUserPasswordLabel = new JLabel();
superUserPasswordLabel.setText(TxtManager.getTxt("VIEW.PASSWORDEDIT.SUPERUSERPASSWORDLABEL"));
panel.add(superUserPasswordLabel, cc.xy(3, 3));
JPasswordField superUserPasswordField = new JPasswordField();
panel.add(superUserPasswordField, cc.xy(5, 3));
JLabel alertSuperPasswordLabelLabel= new JLabel();
alertSuperPasswordLabelLabel.setVisible(false);
panel.add(alertSuperPasswordLabelLabel, cc.xy(7, 3));
JLabel oldPasswordLabel = new JLabel();
oldPasswordLabel.setText(TxtManager.getTxt("VIEW.PASSWORDEDIT.OLDPASSWORDLABEL"));
panel.add(oldPasswordLabel, cc.xy(3, 5));
JPasswordField oldPasswordField = new JPasswordField();
panel.add(oldPasswordField, cc.xy(5, 5));
JLabel alertOldPasswordLabel = new JLabel();
alertOldPasswordLabel.setVisible(false);
panel.add(alertOldPasswordLabel, cc.xy(7, 5));
JLabel newPasswordLabel1 = new JLabel();
newPasswordLabel1.setText(TxtManager.getTxt("VIEW.PASSWORDEDIT.NEWPASSWORDLABEL1"));
panel.add(newPasswordLabel1, cc.xy(3, 7));
JPasswordField newPasswordField1 = new JPasswordField();
panel.add(newPasswordField1, cc.xy(5, 7));
JLabel alertNewPasswordLabel1 = new JLabel();
alertNewPasswordLabel1.setVisible(false);
panel.add(alertNewPasswordLabel1, cc.xy(7, 7));
JLabel newPasswordLabel2 = new JLabel();
newPasswordLabel2.setText(TxtManager.getTxt("VIEW.PASSWORDEDIT.NEWPASSWORDLABEL2"));
panel.add(newPasswordLabel2, cc.xy(3, 9));
JPasswordField newPasswordField2 = new JPasswordField();
panel.add(newPasswordField2, cc.xy(5, 9));
JLabel alertNewPasswordLabel2 = new JLabel();
alertNewPasswordLabel2.setVisible(false);
panel.add(alertNewPasswordLabel2, cc.xy(7, 9));
boolean createNewPassword = false;
if (value == null) {//new password
createNewPassword = true;
}
Object[] options = {TxtManager.getTxt("VIEW.GLOBAL.CONFIRMBUTTON"),
TxtManager.getTxt("VIEW.GLOBAL.CANCELBUTTON"),
TxtManager.getTxt("VIEW.PASSWORDEDIT.SHOWPASSWORD")};
int n = -1;
while ( (n != 0) && (n != 1) ) {
n = JOptionPane.showOptionDialog(this, panel, TxtManager.getTxt("VIEW.PASSWORDEDIT.TITLE"),
JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (n == 2) {
if (createNewPassword) {
DialogManager.showMessageDialog(this, TxtManager.getTxt("INFORMATION.PASSWORDNOTCREATED"));
} else {
if (superUserPasswordField.getPassword().length == 0) {
alertSuperPasswordLabelLabel.setVisible(false);
alertSuperPasswordLabelLabel.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertSuperPasswordLabelLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertSuperPasswordLabelLabel.setVisible(true);
} else {
alertSuperPasswordLabelLabel.setVisible(false);
String superUserPassword1 = new String(superUserPasswordField.getPassword());
String superUserPassword = UserClass.getSuperUserPassword();
if (!superUserPassword.equals(superUserPassword1)) {
alertSuperPasswordLabelLabel.setText(TxtManager.getTxt("VALIDATE.PASSWORDERROR"));
alertSuperPasswordLabelLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertSuperPasswordLabelLabel.setVisible(true);
} else {
String oldPassword = null;
SealedObject so = (SealedObject)value;
try {
oldPassword = PasswordManager.getPassword(so);
} catch (Exception e) {
e.printStackTrace();
}
StringBuffer sb = new StringBuffer(TxtManager.getTxt("INFORMATION.SHOWPASSWORD"));
sb.append(oldPassword + " !");
DialogManager.showMessageDialog(this, sb.toString());
}
}
}
} else if (n == 0) {
if (createNewPassword) {
if (newPasswordField1.getPassword().length == 0) {
alertNewPasswordLabel1.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertNewPasswordLabel1.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel1.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel1.setVisible(false);
}
if (newPasswordField2.getPassword().length == 0) {
alertNewPasswordLabel2.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertNewPasswordLabel2.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel2.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel2.setVisible(false);
}
if (n!= -1) {
String newPassword1 = new String(newPasswordField1.getPassword());
String newPassword2 = new String(newPasswordField2.getPassword());
if (!newPassword1.equals(newPassword2)) {
alertNewPasswordLabel2.setText(TxtManager.getTxt("VALIDATE.PASSWORDNOTEQUAL"));
alertNewPasswordLabel2.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel2.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel2.setVisible(false);
}
}
} else {
if (superUserPasswordField.getPassword().length == 0) {
String oldPassword = null;
SealedObject so = (SealedObject)value;
try {
oldPassword = PasswordManager.getPassword(so);
} catch (Exception e) {
e.printStackTrace();
}
if (oldPasswordField.getPassword().length == 0) {
alertOldPasswordLabel.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertOldPasswordLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertOldPasswordLabel.setVisible(true);
n=-1;
} else {
String oldPassword1 = new String(oldPasswordField.getPassword());
if (!oldPassword.equals(oldPassword1)) {
alertOldPasswordLabel.setText(TxtManager.getTxt("VALIDATE.PASSWORDERROR"));
alertOldPasswordLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertOldPasswordLabel.setVisible(true);
n=-1;
} else {
alertOldPasswordLabel.setVisible(false);
}
}
if (newPasswordField1.getPassword().length == 0) {
alertNewPasswordLabel1.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertNewPasswordLabel1.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel1.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel1.setVisible(false);
}
if (newPasswordField2.getPassword().length == 0) {
alertNewPasswordLabel2.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertNewPasswordLabel2.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel2.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel2.setVisible(false);
}
if (n != -1) {
String newPassword1 = new String(newPasswordField1.getPassword());
String newPassword2 = new String(newPasswordField2.getPassword());
if (!newPassword1.equals(newPassword2)) {
alertNewPasswordLabel2.setText(TxtManager.getTxt("VALIDATE.PASSWORDNOTEQUAL"));
alertNewPasswordLabel2.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel2.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel2.setVisible(false);
}
}
} else {
alertOldPasswordLabel.setVisible(false);
if (newPasswordField1.getPassword().length == 0) {
alertNewPasswordLabel1.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertNewPasswordLabel1.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel1.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel1.setVisible(false);
}
if (newPasswordField2.getPassword().length == 0) {
alertNewPasswordLabel2.setText(TxtManager.getTxt("VALIDATE.NULLVALUE"));
alertNewPasswordLabel2.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel2.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel2.setVisible(false);
}
if (n != -1) {
String newPassword1 = new String(newPasswordField1.getPassword());
String newPassword2 = new String(newPasswordField2.getPassword());
if (!newPassword1.equals(newPassword2)) {
alertNewPasswordLabel2.setText(TxtManager.getTxt("VALIDATE.PASSWORDNOTEQUAL"));
alertNewPasswordLabel2.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertNewPasswordLabel2.setVisible(true);
n = -1;
} else {
alertNewPasswordLabel2.setVisible(false);
}
}
if (n != -1) {
String superUserPassword1 = new String(superUserPasswordField.getPassword());
String superUserPassword = UserClass.getSuperUserPassword();
if (!superUserPassword.equals(superUserPassword1)) {
alertSuperPasswordLabelLabel.setText(TxtManager.getTxt("VALIDATE.PASSWORDERROR"));
alertSuperPasswordLabelLabel.setIcon(ImageManager.getImage(ImageManager.ALERT_IMAGE));
alertSuperPasswordLabelLabel.setVisible(true);
n=-1;
} else {
alertSuperPasswordLabelLabel.setVisible(false);
}
}
}
}
}
}
if (n == 0) {
try {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, GlobalValueManager.getPasswordKey());
String password = new String(newPasswordField1.getPassword());
SealedObject so = new SealedObject(password, cipher);
setValue(so);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Show PasswordEdit Form")) {
showPasswordEditForm();
}
}
public void setValue(Object value) {
this.value = value;
if (value == null)
passwordField.setText(null);
else
passwordField.setText("11111");
}
public Object getValue() {
return value;
}
}