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