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