package com.iailab.netsdk.demo.frame; import java.awt.AWTEvent; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.net.SocketException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; import com.sun.jna.Pointer; import com.iailab.netsdk.common.BorderEx; import com.iailab.netsdk.common.FunctionList; import com.iailab.netsdk.common.Res; import com.iailab.netsdk.demo.module.*; import com.iailab.netsdk.lib.ToolKits; import com.iailab.netsdk.lib.NetSDKLib.*; class DeviceSearchAndInitFrame extends JFrame{ private static final long serialVersionUID = 1L; private Object[][] data; private static int index = 0; private int count = 0; // 设备搜索句柄 private static LLong m_DeviceSearchHandle = new LLong(0); // key:MAC value:密码重置方式 private static Map pwdResetHashMap = new HashMap(); // MAC列表,用于设备搜索过滤 private static ArrayList macArrayList = new ArrayList(); private Component target = this; // true表示单播搜索结束 private volatile boolean bFlag = true; // 线程池,用于单播搜索 private ExecutorService executorService = Executors.newFixedThreadPool(4); public DeviceSearchAndInitFrame() { setTitle(Res.string().getDeviceSearchAndInit()); setSize(700, 560); setLayout(new BorderLayout()); setResizable(false); setLocationRelativeTo(null); LoginModule.init(null, null); // 打开工程,初始化 try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } deviceSearchPanel = new DeviceSearchPanel(); deviceSearchResultShowPanel = new DeviceSearchResultShowListPanel(); deviceIntPanel = new DeviceInitPanel(); add(deviceSearchPanel, BorderLayout.NORTH); add(deviceSearchResultShowPanel, BorderLayout.CENTER); add(deviceIntPanel, BorderLayout.SOUTH); enableEvents(WindowEvent.WINDOW_EVENT_MASK); } @Override protected void processWindowEvent(WindowEvent e) { // 关闭窗口监听事件 if(e.getID() == WindowEvent.WINDOW_CLOSING) { if(!bFlag) { // 等待单播搜索结束 JOptionPane.showMessageDialog(null, Res.string().getSearchingWait(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE); return; } else { for(int i=0 ;i 0 && count <= 256) { executorService.execute(new Runnable() { @Override public void run() { try { for(int i = 0; i 256 && count <= 512){ executorService.execute(new Runnable() { @Override public void run() { try { for(int i = 0; i 512 && count <= 768){ executorService.execute(new Runnable() { @Override public void run() { try { for(int i = 0; i 768 && count <= 1000){ executorService.execute(new Runnable() { @Override public void run() { try { for(int i = 0; i Integer.parseInt(endIp[2])) { JOptionPane.showMessageDialog(null, Res.string().getCheckIp(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return false; } if(Integer.parseInt(startIp[2]) == Integer.parseInt(endIp[2]) && Integer.parseInt(startIp[3]) > Integer.parseInt(endIp[3])) { JOptionPane.showMessageDialog(null, Res.string().getCheckIp(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return false; } count = (Integer.parseInt(endIp[2]) - Integer.parseInt(startIp[2])) * 256 + Integer.parseInt(endIp[3]) - Integer.parseInt(startIp[3]) + 1; if(count > 1000) { JOptionPane.showMessageDialog(null, Res.string().getControlScope(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return false; } return true; } private DeviceSearchPanel deviceSearchPanel; private DeviceSearchResultShowListPanel deviceSearchResultShowPanel; private DeviceInitPanel deviceIntPanel; private JButton deviceInitBtn; private JButton multiAndBroadcastSearchBtn; private JButton unicastSearchBtn; private JTextField startIpTextField; private JTextField endIpTextField; // 列表 private static DefaultTableModel defaultModel; private static JTable table; private static List list =new ArrayList(); private static List handles =new ArrayList(); } class DevcieInitFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; byte passwdReset; String localIp; String mac; int row; DefaultTableModel defaultModel; JTable table; public DevcieInitFrame(String localIp,byte passwdReset, String mac, int row, DefaultTableModel defaultModel, JTable table) { setTitle(Res.string().getDeviceInit()); setSize(300, 350); setLayout(new BorderLayout()); setResizable(false); this.passwdReset = passwdReset; this.localIp = localIp; this.mac = mac; this.row = row; this.defaultModel = defaultModel; this.table = table; initPanel = new InitPanel(); add(initPanel, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }); } private class InitPanel extends JPanel { private static final long serialVersionUID = 1L; public InitPanel() { BorderEx.set(this, Res.string().getDeviceInit(), 2); setLayout(new BorderLayout()); JPanel panel_1 = new JPanel(); JPanel panel_2 = new JPanel(); add(panel_1, BorderLayout.CENTER); add(panel_2, BorderLayout.SOUTH); panel_1.setLayout(new GridLayout(10, 1)); panel_2.setLayout(new BorderLayout()); JLabel userLabel = new JLabel(Res.string().getUserName() + " : "); JLabel passwdLabel = new JLabel(Res.string().getPassword() + " : "); JLabel passwdLabelEx = new JLabel(Res.string().getConfirmPassword() + " : "); JTextField userTextField = new JTextField("admin"); passwdPasswordField = new JPasswordField("admin123"); passwdPasswordFieldEx = new JPasswordField("admin123"); panel_1.add(userLabel); panel_1.add(userTextField); panel_1.add(passwdLabel); panel_1.add(passwdPasswordField); panel_1.add(passwdLabelEx); panel_1.add(passwdPasswordFieldEx); userTextField.setEnabled(false); if((passwdReset >> 1 & 0x01) == 0) { // 手机号 JLabel phoneLabel = new JLabel(Res.string().getPhone() + " : "); phoneTextField = new JTextField(); panel_1.add(phoneLabel); panel_1.add(phoneTextField); } else if((passwdReset >> 1 & 0x01) == 1) { // 邮箱 JLabel mailLabel = new JLabel(Res.string().getMail() + " : "); mailTextField = new JTextField(); panel_1.add(mailLabel); panel_1.add(mailTextField); } deviceInitBtn = new JButton(Res.string().getDeviceInit()); panel_2.add(deviceInitBtn, BorderLayout.CENTER); deviceInitBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // 密码判空 if(new String(passwdPasswordField.getPassword()).equals("")) { JOptionPane.showMessageDialog(null, Res.string().getInputPassword(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return; } // 确认密码判空 if(new String(passwdPasswordFieldEx.getPassword()).equals("")) { JOptionPane.showMessageDialog(null, Res.string().getInputConfirmPassword(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return; } // 密码确认 if(!new String(passwdPasswordField.getPassword()) .equals(new String(passwdPasswordFieldEx.getPassword()))) { JOptionPane.showMessageDialog(null, Res.string().getInconsistent(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return; } // 获取手机或邮箱 String phone_mail = ""; if((passwdReset >> 1 & 0x01) == 0) { phone_mail = phoneTextField.getText(); } else if((passwdReset >> 1 & 0x01) == 1) { phone_mail = mailTextField.getText(); } // 手机或邮箱判空 if(phone_mail.equals("")) { if((passwdReset >> 1 & 0x01) == 0) { // 手机号 JOptionPane.showMessageDialog(null, Res.string().getInputPhone(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return; } else if((passwdReset >> 1 & 0x01) == 1) { // 邮箱 JOptionPane.showMessageDialog(null, Res.string().getInputMail(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); return; } } // 初始化 if(DeviceInitModule.initDevAccount(localIp,mac, new String(passwdPasswordField.getPassword()), phone_mail, passwdReset)) { dispose(); defaultModel.setValueAt(Res.string().getInitialized(), row, 1); for(int i = 0; i < 11; i++) { table.getColumnModel().getColumn(i).setCellRenderer(new MyTableCellRender(defaultModel)); } table.updateUI(); JOptionPane.showMessageDialog(null, Res.string().getDeviceInit() + Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, Res.string().getDeviceInit() + Res.string().getFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); } } }); } } private static class MyTableCellRender implements TableCellRenderer { DefaultTableModel defaultModel; public MyTableCellRender(DefaultTableModel defaultModel) { this.defaultModel = defaultModel; } DefaultTableCellRenderer dCellRenderer = new DefaultTableCellRenderer(); @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelect, boolean hasFocus, int row, int colum) { Component component = dCellRenderer.getTableCellRendererComponent(table, value, isSelect, hasFocus, row, colum); if(String.valueOf(defaultModel.getValueAt(row, 1)).trim().equals(Res.string().getNotInitialized())) { // 未初始化,字体颜色变红 component.setForeground(Color.RED); } else { component.setForeground(Color.BLACK); } // 列表显示居中 dCellRenderer.setHorizontalAlignment(JLabel.CENTER); table.setDefaultRenderer(Object.class, dCellRenderer); return component; } } private InitPanel initPanel; private JPasswordField passwdPasswordField; private JPasswordField passwdPasswordFieldEx; private JTextField phoneTextField; private JTextField mailTextField; private JButton deviceInitBtn; } public class DeviceSearchAndInit { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { DeviceSearchAndInitFrame demo = new DeviceSearchAndInitFrame(); demo.setLocationRelativeTo(null); demo.setVisible(true); } }); } }