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 for user login.
013 *
014 * @author Daniel Bergqvist (C) 2024
015 */
016public class LoginDialog extends JDialog {
017
018    private final JTextField _usernameTextField;
019    private final JPasswordField _passwordTextField;
020
021
022    public LoginDialog(Frame owner) {
023        super(owner, Bundle.getMessage("LoginAction_Title"), 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
041        c.gridx = 1;
042        c.gridy = 0;
043        contentPanel.add(Box.createHorizontalStrut(5), c);
044
045        c.gridx = 2;
046        c.gridy = 0;
047        c.anchor = java.awt.GridBagConstraints.WEST;
048        _usernameTextField = new JTextField(20);
049        contentPanel.add(_usernameTextField, c);
050        c.gridy = 1;
051        _passwordTextField = new JPasswordField(20);
052        contentPanel.add(_passwordTextField, c);
053
054        // Cancel
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
062        // OK
063        JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK"));    // NOI18N
064        buttonPanel.add(buttonOK);
065        buttonOK.addActionListener((ActionEvent e) -> {
066            PermissionManager mngr = InstanceManager.getDefault(PermissionManager.class);
067            if (mngr.isAGuestUser(_usernameTextField.getText())) {
068                // default guest user does log in
069                JmriJOptionPane.showMessageDialog(null,
070                        Bundle.getMessage("LoginAction_GuestMessage"),
071                        jmri.Application.getApplicationName(),
072                        JmriJOptionPane.ERROR_MESSAGE);
073            } else {
074                String password = new String(_passwordTextField.getPassword());
075                if (mngr.login(_usernameTextField.getText(), password)) {
076
077                    JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("LoginAction_UserLoggedIn"), jmri.Application.getApplicationName(), JmriJOptionPane.INFORMATION_MESSAGE);
078                    dispose();
079                }
080            }
081        });
082        buttonOK.setToolTipText(Bundle.getMessage("LoginOkButtonHint"));      // NOI18N
083        getRootPane().setDefaultButton(buttonOK);
084
085        c.gridx = 0;
086        c.gridy = 2;
087        c.gridwidth = 2;
088        c.anchor = java.awt.GridBagConstraints.CENTER;
089        contentPanel.add(buttonPanel, c);
090
091        setLocationRelativeTo(owner);
092        pack();
093    }
094
095}