001package jmri.jmrix.jmriclient.swing.mon;
002
003import jmri.jmrix.jmriclient.JMRIClientListener;
004import jmri.jmrix.jmriclient.JMRIClientMessage;
005import jmri.jmrix.jmriclient.JMRIClientReply;
006import jmri.jmrix.jmriclient.JMRIClientTrafficController;
007import jmri.jmrix.jmriclient.JMRIClientSystemConnectionMemo;
008
009/**
010 * Pane displaying (and logging) JMRIClient command messages.
011 *
012 * @author Bob Jacobsen Copyright (C) 2008
013 */
014public class JMRIClientMonPane extends jmri.jmrix.AbstractMonPane implements JMRIClientListener {
015
016    protected JMRIClientTrafficController tc = null;
017
018    public JMRIClientMonPane() {
019        super();
020    }
021
022    @Override
023    public String getTitle() {
024        return Bundle.getMessage("MenuItemJmriClientCommandMonitorTitle");
025    }
026
027    @Override
028    protected void init() {
029    }
030    
031    @Override
032    public void initContext(Object context) {
033        if (context instanceof JMRIClientSystemConnectionMemo) {
034            JMRIClientSystemConnectionMemo memo = (JMRIClientSystemConnectionMemo) context;
035            // connect to TrafficController
036            tc = memo.getJMRIClientTrafficController();
037            tc.addJMRIClientListener(this);
038        }
039    }
040
041    @Override
042    public void dispose() {
043        tc.removeJMRIClientListener(this);
044        tc = null;
045        super.dispose();
046    }
047
048    @Override
049    public synchronized void message(JMRIClientMessage l) {  // receive a message and log it
050        logMessage("cmd: ",l);
051    }
052
053    @Override
054    public synchronized void reply(JMRIClientReply l) {  // receive a reply message and log it
055        logMessage("rep: ",l);
056    }
057
058    /**
059     * Nested class to create one of these using old-style defaults
060     */
061    static public class Default extends jmri.util.swing.JmriNamedPaneAction {
062
063        public Default() {
064            super(Bundle.getMessage("MenuItemJmriClientCommandMonitorTitle"), JMRIClientMonPane.class.getName());
065            setContext(jmri.InstanceManager.
066                    getDefault(JMRIClientSystemConnectionMemo.class));
067        }
068    }
069
070}