001package jmri.jmrix.dcc4pc.swing.monitor;
002
003import jmri.jmrix.dcc4pc.Dcc4PcListener;
004import jmri.jmrix.dcc4pc.Dcc4PcMessage;
005import jmri.jmrix.dcc4pc.Dcc4PcReply;
006import jmri.jmrix.dcc4pc.Dcc4PcSystemConnectionMemo;
007import jmri.jmrix.dcc4pc.swing.Dcc4PcPanelInterface;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Swing action to create and register a MonFrame object
013 *
014 * @author Bob Jacobsen Copyright (C) 2001, 2008
015 */
016public class Dcc4PcMonPane extends jmri.jmrix.AbstractMonPane implements Dcc4PcListener, Dcc4PcPanelInterface {
017
018    public Dcc4PcMonPane() {
019        super();
020    }
021
022    @Override
023    public String getHelpTarget() {
024        return null;
025    }
026
027    @Override
028    public String getTitle() {
029        return "Dcc4PC Command Monitor";
030    }
031
032    @Override
033    public void dispose() {
034        // disconnect from the DCC4PCTrafficController
035        if(memo.getDcc4PcTrafficController()!=null){
036            memo.getDcc4PcTrafficController().removeDcc4PcListener(this);
037        }
038        // and unwind swing
039        super.dispose();
040    }
041
042    @Override
043    public void init() {
044    }
045
046    Dcc4PcSystemConnectionMemo memo;
047
048    @Override
049    public void initContext(Object context) {
050        if (context instanceof Dcc4PcSystemConnectionMemo) {
051            initComponents((Dcc4PcSystemConnectionMemo) context);
052        }
053    }
054
055    @Override
056    public void initComponents(Dcc4PcSystemConnectionMemo memo) {
057        this.memo = memo;
058        // connect to the DCC4PCTrafficController
059        if(memo.getDcc4PcTrafficController()!=null){
060            memo.getDcc4PcTrafficController().addDcc4PcListener(this);
061        } else {
062            log.error("Connection has not been initiallised");
063        }
064    }
065
066    @Override
067    public synchronized void message(Dcc4PcMessage l) {  // receive a message and log it
068        if (l.isBinary()) {
069            logMessage("Binary cmd: ", l);
070        } else {
071            logMessage("cmd: ",l);
072        }
073    }
074
075    @Override
076    public synchronized void reply(Dcc4PcReply l) {  // receive a reply message and log it
077        if (l.isUnsolicited()) {
078            logMessage("msg: ",l);
079        } else {
080            logMessage("rep: ",l);
081        }
082    }
083
084    public synchronized void notifyMessage(Dcc4PcMessage l) {  // receive a message and log it
085        if (l.isBinary()) {
086            logMessage("binary cmd: ",l);
087        } else {
088            logMessage("cmd: ",l);
089        }
090    }
091
092    public synchronized void notifyReply(Dcc4PcReply l) {  // receive a reply message and log it
093        if (l.isUnsolicited()) {
094            logMessage("msg: ",l);
095        } else {
096            logMessage("rep: ",l);
097        }
098    }
099
100    /**
101     * Nested class to create one of these using old-style defaults
102     */
103    static public class Default extends jmri.jmrix.dcc4pc.swing.Dcc4PcNamedPaneAction {
104
105        public Default() {
106            super("Dcc4PC Command Monitor",
107                    new jmri.util.swing.sdi.JmriJFrameInterface(),
108                    Dcc4PcMonPane.class.getName(),
109                    jmri.InstanceManager.getDefault(Dcc4PcSystemConnectionMemo.class));
110        }
111    }
112
113    @Override
114    public void handleTimeout(Dcc4PcMessage m) {
115        log.info("timeout received to our last message {}", m.toString());
116    }
117
118    private final static Logger log = LoggerFactory.getLogger(Dcc4PcMonPane.class);
119
120}
121
122
123