001package jmri.jmrix.bidib.tcpserver;
002
003import java.awt.event.ActionEvent;
004import javax.swing.AbstractAction;
005import jmri.jmrix.bidib.BiDiBSystemConnectionMemo;
006//import jmri.jmrix.bidib.swing;
007
008/**
009 * This class implements the GUI releated functions for the TCP server.
010 * It is generated by the BiDiB Menu and used for actions from the menu.
011 * The real work is delegated to the TcpSerer.
012 *
013 * @author Alex Shepherd Copyright (C) 2006
014 * @author Mark Underwood Copyright (C) 2015
015 * @author Eckart Meyer Copyright (C) 2023-2024
016 */
017public class TcpServerAction extends AbstractAction {
018    
019    final java.util.ResourceBundle rb = java.util.ResourceBundle.getBundle("jmri.jmrix.bidib.swing.BiDiBSwingBundle"); // NOI18N
020
021    private String enable;
022    private String disable;
023    private TcpServer server = null;
024
025    public TcpServerAction() {
026        log.warn("TcpServer cannot be started on startup because upstream connection is not known. Use the BiDiB connection type menu instead.");
027    }
028
029
030    public TcpServerAction(BiDiBSystemConnectionMemo memo, String enable, String disable) {
031        super(enable);
032        this.enable = enable;
033        this.disable = disable;
034        log.debug("create TcpServerAction for {} - \"{}\" \"{}\"", memo.getUserName(), enable, disable);
035        // Get a server instance to cause the config to be read and the server
036        // started if necessary
037        //TcpServer server = InstanceManager.getDefault(TcpServer.class);
038        server = new TcpServer(memo);
039        log.debug("TCP Server is enabled by default: {}", server.isEnabled());
040        putValue(javax.swing.Action.NAME, server.isEnabled() ? disable : enable);
041    }
042
043    @Override
044    public void actionPerformed(ActionEvent e) {
045        log.debug("TcpServerAction actionPerformed {}", e);
046        //TcpServer server = InstanceManager.getDefault(TcpServer.class);
047        if (server != null) {
048            log.trace("TcpServer: {} for {}, enabled: {}", server, server.getSystemConnectionMemo().getUserName(), server.isEnabled());
049            String text = (String)getValue(javax.swing.Action.NAME);
050            log.trace("menu text was: {}", text);
051            // toggle state of TCPserver
052            if (server.isEnabled()) {
053                server.disable();
054            }
055            else {
056                server.enable();
057            }
058            putValue(javax.swing.Action.NAME, server.isEnabled() ? disable : enable);
059//            ServerFrame f = InstanceManager.getDefault(ServerFrame.class);
060//            f.setVisible(true);
061        }
062        else {
063            log.debug("TcpServer is not created.");
064        }
065    }
066    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TcpServerAction.class);
067
068}