001package jmri.jmrix.sprog.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.sprog.SprogMessage;
006import jmri.jmrix.sprog.SprogSystemConnectionMemo;
007
008/**
009 * Frame for user input of Sprog messages.
010 *
011 * @author Bob Jacobsen Copyright (C) 2001, 2010
012 */
013public class SprogPacketGenFrame extends jmri.util.JmriJFrame {
014
015    private SprogSystemConnectionMemo _memo = null;
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 SprogPacketGenFrame(SprogSystemConnectionMemo memo) {
022        super();
023        _memo = memo;
024    }
025
026    /** 
027     * {@inheritDoc}
028     */
029    @Override
030    public void initComponents() {
031        // the following code sets the frame's initial state
032
033        jLabel1.setText(Bundle.getMessage("CommandLabel"));
034        jLabel1.setVisible(true);
035
036        sendButton.setText(Bundle.getMessage("ButtonSend"));
037        sendButton.setVisible(true);
038        sendButton.setToolTipText(Bundle.getMessage("SendPacketTooltip"));
039
040        packetTextField.setText("");
041        packetTextField.setToolTipText(Bundle.getMessage("SendCommandFieldTooltip"));
042        packetTextField.setMaximumSize(
043                new Dimension(packetTextField.getMaximumSize().width,
044                        packetTextField.getPreferredSize().height
045                )
046        );
047
048        setTitle(Bundle.getMessage("SendCommandTitle"));
049        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
050
051        getContentPane().add(jLabel1);
052        getContentPane().add(packetTextField);
053        getContentPane().add(sendButton);
054
055        sendButton.addActionListener(new java.awt.event.ActionListener() {
056            @Override
057            public void actionPerformed(java.awt.event.ActionEvent e) {
058                sendButtonActionPerformed(e);
059            }
060        });
061
062        // pack for display
063        pack();
064    }
065
066    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
067        String input = packetTextField.getText();
068        // TODO check input + feedback on error. Too easy to cause NPE
069        SprogMessage m = new SprogMessage(input);
070        _memo.getSprogTrafficController().sendSprogMessage(m);
071    }
072
073}