001package jmri.jmrix.nce.ncemon;
002
003import java.awt.Dimension;
004
005import jmri.jmrix.nce.*;
006import jmri.jmrix.nce.swing.NcePanelInterface;
007import jmri.util.swing.JmriJOptionPane;
008
009/**
010 * Swing action to create and register a MonFrame object
011 *
012 * @author Bob Jacobsen Copyright (C) 2001, 2008
013 * @author kcameron Copyright (C) 2011 copied from SerialMonPane.java
014 * @author Daniel Boudreau Copyright (C) 2012 added human readable format
015 */
016public class NceMonPanel extends jmri.jmrix.AbstractMonPane implements NceListener, NcePanelInterface {
017
018    public NceMonPanel() {
019        super();
020    }
021
022    @Override
023    public String getHelpTarget() {
024        return null;
025    }
026
027    @Override
028    public String getTitle() {
029        StringBuilder x = new StringBuilder();
030        if (memo != null) {
031            x.append(memo.getUserName());
032        } else {
033            x.append("NCE_");
034        }
035        x.append(": ");
036        x.append("Command Monitor");
037        return x.toString();
038    }
039
040    /**
041     * The minimum frame size for font size 16
042     */
043    @Override
044    public Dimension getMinimumDimension() {
045        return new Dimension(700, 500);
046    }
047
048    @Override
049    public void dispose() {
050        // disconnect from the NceTrafficController
051        try {
052            memo.getNceTrafficController().removeNceListener(this);
053        } catch (java.lang.NullPointerException e) {
054            log.error("Error on dispose {}", e.toString());
055        }
056        // and unwind swing
057        super.dispose();
058    }
059
060    @Override
061    public void init() {
062    }
063
064    NceSystemConnectionMemo memo;
065
066    @Override
067    public void initContext(Object context) {
068        if (context instanceof NceSystemConnectionMemo) {
069            initComponents((NceSystemConnectionMemo) context);
070        }
071    }
072
073    @Override
074    public void initComponents(NceSystemConnectionMemo memo) {
075        this.memo = memo;
076        // connect to the NceTrafficController
077        try {
078            memo.getNceTrafficController().addNceListener(this);
079        } catch (java.lang.NullPointerException e) {
080            log.error("Unable to start the NCE Command monitor"); // NOI18N
081            JmriJOptionPane.showMessageDialog(null,
082                    Bundle.getMessage("DialogMonError"),
083                    Bundle.getMessage("DialogMonErrorTitle"),
084                    JmriJOptionPane.WARNING_MESSAGE);
085        }
086    }
087
088    @Override
089    public synchronized void message(NceMessage m) {  // receive a message and log it
090        if (m.isBinary()) {
091            logMessage(m);
092        } else {
093            logMessage("cmd: ",m);
094        }
095    }
096
097    @Override
098    public synchronized void reply(NceReply r) {  // receive a reply message and log it
099        if (r.isUnsolicited()) {
100            logMessage("msg: ",r);
101        } else {
102            logMessage(r);
103        }
104    }
105
106    /**
107     * Nested class to create one of these using old-style defaults
108     */
109    static public class Default extends jmri.jmrix.nce.swing.NceNamedPaneAction {
110
111        public Default() {
112            super("Nce Command Monitor",
113                    new jmri.util.swing.sdi.JmriJFrameInterface(),
114                    NceMonPanel.class.getName(),
115                    jmri.InstanceManager.getDefault(NceSystemConnectionMemo.class));
116        }
117    }
118
119    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NceMonPanel.class);
120
121}