001package jmri.jmrix.ecos.swing.statusframe;
002
003import javax.swing.BorderFactory;
004import javax.swing.BoxLayout;
005import javax.swing.JButton;
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008import jmri.jmrix.ecos.EcosListener;
009import jmri.jmrix.ecos.EcosMessage;
010import jmri.jmrix.ecos.EcosReply;
011import jmri.jmrix.ecos.EcosSystemConnectionMemo;
012import jmri.jmrix.ecos.EcosTrafficController;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Panel to show ECoS status
018 *
019 * @author Bob Jacobsen Copyright (C) 2008
020 */
021public class StatusPanel extends jmri.jmrix.ecos.swing.EcosPanel implements EcosListener {
022
023    JPanel statusPanel = new JPanel();
024    String appString = Bundle.getMessage("ApplicationVersionLabel") + " ";
025    String proString = Bundle.getMessage("ProtocolVersionLabel") + " ";
026    String hrdString = Bundle.getMessage("HardwareVersionLabel") + " ";
027    JLabel appVersion = new JLabel(appString + Bundle.getMessage("StateUnknown"));
028    JLabel proVersion = new JLabel(proString + Bundle.getMessage("StateUnknown"));
029    JLabel hrdVersion = new JLabel(hrdString + Bundle.getMessage("StateUnknown"));
030
031    JButton sendButton;
032
033    public StatusPanel() {
034        super();
035    }
036
037    @Override
038    public void initComponents(EcosSystemConnectionMemo memo) {
039        super.initComponents(memo);
040        //memo.getTrafficController().addEcosListener(this);
041        tc = memo.getTrafficController();
042        // Create GUI
043        statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.Y_AXIS));
044        statusPanel.setBorder(BorderFactory.createEtchedBorder());
045        statusPanel.add(appVersion);
046        statusPanel.add(proVersion);
047        statusPanel.add(hrdVersion);
048        add(statusPanel);
049
050        try {
051            // connect to the TrafficManager
052            tc.addEcosListener(this);
053
054            // ask to be notified
055            EcosMessage m = new EcosMessage("request(1, view)");
056            tc.sendEcosMessage(m, this);
057
058            // get initial state
059            m = new EcosMessage("get(1, info)");
060            tc.sendEcosMessage(m, this);
061        } catch (NullPointerException npe) {
062            log.warn("Could not connect to ECoS connection {}", memo);
063        }
064        sendButton = new JButton(Bundle.getMessage("ButtonUpdate"));
065        sendButton.setVisible(true);
066        sendButton.setToolTipText(Bundle.getMessage("UpdateToolTip"));
067
068        add(sendButton);
069        sendButton.addActionListener(new java.awt.event.ActionListener() {
070            @Override
071            public void actionPerformed(java.awt.event.ActionEvent e) {
072                sendButtonActionPerformed(e);
073            }
074        });
075    }
076
077    void reset() {
078        appVersion.setText(appString + Bundle.getMessage("StateUnknown"));
079        proVersion.setText(proString + Bundle.getMessage("StateUnknown"));
080        hrdVersion.setText(hrdString + Bundle.getMessage("StateUnknown"));
081    }
082
083    // to free resources when no longer used
084    @Override
085    public void dispose() {
086        tc.removeEcosListener(this);
087        tc = null;
088    }
089
090    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
091        reset();
092        try {
093            EcosMessage m = new EcosMessage("get(1, info)");
094            tc.sendEcosMessage(m, null);
095        } catch (NullPointerException npe) {
096            log.warn("Could not connect to ECoS connection {}", memo);
097        }
098    }
099
100    EcosTrafficController tc;
101
102    // to listen for status changes from Ecos system
103    @Override
104    public void reply(EcosReply m) {
105        // power message?
106        String msg = m.toString();
107        if (msg.contains("<EVENT 1>") || msg.contains("REPLY get(1,")) {
108            if (msg.contains("info")) {
109                // probably right, extract info
110                int first;
111                int last;
112                first = msg.indexOf("ProtocolVersion[");
113                if (first > 0) {
114                    last = msg.indexOf("]", first + 16);
115                    proVersion.setText(proString + msg.substring(first + 16, last));
116                }
117                first = msg.indexOf("ApplicationVersion[");
118                if (first > 0) {
119                    last = msg.indexOf("]", first + 19);
120                    appVersion.setText(appString + msg.substring(first + 19, last));
121                }
122                first = msg.indexOf("HardwareVersion[");
123                if (first > 0) {
124                    last = msg.indexOf("]", first + 16);
125                    hrdVersion.setText(hrdString + msg.substring(first + 16, last));
126                }
127            }
128        }
129    }
130
131    @Override
132    public void message(EcosMessage m) {
133        // messages are ignored
134    }
135
136    @Override
137    public String getTitle() {
138        if (memo != null) {
139            return Bundle.getMessage("XInfoTitle", memo.getUserName());
140        }
141        return Bundle.getMessage("MenuItemInfo");
142    }
143
144    /**
145     * Nested class to create one of these using old-style defaults.
146     */
147    static public class Default extends jmri.jmrix.ecos.swing.EcosNamedPaneAction {
148
149        public Default() {
150            super(Bundle.getMessage("MenuItemInfo"),
151                    new jmri.util.swing.sdi.JmriJFrameInterface(),
152                    StatusPanel.class.getName(),
153                    jmri.InstanceManager.getDefault(EcosSystemConnectionMemo.class));
154        }
155    }
156
157    private final static Logger log = LoggerFactory.getLogger(StatusPanel.class);
158
159}