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