001package jmri.jmrix.easydcc.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.easydcc.EasyDccMessage;
006import jmri.jmrix.easydcc.EasyDccReply;
007import jmri.jmrix.easydcc.EasyDccSystemConnectionMemo;
008
009/**
010 * Frame for user input of EasyDCC messages.
011 *
012 * @author Bob Jacobsen Copyright (C) 2001
013 */
014public class EasyDccPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.easydcc.EasyDccListener {
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    private EasyDccSystemConnectionMemo _memo = null;
021
022    public EasyDccPacketGenFrame(EasyDccSystemConnectionMemo memo) {
023        super();
024        _memo = memo;
025    }
026
027    /**
028     * {@inheritDoc}
029     */
030    @Override
031    public void initComponents() {
032        // the following code sets the frame's initial state
033
034        jLabel1.setText(Bundle.getMessage("CommandLabel"));
035        jLabel1.setVisible(true);
036
037        sendButton.setText(Bundle.getMessage("ButtonSend"));
038        sendButton.setVisible(true);
039        sendButton.setToolTipText(Bundle.getMessage("SendToolTip"));
040
041        packetTextField.setText("");
042        packetTextField.setToolTipText(Bundle.getMessage("EnterASCIIToolTip"));
043        packetTextField.setMaximumSize(
044                new Dimension(packetTextField.getMaximumSize().width,
045                        packetTextField.getPreferredSize().height
046                )
047        );
048
049        setTitle(Bundle.getMessage("SendCommandTitle"));
050        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
051
052        getContentPane().add(jLabel1);
053        getContentPane().add(packetTextField);
054        getContentPane().add(sendButton);
055
056        sendButton.addActionListener(new java.awt.event.ActionListener() {
057            @Override
058            public void actionPerformed(java.awt.event.ActionEvent e) {
059                sendButtonActionPerformed(e);
060            }
061        });
062
063        // pack for display
064        pack();
065    }
066
067    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
068        String input = packetTextField.getText();
069        // TODO check input + feedback on error. Too easy to cause NPE
070        EasyDccMessage m = new EasyDccMessage(input.length());
071        for (int i = 0; i < input.length(); i++) {
072            m.setElement(i, input.charAt(i));
073        }
074
075        _memo.getTrafficController().sendEasyDccMessage(m, this);
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public void message(EasyDccMessage m) {
083    }  // ignore replies
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public void reply(EasyDccReply r) {
090    } // ignore replies
091
092}