001package jmri.jmrix.cmri.serial.packetgen;
002
003import java.awt.Dimension;
004import java.awt.FlowLayout;
005import javax.swing.BoxLayout;
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008import javax.swing.JSeparator;
009import jmri.jmrix.cmri.CMRISystemConnectionMemo;
010import jmri.jmrix.cmri.serial.SerialMessage;
011import jmri.jmrix.cmri.serial.SerialReply;
012import jmri.util.StringUtil;
013
014
015/**
016 * Frame for user input of CMRI serial messages
017 *
018 * @author Bob Jacobsen Copyright (C) 2002, 2003
019 */
020public class SerialPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.cmri.serial.SerialListener {
021
022    // member declarations
023    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
024    javax.swing.JButton sendButton = new javax.swing.JButton();
025    javax.swing.JTextField packetTextField = new javax.swing.JTextField(12);
026
027    javax.swing.JButton pollButton = new javax.swing.JButton();
028    javax.swing.JTextField uaAddrField = new javax.swing.JTextField(5);
029
030    private CMRISystemConnectionMemo _memo = null;
031
032    public SerialPacketGenFrame(CMRISystemConnectionMemo memo) {
033        super();
034        _memo = memo;
035    }
036
037    /**
038     * {@inheritDoc}
039     */
040    @Override
041    public void initComponents() {
042        // the following code sets the frame's initial state
043
044        jLabel1.setText(Bundle.getMessage("CommandLabel"));
045        jLabel1.setVisible(true);
046
047        sendButton.setText(Bundle.getMessage("ButtonSend"));
048        sendButton.setVisible(true);
049        sendButton.setToolTipText(Bundle.getMessage("SendPacketTitle"));
050
051        packetTextField.setText("");
052        packetTextField.setToolTipText(Bundle.getMessage("EnterHexBytesToolTip"));
053        packetTextField.setMaximumSize(
054                new Dimension(packetTextField.getMaximumSize().width,
055                        packetTextField.getPreferredSize().height
056                )
057        );
058
059        setTitle(Bundle.getMessage("SendSerialCommandTitle") + Bundle.getMessage("WindowConnectionMemo") + _memo.getUserName()); // NOI18N
060        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
061
062        getContentPane().add(jLabel1);
063        getContentPane().add(packetTextField);
064        getContentPane().add(sendButton);
065
066        sendButton.addActionListener(new java.awt.event.ActionListener() {
067            @Override
068            public void actionPerformed(java.awt.event.ActionEvent e) {
069                sendButtonActionPerformed(e);
070            }
071        });
072
073        getContentPane().add(new JSeparator(JSeparator.HORIZONTAL));
074
075        // add poll message buttons
076        JPanel pane3 = new JPanel();
077        pane3.setLayout(new FlowLayout());
078        pane3.add(new JLabel(Bundle.getMessage("UALabel")));
079        pane3.add(uaAddrField);
080        pollButton.setText(Bundle.getMessage("LabelPoll"));
081        pane3.add(pollButton);
082        uaAddrField.setText("0"); // NOI18N
083        uaAddrField.setToolTipText(Bundle.getMessage("EnterNodeAddressToolTip"));
084        getContentPane().add(pane3);
085
086        pollButton.addActionListener(new java.awt.event.ActionListener() {
087            @Override
088            public void actionPerformed(java.awt.event.ActionEvent e) {
089                pollButtonActionPerformed(e);
090            }
091        });
092        pollButton.setToolTipText(Bundle.getMessage("PollToolTip"));
093
094        // pack for display
095        pack();
096    }
097
098    public void pollButtonActionPerformed(java.awt.event.ActionEvent e) {
099        SerialMessage msg = SerialMessage.getPoll(Integer.parseInt(uaAddrField.getText()));
100        _memo.getTrafficController().sendSerialMessage(msg, this);
101    }
102
103    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
104        String input = packetTextField.getText();
105        // TODO check input + feedback on error. Too easy to cause NPE
106        _memo.getTrafficController().sendSerialMessage(createPacket(input), this);
107    }
108
109    SerialMessage createPacket(String s) {
110        // gather bytes in result
111        byte b[] = StringUtil.bytesFromHexString(s);
112        if (b.length == 0) {
113            return null;  // no such thing as a zero-length message
114        }
115        SerialMessage m = new SerialMessage(b.length);
116        for (int i = 0; i < b.length; i++) {
117            m.setElement(i, b[i]);
118        }
119        return m;
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public void message(SerialMessage m) {
127    }  // ignore replies
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public void reply(SerialReply r) {
134    } // ignore replies
135
136}