001package jmri.jmrix.ieee802154.xbee.swing.nodeconfig;
002
003import com.digi.xbee.api.RemoteXBeeDevice;
004import com.digi.xbee.api.models.XBee16BitAddress;
005import com.digi.xbee.api.models.XBee64BitAddress;
006import java.awt.Container;
007import java.awt.FlowLayout;
008import java.awt.event.WindowEvent;
009import javax.swing.BoxLayout;
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012import jmri.jmrix.ieee802154.xbee.XBeeNode;
013import jmri.jmrix.ieee802154.xbee.XBeeTrafficController;
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Frame for Editing Nodes
019 *
020 * @author Bob Jacobsen Copyright (C) 2004
021 * @author Dave Duchamp Copyright (C) 2004
022 * @author Paul Bender Copyright (C) 2013,2016,2018
023 */
024public class XBeeEditNodeFrame extends jmri.jmrix.ieee802154.swing.nodeconfig.EditNodeFrame {
025
026    private XBeeTrafficController xtc = null;
027    private javax.swing.JTextField nodeIdentifierField = new javax.swing.JTextField();
028    private XBeeNodeConfigFrame parent = null;
029
030
031    /**
032     * Constructor method
033     *
034     * @param tc the XBeeTrafficController associated with this connection.
035     * @param node Xbee node details
036     * @param source the XBeeNodeConfigFrame that started this add.
037     */
038    public XBeeEditNodeFrame(XBeeTrafficController tc,XBeeNode node,XBeeNodeConfigFrame source) {
039        super(tc,node);
040        xtc = tc;
041        parent = source;
042        curNode = node;
043    }
044
045    /**
046     * Initialize the config window
047     */
048    @Override
049    public void initComponents() {
050        setTitle(Bundle.getMessage("EditNodeWindowTitle"));
051        Container contentPane = getContentPane();
052        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
053
054        // Set up node address and node type
055        JPanel panel = new JPanel();
056        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
057        panel.add(new JLabel(Bundle.getMessage("LabelNodeAddress") + " "));
058        panel.add(nodeAddrField);
059        nodeAddrField.setToolTipText(Bundle.getMessage("TipNodeAddress"));
060        panel.add(new JLabel(Bundle.getMessage("LabelNodeAddress64") + " "));
061        panel.add(nodeAddr64Field);
062        nodeAddr64Field.setToolTipText(Bundle.getMessage("TipNodeAddress64"));
063        panel.add(new JLabel(Bundle.getMessage("LabelNodeIdentifier") + " "));
064        panel.add(nodeIdentifierField);
065        nodeIdentifierField.setToolTipText(Bundle.getMessage("TipNodeIdentifier"));
066
067        initAddressBoxes();
068        contentPane.add(panel);
069
070        StreamConfigPane streamPane = StreamConfigPane.createPanel((XBeeNode)curNode);
071        streamPane.setXBeeNode((XBeeNode)curNode);
072        contentPane.add(streamPane);
073
074
075        // Set up buttons
076        JPanel panel4 = new JPanel();
077        panel4.setLayout(new FlowLayout());
078        editButton.setText(Bundle.getMessage("ButtonEdit"));
079        editButton.setVisible(true);
080        editButton.setToolTipText(Bundle.getMessage("TipEditButton"));
081        editButton.addActionListener(new java.awt.event.ActionListener() {
082            @Override
083            public void actionPerformed(java.awt.event.ActionEvent e) {
084                editButtonActionPerformed();
085            }
086        });
087        panel4.add(editButton);
088        panel4.add(cancelButton);
089        cancelButton.setText(Bundle.getMessage("ButtonCancel"));
090        cancelButton.setVisible(true);
091        cancelButton.setToolTipText(Bundle.getMessage("TipCancelButton"));
092        panel4.add(cancelButton);
093        cancelButton.addActionListener(new java.awt.event.ActionListener() {
094            @Override
095            public void actionPerformed(java.awt.event.ActionEvent e) {
096                cancelButtonActionPerformed();
097            }
098        });
099        contentPane.add(panel4);
100
101        // pack for display
102        pack();
103    }
104
105    /**
106     * Method to handle edit button
107     */
108    @Override
109    public void editButtonActionPerformed() {
110        if(nodeAddr64Field.getText().equals("") &&
111           nodeAddrField.getText().equals("")) {
112           // no address, just return.
113           return;
114        }
115
116        // to update the node's associated XBee Device, we have to
117        // create a new one, as the library provides no way to update
118        // the RemoteXBeeDevice object.
119
120        // Check that a node with this address does not exist
121        // if the 64 bit address field is blank, use the "Unknown" address".
122        XBee64BitAddress guid;
123        if(!(nodeAddr64Field.getText().equals(""))) {
124           byte GUID[] = jmri.util.StringUtil.bytesFromHexString(nodeAddr64Field.getText());
125           guid = new XBee64BitAddress(GUID);
126        } else {
127           guid = XBee64BitAddress.UNKNOWN_ADDRESS;
128        }
129        // if the 16 bit address field is blank, use the "Unknown" address".
130        XBee16BitAddress address;
131        if(!(nodeAddrField.getText().equals(""))){
132           byte addr[] = jmri.util.StringUtil.bytesFromHexString(nodeAddrField.getText());
133           address = new XBee16BitAddress(addr);
134        } else {
135           address = XBee16BitAddress.UNKNOWN_ADDRESS;
136        }
137        String Identifier = nodeIdentifierField.getText();
138        // create the RemoteXBeeDevice for the node.
139        RemoteXBeeDevice remoteDevice = new RemoteXBeeDevice(xtc.getXBee(),
140              guid,address,Identifier);
141        // save the old remote device
142        RemoteXBeeDevice oldDevice = ((XBeeNode)curNode).getXBee();
143        // and then add the new device to the network
144        xtc.getXBee().getNetwork().addRemoteDevice(remoteDevice);
145
146        // remove the old one from the network
147        xtc.getXBee().getNetwork().removeRemoteDevice(oldDevice);
148
149        //and update the current node.
150        ((XBeeNode)curNode).setXBee(remoteDevice);
151
152        parent.nodeListChanged();
153
154        this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
155    }
156
157    /**
158     * Method to handle cancel button
159     */
160    @Override
161    public void cancelButtonActionPerformed() {
162        // Reset
163        curNode = null;
164        // Switch buttons
165        editButton.setVisible(true);
166        cancelButton.setVisible(false);
167        this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
168    }
169
170    // Initialize the text boxes for the addresses.
171    @Override
172    protected void initAddressBoxes() {
173        nodeAddrField.setText(jmri.util.StringUtil.hexStringFromBytes(curNode.getUserAddress()));
174        nodeAddr64Field.setText(jmri.util.StringUtil.hexStringFromBytes(curNode.getGlobalAddress()));
175        nodeIdentifierField.setText(((XBeeNode)curNode).getIdentifier());
176    }
177
178    //private final static Logger log = LoggerFactory.getLogger(XBeeEditNodeFrame.class);
179
180}