001package jmri.jmrix.ieee802154.swing.nodeconfig; 002 003import java.awt.Container; 004import java.awt.FlowLayout; 005import java.awt.event.WindowEvent; 006import javax.swing.BoxLayout; 007import javax.swing.JLabel; 008import javax.swing.JPanel; 009 010import jmri.jmrix.ieee802154.IEEE802154Node; 011import jmri.jmrix.ieee802154.IEEE802154TrafficController; 012import jmri.util.swing.JmriJOptionPane; 013 014/** 015 * Frame for Adding new Nodes 016 * 017 * @author Bob Jacobsen Copyright (C) 2004 018 * @author Dave Duchamp Copyright (C) 2004 019 * @author Paul Bender Copyright (C) 2013,2016 020 */ 021public class AddNodeFrame extends jmri.util.JmriJFrame { 022 023 protected javax.swing.JTextField nodeAddrField = new javax.swing.JTextField(); 024 protected javax.swing.JTextField nodeAddr64Field = new javax.swing.JTextField(); 025 protected javax.swing.JButton addButton = new javax.swing.JButton(Bundle.getMessage("ButtonAdd")); 026 protected javax.swing.JButton cancelButton = new javax.swing.JButton(Bundle.getMessage("ButtonCancel")); 027 028 protected IEEE802154Node curNode = null; // IEEE802154 Node being editted 029 030 private IEEE802154TrafficController itc = null; 031 032 /** 033 * Constructor method 034 * @param tc tc for connection for node 035 */ 036 public AddNodeFrame(IEEE802154TrafficController tc) { 037 super(); 038 addHelpMenu("package.jmri.jmrix.ieee802154.swing.nodeconfig.AddNodeFrame", true); 039 itc = tc; 040 } 041 042 /** 043 * Initialize the config window 044 */ 045 @Override 046 public void initComponents() { 047 setTitle(Bundle.getMessage("AddNodeWindowTitle")); 048 Container contentPane = getContentPane(); 049 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 050 051 // Set up node address and node type 052 JPanel panel = new JPanel(); 053 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 054 panel.add(new JLabel(Bundle.getMessage("LabelNodeAddress") + " ")); 055 panel.add(nodeAddrField); 056 /*nodeAddrField.addActionListener(new java.awt.event.ActionListener() { 057 058 @Override 059 public void actionPerformed(java.awt.event.ActionEvent e) { 060 } 061 });*/ 062 nodeAddrField.setToolTipText(Bundle.getMessage("TipNodeAddress")); 063 panel.add(new JLabel(Bundle.getMessage("LabelNodeAddress64") + " ")); 064 panel.add(nodeAddr64Field); 065 nodeAddr64Field.setToolTipText(Bundle.getMessage("TipNodeAddress64")); 066 /*nodeAddr64Field.addActionListener(new java.awt.event.ActionListener() { 067 068 @Override 069 public void actionPerformed(java.awt.event.ActionEvent e) { 070 } 071 });*/ 072 073 initAddressBoxes(); 074 contentPane.add(panel); 075 076 // Set up buttons 077 JPanel panel4 = new JPanel(); 078 panel4.setLayout(new FlowLayout()); 079 addButton.setText(Bundle.getMessage("ButtonAdd")); 080 addButton.setVisible(true); 081 addButton.setToolTipText(Bundle.getMessage("TipAddButton")); 082 addButton.addActionListener((java.awt.event.ActionEvent e) -> { 083 addButtonActionPerformed(); 084 }); 085 panel4.add(addButton); 086 panel4.add(cancelButton); 087 cancelButton.setText(Bundle.getMessage("ButtonCancel")); 088 cancelButton.setVisible(true); 089 cancelButton.setToolTipText(Bundle.getMessage("TipCancelButton")); 090 panel4.add(cancelButton); 091 cancelButton.addActionListener((java.awt.event.ActionEvent e) -> { 092 cancelButtonActionPerformed(); 093 }); 094 contentPane.add(panel4); 095 096 // pack for display 097 pack(); 098 } 099 100 /** 101 * Method to handle Add button 102 */ 103 public void addButtonActionPerformed() { 104 // Check that a node with this address does not exist 105 String nodeAddress = readNodeAddress(); 106 if (nodeAddress.equals("")) { 107 return; 108 } 109 // get a IEEE802154 Node corresponding to this node address if one exists 110 curNode = (IEEE802154Node) itc.getNodeFromAddress(nodeAddress); 111 if (curNode != null) { 112 JmriJOptionPane.showMessageDialog(this, 113 Bundle.getMessage("Error1",nodeAddress), 114 Bundle.getMessage("AddNodeErrorTitle"),JmriJOptionPane.ERROR_MESSAGE); 115 log.error("Error creating IEEE802154 Node, Node exists."); 116 return; 117 } 118 // get node information from window 119 120 // all ready, create the new node 121 curNode = itc.newNode(); 122 if (curNode == null) { 123 JmriJOptionPane.showMessageDialog(this,Bundle.getMessage("Error3"), 124 Bundle.getMessage("AddNodeErrorTitle"),JmriJOptionPane.ERROR_MESSAGE); 125 log.error("Error creating IEEE802154 Node, constructor returned null"); 126 return; 127 } 128 this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); 129 } 130 131 /** 132 * Method to handle cancel button 133 */ 134 public void cancelButtonActionPerformed() { 135 // Reset 136 curNode = null; 137 // Switch buttons 138 addButton.setVisible(true); 139 cancelButton.setVisible(false); 140 this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); 141 } 142 143 /** 144 * Read node address from the nodeAddressField or nodeAddr64Field 145 * as appropriate and return as a string. 146 * 147 * @return String containing the short (two byte) address of the node. 148 * if the two byte node address is either "FF FF" or "FF FE", 149 * returns the long (64 bit) address. 150 */ 151 private String readNodeAddress() { 152 String addr = nodeAddrField.getText(); 153 if (addr.equals("FF FF ") || addr.equals("FF FE ") || addr.equals("")) { 154 addr = nodeAddr64Field.getText(); 155 } 156 return (addr); 157 } 158 159 // Initilize the text boxes for the addresses. 160 protected void initAddressBoxes() { 161 nodeAddrField.setText(""); 162 nodeAddr64Field.setText(""); 163 } 164 165 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AddNodeFrame.class); 166 167}