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