001package jmri.jmrix.ecos.swing.monitor;
002
003import jmri.jmrix.ecos.EcosListener;
004import jmri.jmrix.ecos.EcosMessage;
005import jmri.jmrix.ecos.EcosReply;
006import jmri.jmrix.ecos.EcosSystemConnectionMemo;
007import jmri.jmrix.ecos.swing.EcosPanelInterface;
008
009/**
010 * Swing action to create and register a MonFrame object
011 *
012 * @author Bob Jacobsen Copyright (C) 2001, 2008
013 */
014public class EcosMonPane extends jmri.jmrix.AbstractMonPane implements EcosListener, EcosPanelInterface {
015
016    public EcosMonPane() {
017        super();
018    }
019
020    @Override
021    public String getHelpTarget() {
022        return null;
023    }
024
025    @Override
026    public String getTitle() {
027        if (memo != null) {
028            return memo.getUserName() + " Command Monitor";
029        }
030        return "ECoS Command Monitor";
031    }
032
033    @Override
034    public void dispose() {
035        // disconnect from the ECosTrafficController
036        memo.getTrafficController().removeEcosListener(this);
037        // and unwind swing
038        super.dispose();
039    }
040
041    @Override
042    public void init() {
043    }
044
045    EcosSystemConnectionMemo memo;
046
047    @Override
048    public void initContext(Object context) {
049        if (context instanceof EcosSystemConnectionMemo) {
050            initComponents((EcosSystemConnectionMemo) context);
051        }
052    }
053
054    @Override
055    public void initComponents(EcosSystemConnectionMemo memo) {
056        this.memo = memo;
057        // connect to the LnTrafficController
058        memo.getTrafficController().addEcosListener(this);
059    }
060
061    @Override
062    public synchronized void message(EcosMessage l) {  // receive a message and log it
063        if (l.isBinary()) {
064            logMessage("binary cmd: ",l);
065        } else {
066            logMessage("cmd: ",l);
067        }
068    }
069
070    @Override
071    public synchronized void reply(EcosReply l) {  // receive a reply message and log it
072        if (l.isUnsolicited()) {
073            logMessage("msg: ",l);
074        } else {
075            logMessage("rep: ",l);
076        }
077    }
078
079    /**
080     * Nested class to create one of these using old-style defaults
081     */
082    static public class Default extends jmri.jmrix.ecos.swing.EcosNamedPaneAction {
083
084        public Default() {
085            super("ECoS Command Monitor",
086                    new jmri.util.swing.sdi.JmriJFrameInterface(),
087                    EcosMonPane.class.getName(),
088                    jmri.InstanceManager.getDefault(EcosSystemConnectionMemo.class));
089        }
090    }
091
092}