001package jmri.jmrit.permission.swing;
002
003import java.awt.event.ActionEvent;
004
005import javax.swing.Icon;
006
007import jmri.InstanceManager;
008import jmri.PermissionManager;
009import jmri.util.swing.*;
010
011/**
012 * Let a user login to the permission manager.
013 *
014 * @author Daniel Bergqvist (C) 2024
015 */
016public class LoginAction extends JmriAbstractAction {
017
018    public LoginAction(String s, WindowInterface wi) {
019        super(s, wi);
020        checkPermission();
021    }
022
023    public LoginAction(String s, Icon i, WindowInterface wi) {
024        super(s, i, wi);
025        checkPermission();
026    }
027
028    public LoginAction() {
029        super(Bundle.getMessage("LoginAction_Title"));
030        checkPermission();
031    }
032
033    private void checkPermission() {
034        var permissionManager = InstanceManager.getDefault(PermissionManager.class);
035        if (permissionManager.isEnabled()) {
036            setEnabled(!permissionManager.isLoggedIn());
037            permissionManager.addLoginListener((isLogin) -> {
038                setEnabled(!isLogin);
039            });
040        } else {
041            setEnabled(false);
042        }
043    }
044
045    @Override
046    public void actionPerformed(ActionEvent e) {
047        new LoginDialog(null).setVisible(true);
048    }
049
050    @Override
051    public JmriPanel makePanel() {
052        throw new IllegalArgumentException("Should not be invoked");
053    }
054
055}