001package jmri.jmrix.maple.packetgen;
002
003import java.awt.Dimension;
004import java.awt.FlowLayout;
005import javax.swing.BoxLayout;
006import javax.swing.JButton;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JSeparator;
010import javax.swing.JSpinner;
011import javax.swing.JTextField;
012import javax.swing.SpinnerNumberModel;
013
014import jmri.jmrix.maple.InputBits;
015import jmri.jmrix.maple.SerialMessage;
016import jmri.jmrix.maple.MapleSystemConnectionMemo;
017import jmri.util.StringUtil;
018
019/**
020 * Frame for user input of serial messages.
021 *
022 * @author Bob Jacobsen Copyright (C) 2002, 2003
023 */
024public class SerialPacketGenFrame extends jmri.util.JmriJFrame {
025
026    private MapleSystemConnectionMemo _memo = null;
027
028    // member declarations
029    JLabel jLabel1 = new JLabel();
030    JButton sendButton = new JButton();
031    JTextField packetTextField = new JTextField(12);
032
033    JButton pollButton = new JButton(Bundle.getMessage("LabelPoll")); // I18N using jmrix.Bundle
034    protected JSpinner nodeAddrSpinner;
035
036    public SerialPacketGenFrame(MapleSystemConnectionMemo memo) {
037        super();
038        _memo = memo;
039        nodeAddrSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 99, 1));
040        nodeAddrSpinner.setToolTipText(Bundle.getMessage("TooltipNodeAddress"));
041    }
042
043    /** 
044     * {@inheritDoc}
045     */
046    @Override
047    public void initComponents() {
048        // the following code sets the frame's initial state
049
050        jLabel1.setText(Bundle.getMessage("CommandLabel"));
051        jLabel1.setVisible(true);
052
053        sendButton.setText(Bundle.getMessage("ButtonSend"));
054        sendButton.setVisible(true);
055        sendButton.setToolTipText(Bundle.getMessage("TooltipSendPacket"));
056
057        packetTextField.setText("");
058        packetTextField.setToolTipText(Bundle.getMessage("EnterHexToolTip"));
059        packetTextField.setMaximumSize(
060                new Dimension(packetTextField.getMaximumSize().width,
061                        packetTextField.getPreferredSize().height
062                )
063        );
064
065        setTitle(Bundle.getMessage("SendXCommandTitle", "Maple"));
066        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
067
068        getContentPane().add(jLabel1);
069        getContentPane().add(packetTextField);
070        getContentPane().add(sendButton);
071
072        sendButton.addActionListener(this::sendButtonActionPerformed);
073
074        getContentPane().add(new JSeparator(JSeparator.HORIZONTAL));
075
076        // add poll message buttons
077        JPanel pane3 = new JPanel();
078        pane3.setLayout(new FlowLayout());
079        pane3.add(new JLabel(Bundle.getMessage("LabelNodeAddress")));
080        
081        pane3.add(nodeAddrSpinner);
082        pane3.add(pollButton);
083        getContentPane().add(pane3);
084
085        pollButton.addActionListener(this::pollButtonActionPerformed);
086        pollButton.setToolTipText(Bundle.getMessage("PollToolTipMulti"));
087
088        // pack for display
089        pack();
090    }
091
092    public void pollButtonActionPerformed(java.awt.event.ActionEvent e) {
093        int endAddr = InputBits.getNumInputBits();
094        if (endAddr > 99) {
095            endAddr = 99;
096        }
097        SerialMessage msg = SerialMessage.getPoll((Integer) nodeAddrSpinner.getValue(), 1, endAddr);
098        _memo.getTrafficController().sendSerialMessage(msg, null);
099    }
100
101    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
102        String input = packetTextField.getText();
103        // TODO check input + feedback on error. Too easy to cause NPE
104        _memo.getTrafficController().sendSerialMessage(createPacket(input), null);
105    }
106
107    SerialMessage createPacket(String s) {
108        // gather bytes in result
109        byte b[] = StringUtil.bytesFromHexString(s);
110        if (b.length == 0) {
111            return null;  // no such thing as a zero-length message
112        }
113        SerialMessage m = new SerialMessage(b.length);
114        for (int i = 0; i < b.length; i++) {
115            m.setElement(i, b[i]);
116        }
117        return m;
118    }
119
120
121
122}