001package jmri.jmrix.nce.ncemon;
002
003import javax.swing.JOptionPane;
004import jmri.jmrix.nce.NceListener;
005import jmri.jmrix.nce.NceMessage;
006import jmri.jmrix.nce.NceReply;
007import jmri.jmrix.nce.NceSystemConnectionMemo;
008import jmri.jmrix.nce.swing.NcePanelInterface;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Swing action to create and register a MonFrame object
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2008
016 * @author kcameron Copyright (C) 2011 copied from SerialMonPane.java
017 * @author Daniel Boudreau Copyright (C) 2012 added human readable format
018 */
019public class NceMonPanel extends jmri.jmrix.AbstractMonPane implements NceListener, NcePanelInterface {
020
021    public NceMonPanel() {
022        super();
023    }
024
025    @Override
026    public String getHelpTarget() {
027        return null;
028    }
029
030    @Override
031    public String getTitle() {
032        StringBuilder x = new StringBuilder();
033        if (memo != null) {
034            x.append(memo.getUserName());
035        } else {
036            x.append("NCE_");
037        }
038        x.append(": ");
039        x.append("Command Monitor");
040        return x.toString();
041    }
042
043    @Override
044    public void dispose() {
045        // disconnect from the NceTrafficController
046        try {
047            memo.getNceTrafficController().removeNceListener(this);
048        } catch (java.lang.NullPointerException e) {
049            log.error("Error on dispose {}", e.toString());
050        }
051        // and unwind swing
052        super.dispose();
053    }
054
055    @Override
056    public void init() {
057    }
058
059    NceSystemConnectionMemo memo;
060
061    @Override
062    public void initContext(Object context) {
063        if (context instanceof NceSystemConnectionMemo) {
064            initComponents((NceSystemConnectionMemo) context);
065        }
066    }
067
068    @Override
069    public void initComponents(NceSystemConnectionMemo memo) {
070        this.memo = memo;
071        // connect to the NceTrafficController
072        try {
073            memo.getNceTrafficController().addNceListener(this);
074        } catch (java.lang.NullPointerException e) {
075            log.error("Unable to start the NCE Command monitor");
076            JOptionPane.showMessageDialog(null, "An Error has occurred that prevents the NCE Command Monitor from being loaded.\nPlease check the System Console for more information", "No Connection", JOptionPane.WARNING_MESSAGE);
077        }
078    }
079
080    @Override
081    public synchronized void message(NceMessage m) {  // receive a message and log it
082        if (m.isBinary()) {
083            logMessage(m);
084        } else {
085            logMessage("cmd: ",m);
086        }
087    }
088
089    @Override
090    public synchronized void reply(NceReply r) {  // receive a reply message and log it
091        if (r.isUnsolicited()) {
092            logMessage("msg: ",r);
093        } else {
094            logMessage(r);
095        }
096    }
097
098    /**
099     * Nested class to create one of these using old-style defaults
100     */
101    static public class Default extends jmri.jmrix.nce.swing.NceNamedPaneAction {
102
103        public Default() {
104            super("Nce Command Monitor",
105                    new jmri.util.swing.sdi.JmriJFrameInterface(),
106                    NceMonPanel.class.getName(),
107                    jmri.InstanceManager.getDefault(NceSystemConnectionMemo.class));
108        }
109    }
110
111    private final static Logger log = LoggerFactory.getLogger(NceMonPanel.class);
112
113}