001package jmri.jmrix.tams.swing.packetgen; 002 003import java.awt.Dimension; 004 005import javax.swing.BoxLayout; 006 007import jmri.jmrix.tams.TamsConstants; 008import jmri.jmrix.tams.TamsMessage; 009import jmri.util.StringUtil; 010import jmri.util.swing.JmriJOptionPane; 011 012/** 013 * Frame for user input of Tams messages Based on work by Bob Jacobsen and Kevin Dickerson 014 * 015 * @author Jan Boen 016 */ 017public class PacketGenPanel extends jmri.jmrix.tams.swing.TamsPanel { 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 javax.swing.JCheckBox checkBoxBinCmd = new javax.swing.JCheckBox(); 024 javax.swing.JCheckBox checkBoxReplyType = new javax.swing.JCheckBox(); 025 026 public PacketGenPanel() { 027 super(); 028 } 029 030 /** 031 * {@inheritDoc} 032 */ 033 @Override 034 public void initComponents() { 035 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 036 // the following code sets the frame's initial state 037 { 038 jLabel1.setText("Command: "); // TODO I18N using Bundle.getMessage("key") - many keys available in JmrixBundle 039 jLabel1.setVisible(true); 040 041 sendButton.setText("Send"); 042 sendButton.setVisible(true); 043 sendButton.setToolTipText("Send packet"); 044 045 packetTextField.setText(""); 046 packetTextField.setToolTipText("Enter command"); 047 packetTextField.setMaximumSize(new Dimension(packetTextField 048 .getMaximumSize().width, packetTextField.getPreferredSize().height)); 049 050 checkBoxBinCmd.setText("Binary"); 051 checkBoxBinCmd.setVisible(true); 052 checkBoxBinCmd.setToolTipText("Check to enable binary commands"); 053 checkBoxBinCmd.setSelected(false); 054 055 add(jLabel1); 056 add(packetTextField); 057 add(checkBoxBinCmd); 058 add(sendButton); 059 060 sendButton.addActionListener(this::sendButtonActionPerformed); 061 } 062 } 063 064 @Override 065 public String getHelpTarget() { 066 return "package.jmri.jmrix.tams.swing.packetgen.PacketGenFrame"; 067 } 068 069 @Override 070 public String getTitle() { 071 return "Send Tams command"; 072 } 073 074 public void sendButtonActionPerformed(java.awt.event.ActionEvent e) { 075 TamsMessage m; 076 String input = packetTextField.getText(); 077 // TODO check input + feedback on error. Too easy to cause NPE 078 if (checkBoxBinCmd.isSelected()) {//Binary TamsMessage to be sent 079 m = createPacket(input); 080 if (m == null) { 081 JmriJOptionPane.showMessageDialog(this, 082 "Enter hexadecimal numbers only", "Tams Binary Command", 083 JmriJOptionPane.ERROR_MESSAGE); 084 return; 085 } 086 //Set replyType to unknown just in case 087 m.setReplyType('M'); 088 m.setBinary(true); 089 //Check which command is issued and replace by predefined TamsMessage 090 if (m.getElement(1) == TamsConstants.XPWROFF){ 091 m = TamsMessage.setXPwrOff(); 092 } 093 if (m.getElement(1) == TamsConstants.XPWRON){ 094 m = TamsMessage.setXPwrOn(); 095 } 096 /*if (m.getElement(1) == TamsConstants.XEVENT){ 097 m = TamsMessage.getXStatus(); 098 } 099 if (m.getElement(1) == TamsConstants.XEVTSEN){ 100 m = TamsMessage.getXEvtSen(); 101 } 102 if (m.getElement(1) == TamsConstants.XEVTLOK){ 103 m = TamsMessage.getXEvtLok(); 104 } 105 if (m.getElement(1) == TamsConstants.XEVTTRN){ 106 m = TamsMessage.getXEvtTrn(); 107 }*/ 108 } else {//ASCII TamsMessage to be sent 109 m = new TamsMessage(input.length()); 110 for (int i = 0; i < input.length(); i++) { 111 m.setElement(i, input.charAt(i)); 112 } 113 //Set replyType to unknown just in case 114 m.setReplyType('M'); 115 m.setBinary(false); 116 //Check which command is issued and set correct Reply Type 117 if (m.getElement(1) == 'P'){//Programming message 118 m.setReplyType('C'); 119 } 120 /*if (m.getElement(1) == 'T'){//Turnout message 121 m.setReplyType('T'); 122 } 123 if (m.getElement(1) == 'S'){//Sensor message 124 m.setReplyType('S'); 125 }*/ 126 } 127 if ( memo != null ) { 128 memo.getTrafficController().sendTamsMessage(m, null); 129 } 130 } 131 132 TamsMessage createPacket(String s) { 133 // gather bytes in result 134 byte b[]; 135 try { 136 b = StringUtil.bytesFromHexString(s); 137 } catch (NumberFormatException e) { 138 return null; 139 } 140 if (b.length == 0) { 141 return null; // no such thing as a zero-length message 142 } 143 TamsMessage m = new TamsMessage(b.length); 144 for (int i = 0; i < b.length; i++) { 145 m.setElement(i, b[i]); 146 } 147 return m; 148 } 149 150}