001package jmri.jmrix.xpa.swing.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.xpa.XpaMessage;
006
007/**
008 * Frame for user input of Xpa+Modem (dialing) messages.
009 *
010 * @author Paul Bender Copyright (C) 2004
011 */
012public class XpaPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.xpa.XpaListener {
013
014    // member declarations
015    final javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
016    final javax.swing.JButton sendButton = new javax.swing.JButton();
017    final javax.swing.JTextField packetTextField = new javax.swing.JTextField(12);
018
019    final jmri.jmrix.xpa.XpaSystemConnectionMemo memo;
020
021    public XpaPacketGenFrame(jmri.jmrix.xpa.XpaSystemConnectionMemo m) {
022        super();
023        memo = m;
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("SendToolTip"));
039
040        packetTextField.setText("");
041        packetTextField.setToolTipText(Bundle.getMessage("EnterASCIIToolTip"));
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(this::sendButtonActionPerformed);
056
057        // pack for display
058        pack();
059    }
060
061    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
062        String input = packetTextField.getText();
063        // TODO check input + feedback on error. Too easy to cause NPE
064        XpaMessage m = new XpaMessage(input.length());
065        for (int i = 0; i < input.length(); i++) {
066            m.setElement(i, input.charAt(i));
067        }
068
069        memo.getXpaTrafficController().sendXpaMessage(m, this);
070    }
071
072    @Override
073    public void message(XpaMessage m) {
074    }  // ignore replies
075
076    @Override
077    public void reply(XpaMessage r) {
078    } // ignore replies
079
080}