001package jmri.jmrix.zimo.swing.packetgen;
002
003import java.awt.Dimension;
004import javax.swing.BoxLayout;
005import jmri.jmrix.zimo.Mx1Message;
006import jmri.jmrix.zimo.Mx1SystemConnectionMemo;
007import jmri.jmrix.zimo.Mx1TrafficController;
008import jmri.util.StringUtil;
009
010/**
011 * Frame for user input of Mrc messages
012 *
013 * @author Ken Cameron Copyright (C) 2010
014 * @author Bob Jacobsen Copyright (C) 2001
015 * @author Dan Boudreau Copyright (C) 2007
016 */
017public class Mx1PacketGenPanel extends jmri.jmrix.zimo.swing.Mx1Panel {
018
019    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
020    javax.swing.JButton sendButton = new javax.swing.JButton();
021    javax.swing.JTextField packetTextField = new javax.swing.JTextField(20);
022
023    private Mx1TrafficController tc = null;
024
025    public Mx1PacketGenPanel() {
026        super();
027    }
028
029    /**
030     * {@inheritDoc}
031     */
032    @Override
033    public void initContext(Object context) {
034        if (context instanceof Mx1SystemConnectionMemo) {
035            initComponents((Mx1SystemConnectionMemo) context);
036        }
037    }
038
039    /**
040     * {@inheritDoc}
041     */
042    @Override
043    public String getHelpTarget() {
044        return "package.jmri.jmrix.zimo.swing.packetgen.Mx1PacketGenPanel";
045    }// NOI18N
046
047    /**
048     * {@inheritDoc}
049     */
050    @Override
051    public String getTitle() {
052        StringBuilder x = new StringBuilder();
053        if (memo != null) {
054            x.append(memo.getUserName());
055        } else {
056            x.append("MX1_");// NOI18N
057        }
058        x.append(": ");
059        x.append(Bundle.getMessage("Title"));// NOI18N
060        return x.toString();
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public void initComponents(Mx1SystemConnectionMemo m) {
068        this.memo = m;
069        this.tc = m.getMx1TrafficController();
070
071        // the following code sets the frame's initial state
072        jLabel1.setText("Command: ");// NOI18N
073        jLabel1.setVisible(true);
074
075        sendButton.setText("Send");// NOI18N
076        sendButton.setVisible(true);
077        sendButton.setToolTipText("Send packet");// NOI18N
078
079        packetTextField.setText("");
080        packetTextField.setToolTipText("Enter command"); // NOI18N
081        packetTextField.setMaximumSize(new Dimension(packetTextField
082                .getMaximumSize().width, packetTextField.getPreferredSize().height));
083
084        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
085
086        add(jLabel1);
087        add(packetTextField);
088        add(sendButton);
089
090        sendButton.addActionListener(new java.awt.event.ActionListener() {
091            @Override
092            public void actionPerformed(java.awt.event.ActionEvent e) {
093                sendButtonActionPerformed(e);
094            }
095        });
096
097    }
098
099    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
100        String input = packetTextField.getText();
101        // TODO check input + feedback on error. Too easy to cause NPE
102        /*Mx1Message m = new Mx1Message(input.length());
103         for (int i = 0; i < input.length(); i++)
104         m.setElement(i, input.charAt(i));*/
105        tc.sendMx1Message(createPacket(input), null);
106    }
107
108    Mx1Message createPacket(String s) {
109        // gather bytes in result
110        byte b[];
111        try {
112            b = StringUtil.bytesFromHexString(s);
113        } catch (NumberFormatException e) {
114            return null;
115        }
116        if (b.length == 0) {
117            return null; // no such thing as a zero-length message
118        }
119        Mx1Message m = new Mx1Message(b.length);
120        for (int i = 0; i < b.length; i++) {
121            m.setElement(i, (b[i] & 0xff));
122        }
123        return m;
124    }
125
126    /**
127     * Nested class to create one of these using old-style defaults
128     */
129    static public class Default extends jmri.jmrix.zimo.swing.Mx1NamedPaneAction {
130
131        public Default() {
132            super("Open MRC Send Binary Command",
133                    new jmri.util.swing.sdi.JmriJFrameInterface(),
134                    Mx1PacketGenPanel.class.getName(),
135                    jmri.InstanceManager.getDefault(Mx1SystemConnectionMemo.class));// NOI18N
136        }
137    }
138}