/*
 * DlgSelectConfigManner_serverScan.java
 *
 * Created on 2009年2月13日, 下午4:53
 */

package serverScan;

import configure.DlgConfigure;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author  sunflower
 */
public class DlgSelectConfigManner_serverScan extends javax.swing.JDialog {
    
    /** Creates new form DlgSelectConfigManner_serverScan */
    public DlgSelectConfigManner_serverScan(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        this.setTitle("连接方式--服务器扫描");
        this.setResizable(false);
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jRbtn_configManually = new javax.swing.JRadioButton();
        jRbtn_UseStrategy = new javax.swing.JRadioButton();
        jRbtn_UseStrategy.setSelected(true);
        jbtn_config = new javax.swing.JButton();
        jbtn_confirm = new javax.swing.JButton();
        jbtn_cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        buttonGroup1.add(jRbtn_configManually);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mssql_riskscanner.MSSQL_RiskScannerApp.class).getContext().getResourceMap(DlgSelectConfigManner_serverScan.class);
        jRbtn_configManually.setText(resourceMap.getString("jRbtn_configManually.text")); // NOI18N
        jRbtn_configManually.setName("jRbtn_configManually"); // NOI18N
        jRbtn_configManually.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRbtn_configManuallyMouseClicked(evt);
            }
        });

        buttonGroup1.add(jRbtn_UseStrategy);
        jRbtn_UseStrategy.setSelected(true);
        jRbtn_UseStrategy.setText(resourceMap.getString("jRbtn_UseStrategy.text")); // NOI18N
        jRbtn_UseStrategy.setName("jRbtn_UseStrategy"); // NOI18N
        jRbtn_UseStrategy.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRbtn_UseStrategyMouseClicked(evt);
            }
        });

        jbtn_config.setText(resourceMap.getString("jbtn_config.text")); // NOI18N
        jbtn_config.setName("jbtn_config"); // NOI18N
        jbtn_config.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_configActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jRbtn_UseStrategy)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(jbtn_config))
                    .addComponent(jRbtn_configManually))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRbtn_configManually)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRbtn_UseStrategy)
                    .addComponent(jbtn_config))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtn_confirm.setText(resourceMap.getString("jbtn_confirm.text")); // NOI18N
        jbtn_confirm.setName("jbtn_confirm"); // NOI18N
        jbtn_confirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_confirmActionPerformed(evt);
            }
        });
        jbtn_confirm.registerKeyboardAction(jbtn_confirm.getActionListeners()[0], KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

        jbtn_cancel.setText(resourceMap.getString("jbtn_cancel.text")); // NOI18N
        jbtn_cancel.setName("jbtn_cancel"); // NOI18N
        jbtn_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_cancelActionPerformed(evt);
            }
        });
        jbtn_cancel.registerKeyboardAction(jbtn_cancel.getActionListeners()[0], KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(179, Short.MAX_VALUE)
                .addComponent(jbtn_confirm)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtn_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbtn_cancel, jbtn_confirm});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, 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(jbtn_cancel)
                    .addComponent(jbtn_confirm))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRbtn_configManuallyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRbtn_configManuallyMouseClicked
        // TODO add your handling code here:
        jbtn_config.setEnabled(false);
    }//GEN-LAST:event_jRbtn_configManuallyMouseClicked

    private void jRbtn_UseStrategyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRbtn_UseStrategyMouseClicked
        // TODO add your handling code here:
        jbtn_config.setEnabled(true);
    }//GEN-LAST:event_jRbtn_UseStrategyMouseClicked

    private void jbtn_configActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_configActionPerformed
        // TODO add your handling code here:
        if(dlgConfigure == null)
        {
            dlgConfigure = new DlgConfigure(null, true);
            dlgConfigure.setLocationRelativeTo(this);
        }
        dlgConfigure.setVisible(true);
    }//GEN-LAST:event_jbtn_configActionPerformed

    private void jbtn_confirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_confirmActionPerformed
        // TODO add your handling code here:
        if(jRbtn_UseStrategy.isSelected())
        {
            try {
                //解析configurations\\serverScan\\ip.xml文件，初始化initialize.Alist_serverScanParam
                SAXReader saxReader = new SAXReader();
                String str_directory = "configurations\\serverScan\\ip.xml";
                Document doc = saxReader.read(new File(str_directory));
                
                Element Root = doc.getRootElement();
                List itemList = Root.selectNodes("item");
                Object[] ipAddress;
                
                for(int i=0; i<itemList.size(); i++)
                {
                    Element itemNode = (Element)itemList.get(i);
                    
                    if(itemNode.elementText("isSelected").equals("true"))
                    {
                        if(itemNode.elementText("end_ip") == null) //只设置了begin_ip
                        {
                            ipAddress = new Object[1];
                            ipAddress[0] = itemNode.elementText("begin_ip");
                        }
                        else //设置了begin_ip和end_ip
                        {
                            ipAddress = new Object[2];
                            ipAddress[0] = itemNode.elementText("begin_ip");
                            ipAddress[1] = itemNode.elementText("end_ip");
                        }
                        InitializeForServerScan.Alist_serverScanParam.add(ipAddress);
                    }
                }
                
                if(InitializeForServerScan.Alist_serverScanParam.isEmpty())
                    JOptionPane.showMessageDialog(null, "请选择一项扫描参数！", "提示", JOptionPane.WARNING_MESSAGE);
                
            } catch (DocumentException ex) {
                Logger.getLogger(DlgSelectConfigManner_serverScan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MalformedURLException ex) {
                Logger.getLogger(DlgSelectConfigManner_serverScan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else //手动配置扫描参数
        {
            dlgConfigManually = new DlgConfigManually(null, true);
            dlgConfigManually.setLocationRelativeTo(this);
            dlgConfigManually.setVisible(true);
        }
        
        this.setVisible(false);
}//GEN-LAST:event_jbtn_confirmActionPerformed

    private void jbtn_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_cancelActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_jbtn_cancelActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DlgSelectConfigManner_serverScan dialog = new DlgSelectConfigManner_serverScan(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRbtn_UseStrategy;
    private javax.swing.JRadioButton jRbtn_configManually;
    private javax.swing.JButton jbtn_cancel;
    private javax.swing.JButton jbtn_config;
    private javax.swing.JButton jbtn_confirm;
    // End of variables declaration//GEN-END:variables
    
    private DlgConfigure dlgConfigure;
    private DlgConfigManually dlgConfigManually;
}
