001package jmri.jmrit.permission.swing;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import java.util.Objects;
006
007import javax.swing.*;
008
009import jmri.*;
010import jmri.util.swing.JmriJOptionPane;
011
012/**
013 * Dialog for user login.
014 *
015 * @author Daniel Bergqvist (C) 2024
016 */
017public class LoginDialog extends JDialog {
018
019    private final JTextField _usernameTextField;
020    private final JPasswordField _passwordTextField;
021
022
023    public LoginDialog(Frame owner) {
024        super(owner, Bundle.getMessage("LoginAction_Title"), true);
025
026        JPanel contentPanel = new JPanel();
027        rootPane.getContentPane().add(contentPanel);
028
029        JPanel p = contentPanel;
030
031        p.setLayout(new java.awt.GridBagLayout());
032        java.awt.GridBagConstraints c = new java.awt.GridBagConstraints();
033        c.gridwidth = 1;
034        c.gridheight = 1;
035        c.gridx = 0;
036        c.gridy = 0;
037        c.anchor = java.awt.GridBagConstraints.EAST;
038        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_UserName")), c);
039        c.gridy = 1;
040        contentPanel.add(new JLabel(Bundle.getMessage("AddUserDialog_Password")), c);
041
042        c.gridx = 1;
043        c.gridy = 0;
044        contentPanel.add(Box.createHorizontalStrut(5), c);
045
046        c.gridx = 2;
047        c.gridy = 0;
048        c.anchor = java.awt.GridBagConstraints.WEST;
049        _usernameTextField = new JTextField(20);
050        contentPanel.add(_usernameTextField, c);
051        c.gridy = 1;
052        _passwordTextField = new JPasswordField(20);
053        contentPanel.add(_passwordTextField, c);
054
055        JPanel buttonPanel = new JPanel();
056        JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel"));    // NOI18N
057        buttonPanel.add(buttonCancel);
058        buttonCancel.addActionListener((ActionEvent e) -> {
059            dispose();
060        });
061        //buttonCancel.setToolTipText(Bundle.getMessage("LoginCancelButtonHint");      // NOI18N
062
063        // OK
064        JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK"));    // NOI18N
065        buttonPanel.add(buttonOK);
066        buttonOK.addActionListener((ActionEvent e) -> {
067            if (Objects.equals(_usernameTextField.getText(),
068                    Bundle.getMessage("PermissionManager_User_Guest").toLowerCase())) {
069                // default guest user does log in
070                JmriJOptionPane.showMessageDialog(null,
071                        Bundle.getMessage("LoginAction_GuestMessage"),
072                        jmri.Application.getApplicationName(),
073                        JmriJOptionPane.ERROR_MESSAGE);
074            } else {
075                String password = new String(_passwordTextField.getPassword());
076                if (InstanceManager.getDefault(PermissionManager.class).login(_usernameTextField.getText(), password)) {
077
078                    JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("LoginAction_UserLoggedIn"), jmri.Application.getApplicationName(), JmriJOptionPane.INFORMATION_MESSAGE);
079                    dispose();
080                }
081            }
082        });
083        //buttonOK.setToolTipText(Bundle.getMessage("LoginOkButtonHint");      // NOI18N
084        getRootPane().setDefaultButton(buttonOK);
085
086        c.gridx = 0;
087        c.gridy = 2;
088        c.gridwidth = 2;
089        c.anchor = java.awt.GridBagConstraints.CENTER;
090        contentPanel.add(buttonPanel, c);
091
092        setLocationRelativeTo(owner);
093        pack();
094    }
095
096}