001package jmri.jmrix.srcp.swing.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.srcp.SRCPMessage;
006import jmri.jmrix.srcp.SRCPReply;
007import jmri.jmrix.srcp.SRCPSystemConnectionMemo;
008import jmri.jmrix.srcp.SRCPTrafficController;
009
010/**
011 * Frame for user input of SRCP messages.
012 *
013 * @author Bob Jacobsen Copyright (C) 2008
014 */
015public class PacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.srcp.SRCPListener {
016
017    // member declarations
018    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
019    javax.swing.JButton sendButton = new javax.swing.JButton();
020    javax.swing.JTextField packetTextField = new javax.swing.JTextField(12);
021
022    private SRCPTrafficController tc = null;
023
024    public PacketGenFrame(SRCPSystemConnectionMemo memo) {
025        super();
026        tc = memo.getTrafficController();
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("Command:");
037        jLabel1.setVisible(true);
038
039        sendButton.setText("Send");
040        sendButton.setVisible(true);
041        sendButton.setToolTipText("Send packet");
042        packetTextField.setText("");
043        packetTextField.setToolTipText("Enter command as ASCII string (hex not yet available)");
044        packetTextField.setMaximumSize(
045                new Dimension(packetTextField.getMaximumSize().width,
046                        packetTextField.getPreferredSize().height
047                )
048        );
049
050        setTitle("Send SRCP command");
051        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
052
053        getContentPane().add(jLabel1);
054        getContentPane().add(packetTextField);
055        getContentPane().add(sendButton);
056
057        sendButton.addActionListener(new java.awt.event.ActionListener() {
058            @Override
059            public void actionPerformed(java.awt.event.ActionEvent e) {
060                sendButtonActionPerformed(e);
061            }
062        });
063
064        // pack for display
065        pack();
066    }
067
068    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
069        String input = packetTextField.getText();
070        // TODO check input + feedback on error. Too easy to cause NPE
071        SRCPMessage m = new SRCPMessage(input.length() + 1);
072        for (int i = 0; i < input.length(); i++) {
073            m.setElement(i, input.charAt(i));
074        }
075
076        m.setElement(input.length(), '\n');
077        tc.sendSRCPMessage(m, this);
078    }
079
080    @Override
081    public void message(SRCPMessage m) {
082    }  // ignore replies
083
084    @Override
085    public void reply(SRCPReply r) {
086    } // ignore replies
087
088    @Override
089    public void reply(jmri.jmrix.srcp.parser.SimpleNode n) {
090    } // ignore replies
091}