* For now it only has option to change password<br />
* @return JPanel with all the password fields for user to change their password
*/
public final JPanel settingsPanel(){
JPanel p = new JPanel();
final JPasswordField oldpassword = new JPasswordField("",15);
final JPasswordField password = new JPasswordField("",15);
final JPasswordField repassword = new JPasswordField("",15);
JButton registerButton = new JButton("Change");
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
boolean noErrors = true;
String error = "";
final String oldpass = new String(oldpassword.getPassword());
final String pass = new String(password.getPassword());
final String repass = new String(repassword.getPassword());
File file = new File("login");
if(file.exists()){
Json localJson= null;
String md5pass = "";
try {
md5pass = md5(oldpass);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE, "Couldn't get md5 hash of the password", ex);
}
try {
String strLine = "";
DataInputStream in = new DataInputStream(new FileInputStream(file));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try {
while ((strLine = br.readLine())!= null) {
break;
}
in.close();
localJson = Json.read(strLine);
} catch (IOException ex) {
Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE,"Cannot read line in the file", ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE,"File not found", ex);
}
final String id = localJson.at("id").asString();
if(!md5pass.equals(localJson.at("password").asString())){
noErrors = false;
error += "Old password dont match \n";
}
if(!(pass.equals(repass)) || pass.length() == 0){
noErrors = false;
error += "Both the password must match with each other \n";
}
if(noErrors){
Map<String,String> rdata = new HashMap<String, String>(){{put("id",id);put("oldpass",oldpass);put("newpass",pass);}};
bFetchURL url = new bFetchURL("http://binaydevkota.com/javabus/changepassword.php",rdata,"GET");
String returnedText = null;
try {
returnedText = url.content();
} catch (NullPointerException ex){
JOptionPane.showMessageDialog(null, "Sorry cannot connect to server, change password failed", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Sorry cannot connect to server, change password failed", "Error", JOptionPane.ERROR_MESSAGE);
}
Json returnedJson = Json.read(returnedText);
if(!returnedJson.at("error").isNull()){
JOptionPane.showMessageDialog(null, returnedJson.at("error").asString(), "Error", JOptionPane.ERROR_MESSAGE);
}
if(!returnedJson.at("info").isNull()){
JOptionPane.showMessageDialog(null, returnedJson.at("info").asString(), "Info", JOptionPane.INFORMATION_MESSAGE);
}
if(!returnedJson.at("success").isNull()){
File files = new File("login");
files.delete();
new cLoginWindow();
dispose();
}
}
else{
JOptionPane.showMessageDialog(null, error, "Error",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "You need to login to change password");
}
}
});
p.setLayout(new GridLayout(0,2));
p.add(new JLabel("Old Password: "));
oldpassword.setFont(new Font("Dialog", 1,20));
p.add(oldpassword);
p.add(new JLabel(""));
p.add(new JLabel(""));
p.add(new JLabel("New Password: "));
password.setFont(new Font("Dialog", 1,20));
p.add(password);
p.add(new JLabel(""));
p.add(new JLabel(""));
p.add(new JLabel("Password again: "));
repassword.setFont(new Font("Dialog", 1,20));
p.add(repassword);
p.add(new JLabel(""));
p.add(new JLabel(""));