001package jmri.jmrix.can.swing.monitor;
002
003import jmri.jmrix.can.CanListener;
004import jmri.jmrix.can.CanMessage;
005import jmri.jmrix.can.CanReply;
006import jmri.jmrix.can.CanSystemConnectionMemo;
007import jmri.jmrix.can.swing.CanPanelInterface;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Frame displaying (and logging) CAN frames
013 *
014 * @author Bob Jacobsen Copyright (C) 2009
015 */
016public class MonitorPane extends jmri.jmrix.AbstractMonPane implements CanListener, CanPanelInterface {
017
018    public MonitorPane() {
019        super();
020    }
021
022    CanSystemConnectionMemo memo;
023
024    @Override
025    public void initContext(Object context) {
026        if (context instanceof CanSystemConnectionMemo) {
027            initComponents((CanSystemConnectionMemo) context);
028        }
029    }
030
031    @Override
032    public void initComponents(CanSystemConnectionMemo memo) {
033        this.memo = memo;
034
035        memo.getTrafficController().addCanConsoleListener(this);
036        try {
037            initComponents();
038        } catch (Exception e) {
039            log.error("Cannot initComponents, {}", e.toString());
040        }
041    }
042
043    @Override
044    public String getTitle() {
045        return Bundle.getMessage("CanMonitorTitle");
046    }
047
048    @Override
049    public void init() {
050    }
051
052    @Override
053    public synchronized void message(CanMessage l) {  // receive a message and log it
054        if (log.isDebugEnabled()) {
055            log.debug("Message: {}", l.toString());
056        }
057        logMessage("M: ",l);
058    }
059
060    @Override
061    public synchronized void reply(CanReply l) {  // receive a reply and log it
062        if (log.isDebugEnabled()) {
063            log.debug("Reply: {}", l.toString());
064        }
065        logMessage("R: ",l);
066    }
067
068    @Override
069    public void dispose() {
070        // disconnect from the LnTrafficController
071        memo.getTrafficController().removeCanListener(this);
072        // and unwind swing
073        super.dispose();
074    }
075
076    /**
077     * Nested class to create one of these using old-style defaults
078     */
079    static public class Default extends jmri.jmrix.can.swing.CanNamedPaneAction {
080
081        public Default() {
082            super(Bundle.getMessage("CanMonitorTitle"),
083                    new jmri.util.swing.sdi.JmriJFrameInterface(),
084                    MonitorPane.class.getName(),
085                    jmri.InstanceManager.getDefault(CanSystemConnectionMemo.class));
086        }
087    }
088
089    private final static Logger log = LoggerFactory.getLogger(MonitorPane.class);
090
091}