001package jmri.jmrix.dcc4pc.swing;
002
003import javax.swing.BoxLayout;
004import javax.swing.JButton;
005import javax.swing.JLabel;
006import jmri.jmrix.dcc4pc.Dcc4PcListener;
007import jmri.jmrix.dcc4pc.Dcc4PcMessage;
008import jmri.jmrix.dcc4pc.Dcc4PcReply;
009import jmri.jmrix.dcc4pc.Dcc4PcSystemConnectionMemo;
010import jmri.jmrix.dcc4pc.Dcc4PcTrafficController;
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014/**
015 * Panel to show DCC4PC status
016 *
017 * @author Bob Jacobsen Copyright (C) 2008
018 */
019public class StatusPanel extends jmri.jmrix.dcc4pc.swing.Dcc4PcPanel implements Dcc4PcListener, Dcc4PcPanelInterface {
020
021    String appString = "Info        : ";
022    String proString = "Description : ";
023    String hrdString = "Serial No   : ";
024    JLabel infoText = new JLabel(appString + "<unknown>");
025    JLabel infoDescription = new JLabel(proString + "<unknown>");
026    JLabel serialNo = new JLabel(hrdString + "<unknown>");
027
028    JButton sendButton;
029
030    public StatusPanel() {
031        super();
032    }
033
034    /**
035     * {@inheritDoc}
036     */
037    @Override
038    public void initComponents(Dcc4PcSystemConnectionMemo memo) {
039        super.initComponents(memo);
040        tc = memo.getDcc4PcTrafficController();
041        // Create GUI
042        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
043        add(infoText);
044        add(infoDescription);
045        add(serialNo);
046
047        // ask to be notified
048        Dcc4PcMessage m = new jmri.jmrix.dcc4pc.Dcc4PcMessage(new byte[]{(byte) 0x00});
049        nextPacket = 0x00;
050        if(tc!=null){
051            tc.sendDcc4PcMessage(m, this);
052        } else {
053            log.error("no Traffic Controller Found");
054        }
055
056        sendButton = new JButton("Update");
057        sendButton.setVisible(true);
058        sendButton.setToolTipText("Request status update from DCC4PC");
059
060        add(sendButton);
061        sendButton.addActionListener(new java.awt.event.ActionListener() {
062            @Override
063            public void actionPerformed(java.awt.event.ActionEvent e) {
064                sendButtonActionPerformed(e);
065            }
066        });
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public void initComponents() {
074    }
075
076    void reset() {
077        infoText.setText(appString + "<unknown>");
078        infoDescription.setText(proString + "<unknown>");
079        serialNo.setText(hrdString + "<unknown>");
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public void dispose() {
087        if(tc!=null){
088            tc.removeDcc4PcListener(this);
089            tc = null;
090        }
091    }
092
093    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
094        if(tc!=null){
095            reset();
096            Dcc4PcMessage m = new jmri.jmrix.dcc4pc.Dcc4PcMessage(new byte[]{(byte) 0x00});
097            nextPacket = 0x00;
098            tc.sendDcc4PcMessage(m, this);
099        }
100    }
101
102    Dcc4PcTrafficController tc;
103
104    public void notifyReply(Dcc4PcReply r) {
105    }
106
107    public void notifyMessage(Dcc4PcMessage m) {
108    }
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public void reply(Dcc4PcReply r) {
115        // power message?
116        switch (nextPacket) {
117            case 0x00: {
118                nextPacket = -1;
119                int i = 0;
120                StringBuffer buf = new StringBuffer();
121                while (i < 4) {
122                    buf.append((char) r.getElement(i));
123                    i++;
124                }
125                //skip supported speeds for now
126                String str = buf.toString();
127                i = i + 2;
128                str = str + " ver ";
129                str = str + r.getElement(i) + ".";
130                i++;
131                str = str + r.getElement(i) + " Max Bus Speed : ";
132                i++;
133                str = str + r.getElement(i);
134                infoText.setText(appString + str);
135                i++;
136                jmri.jmrix.dcc4pc.Dcc4PcMessage m = new jmri.jmrix.dcc4pc.Dcc4PcMessage(new byte[]{(byte) 0x01});
137                nextPacket = 0x01;
138                tc.sendDcc4PcMessage(m, this);
139                break;
140            }
141            case 0x01: {
142
143                infoDescription.setText(proString + r.toString());
144                jmri.jmrix.dcc4pc.Dcc4PcMessage m = new jmri.jmrix.dcc4pc.Dcc4PcMessage(new byte[]{(byte) 0x02});
145                nextPacket = 0x02;
146                tc.sendDcc4PcMessage(m, this);
147                break;
148            }
149            case 0x02: {
150                nextPacket = -1;
151                serialNo.setText(hrdString + r.toString());
152                break;
153            }
154            default:
155                break;
156        }
157    }
158
159    int nextPacket = -1;
160
161    /**
162     * {@inheritDoc}
163     */
164    @Override
165    public void message(Dcc4PcMessage m) {
166        byte[] theByteArray = m.getFormattedMessage();
167        if (theByteArray[0] == 0x00) {
168            nextPacket = 0x00;
169        } else if (theByteArray[0] == 0x01) {
170            nextPacket = 0x01;
171        } else if (theByteArray[0] == 0x02) {
172            nextPacket = 0x02;
173        }
174    }
175
176    /**
177     * {@inheritDoc}
178     */
179    @Override
180    public void handleTimeout(Dcc4PcMessage m) {
181    }
182    
183    private final static Logger log = LoggerFactory.getLogger(StatusPanel.class);
184}