001package jmri.jmrix.oaktree.packetgen; 002 003import java.awt.Dimension; 004import java.awt.FlowLayout; 005import javax.swing.BoxLayout; 006import javax.swing.JLabel; 007import javax.swing.JPanel; 008import javax.swing.JSeparator; 009import jmri.jmrix.oaktree.SerialMessage; 010import jmri.jmrix.oaktree.SerialReply; 011import jmri.jmrix.oaktree.OakTreeSystemConnectionMemo; 012import jmri.util.StringUtil; 013 014/** 015 * Frame for user input of serial messages. 016 * 017 * @author Bob Jacobsen Copyright (C) 2002, 2003, 2006 018 */ 019public class SerialPacketGenFrame extends jmri.util.JmriJFrame implements jmri.jmrix.oaktree.SerialListener { 020 021 // member declarations 022 javax.swing.JLabel jLabel1 = new javax.swing.JLabel(); 023 javax.swing.JButton sendButton = new javax.swing.JButton(); 024 javax.swing.JTextField packetTextField = new javax.swing.JTextField(12); 025 026 javax.swing.JButton pollButton = new javax.swing.JButton(Bundle.getMessage("LabelPoll")); 027 javax.swing.JTextField uaAddrField = new javax.swing.JTextField(5); 028 029 private OakTreeSystemConnectionMemo _memo = null; 030 031 public SerialPacketGenFrame(OakTreeSystemConnectionMemo memo) { 032 super(); 033 _memo = memo; 034 } 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 public void initComponents() { 041 // the following code sets the frame's initial state 042 043 jLabel1.setText(Bundle.getMessage("CommandLabel")); 044 jLabel1.setVisible(true); 045 046 sendButton.setText(Bundle.getMessage("ButtonSend")); 047 sendButton.setVisible(true); 048 sendButton.setToolTipText(Bundle.getMessage("TooltipSendPacket")); 049 050 packetTextField.setText(""); 051 packetTextField.setToolTipText(Bundle.getMessage("EnterHexToolTip")); 052 packetTextField.setMaximumSize( 053 new Dimension(packetTextField.getMaximumSize().width, 054 packetTextField.getPreferredSize().height 055 ) 056 ); 057 058 setTitle(Bundle.getMessage("SendXCommandTitle", "OakTree")); 059 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 060 061 getContentPane().add(jLabel1); 062 getContentPane().add(packetTextField); 063 getContentPane().add(sendButton); 064 065 sendButton.addActionListener(new java.awt.event.ActionListener() { 066 @Override 067 public void actionPerformed(java.awt.event.ActionEvent e) { 068 sendButtonActionPerformed(e); 069 } 070 }); 071 072 getContentPane().add(new JSeparator(JSeparator.HORIZONTAL)); 073 074 // add poll message buttons 075 JPanel pane3 = new JPanel(); 076 pane3.setLayout(new FlowLayout()); 077 pane3.add(new JLabel(Bundle.getMessage("LabelNodeAddress"))); 078 pane3.add(uaAddrField); 079 pane3.add(pollButton); 080 pollButton.setToolTipText(Bundle.getMessage("PollToolTip")); 081 uaAddrField.setText("0"); 082 uaAddrField.setToolTipText(Bundle.getMessage("TooltipNodeAddress")); 083 getContentPane().add(pane3); 084 085 pollButton.addActionListener(new java.awt.event.ActionListener() { 086 @Override 087 public void actionPerformed(java.awt.event.ActionEvent e) { 088 pollButtonActionPerformed(e); 089 } 090 }); 091 092 // pack for display 093 pack(); 094 } 095 096 public void pollButtonActionPerformed(java.awt.event.ActionEvent e) { 097 SerialMessage msg = SerialMessage.getPoll(Integer.parseInt(uaAddrField.getText())); 098 _memo.getTrafficController().sendSerialMessage(msg, this); 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), this); 105 } 106 107 SerialMessage createPacket(String s) { 108 // gather bytes in result 109 byte b[] = StringUtil.bytesFromHexString(s); 110 if ((b.length < 4) || (b.length > 5)) { 111 return null; // no such thing as message with other than 4 or 5 bytes 112 } 113 SerialMessage m = new SerialMessage(5); 114 for (int i = 0; i < b.length; i++) { 115 m.setElement(i, b[i]); 116 } 117 return m; 118 } 119 120 /** 121 * {@inheritDoc} 122 * Ignores messages. 123 */ 124 @Override 125 public void message(SerialMessage m) { 126 } 127 128 /** 129 * {@inheritDoc} 130 * Ignore replies. 131 */ 132 @Override 133 public void reply(SerialReply r) { 134 } 135 136}