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 a user password. 013 * 014 * @author Daniel Bergqvist (C) 2024 015 */ 016public class ChangeUserPasswordDialog extends JDialog { 017 018 private final PermissionManager _mngr; 019 private final JPasswordField _passwordTextField; 020 private final JPasswordField _secondPasswordTextField; 021 022 023 public ChangeUserPasswordDialog( 024 PermissionManager mngr, Frame owner, User user, Runnable passwordChangedRunnable) { 025 026 super(owner, Bundle.getMessage("ChangeUserPasswordDialog_ChangePasswordTitle"), true); 027 028 this._mngr = mngr; 029 030 JPanel contentPanel = new JPanel(); 031 rootPane.getContentPane().add(contentPanel); 032 033 JPanel p = contentPanel; 034 035 p.setLayout(new java.awt.GridBagLayout()); 036 java.awt.GridBagConstraints c = new java.awt.GridBagConstraints(); 037 c.gridwidth = 1; 038 c.gridheight = 1; 039 c.gridx = 0; 040 c.gridy = 0; 041 c.anchor = java.awt.GridBagConstraints.EAST; 042 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_UserName")), c); 043 c.gridy = 1; 044 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Password")), c); 045 c.gridy = 2; 046 contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_PasswordAgain")), c); 047 048 c.gridx = 1; 049 c.gridy = 0; 050 contentPanel.add(Box.createHorizontalStrut(5), c); 051 052 c.gridx = 2; 053 c.gridy = 0; 054 c.anchor = java.awt.GridBagConstraints.WEST; 055 contentPanel.add(new JLabel(user.getUserName()), c); 056 c.gridy = 1; 057 _passwordTextField = new JPasswordField(20); 058 contentPanel.add(_passwordTextField, c); 059 c.gridy = 2; 060 _secondPasswordTextField = new JPasswordField(20); 061 contentPanel.add(_secondPasswordTextField, c); 062 063 JPanel buttonPanel = new JPanel(); 064 JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel")); // NOI18N 065 buttonPanel.add(buttonCancel); 066 buttonCancel.addActionListener((ActionEvent e) -> { 067 dispose(); 068 }); 069// cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint")); // NOI18N 070 buttonCancel.setToolTipText("CancelLogixButtonHint"); // NOI18N 071 072 // OK 073 JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK")); // NOI18N 074 buttonPanel.add(buttonOK); 075 buttonOK.addActionListener((ActionEvent e) -> { 076 if (okPressed(user)) { 077 passwordChangedRunnable.run(); 078 dispose(); 079 } 080 }); 081// cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint")); // NOI18N 082 buttonOK.setToolTipText("CancelLogixButtonHint"); // NOI18N 083 084 c.gridx = 0; 085 c.gridy = 3; 086 c.gridwidth = 2; 087 c.anchor = java.awt.GridBagConstraints.CENTER; 088 contentPanel.add(buttonPanel, c); 089 090 setLocationRelativeTo(owner); 091 pack(); 092 } 093 094 private boolean okPressed(User user) { 095 String passwd1 = new String(_passwordTextField.getPassword()); 096 String passwd2 = new String(_secondPasswordTextField.getPassword()); 097 098 if (passwd1.isBlank() && !_mngr.isAllowEmptyPasswords()) { 099 JmriJOptionPane.showMessageDialog(null, 100 Bundle.getMessage("AddUserDialog_PasswordEmpty"), 101 jmri.Application.getApplicationName(), 102 JmriJOptionPane.ERROR_MESSAGE); 103 return false; 104 } 105 106 if (!passwd1.equals(passwd1.trim())) { 107 JmriJOptionPane.showMessageDialog(null, 108 Bundle.getMessage("AddUserDialog_SpaceNotAllowedInPassword"), 109 jmri.Application.getApplicationName(), 110 JmriJOptionPane.ERROR_MESSAGE); 111 return false; 112 } 113 114 if (!passwd1.equals(passwd2)) { 115 JmriJOptionPane.showMessageDialog(null, 116 Bundle.getMessage("AddUserDialog_PasswordsAreNotEqual"), 117 jmri.Application.getApplicationName(), 118 JmriJOptionPane.ERROR_MESSAGE); 119 return false; 120 } 121 122 user.setPassword(passwd1); 123 return true; 124 } 125 126}