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