001package jmri.jmrit.permission.swing; 002 003import java.awt.Frame; 004import java.awt.event.ActionEvent; 005 006import javax.swing.*; 007 008import jmri.*; 009import jmri.util.swing.JmriJOptionPane; 010 011/** 012 * Dialog to change the user's own password. 013 * 014 * @author Daniel Bergqvist (C) 2024 015 */ 016public class ChangePasswordDialog extends JDialog { 017 018 private final JPasswordField _oldPasswordTextField; 019 private final JPasswordField _passwordTextField; 020 private final JPasswordField _secondPasswordTextField; 021 022 023 public ChangePasswordDialog() { 024 super((Frame)null, Bundle.getMessage("ChangePasswordDialog_Title", 025 InstanceManager.getDefault(PermissionManager.class).getCurrentUserName()), true); 026 027 JPanel contentPanel = new JPanel(); 028 rootPane.getContentPane().add(contentPanel); 029 030 JPanel p = contentPanel; 031 032 p.setLayout(new java.awt.GridBagLayout()); 033 java.awt.GridBagConstraints c = new java.awt.GridBagConstraints(); 034 c.gridwidth = 1; 035 c.gridheight = 1; 036 c.gridx = 0; 037 c.gridy = 0; 038 c.anchor = java.awt.GridBagConstraints.EAST; 039 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_UserName")), c); 040 c.gridy = 1; 041 contentPanel.add(new JLabel(Bundle.getMessage("ChangePasswordDialog_OldPassword")), c); 042 c.gridy = 2; 043 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Password")), c); 044 c.gridy = 3; 045 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_PasswordAgain")), c); 046 047 c.gridx = 1; 048 c.gridy = 0; 049 contentPanel.add(Box.createHorizontalStrut(5), c); 050 051 c.gridx = 2; 052 c.gridy = 0; 053 c.anchor = java.awt.GridBagConstraints.WEST; 054 contentPanel.add(new JLabel(InstanceManager 055 .getDefault(PermissionManager.class).getCurrentUserName()), c); 056 c.gridy = 1; 057 _oldPasswordTextField = new JPasswordField(20); 058 contentPanel.add(_oldPasswordTextField, c); 059 c.gridy = 2; 060 _passwordTextField = new JPasswordField(20); 061 contentPanel.add(_passwordTextField, c); 062 c.gridy = 3; 063 _secondPasswordTextField = new JPasswordField(20); 064 contentPanel.add(_secondPasswordTextField, c); 065 066 JPanel buttonPanel = new JPanel(); 067 JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel")); // NOI18N 068 buttonPanel.add(buttonCancel); 069 buttonCancel.addActionListener((ActionEvent e) -> { 070 dispose(); 071 }); 072// cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint")); // NOI18N 073 buttonCancel.setToolTipText("CancelLogixButtonHint"); // NOI18N 074 075 // OK 076 JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK")); // NOI18N 077 buttonPanel.add(buttonOK); 078 buttonOK.addActionListener((ActionEvent e) -> { 079 if (okPressed()) { 080 dispose(); 081 } 082 }); 083// cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint")); // NOI18N 084 buttonOK.setToolTipText("CancelLogixButtonHint"); // NOI18N 085 086 c.gridx = 0; 087 c.gridy = 4; 088 c.gridwidth = 2; 089 c.anchor = java.awt.GridBagConstraints.CENTER; 090 contentPanel.add(buttonPanel, c); 091 092 setLocationRelativeTo(null); 093 pack(); 094 } 095 096 private boolean okPressed() { 097 PermissionManager mngr = InstanceManager.getDefault(PermissionManager.class); 098 099 String oldPasswd = new String(_oldPasswordTextField.getPassword()); 100 String passwd1 = new String(_passwordTextField.getPassword()); 101 String passwd2 = new String(_secondPasswordTextField.getPassword()); 102 103 if (passwd1.isBlank() && !mngr.isAllowEmptyPasswords()) { 104 JmriJOptionPane.showMessageDialog(null, 105 Bundle.getMessage("AddUserDialog_PasswordEmpty"), 106 jmri.Application.getApplicationName(), 107 JmriJOptionPane.ERROR_MESSAGE); 108 return false; 109 } 110 111 if (!passwd1.equals(passwd1.trim())) { 112 JmriJOptionPane.showMessageDialog(null, 113 Bundle.getMessage("AddUserDialog_SpaceNotAllowedInPassword"), 114 jmri.Application.getApplicationName(), 115 JmriJOptionPane.ERROR_MESSAGE); 116 return false; 117 } 118 119 if (!passwd1.equals(passwd2)) { 120 JmriJOptionPane.showMessageDialog(null, 121 Bundle.getMessage("AddUserDialog_PasswordsAreNotEqual"), 122 jmri.Application.getApplicationName(), 123 JmriJOptionPane.ERROR_MESSAGE); 124 return false; 125 } 126 127 InstanceManager.getDefault(PermissionManager.class).changePassword(oldPasswd, passwd1); 128 return true; 129 } 130 131}