001package jmri.jmrix.dccpp.swing.packetgen;
002
003import jmri.jmrix.dccpp.DCCppMessage;
004import jmri.jmrix.dccpp.DCCppSystemConnectionMemo;
005import jmri.jmrix.dccpp.DCCppTrafficController;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Frame for user input of XpressNet messages
011 *
012 * @author Bob Jacobsen Copyright (C) 2001,2002
013 * @author      Mark Underwood Copyright (C) 2015
014 */
015public class PacketGenFrame extends jmri.jmrix.swing.AbstractPacketGenFrame {
016
017    // private data
018    private DCCppTrafficController _tc = null;
019    private DCCppSystemConnectionMemo _memo;
020
021    public PacketGenFrame(DCCppSystemConnectionMemo memo) {
022        super();
023        _tc = memo.getDCCppTrafficController();
024        _memo = memo;
025    }
026
027    /**
028     * {@inheritDoc}
029     */
030    @Override
031    public void initComponents() {
032        super.initComponents();
033
034        // all we need to do is set the title, include prefix in event of multiple connections 
035        setTitle(Bundle.getMessage("PacketGenFrameTitle") + " (" + _memo.getSystemPrefix() + ")");
036        packetTextField.setToolTipText("Enter packet as a text string without the < > brackets");
037
038        // pack to cause display
039        pack();
040    }
041
042    /**
043     * {@inheritDoc}
044     */
045    @Override
046    public void sendButtonActionPerformed(java.awt.event.ActionEvent e) {
047        DCCppMessage msg = createPacket(packetTextField.getSelectedItem().toString());
048        if (msg != null) {
049            _tc.sendDCCppMessage(msg, null);
050        } else {
051            log.error("Frame packet '{}' not valid", packetTextField.getSelectedItem().toString());
052        }
053    }
054
055    DCCppMessage createPacket(String s) {
056        if (s.equals("")) {
057            log.debug("Empty Packet...");
058            return null; // message cannot be empty
059        }
060        // Strip off the brackets, if present.
061        if (s.charAt(0) == '<') {
062            s = s.substring(1);
063        }
064        if (s.lastIndexOf('>') != -1) {
065            s = s.substring(0, s.lastIndexOf('>'));
066        }
067        DCCppMessage m = new DCCppMessage(s);
068        log.debug("Sending: '{}'", m);
069        return(m);
070    }
071   
072    private static final Logger log = LoggerFactory.getLogger(PacketGenFrame.class);
073
074}