Jay
2024-11-01 03e8aca3ad6201c0d74e00d4c8d7367cdaaa54f9
提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.frame;
H 2
3 import java.awt.AWTEvent;
4 import java.awt.BorderLayout;
5 import java.awt.Color;
6 import java.awt.Component;
7 import java.awt.Dimension;
8 import java.awt.EventQueue;
9 import java.awt.FlowLayout;
10 import java.awt.GridLayout;
11 import java.awt.Toolkit;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.awt.event.WindowAdapter;
15 import java.awt.event.WindowEvent;
16 import java.net.SocketException;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Executors;
23
24 import javax.swing.JButton;
25 import javax.swing.JFrame;
26 import javax.swing.JLabel;
27 import javax.swing.JOptionPane;
28 import javax.swing.JPanel;
29 import javax.swing.JPasswordField;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTable;
32 import javax.swing.JTextField;
33 import javax.swing.ListSelectionModel;
34 import javax.swing.SwingUtilities;
35 import javax.swing.UIManager;
36 import javax.swing.table.DefaultTableCellRenderer;
37 import javax.swing.table.DefaultTableModel;
38 import javax.swing.table.TableCellRenderer;
39
40 import com.sun.jna.Pointer;
41
42 import com.netsdk.common.BorderEx;
43 import com.netsdk.common.FunctionList;
44 import com.netsdk.common.Res;
45 import com.netsdk.demo.module.*;
46 import com.netsdk.lib.ToolKits;
47 import com.netsdk.lib.NetSDKLib.*;
48
49 class DeviceSearchAndInitFrame extends JFrame{
50     private static final long serialVersionUID = 1L;
51
52     private Object[][] data;
53     
54     private static int index = 0;
55     
56     private int count = 0;
57     
58     // 设备搜索句柄
59     private static LLong m_DeviceSearchHandle = new LLong(0);
60     
61     // key:MAC  value:密码重置方式
62     private static Map<String, Byte> pwdResetHashMap = new HashMap<String, Byte>();
63     
64     // MAC列表,用于设备搜索过滤
65     private static ArrayList<String> macArrayList = new ArrayList<String>();
66     
67     private Component  target     = this;
68     
69     // true表示单播搜索结束
70     private volatile boolean bFlag = true;
71     
72     // 线程池,用于单播搜索
73     private ExecutorService executorService = Executors.newFixedThreadPool(4);
74     
75     public DeviceSearchAndInitFrame() {
76         setTitle(Res.string().getDeviceSearchAndInit());
77         setSize(700, 560);
78         setLayout(new BorderLayout());
79         setResizable(false);
80         setLocationRelativeTo(null);
81         LoginModule.init(null, null);   // 打开工程,初始化
82         
83         try {
84             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
85         } catch (Exception e) {
86             e.printStackTrace();
87         } 
88      
89         deviceSearchPanel = new DeviceSearchPanel();
90         deviceSearchResultShowPanel = new DeviceSearchResultShowListPanel();
91         deviceIntPanel = new DeviceInitPanel();
92
93         add(deviceSearchPanel, BorderLayout.NORTH);
94         add(deviceSearchResultShowPanel, BorderLayout.CENTER);
95         add(deviceIntPanel, BorderLayout.SOUTH);
96         
97         enableEvents(WindowEvent.WINDOW_EVENT_MASK);
98     }
99     
100     @Override
101     protected void processWindowEvent(WindowEvent e) {
102         // 关闭窗口监听事件
103         if(e.getID() == WindowEvent.WINDOW_CLOSING) {
104             if(!bFlag) {
105                 // 等待单播搜索结束
106                 JOptionPane.showMessageDialog(null, Res.string().getSearchingWait(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
107                 return;
108             } else {                
109                 for(int i=0 ;i<handles.size() ;i++) {
110                     DeviceSearchModule.stopDeviceSearch(handles.get(i));
111                 }                
112                 if(!executorService.isShutdown()) {
113                     executorService.shutdown();            
114                 }
115         
116                 LoginModule.cleanup();   // 关闭工程,释放资源
117                 dispose();    
118                 
119                 SwingUtilities.invokeLater(new Runnable() {
120                     public void run() {
121                         FunctionList demo = new FunctionList();
122                         demo.setVisible(true);
123                     }
124                 });
125             }
126         } 
127         
128         super.processWindowEvent(e);
129     }
130
131     /*
132      * 设备搜索操作面板
133      */
134     private class DeviceSearchPanel extends JPanel {
135         private static final long serialVersionUID = 1L;
136         
137         public DeviceSearchPanel() {
138             BorderEx.set(this, Res.string().getDeviceSearchOperate(), 2);
139             setLayout(new BorderLayout());
140             Dimension dimension = new Dimension();
141             dimension.height = 85;
142             setPreferredSize(dimension);
143     
144             MulticastAndBroadcastDeviceSearchPanel multiAndBroadPanel = new MulticastAndBroadcastDeviceSearchPanel();
145             UnicastDeviceSearchPanel unicastPanel = new UnicastDeviceSearchPanel();
146             
147             add(multiAndBroadPanel, BorderLayout.WEST);
148             add(unicastPanel, BorderLayout.CENTER);
149         }
150     }
151     
152     /*
153      * 设备组播和广播搜索面板(设备搜索)
154      */
155     private class MulticastAndBroadcastDeviceSearchPanel extends JPanel {
156         private static final long serialVersionUID = 1L;
157         
158         public MulticastAndBroadcastDeviceSearchPanel() {
159             BorderEx.set(this, Res.string().getDeviceSearch(), 1);
160             setLayout(new FlowLayout());
161             Dimension dimension = new Dimension();
162             dimension.width = 220;
163             setPreferredSize(dimension);
164             
165             multiAndBroadcastSearchBtn = new JButton(Res.string().getStartSearch());
166             multiAndBroadcastSearchBtn.setPreferredSize(new Dimension(120, 20));
167             add(multiAndBroadcastSearchBtn);
168             
169             multiAndBroadcastSearchBtn.addActionListener(new ActionListener() {        
170                 @Override
171                 public void actionPerformed(ActionEvent arg0) {    
172                     deviceInitBtn.setEnabled(true);
173                     for(int i=0 ;i<handles.size() ;i++) {
174                         DeviceSearchModule.stopDeviceSearch(handles.get(i));
175                     }    
176
177                     // 列表清空
178                     data = new Object[1000][11];
179                     defaultModel = new DefaultTableModel(data, Res.string().getDeviceTableName());
180                     table.setModel(defaultModel);
181
182                     table.getColumnModel().getColumn(0).setPreferredWidth(50);
183                     table.getColumnModel().getColumn(1).setPreferredWidth(80);
184                     table.getColumnModel().getColumn(2).setPreferredWidth(80);
185                     table.getColumnModel().getColumn(3).setPreferredWidth(120);
186                     table.getColumnModel().getColumn(4).setPreferredWidth(80);
187                     table.getColumnModel().getColumn(5).setPreferredWidth(120);
188                     table.getColumnModel().getColumn(6).setPreferredWidth(120);
189                     table.getColumnModel().getColumn(7).setPreferredWidth(140);
190                     table.getColumnModel().getColumn(8).setPreferredWidth(100);
191                     table.getColumnModel().getColumn(9).setPreferredWidth(100);
192                     table.getColumnModel().getColumn(10).setPreferredWidth(100);                    
193                     
194
195                     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
196                     
197                     pwdResetHashMap.clear();
198                     macArrayList.clear();
199                     list.clear();
200                     index = 0;
201                     try {
202                         for(int i=0 ;i<DeviceSearchModule.getHostAddress().size(); i++) {
203                         LLong handle= m_DeviceSearchHandle = DeviceSearchModule.multiBroadcastDeviceSearch(callbackEx,DeviceSearchModule.getHostAddress().get(i));
204                         handles.add(handle);
205                         }
206                     } catch (SocketException e) {
207                         // TODO Auto-generated catch block
208                         e.printStackTrace();
209                     }                
210                 }
211             });
212             
213         }
214     }
215     
216     /*
217      * 设备IP单播搜索面板(设备IP点到点搜索)
218      */
219     private class UnicastDeviceSearchPanel extends JPanel {
220         private static final long serialVersionUID = 1L;
221         
222         public UnicastDeviceSearchPanel() {
223             BorderEx.set(this, Res.string().getDevicePointToPointSearch(), 1);
224             setLayout(new FlowLayout());
225             
226             JLabel startIpLabel = new JLabel(Res.string().getStartIp());
227             JLabel endIpLabel = new JLabel(Res.string().getEndIp());
228             
229             startIpTextField = new JTextField("172.23.3.0");
230             endIpTextField = new JTextField("172.23.3.231");
231             
232             unicastSearchBtn = new JButton(Res.string().getStartSearch());
233             
234             startIpTextField.setPreferredSize(new Dimension(100, 20));
235             endIpTextField.setPreferredSize(new Dimension(100, 20));
236             unicastSearchBtn.setPreferredSize(new Dimension(120, 20));
237             
238             add(startIpLabel);
239             add(startIpTextField);
240             add(endIpLabel);
241             add(endIpTextField);
242             add(unicastSearchBtn);
243             
244             unicastSearchBtn.addActionListener(new ActionListener() {            
245                 @Override
246                 public void actionPerformed(ActionEvent arg0) {        
247                     deviceInitBtn.setEnabled(false);
248                     index = 0;
249                     count = 0;
250                     bFlag = false;
251                     
252                     if(!checkIP()) {
253                         return;
254                     }
255                     
256                     SwingUtilities.invokeLater(new Runnable() {                
257                         @Override
258                         public void run() {
259                             unicastSearchBtn.setEnabled(false);
260                         }
261                     });
262                     
263                     // 清空列表
264                     data = new Object[1000][11];
265                     defaultModel = new DefaultTableModel(data, Res.string().getDeviceTableName());
266                     table.setModel(defaultModel);
267
268                     table.getColumnModel().getColumn(0).setPreferredWidth(50);
269                     table.getColumnModel().getColumn(1).setPreferredWidth(80);
270                     table.getColumnModel().getColumn(2).setPreferredWidth(80);
271                     table.getColumnModel().getColumn(3).setPreferredWidth(120);
272                     table.getColumnModel().getColumn(4).setPreferredWidth(80);
273                     table.getColumnModel().getColumn(5).setPreferredWidth(120);
274                     table.getColumnModel().getColumn(6).setPreferredWidth(120);
275                     table.getColumnModel().getColumn(7).setPreferredWidth(140);
276                     table.getColumnModel().getColumn(8).setPreferredWidth(100);
277                     table.getColumnModel().getColumn(9).setPreferredWidth(100);
278                     table.getColumnModel().getColumn(10).setPreferredWidth(100);
279                     
280                     
281                     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
282                     
283                     pwdResetHashMap.clear();
284                     macArrayList.clear();
285                     
286                     for(int i=0 ;i<handles.size() ;i++) {
287                         DeviceSearchModule.stopDeviceSearch(handles.get(i));
288                     }    
289                     
290                     if(count > 0 && count <= 256) {    
291                         executorService.execute(new Runnable() {                    
292                             @Override
293                             public void run() {
294                                 try {
295                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
296                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),startIpTextField.getText(), count, callback);
297                                     }
298                                 } catch (SocketException e) {
299                                     // TODO Auto-generated catch block
300                                     e.printStackTrace();
301                                 }                            
302
303                                 bFlag = true;
304                                 SwingUtilities.invokeLater(new Runnable() {                            
305                                     @Override
306                                     public void run() {
307                                         unicastSearchBtn.setEnabled(true);
308                                     }
309                                 });                            
310                             }
311                         });
312                     } else if(count > 256 && count <= 512){    
313                         executorService.execute(new Runnable() {                    
314                             @Override
315                             public void run() {
316                                 try {
317                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
318                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),startIpTextField.getText(), 256, callback);
319                                     }
320                                 } catch (SocketException e) {
321                                     // TODO Auto-generated catch block
322                                     e.printStackTrace();
323                                 }
324                             }
325                         });
326                         
327                         executorService.execute(new Runnable() {                    
328                             @Override
329                             public void run() {
330                                 String[] szIp = startIpTextField.getText().split("\\.");
331                                 try {
332                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
333                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),DeviceSearchModule.getIp(szIp, 255), count - 256, callback);
334                                     }
335                                 } catch (SocketException e) {
336                                     // TODO Auto-generated catch block
337                                     e.printStackTrace();
338                                 }                        
339
340                                 bFlag = true;
341                                 SwingUtilities.invokeLater(new Runnable() {                            
342                                     @Override
343                                     public void run() {
344                                         unicastSearchBtn.setEnabled(true);
345                                     }
346                                 });                    
347                             }
348                         });            
349                     } else if(count > 512 && count <= 768){    
350                         executorService.execute(new Runnable() {                    
351                             @Override
352                             public void run() {
353                                 try {
354                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
355                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),startIpTextField.getText(), 256, callback);
356                                     }
357                                 } catch (SocketException e) {
358                                     // TODO Auto-generated catch block
359                                     e.printStackTrace();
360                                 }
361                             }
362                         });
363                         
364                         executorService.execute(new Runnable() {                    
365                             @Override
366                             public void run() {
367                                 String[] szIp = startIpTextField.getText().split("\\.");
368                                 try {
369                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
370                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),DeviceSearchModule.getIp(szIp, 255), 256, callback);
371                                     }
372                                 } catch (SocketException e) {
373                                     // TODO Auto-generated catch block
374                                     e.printStackTrace();
375                                 }
376                             }
377                         });
378                         
379                         executorService.execute(new Runnable() {                    
380                             @Override
381                             public void run() {
382                                 String[] szIp = startIpTextField.getText().split("\\.");
383                                 szIp = DeviceSearchModule.getIp(szIp, 255).split("\\.");
384                                 try {
385                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
386                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),DeviceSearchModule.getIp(szIp, 255), count - 512, callback);
387                                     }
388                                 } catch (SocketException e) {
389                                     // TODO Auto-generated catch block
390                                     e.printStackTrace();
391                                 }
392                                                         
393                                 bFlag = true;
394                                 SwingUtilities.invokeLater(new Runnable() {                            
395                                     @Override
396                                     public void run() {
397                                         unicastSearchBtn.setEnabled(true);
398                                     }
399                                 });                            
400                             }
401                         });                        
402                     } else if(count > 768 && count <= 1000){
403                         executorService.execute(new Runnable() {                    
404                             @Override
405                             public void run() {
406                                 try {
407                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
408                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),startIpTextField.getText(), 256, callback);
409                                     }
410                                 } catch (SocketException e) {
411                                     // TODO Auto-generated catch block
412                                     e.printStackTrace();
413                                 }
414                             }
415                         });
416                         
417                         executorService.execute(new Runnable() {                    
418                             @Override
419                             public void run() {
420                                 String[] szIp = startIpTextField.getText().split("\\.");
421                                 try {
422                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
423                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),DeviceSearchModule.getIp(szIp, 255), 256, callback);
424                                     }
425                                 } catch (SocketException e) {
426                                     // TODO Auto-generated catch block
427                                     e.printStackTrace();
428                                 }    
429                             }
430                         });
431                         
432                         executorService.execute(new Runnable() {                    
433                             @Override
434                             public void run() {
435                                 String[] szIp = startIpTextField.getText().split("\\.");
436                                 szIp = DeviceSearchModule.getIp(szIp, 255).split("\\.");
437                                 try {
438                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
439                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),DeviceSearchModule.getIp(szIp, 255), 256, callback);
440                                     }
441                                 } catch (SocketException e) {
442                                     // TODO Auto-generated catch block
443                                     e.printStackTrace();
444                                 }
445                             }
446                         });
447                         
448                         executorService.execute(new Runnable() {                    
449                             @Override
450                             public void run() {
451                                 String[] szIp = startIpTextField.getText().split("\\.");
452                                 szIp = DeviceSearchModule.getIp(szIp, 255).split("\\.");
453                                 szIp = DeviceSearchModule.getIp(szIp, 255).split("\\.");
454                                 try {
455                                     for(int i = 0; i<DeviceSearchModule.getHostAddress().size() ; i++) {
456                                     DeviceSearchModule.unicastDeviceSearch(DeviceSearchModule.getHostAddress().get(i),DeviceSearchModule.getIp(szIp, 255), count - 768, callback);
457                                     }
458                                 } catch (SocketException e) {
459                                     // TODO Auto-generated catch block
460                                     e.printStackTrace();
461                                 }
462                                 
463                                 bFlag = true;
464                                 SwingUtilities.invokeLater(new Runnable() {                            
465                                     @Override
466                                     public void run() {
467                                         unicastSearchBtn.setEnabled(true);
468                                     }
469                                 });                                
470                             }
471                         });
472                     }    
473                 }
474             });
475         }
476     }
477     
478     /*
479      * 设备搜索结果显示列表面板
480      */
481     private class DeviceSearchResultShowListPanel extends JPanel {
482         private static final long serialVersionUID = 1L;
483         
484         public DeviceSearchResultShowListPanel() {
485             BorderEx.set(this, Res.string().getDeviceSearchResult(), 2);
486             setLayout(new BorderLayout());
487             
488             data = new Object[1000][11];            
489             defaultModel = new DefaultTableModel(data, Res.string().getDeviceTableName());
490             table = new JTable(defaultModel) {   // 列表不可编辑
491                 private static final long serialVersionUID = 1L;
492                 @Override
493                 public boolean isCellEditable(int row, int column) {
494                     return false;
495                 }
496             };
497             
498             table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行
499             
500             table.getColumnModel().getColumn(0).setPreferredWidth(50);
501             table.getColumnModel().getColumn(1).setPreferredWidth(80);
502             table.getColumnModel().getColumn(2).setPreferredWidth(80);
503             table.getColumnModel().getColumn(3).setPreferredWidth(120);
504             table.getColumnModel().getColumn(4).setPreferredWidth(80);
505             table.getColumnModel().getColumn(5).setPreferredWidth(120);
506             table.getColumnModel().getColumn(6).setPreferredWidth(120);
507             table.getColumnModel().getColumn(7).setPreferredWidth(140);
508             table.getColumnModel().getColumn(8).setPreferredWidth(100);
509             table.getColumnModel().getColumn(9).setPreferredWidth(100);
510             table.getColumnModel().getColumn(10).setPreferredWidth(100);            
511             
512             table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
513     
514             JScrollPane scrollPane = new JScrollPane(table);
515             scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
516
517             add(scrollPane, BorderLayout.CENTER);
518             
519         }
520     }
521     
522     /*
523      * 设备初始化操作面板
524      */
525     private class DeviceInitPanel extends JPanel {
526         private static final long serialVersionUID = 1L;
527         
528         public DeviceInitPanel() {
529             BorderEx.set(this, Res.string().getDeviceInit(), 2);
530             setLayout(new BorderLayout());
531             Dimension dimension = new Dimension();
532             dimension.height = 55;
533             setPreferredSize(dimension);
534             
535             deviceInitBtn = new JButton(Res.string().getDeviceInit());
536             
537             add(deviceInitBtn, BorderLayout.WEST);
538             
539             deviceInitBtn.addActionListener(new ActionListener() {    
540                 @Override
541                 public void actionPerformed(ActionEvent arg0) {    
542                     int row = -1;
543                     row = table.getSelectedRow(); //获得所选的单行
544                     System.out.println(new String (list.get(row).szLocalIP));
545                     if(defaultModel == null) {
546                         JOptionPane.showMessageDialog(null, Res.string().getPleaseSelectInitializedDevice(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
547                         return;
548                     }
549                     
550                     if(row < 0) {
551                         JOptionPane.showMessageDialog(null, Res.string().getPleaseSelectInitializedDevice(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
552                         return;
553                     }
554                     
555
556                     if(defaultModel.getValueAt(row, 7) == null || String.valueOf(defaultModel.getValueAt(row, 7)).trim().equals("")) {
557                         JOptionPane.showMessageDialog(null, Res.string().getPleaseSelectInitializedDevice(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
558                         return;
559                     }
560
561                     if(defaultModel.getValueAt(row, 1) == null || String.valueOf(defaultModel.getValueAt(row, 1)).trim().equals(Res.string().getInitialized())) {
562                         JOptionPane.showMessageDialog(null, Res.string().getInitialized(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
563                         return;
564                     }                        
565                 
566                     String localIp = new String(list.get(row).szLocalIP).trim();
567                     
568                     String mac = String.valueOf(defaultModel.getValueAt(row, 7)).trim(); // MAC地址
569                     byte passwdReset = pwdResetHashMap.get(mac);  // 密码重置方式
570                             
571                     DevcieInitFrame demo = new DevcieInitFrame(localIp,passwdReset, mac, row, defaultModel, table);
572                     demo.setLocationRelativeTo(null);
573                     demo.setVisible(true);            
574                 }
575             });
576         }
577     }
578     
579     /*
580      *  设备组播和广播搜索回调
581      */
582     private Test_fSearchDevicesCbEx callbackEx = new Test_fSearchDevicesCbEx();
583     private class Test_fSearchDevicesCbEx implements fSearchDevicesCBEx {
584         
585         /*
586          * @Override public void invoke(Pointer pDevNetInfo, Pointer pUserData) {
587          * DEVICE_NET_INFO_EX deviceInfo = new DEVICE_NET_INFO_EX();
588          * ToolKits.GetPointerData(pDevNetInfo, deviceInfo);
589          * 
590          * EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue(); if
591          * (eventQueue != null) { eventQueue.postEvent( new DeviceSearchList(target,
592          * deviceInfo)); } }
593          */
594
595         @Override
596         public void invoke(LLong lSearchHandle, Pointer pDevNetInfo, Pointer pUserData) {
597             // TODO Auto-generated method stub
598             DEVICE_NET_INFO_EX2  deviceInfo =  new DEVICE_NET_INFO_EX2();    
599             ToolKits.GetPointerData(pDevNetInfo, deviceInfo);
600             
601             EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
602             if (eventQueue != null) {
603                 eventQueue.postEvent( new DeviceSearchListEx(target, deviceInfo));
604             }  
605         }    
606     }
607     
608     /*
609      *  设备单播搜索回调
610      */
611     private Test_fSearchDevicesCB callback = new Test_fSearchDevicesCB();
612     private class Test_fSearchDevicesCB implements fSearchDevicesCB {
613         
614         
615           @Override 
616           public void invoke(Pointer pDevNetInfo, Pointer pUserData) {
617           DEVICE_NET_INFO_EX deviceInfo = new DEVICE_NET_INFO_EX();
618           ToolKits.GetPointerData(pDevNetInfo, deviceInfo);
619           
620           EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
621           if(eventQueue != null) 
622           { 
623               eventQueue.postEvent( new DeviceSearchList(target,deviceInfo)); 
624               } 
625           }         
626     }
627     
628     /*
629      *  设备搜索的信息处理
630      */
631     class DeviceSearchList extends AWTEvent {
632         private static final long serialVersionUID = 1L;
633         public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
634         
635         private DEVICE_NET_INFO_EX deviceInfo;
636         
637         public DeviceSearchList(Object target,
638                                 DEVICE_NET_INFO_EX deviceInfo) {
639             super(target,EVENT_ID);
640
641             this.deviceInfo = deviceInfo;
642         }
643         
644         public DEVICE_NET_INFO_EX getDeviceInfo() {
645             return deviceInfo;
646         }    
647     }
648     
649     
650     
651     /*
652      *  设备搜索的信息处理
653      */
654     class DeviceSearchListEx extends AWTEvent {
655         private static final long serialVersionUID = 1L;
656         public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
657         
658         private DEVICE_NET_INFO_EX2 deviceInfo;
659         
660         public DeviceSearchListEx(Object target,
661                                 DEVICE_NET_INFO_EX2 deviceInfo) {
662             super(target,EVENT_ID);
663
664             this.deviceInfo = deviceInfo;
665         }
666         
667         public DEVICE_NET_INFO_EX2 getDeviceInfo() {
668             return deviceInfo;
669         }    
670     }
671     
672     @Override
673     protected void processEvent( AWTEvent event)
674     {
675         if ( event instanceof DeviceSearchListEx )
676         {
677             
678             DeviceSearchListEx ev = (DeviceSearchListEx) event;
679             
680             DEVICE_NET_INFO_EX2 deviceInfo =  ev.getDeviceInfo();            
681             
682             if(!macArrayList.contains(new String(deviceInfo.stuDevInfo.szMac))) {  
683                 list.add(deviceInfo);                
684                 if(index < 1000) {   // 此demo,只显示1000行搜索结果    
685                     macArrayList.add(new String(deviceInfo.stuDevInfo.szMac));
686
687                     // 序号
688                     defaultModel.setValueAt(index + 1, index, 0);
689                     
690                     // 初始化状态
691                     defaultModel.setValueAt(Res.string().getInitStateInfo(deviceInfo.stuDevInfo.byInitStatus & 0x03), index, 1);
692                     
693                     // IP版本
694                     defaultModel.setValueAt("IPV" + String.valueOf(deviceInfo.stuDevInfo.iIPVersion), index, 2);
695                     
696                     // IP
697                     if(!new String(deviceInfo.stuDevInfo.szIP).trim().isEmpty()) {
698                         defaultModel.setValueAt(new String(deviceInfo.stuDevInfo.szIP).trim(), index, 3);
699                     } else {
700                         defaultModel.setValueAt("", index, 3);
701                     }
702                     
703                     // 端口号
704                     defaultModel.setValueAt(String.valueOf(deviceInfo.stuDevInfo.nPort), index, 4);
705                     
706                     // 子网掩码
707                     if(!new String(deviceInfo.stuDevInfo.szSubmask).trim().isEmpty()) {
708                         defaultModel.setValueAt(new String(deviceInfo.stuDevInfo.szSubmask).trim(), index, 5);
709                     } else {
710                         defaultModel.setValueAt("", index, 5);
711                     }            
712                     
713                     // 网关
714                     if(!new String(deviceInfo.stuDevInfo.szGateway).trim().isEmpty()) {
715                         defaultModel.setValueAt(new String(deviceInfo.stuDevInfo.szGateway).trim(), index, 6);
716                     } else {
717                         defaultModel.setValueAt("", index, 6);
718                     }
719                     
720                     // MAC地址
721                     if(!new String(deviceInfo.stuDevInfo.szMac).trim().isEmpty()) {
722                         defaultModel.setValueAt(new String(deviceInfo.stuDevInfo.szMac).trim(), index, 7);
723                     } else {
724                         defaultModel.setValueAt("", index, 7);
725                     }
726                     
727                     // 设备类型
728                     if(!new String(deviceInfo.stuDevInfo.szDeviceType).trim().isEmpty()) {
729                         defaultModel.setValueAt(new String(deviceInfo.stuDevInfo.szDeviceType).trim(), index, 8);
730                     } else {
731                         defaultModel.setValueAt("", index, 8);
732                     }
733                     
734                     // 详细类型
735                     if(!new String(deviceInfo.stuDevInfo.szNewDetailType).trim().isEmpty()) {
736                         defaultModel.setValueAt(new String(deviceInfo.stuDevInfo.szNewDetailType).trim(), index, 9);
737                     } else {
738                         defaultModel.setValueAt("", index, 9);
739                     }
740                     
741                     // HTTP端口号
742                     defaultModel.setValueAt(String.valueOf(deviceInfo.stuDevInfo.nHttpPort), index, 10);
743             
744                     // 将MAC地址   跟 密码重置方式,放进容器
745                     pwdResetHashMap.put(new String(deviceInfo.stuDevInfo.szMac).trim(), deviceInfo.stuDevInfo.byPwdResetWay);
746         
747                     for(int i = 0; i < 11; i++) {
748                         table.getColumnModel().getColumn(i).setCellRenderer(new MyTableCellRender());
749                     }
750                     table.updateUI();
751
752                     index++;
753                 }
754             }
755         }
756         
757         else if ( event instanceof DeviceSearchList )
758         {
759             
760             DeviceSearchList ev = (DeviceSearchList) event;
761             
762             DEVICE_NET_INFO_EX deviceInfo =  ev.getDeviceInfo();
763
764             if(!macArrayList.contains(new String(deviceInfo.szMac))) {  
765                 if(index < 1000) {   // 此demo,只显示1000行搜索结果    
766                     macArrayList.add(new String(deviceInfo.szMac));
767
768                     // 序号
769                     defaultModel.setValueAt(index + 1, index, 0);
770                     
771                     // 初始化状态
772                     defaultModel.setValueAt(Res.string().getInitStateInfo(deviceInfo.byInitStatus & 0x03), index, 1);
773                     
774                     // IP版本
775                     defaultModel.setValueAt("IPV" + String.valueOf(deviceInfo.iIPVersion), index, 2);
776                     
777                     // IP
778                     if(!new String(deviceInfo.szIP).trim().isEmpty()) {
779                         defaultModel.setValueAt(new String(deviceInfo.szIP).trim(), index, 3);
780                     } else {
781                         defaultModel.setValueAt("", index, 3);
782                     }
783                     
784                     // 端口号
785                     defaultModel.setValueAt(String.valueOf(deviceInfo.nPort), index, 4);
786                     
787                     // 子网掩码
788                     if(!new String(deviceInfo.szSubmask).trim().isEmpty()) {
789                         defaultModel.setValueAt(new String(deviceInfo.szSubmask).trim(), index, 5);
790                     } else {
791                         defaultModel.setValueAt("", index, 5);
792                     }            
793                     
794                     // 网关
795                     if(!new String(deviceInfo.szGateway).trim().isEmpty()) {
796                         defaultModel.setValueAt(new String(deviceInfo.szGateway).trim(), index, 6);
797                     } else {
798                         defaultModel.setValueAt("", index, 6);
799                     }
800                     
801                     // MAC地址
802                     if(!new String(deviceInfo.szMac).trim().isEmpty()) {
803                         defaultModel.setValueAt(new String(deviceInfo.szMac).trim(), index, 7);
804                     } else {
805                         defaultModel.setValueAt("", index, 7);
806                     }
807                     
808                     // 设备类型
809                     if(!new String(deviceInfo.szDeviceType).trim().isEmpty()) {
810                         defaultModel.setValueAt(new String(deviceInfo.szDeviceType).trim(), index, 8);
811                     } else {
812                         defaultModel.setValueAt("", index, 8);
813                     }
814                     
815                     // 详细类型
816                     if(!new String(deviceInfo.szNewDetailType).trim().isEmpty()) {
817                         defaultModel.setValueAt(new String(deviceInfo.szNewDetailType).trim(), index, 9);
818                     } else {
819                         defaultModel.setValueAt("", index, 9);
820                     }
821                     
822                     // HTTP端口号
823                     defaultModel.setValueAt(String.valueOf(deviceInfo.nHttpPort), index, 10);
824     
825                     // 将MAC地址   跟 密码重置方式,放进容器
826                     pwdResetHashMap.put(new String(deviceInfo.szMac).trim(), deviceInfo.byPwdResetWay);
827         
828                     for(int i = 0; i < 11; i++) {
829                         table.getColumnModel().getColumn(i).setCellRenderer(new MyTableCellRender());
830                     }
831                     table.updateUI();
832
833                     index++;
834                 }
835             }
836         }
837         
838         else    
839         {
840             super.processEvent( event );   
841         }
842     } 
843     
844     private static class MyTableCellRender implements TableCellRenderer {
845         public MyTableCellRender() {}
846         
847         DefaultTableCellRenderer dCellRenderer = new DefaultTableCellRenderer();
848         
849         @Override
850         public Component getTableCellRendererComponent(JTable table,
851                 Object value, boolean isSelect, boolean hasFocus, int row, int colum) {
852             
853             Component component = dCellRenderer.getTableCellRendererComponent(table, value, 
854                     isSelect, hasFocus, row, colum);
855             if(String.valueOf(defaultModel.getValueAt(row, 1)).trim().equals(Res.string().getNotInitialized())) { // 未初始化,字体颜色变红
856                 component.setForeground(Color.RED);
857             } else {
858                 component.setForeground(Color.BLACK);
859             }
860             
861             // 列表显示居中
862             dCellRenderer.setHorizontalAlignment(JLabel.CENTER);
863             table.setDefaultRenderer(Object.class, dCellRenderer);    
864             
865             return component;
866         }
867     }
868     
869     /*
870      * 检查设备IP点到点搜索的IP范围
871      */
872     private boolean checkIP() {
873         String[] startIp = startIpTextField.getText().split("\\.");
874         
875         String[] endIp = endIpTextField.getText().split("\\.");
876         
877         if(startIpTextField.getText().isEmpty()) {
878             JOptionPane.showMessageDialog(null, Res.string().getInputDeviceIP(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
879             return false;
880         }
881         
882         if(endIpTextField.getText().isEmpty()) {
883             JOptionPane.showMessageDialog(null, Res.string().getInputDeviceIP(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
884             return false;
885         }
886
887         if(!startIp[0].equals(endIp[0])) {
888             JOptionPane.showMessageDialog(null, Res.string().getCheckIp(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
889             return false;
890         }
891         
892         if(!startIp[1].equals(endIp[1])) {
893             JOptionPane.showMessageDialog(null, Res.string().getCheckIp(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
894             return false;
895         }
896         
897         if(Integer.parseInt(startIp[2]) > Integer.parseInt(endIp[2])) {
898             JOptionPane.showMessageDialog(null, Res.string().getCheckIp(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
899             return false;
900         }
901         
902         if(Integer.parseInt(startIp[2]) == Integer.parseInt(endIp[2])
903                 && Integer.parseInt(startIp[3]) > Integer.parseInt(endIp[3])) {
904             JOptionPane.showMessageDialog(null, Res.string().getCheckIp(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
905             return false;
906         }
907         
908         count = (Integer.parseInt(endIp[2]) - Integer.parseInt(startIp[2])) * 256
909                 + Integer.parseInt(endIp[3]) - Integer.parseInt(startIp[3]) + 1;
910         
911         if(count > 1000) {
912             JOptionPane.showMessageDialog(null, Res.string().getControlScope(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
913             return false;
914         }
915         
916         return true;
917     }
918     
919     private DeviceSearchPanel deviceSearchPanel;
920     private DeviceSearchResultShowListPanel deviceSearchResultShowPanel;
921     private DeviceInitPanel deviceIntPanel;
922     
923     private JButton deviceInitBtn;
924     private JButton multiAndBroadcastSearchBtn;
925     private JButton unicastSearchBtn;
926     
927     private JTextField startIpTextField;
928     private JTextField endIpTextField;
929     
930     // 列表
931     private static DefaultTableModel defaultModel;
932     private static JTable table;
933     private static List<DEVICE_NET_INFO_EX2> list =new ArrayList<DEVICE_NET_INFO_EX2>();
934     private static List<LLong> handles =new ArrayList<LLong>();
935 }
936
937 class DevcieInitFrame extends JFrame {
938     /**
939      * 
940      */
941     private static final long serialVersionUID = 1L;
942     byte passwdReset; 
943     String localIp;
944     String mac;
945     int row;
946     DefaultTableModel defaultModel;
947     JTable table;
948     
949     public DevcieInitFrame(String localIp,byte passwdReset, String mac, int row, DefaultTableModel defaultModel, JTable table) {
950         setTitle(Res.string().getDeviceInit());
951         setSize(300, 350);
952         setLayout(new BorderLayout());
953         setResizable(false);
954         
955         this.passwdReset = passwdReset;
956         this.localIp = localIp;
957         this.mac = mac;
958         this.row = row;
959         this.defaultModel = defaultModel;
960         this.table = table;
961         
962         initPanel = new InitPanel();
963         
964         add(initPanel, BorderLayout.CENTER);
965         
966         addWindowListener(new WindowAdapter() {
967             public void windowClosing(WindowEvent e) {
968                 dispose();    
969             }
970         });    
971     }
972     
973     private class InitPanel extends JPanel {
974         private static final long serialVersionUID = 1L;
975         
976         public InitPanel() {
977             BorderEx.set(this, Res.string().getDeviceInit(), 2);
978             setLayout(new BorderLayout());
979             
980             JPanel panel_1 = new JPanel();
981             JPanel panel_2 = new JPanel();
982             
983             add(panel_1, BorderLayout.CENTER);
984             add(panel_2, BorderLayout.SOUTH);
985             
986             panel_1.setLayout(new GridLayout(10, 1));
987             panel_2.setLayout(new BorderLayout());
988             
989             JLabel userLabel = new JLabel(Res.string().getUserName() + " : ");
990             JLabel passwdLabel = new JLabel(Res.string().getPassword() + " : ");    
991             JLabel passwdLabelEx = new JLabel(Res.string().getConfirmPassword() + " : ");
992             JTextField userTextField = new JTextField("admin");
993             passwdPasswordField = new JPasswordField("admin123");
994             passwdPasswordFieldEx = new JPasswordField("admin123");
995             
996             panel_1.add(userLabel);
997             panel_1.add(userTextField);
998             panel_1.add(passwdLabel);
999             panel_1.add(passwdPasswordField);
1000             panel_1.add(passwdLabelEx);
1001             panel_1.add(passwdPasswordFieldEx);
1002             
1003             userTextField.setEnabled(false);
1004             
1005             if((passwdReset >> 1 & 0x01) == 0) {   // 手机号
1006                 JLabel phoneLabel = new JLabel(Res.string().getPhone() + " : ");
1007                 phoneTextField = new JTextField();
1008                 panel_1.add(phoneLabel);
1009                 panel_1.add(phoneTextField);
1010             } else if((passwdReset >> 1 & 0x01) == 1) {  // 邮箱
1011                 JLabel mailLabel = new JLabel(Res.string().getMail() + " : ");
1012                 mailTextField = new JTextField();
1013                 panel_1.add(mailLabel);
1014                 panel_1.add(mailTextField);
1015             }
1016             
1017             deviceInitBtn = new JButton(Res.string().getDeviceInit());
1018             panel_2.add(deviceInitBtn, BorderLayout.CENTER);
1019             
1020             deviceInitBtn.addActionListener(new ActionListener() {        
1021                 @Override
1022                 public void actionPerformed(ActionEvent arg0) {    
1023                     // 密码判空
1024                     if(new String(passwdPasswordField.getPassword()).equals("")) {
1025                         JOptionPane.showMessageDialog(null, Res.string().getInputPassword(), 
1026                                   Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1027                         return;
1028                     }
1029                     
1030                     // 确认密码判空
1031                     if(new String(passwdPasswordFieldEx.getPassword()).equals("")) {
1032                         JOptionPane.showMessageDialog(null, Res.string().getInputConfirmPassword(), 
1033                                   Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1034                         return;
1035                     }
1036                     
1037                     // 密码确认
1038                     if(!new String(passwdPasswordField.getPassword())
1039                             .equals(new String(passwdPasswordFieldEx.getPassword()))) {
1040                         JOptionPane.showMessageDialog(null, Res.string().getInconsistent(), 
1041                                   Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1042                         return;
1043                     }
1044                     
1045                     // 获取手机或邮箱
1046                     String phone_mail = "";
1047                     if((passwdReset >> 1 & 0x01) == 0) {
1048                         phone_mail = phoneTextField.getText();                            
1049                     } else if((passwdReset >> 1 & 0x01) == 1) {
1050                         phone_mail = mailTextField.getText();
1051                     }    
1052                     
1053                     // 手机或邮箱判空
1054                     if(phone_mail.equals("")) {
1055                         if((passwdReset >> 1 & 0x01) == 0) {   // 手机号
1056                             JOptionPane.showMessageDialog(null, Res.string().getInputPhone(), 
1057                                       Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1058                             
1059                             return;
1060                         } else if((passwdReset >> 1 & 0x01) == 1) {  // 邮箱
1061                             JOptionPane.showMessageDialog(null, Res.string().getInputMail(), 
1062                                       Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1063                             
1064                             return;
1065                         }
1066                     }
1067
1068                     // 初始化
1069                     if(DeviceInitModule.initDevAccount(localIp,mac, new String(passwdPasswordField.getPassword()), phone_mail, passwdReset)) {
1070                         dispose();
1071                         
1072                         defaultModel.setValueAt(Res.string().getInitialized(), row, 1);
1073                         
1074                         for(int i = 0; i < 11; i++) {
1075                             table.getColumnModel().getColumn(i).setCellRenderer(new MyTableCellRender(defaultModel));
1076                         }
1077                         table.updateUI();
1078                         
1079                         JOptionPane.showMessageDialog(null, Res.string().getDeviceInit() + Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
1080                     } else {
1081                         JOptionPane.showMessageDialog(null, Res.string().getDeviceInit() + Res.string().getFailed() + "," + ToolKits.getErrorCodeShow(), 
1082                                   Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1083                     }    
1084                 }
1085             });
1086         }
1087     }
1088     
1089     private static class MyTableCellRender implements TableCellRenderer {
1090         DefaultTableModel defaultModel;
1091         public MyTableCellRender(DefaultTableModel defaultModel) {
1092             this.defaultModel = defaultModel;
1093         }
1094         
1095         DefaultTableCellRenderer dCellRenderer = new DefaultTableCellRenderer();
1096         
1097         @Override
1098         public Component getTableCellRendererComponent(JTable table,
1099                 Object value, boolean isSelect, boolean hasFocus, int row, int colum) {
1100             
1101             Component component = dCellRenderer.getTableCellRendererComponent(table, value, 
1102                     isSelect, hasFocus, row, colum);
1103             if(String.valueOf(defaultModel.getValueAt(row, 1)).trim().equals(Res.string().getNotInitialized())) { // 未初始化,字体颜色变红
1104                 component.setForeground(Color.RED);
1105             } else {
1106                 component.setForeground(Color.BLACK);
1107             }
1108             
1109             // 列表显示居中
1110             dCellRenderer.setHorizontalAlignment(JLabel.CENTER);
1111             table.setDefaultRenderer(Object.class, dCellRenderer);    
1112             
1113             return component;
1114         }
1115     }
1116     
1117     private InitPanel initPanel;
1118     private JPasswordField passwdPasswordField;
1119     private JPasswordField passwdPasswordFieldEx;
1120     private JTextField phoneTextField;
1121     private JTextField mailTextField;
1122     private JButton deviceInitBtn;
1123
1124 }
1125 public class DeviceSearchAndInit {
1126     public static void main(String[] args) {    
1127         SwingUtilities.invokeLater(new Runnable() {
1128             public void run() {
1129                 DeviceSearchAndInitFrame demo = new DeviceSearchAndInitFrame();    
1130                 demo.setLocationRelativeTo(null);
1131                 demo.setVisible(true);
1132             }
1133         });        
1134     }
1135 }