001package jmri.jmrix.jmriclient.swing.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.jmriclient.JMRIClientMessage;
006import jmri.jmrix.jmriclient.JMRIClientReply;
007import jmri.jmrix.jmriclient.JMRIClientTrafficController;
008
009/**
010 * Frame for user input of JMRIClient messages
011 *
012 * @author Bob Jacobsen Copyright (C) 2008
013 */
014public class PacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.jmriclient.JMRIClientListener {
015
016    // member declarations
017    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
018    javax.swing.JButton sendButton = new javax.swing.JButton();
019    javax.swing.JTextField packetTextField = new javax.swing.JTextField(12);
020
021    public PacketGenFrame() {
022        super();
023    }
024
025    /**
026     * {@inheritDoc}
027     */
028    @Override
029    public void initComponents() {
030        // the following code sets the frame's initial state
031
032        jLabel1.setText("Command:");
033        jLabel1.setVisible(true);
034
035        sendButton.setText("Send");
036        sendButton.setVisible(true);
037        sendButton.setToolTipText("Send packet");
038
039        packetTextField.setText("");
040        packetTextField.setToolTipText("Enter command as ASCII string");
041        packetTextField.setMaximumSize(
042                new Dimension(packetTextField.getMaximumSize().width,
043                        packetTextField.getPreferredSize().height
044                )
045        );
046
047        setTitle("Send JMRI Client command");
048        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
049
050        getContentPane().add(jLabel1);
051        getContentPane().add(packetTextField);
052        getContentPane().add(sendButton);
053
054        sendButton.addActionListener(new java.awt.event.ActionListener() {
055            @Override
056            public void actionPerformed(java.awt.event.ActionEvent e) {
057                sendButtonActionPerformed(e);
058            }
059        });
060
061        // pack for display
062        pack();
063    }
064
065    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
066        String input = packetTextField.getText();
067        // TODO check input + feedback on error. Too easy to cause NPE
068        JMRIClientMessage m = new JMRIClientMessage(input.length() + 1);
069        for (int i = 0; i < input.length(); i++) {
070            m.setElement(i, input.charAt(i));
071        }
072
073        m.setElement(input.length(), '\n');
074        tc.sendJMRIClientMessage(m, this);
075    }
076
077    /**
078     * {@inheritDoc}
079     * Ignore messages.
080     */
081    @Override
082    public void message(JMRIClientMessage m) {
083    }
084
085    /**
086     * {@inheritDoc}
087     * Ignore replies.
088     */
089    @Override
090    public void reply(JMRIClientReply r) {
091    }
092
093    // connect to the TrafficController
094    public void connect(JMRIClientTrafficController t) {
095        tc = t;
096    }
097
098    // private data
099    private JMRIClientTrafficController tc = null;
100
101}