001package jmri.jmrix.ecos.swing.packetgen;
002
003import java.awt.Dimension;
004
005import javax.swing.BoxLayout;
006
007import jmri.jmrix.ecos.*;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Frame for user input of ECoS messages
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2008
016 * @author Dan Boudreau Copyright (C) 2007
017 */
018public class PacketGenPanel extends jmri.jmrix.ecos.swing.EcosPanel {
019
020    // member declarations
021    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
022    javax.swing.JButton sendButton = new javax.swing.JButton();
023    javax.swing.JTextField packetTextField = new javax.swing.JTextField(20);
024
025    public PacketGenPanel() {
026        super();
027    }
028
029    /**
030     * {@inheritDoc}
031     */
032    @Override
033    public void initComponents() {
034        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
035        // the following code sets the frame's initial state
036        {
037            jLabel1.setText(Bundle.getMessage("CommandLabel"));
038            jLabel1.setVisible(true);
039
040            sendButton.setText(Bundle.getMessage("ButtonSend"));
041            sendButton.setVisible(true);
042            sendButton.setToolTipText(Bundle.getMessage("SendToolTip"));
043
044            packetTextField.setText("");
045            packetTextField.setToolTipText(Bundle.getMessage("EnterASCIIToolTip"));
046            packetTextField.setMaximumSize(new Dimension(packetTextField
047                    .getMaximumSize().width, packetTextField.getPreferredSize().height));
048
049            add(jLabel1);
050            add(packetTextField);
051            add(sendButton);
052
053            sendButton.addActionListener(this::sendButtonActionPerformed);
054        }
055    }
056
057    /**
058     * {@inheritDoc}
059     */
060    @Override
061    public String getHelpTarget() {
062        return "package.jmri.jmrix.ecos.swing.packetgen.PacketGenFrame";
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public String getTitle() {
070        if (memo != null) {
071            return Bundle.getMessage("SendXCommandTitle", memo.getUserName());
072        }
073        return Bundle.getMessage("MenuItemSendPacket");
074    }
075
076    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
077        String input = packetTextField.getText();
078        // TODO check input + feedback on error. Too easy to cause NPE
079        EcosMessage m = new EcosMessage(input.length());
080        for (int i = 0; i < input.length(); i++) {
081            m.setElement(i, input.charAt(i));
082        }
083        if ( memo == null ) {
084            log.error("no System Connection Memo Found when sending {}", m);
085            return; 
086        }
087        EcosTrafficController tc = memo.getTrafficController();
088        if (tc ==null ) {
089            log.error("no Traffic Controller for Memo {} when sending {}", memo.getUserName(), m);
090            return; 
091        }
092        tc.sendEcosMessage(m, null);
093    }
094
095    private final static Logger log = LoggerFactory.getLogger(PacketGenPanel.class);
096        
097}