001package jmri.jmrix.powerline.swing.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.powerline.SerialListener;
006import jmri.jmrix.powerline.SerialMessage;
007import jmri.jmrix.powerline.SerialReply;
008import jmri.jmrix.powerline.SerialSystemConnectionMemo;
009import jmri.jmrix.powerline.SerialTrafficController;
010import jmri.util.StringUtil;
011
012/**
013 * Frame for user input of Powerline messages.
014 *
015 * @author Ken Cameron Copyright (C) 2010 derived from:
016 * @author Bob Jacobsen Copyright (C) 2001
017 * @author Dan Boudreau Copyright (C) 2007
018 */
019public class SerialPacketGenPane extends jmri.jmrix.powerline.swing.PowerlinePanel implements SerialListener {
020
021    // member declarations
022    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
023    javax.swing.JButton sendButton = new javax.swing.JButton();
024    javax.swing.JTextField packetTextField = new javax.swing.JTextField(20);
025    javax.swing.JCheckBox checkBoxBinCmd = new javax.swing.JCheckBox();
026    javax.swing.JTextField replyLenTextField = new javax.swing.JTextField(2);
027    javax.swing.JCheckBox interlockButton = new javax.swing.JCheckBox(Bundle.getMessage("InterlockBoxLabel"));
028
029    private SerialTrafficController tc = null;
030    private SerialSystemConnectionMemo memo = null;
031
032    public SerialPacketGenPane() {
033        super();
034    }
035
036    public void init() {
037    }
038
039    /**
040     * {@inheritDoc}
041     */
042    @Override
043    public void initContext(Object context) {
044        if (context instanceof SerialSystemConnectionMemo) {
045            this.memo = (SerialSystemConnectionMemo) context;
046            initComponents();
047        }
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public String getHelpTarget() {
055        return "package.jmri.jmrix.powerline.packetgen.PowerlinePacketGenPane";
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public String getTitle() {
063        StringBuilder x = new StringBuilder();
064        if (memo != null) {
065            x.append(memo.getUserName());
066        } else {
067            x.append(Bundle.getMessage("DefaultTag"));
068        }
069        x.append(": ");
070        x.append(Bundle.getMessage("Title"));
071        return x.toString();
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public void initComponents(SerialSystemConnectionMemo memo) {
079        this.memo = memo;
080        tc = memo.getTrafficController();
081        
082        // the following code sets the frame's initial state
083
084        jLabel1.setText(Bundle.getMessage("CommandLabel")); // I18N using Bundle.getMessage("key") with keys already available in JmrixBundle
085        jLabel1.setVisible(true);
086
087        sendButton.setText(Bundle.getMessage("ButtonSend"));
088        sendButton.setVisible(true);
089        sendButton.setToolTipText(Bundle.getMessage("TooltipSendPacket"));
090
091        packetTextField.setText("");
092        packetTextField.setToolTipText(Bundle.getMessage("EnterHexToolTip"));
093        packetTextField.setMaximumSize(
094                new Dimension(packetTextField.getMaximumSize().width,
095                        packetTextField.getPreferredSize().height
096                )
097        );
098
099        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
100
101        add(jLabel1);
102        add(packetTextField);
103        add(interlockButton);
104        add(sendButton);
105
106        sendButton.addActionListener(new java.awt.event.ActionListener() {
107            @Override
108            public void actionPerformed(java.awt.event.ActionEvent e) {
109                sendButtonActionPerformed(e);
110            }
111        });
112
113    }
114
115    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
116        String input = packetTextField.getText();
117        // TODO check input + feedback on error. Too easy to cause NPE
118        tc.sendSerialMessage(createPacket(input), this);
119    }
120
121    SerialMessage createPacket(String s) {
122        // gather bytes in result
123        byte b[] = StringUtil.bytesFromHexString(s);
124        SerialMessage m = memo.getTrafficController().getSerialMessage(b.length);
125        for (int i = 0; i < b.length; i++) {
126            m.setElement(i, b[i]);
127        }
128        m.setInterlocked(interlockButton.isSelected());
129        return m;
130    }
131
132    @Override
133    public void message(SerialMessage m) {
134    }  // ignore replies
135
136    @Override
137    public void reply(SerialReply r) {
138    } // ignore replies
139
140}