001package jmri.jmrix.powerline.swing.packetgen;
002
003import java.awt.Dimension;
004import java.awt.FlowLayout;
005import javax.swing.BoxLayout;
006import javax.swing.JPanel;
007import javax.swing.JSeparator;
008import jmri.jmrix.powerline.SerialMessage;
009import jmri.jmrix.powerline.SerialReply;
010import jmri.jmrix.powerline.SerialTrafficController;
011import jmri.util.StringUtil;
012
013/**
014 * Frame for user input of serial messages.
015 *
016 * @author Bob Jacobsen Copyright (C) 2002, 2003, 2006, 2007, 2008 Converted to
017 * multiple connection
018 * @author kcameron Copyright (C) 2011
019 */
020public class SerialPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.powerline.SerialListener {
021
022    // member declarations
023    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
024    javax.swing.JButton sendButton = new javax.swing.JButton();
025    javax.swing.JTextField packetTextField = new javax.swing.JTextField(12);
026    javax.swing.JCheckBox interlockButton = new javax.swing.JCheckBox(Bundle.getMessage("InterlockBoxLabel"));
027
028    public SerialPacketGenFrame(SerialTrafficController tc) {
029        super();
030        this.tc = tc;
031    }
032    SerialTrafficController tc = null;
033
034    /** 
035     * {@inheritDoc}
036     */
037    @Override
038    public void initComponents() {
039        // the following code sets the frame's initial state
040
041        jLabel1.setText(Bundle.getMessage("CommandLabel")); // I18N using Bundle.getMessage("key") with keys already available in JmrixBundle
042        jLabel1.setVisible(true);
043
044        sendButton.setText(Bundle.getMessage("ButtonSend"));
045        sendButton.setVisible(true);
046        sendButton.setToolTipText(Bundle.getMessage("TooltipSendPacket"));
047
048        packetTextField.setText("");
049        packetTextField.setToolTipText(Bundle.getMessage("EnterHexToolTip"));
050        packetTextField.setMaximumSize(
051                new Dimension(packetTextField.getMaximumSize().width,
052                        packetTextField.getPreferredSize().height
053                )
054        );
055
056        setTitle(Bundle.getMessage("SendPacketTitle"));
057        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
058
059        getContentPane().add(jLabel1);
060        getContentPane().add(packetTextField);
061
062        JPanel p2 = new JPanel();
063        p2.setLayout(new FlowLayout());
064        p2.add(interlockButton);
065        p2.add(sendButton);
066        getContentPane().add(p2);
067
068        sendButton.addActionListener(new java.awt.event.ActionListener() {
069            @Override
070            public void actionPerformed(java.awt.event.ActionEvent e) {
071                sendButtonActionPerformed(e);
072            }
073        });
074
075        getContentPane().add(new JSeparator(JSeparator.HORIZONTAL));
076
077        // pack for display
078        pack();
079    }
080
081    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
082        String input = packetTextField.getText();
083        // TODO check input + feedback on error. Too easy to cause NPE
084        tc.sendSerialMessage(createPacket(input), this);
085    }
086
087    SerialMessage createPacket(String s) {
088        // gather bytes in result
089        byte b[] = StringUtil.bytesFromHexString(s);
090        SerialMessage m = tc.getSerialMessage(b.length);
091        for (int i = 0; i < b.length; i++) {
092            m.setElement(i, b[i]);
093        }
094        m.setInterlocked(interlockButton.isSelected());
095        return m;
096    }
097
098    /** 
099     * {@inheritDoc}
100     * Ignores messages.
101     */
102    @Override
103    public void message(SerialMessage m) {
104    }
105
106    /** 
107     * {@inheritDoc}
108     * Ignores replies.
109     */
110    @Override
111    public void reply(SerialReply r) {
112    }
113
114}