001package jmri.jmrix.acela.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import javax.swing.JButton;
006import javax.swing.JLabel;
007import javax.swing.JTextField;
008import jmri.jmrix.acela.AcelaMessage;
009import jmri.jmrix.acela.AcelaReply;
010
011/**
012 * Frame for user input of Acela messages
013 *
014 * @author Bob Jacobsen Copyright (C) 2001
015 * @author Bob Coleman, Copyright (C) 2007, 2008 Based on CMRI serial example,
016 * modified to establish Acela support.
017 */
018public class AcelaPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.acela.AcelaListener {
019
020    // member declarations
021    JLabel jLabel1 = new JLabel();
022    JButton sendButton = new JButton();
023    JTextField packetTextField = new JTextField(12);
024
025    private jmri.jmrix.acela.AcelaSystemConnectionMemo _memo = null;
026
027    public AcelaPacketGenFrame(jmri.jmrix.acela.AcelaSystemConnectionMemo memo) {
028        _memo = memo;
029    }
030
031    /**
032     * {@inheritDoc}
033     */
034    @Override
035    public void initComponents() {
036        // the following code sets the frame's initial state
037
038        jLabel1.setText(Bundle.getMessage("CommandLabel"));
039        jLabel1.setVisible(true);
040
041        sendButton.setText(Bundle.getMessage("ButtonSend"));
042        sendButton.setVisible(true);
043        sendButton.setToolTipText(Bundle.getMessage("MenuItemSendCommand"));
044
045        packetTextField.setText("");
046        packetTextField.setToolTipText(Bundle.getMessage("EnterHexBytesToolTip"));
047        packetTextField.setMaximumSize(
048                new Dimension(packetTextField.getMaximumSize().width,
049                        packetTextField.getPreferredSize().height));
050
051        setTitle(Bundle.getMessage("AcelaSendCommandTitle"));
052        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
053
054        getContentPane().add(jLabel1);
055        getContentPane().add(packetTextField);
056        getContentPane().add(sendButton);
057
058        sendButton.addActionListener(new java.awt.event.ActionListener() {
059
060            /**
061             * {@inheritDoc}
062             */
063            @Override
064                    public void actionPerformed(java.awt.event.ActionEvent e) {
065                        sendButtonActionPerformed(e);
066                    }
067                }
068        );
069
070        addHelpMenu("package.jmri.jmrix.acela.packetgen.AcelaPacketGenFrame", true); // NOI18N
071
072        // pack for display
073        pack();
074    }
075
076    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
077        int sendMessageLength;
078        String input = packetTextField.getText();
079        // TODO check input + feedback on error. Too easy to cause NPE
080        if (((input.length() + 1) % 3) == 0) {
081            sendMessageLength = (input.length() + 1) / 3;
082        } else {
083            sendMessageLength = 0;
084        }
085
086        if (sendMessageLength > 0) {
087            AcelaMessage m = new AcelaMessage(sendMessageLength);
088            m.setBinary(true);
089
090            int texti = 0;
091            int messi = 0;
092            while (texti < input.length()) {
093                int firstChar = 0;
094                firstChar = input.charAt(texti);
095                if ((firstChar >= '0') && (firstChar <= '9')) {  // Assumes 0 to 9 are sequential in character set
096                    firstChar = firstChar - '0';
097                } else {
098                    if ((firstChar >= 'a') && (firstChar <= 'f')) {  // Assumes a to f are sequential in character set
099                        firstChar = firstChar - 'a' + 10;
100                    } else {
101                        if ((firstChar >= 'A') && (firstChar <= 'F')) {  // Assumes A to F are sequential in character set
102                            firstChar = firstChar - 'A' + 10;
103                        } else {
104                            firstChar = 0;
105                        }
106                    }
107                }
108                texti = texti + 1;
109                int secondChar = 0;
110                secondChar = input.charAt(texti);
111                if ((secondChar >= '0') && (secondChar <= '9')) {  // Assumes 0 to 9 are sequential in character set
112                    secondChar = secondChar - '0';
113                } else {
114                    if ((secondChar >= 'a') && (secondChar <= 'f')) {  // Assumes a to f are sequential in character set 
115                        secondChar = secondChar - 'a' + 10;
116                    } else {
117                        if ((secondChar >= 'A') && (secondChar <= 'F')) {  // Assumes A to F are sequential in character set
118                            secondChar = secondChar - 'A' + 10;
119                        } else {
120                            secondChar = 0;
121                        }
122                    }
123                }
124                texti = texti + 1;
125                byte theNewByte = 0x00;
126                theNewByte = (byte) ((firstChar * 16) + secondChar);
127                m.setElement(messi, theNewByte);
128                messi = messi + 1;  // need some error checking here
129                texti = texti + 1;  // Skip space -- need more error checking here
130            }
131
132            _memo.getTrafficController().sendAcelaMessage(m, this);
133        }
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    @Override
140    public void message(AcelaMessage m) {
141    }  // ignore replies
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override
147    public void reply(AcelaReply r) {
148    } // ignore replies
149
150}