001package jmri.jmrix.bidib.netbidib;
002
003import java.awt.event.ActionEvent;
004import javax.swing.AbstractAction;
005
006import jmri.jmrix.bidib.BiDiBSystemConnectionMemo;
007import jmri.jmrix.bidib.BiDiBTrafficController;
008
009/**
010 * This class is generated by the BiDiB Menu and used for actions from the menu.
011 * The real work is delegated to the traffic controller.
012 *
013 * @author Eckart Meyer Copyright (C) 2024
014 */
015public class NetBiDiBLogonAction extends AbstractAction {
016    
017    //final java.util.ResourceBundle rb = java.util.ResourceBundle.getBundle("jmri.jmrix.bidib.swing.BiDiBSwingBundle"); // NOI18N
018
019    private String logon;
020    private String logoff;
021    private BiDiBTrafficController tc;
022
023    public NetBiDiBLogonAction() {
024        log.warn("no connection");
025    }
026
027
028    @SuppressWarnings("LeakingThisInConstructor")
029    public NetBiDiBLogonAction(BiDiBSystemConnectionMemo memo, String logon, String logoff) {
030        super(logoff);
031        this.logon = logon;
032        this.logoff = logoff;
033        this.tc = memo.getBiDiBTrafficController();
034        log.debug("create NetBiDiBLogonAction for {} - \"{}\" \"{}\"", memo.getUserName(), logon, logoff);
035        if (tc != null) {
036            tc.addConnectionChangedListener(this); //also listens on connecttion change events
037            setEnabled(tc.getBidib().isOpened());
038            putValue(javax.swing.Action.NAME, tc.isDetached() ? logon : logoff);
039        }
040    }
041
042    /**
043     * Implementing ActionListener
044     * 
045     * Called when:
046     * - the menu entry was activated
047     * - the traffic controller signals a change of the connection
048     * 
049     * @param e - Action event
050     */
051    @Override
052    public void actionPerformed(ActionEvent e) {
053        log.debug("NetBiDiBLogonAction actionPerformed {}", e.paramString());
054        if (tc != null  &&  tc.isNetBiDiB()  &&  tc.getBidib() != null) {
055            if (tc.getBidib().isOpened()) {
056                if (e.getActionCommand().equals(logon)) {
057                    log.info("local node logon requested from menu");
058                    tc.setLogon(true);
059                }
060                else if (e.getActionCommand().equals(logoff)) {
061                    log.info("local node logoff requested from menu");
062                    tc.setLogon(false);
063                }
064                else {
065                    log.info("connection changed: {}", e.getActionCommand());
066                }
067                setEnabled(true);
068                putValue(javax.swing.Action.NAME, tc.isDetached() ? logon : logoff);
069            }
070            else {
071                setEnabled(false);                
072                putValue(javax.swing.Action.NAME, logoff);
073            }
074        }
075    }
076    
077    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NetBiDiBLogonAction.class);
078
079}