package ciscoroutertool.gui; import ciscoroutertool.utils.Host; import javax.swing.*; import java.net.InetAddress; import java.net.UnknownHostException; /** * Allows a user to add a new device * @version 0.01ALPHA * @author Andrew Johnston */ public class NewDeviceDialog extends javax.swing.JFrame { /** * The parent window of the device dialog which will receive the input. */ private MainGUI parent; /** * Creates new form NewDeviceDialog */ public NewDeviceDialog(MainGUI p) { parent = p; initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); fieldIPAddr = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); btnCloseButton = new javax.swing.JButton(); btnAddDevice = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); sshUsername = new javax.swing.JTextField(); sshPassword = new javax.swing.JPasswordField(); checkNeedsEnable = new javax.swing.JCheckBox(); jLabel6 = new javax.swing.JLabel(); enablePassword = new javax.swing.JPasswordField(); jButton1.setText("jButton1"); setTitle("Add a New Device"); jLabel1.setText("Please enter in the following information to add the device to the scan: "); jLabel2.setText("IP Address: "); jLabel3.setText("Connection Details: "); btnCloseButton.setText("Close"); btnCloseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCloseButtonActionPerformed(evt); } }); btnAddDevice.setText("Add Device"); btnAddDevice.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddDeviceActionPerformed(evt); } }); jLabel4.setText("SSH Username: "); jLabel5.setText("SSH Password: "); sshPassword.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sshPasswordActionPerformed(evt); } }); checkNeedsEnable.setText("This router requires superuser privileges"); checkNeedsEnable.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { checkNeedsEnableActionPerformed(evt); } }); jLabel6.setText("Superuser password: "); enablePassword.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnAddDevice) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnCloseButton) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fieldIPAddr, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(checkNeedsEnable) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(sshUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(sshPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(enablePassword, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))))) .addContainerGap(30, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(fieldIPAddr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(sshUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(sshPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(checkNeedsEnable) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(enablePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnCloseButton) .addComponent(btnAddDevice)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents /** * Closes the window. * @param evt The ActionEvent object with relevant data */ private void btnCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseButtonActionPerformed this.dispose(); }//GEN-LAST:event_btnCloseButtonActionPerformed private void sshPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sshPasswordActionPerformed //FIX ME: Not used }//GEN-LAST:event_sshPasswordActionPerformed /** * Adds the device to the current scanning configuration. * @param evt The ActionEvent object with relevant data */ private void btnAddDeviceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddDeviceActionPerformed InetAddress hostname = null; String host = fieldIPAddr.getText().trim(); try { hostname = InetAddress.getByName(host); } catch (UnknownHostException ex) { JOptionPane.showMessageDialog(this, "Unknown Host", "Please reenter the host", JOptionPane.ERROR_MESSAGE); } String username = sshUsername.getText(); String password = new String(sshPassword.getPassword()); Host newHost = new Host(hostname, username, password); if (checkNeedsEnable.isSelected()) { newHost.setEnable(true); newHost.setEnablePass(new String(enablePassword.getPassword())); } parent.updateTable(newHost); this.dispose(); }//GEN-LAST:event_btnAddDeviceActionPerformed /** * Handles the click of the "Needs superuser privileges" box * @param evt */ private void checkNeedsEnableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkNeedsEnableActionPerformed enablePassword.setEnabled(checkNeedsEnable.isSelected()); }//GEN-LAST:event_checkNeedsEnableActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAddDevice; private javax.swing.JButton btnCloseButton; private javax.swing.JCheckBox checkNeedsEnable; private javax.swing.JPasswordField enablePassword; private javax.swing.JTextField fieldIPAddr; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPasswordField sshPassword; private javax.swing.JTextField sshUsername; // End of variables declaration//GEN-END:variables }