001package jmri.jmrix.tmcc.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import javax.swing.JSeparator;
006import jmri.jmrix.tmcc.SerialMessage;
007import jmri.jmrix.tmcc.SerialReply;
008import jmri.jmrix.tmcc.TmccSystemConnectionMemo;
009import jmri.util.StringUtil;
010
011/**
012 * Frame for user input of serial messages.
013 *
014 * @author Bob Jacobsen Copyright (C) 2002, 2003, 2006
015 */
016public class SerialPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.tmcc.SerialListener {
017
018    // member declarations
019    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
020    javax.swing.JButton sendButton = new javax.swing.JButton();
021    javax.swing.JTextField packetTextField = new javax.swing.JTextField(12);
022    private TmccSystemConnectionMemo _memo = null;
023
024    public SerialPacketGenFrame(TmccSystemConnectionMemo memo) {
025        super();
026        _memo = memo;
027    }
028
029    /** 
030     * {@inheritDoc}
031     */
032    @Override
033    public void initComponents() {
034        // the following code sets the frame's initial state
035
036        jLabel1.setText(Bundle.getMessage("CommandLabel"));
037        jLabel1.setVisible(true);
038
039        sendButton.setText(Bundle.getMessage("ButtonSend"));
040        sendButton.setVisible(true);
041        sendButton.setToolTipText(Bundle.getMessage("SendToolTip"));
042
043        packetTextField.setText("");
044        packetTextField.setToolTipText(Bundle.getMessage("EnterHexBytesToolTip"));
045        packetTextField.setMaximumSize(
046                new Dimension(packetTextField.getMaximumSize().width,
047                        packetTextField.getPreferredSize().height
048                )
049        );
050
051        setTitle(Bundle.getMessage("SendCommandTitle"));
052        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
053
054        getContentPane().add(jLabel1);
055        getContentPane().add(packetTextField);
056        getContentPane().add(sendButton);
057
058        sendButton.addActionListener(new java.awt.event.ActionListener() {
059            @Override
060            public void actionPerformed(java.awt.event.ActionEvent e) {
061                sendButtonActionPerformed(e);
062            }
063        });
064
065        getContentPane().add(new JSeparator(JSeparator.HORIZONTAL));
066
067        // pack for display
068        pack();
069    }
070
071    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
072        String input = packetTextField.getText();
073        // TODO check input + feedback on error. Too easy to cause NPE
074        _memo.getTrafficController().sendSerialMessage(createPacket(input), this);
075    }
076
077    SerialMessage createPacket(String s) {
078        // gather bytes in result
079        byte b[] = StringUtil.bytesFromHexString(s);
080        if (b.length != 3) {
081            return null;  // no such thing as message of other than 3 bytes
082        }
083        SerialMessage m = new SerialMessage();
084        for (int i = 0; i < b.length; i++) {
085            m.setElement(i, b[i]);
086        }
087        return m;
088    }
089
090    @Override
091    public void message(SerialMessage m) {
092    }  // ignore replies
093
094    @Override
095    public void reply(SerialReply r) {
096    } // ignore replies
097
098}