001package jmri.jmrix.marklin.swing.monitor;
002
003import jmri.jmrix.marklin.MarklinListener;
004import jmri.jmrix.marklin.MarklinMessage;
005import jmri.jmrix.marklin.MarklinReply;
006import jmri.jmrix.marklin.MarklinSystemConnectionMemo;
007import jmri.jmrix.marklin.swing.MarklinPanelInterface;
008
009/**
010 * Swing action to create and register a MonFrame object
011 *
012 * @author Bob Jacobsen Copyright (C) 2001, 2008
013 */
014public class MarklinMonPane extends jmri.jmrix.AbstractMonPane implements MarklinListener, MarklinPanelInterface {
015
016    public MarklinMonPane() {
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 Bundle.getMessage("MonitorXTitle", memo.getUserName());
029        }
030        return Bundle.getMessage("MarklinMonitorTitle");
031    }
032
033    @Override
034    public void dispose() {
035        // disconnect from the LnTrafficController
036        memo.getTrafficController().removeMarklinListener(this);
037        // and unwind swing
038        super.dispose();
039    }
040
041    @Override
042    public void init() {
043    }
044
045    MarklinSystemConnectionMemo memo;
046
047    @Override
048    public void initContext(Object context) {
049        if (context instanceof MarklinSystemConnectionMemo) {
050            initComponents((MarklinSystemConnectionMemo) context);
051        }
052    }
053
054    @Override
055    public void initComponents(MarklinSystemConnectionMemo memo) {
056        this.memo = memo;
057        // connect to the MarklinTrafficController
058        memo.getTrafficController().addMarklinListener(this);
059    }
060
061    @Override
062    public synchronized void message(MarklinMessage 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(MarklinReply 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.marklin.swing.MarklinNamedPaneAction {
083
084        public Default() {
085            super(Bundle.getMessage("MarklinMonitorTitle"),
086                    new jmri.util.swing.sdi.JmriJFrameInterface(),
087                    MarklinMonPane.class.getName(),
088                    jmri.InstanceManager.getDefault(MarklinSystemConnectionMemo.class));
089        }
090    }
091
092}