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 logout from the permission manager.
013 *
014 * @author Daniel Bergqvist (C) 2024
015 */
016public class LogoutAction extends JmriAbstractAction {
017
018    public LogoutAction(String s, WindowInterface wi) {
019        super(s, wi);
020        checkPermission();
021    }
022
023    public LogoutAction(String s, Icon i, WindowInterface wi) {
024        super(s, i, wi);
025        checkPermission();
026    }
027
028    public LogoutAction() {
029        super(Bundle.getMessage("LogoutAction_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        InstanceManager.getDefault(PermissionManager.class).logout();
048
049        JmriJOptionPane.showMessageDialog(null,
050                Bundle.getMessage("LogoutAction_UserLoggedOut"),
051                jmri.Application.getApplicationName(),
052                JmriJOptionPane.INFORMATION_MESSAGE);
053    }
054
055    @Override
056    public JmriPanel makePanel() {
057        throw new IllegalArgumentException("Should not be invoked");
058    }
059
060}