001package jmri.jmrix.lenz.swing.mon;
002
003import jmri.jmrix.lenz.XNetListener;
004import jmri.jmrix.lenz.XNetMessage;
005import jmri.jmrix.lenz.XNetReply;
006import jmri.jmrix.lenz.XNetSystemConnectionMemo;
007import jmri.jmrix.lenz.XNetTrafficController;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Panel displaying (and logging) XpressNet messages derived from XNetMonFrame.
013 *
014 * @author Bob Jacobsen Copyright (C) 2002
015 * @author Paul Bender Copyright (C) 2004-2014
016 * @author Giorgio Terdina Copyright (C) 2007
017 */
018public class XNetMonPane extends jmri.jmrix.AbstractMonPane implements XNetListener {
019
020    protected XNetTrafficController tc = null;
021    protected XNetSystemConnectionMemo memo = null;
022
023    @Override
024    public String getTitle() {
025        return (Bundle.getMessage("MenuItemXNetCommandMonitor"));
026    }
027
028    @Override
029    public void initContext(Object context) {
030        if (context instanceof XNetSystemConnectionMemo) {
031            memo = (XNetSystemConnectionMemo) context;
032            tc = memo.getXNetTrafficController();
033            // connect to the TrafficController
034            tc.addXNetListener(~0, this);
035        }
036    }
037
038    /**
039     * Initialize the data source.
040     */
041    @Override
042    protected void init() {
043    }
044
045    @Override
046    public void dispose() {
047        // disconnect from the LnTrafficController
048        tc.removeXNetListener(~0, this);
049        // and unwind swing
050        super.dispose();
051    }
052
053    @Override
054    public synchronized void message(XNetReply l) { // receive an XpressNet message and log it
055        logMessage("","packet:",l);
056    }
057
058    /**
059     * Listen for the messages to the LI100/LI101
060     */
061    @Override
062    public synchronized void message(XNetMessage l) {
063        logMessage("", "packet:", l);
064    }
065
066    /**
067     * Handle a timeout notification
068     */
069    @Override
070    public void notifyTimeout(XNetMessage msg) {
071        log.debug("Notified of timeout on message {}", msg.toString());
072    }
073
074    /**
075     * Nested class to create one of these using old-style defaults
076     */
077    static public class Default extends jmri.util.swing.JmriNamedPaneAction {
078
079        public Default() {
080            super(Bundle.getMessage("MenuItemXNetCommandMonitor"), XNetMonPane.class.getName());
081            setContext(jmri.InstanceManager.
082                    getDefault(XNetSystemConnectionMemo.class));
083        }
084    }
085
086    private static final Logger log = LoggerFactory.getLogger(XNetMonPane.class);
087
088}