826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
package dell_system;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import dell_targets.Dell_MessageType;
import dell_targets.Dell_tag;
import udptcp.UDPPortAReceiver;
import udptcp.UDPPortBReceiver;
 
public class MessageViewPanel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
 
    private ResourceBundle messages;   // ¹ú¼Ê»¯ÏûÏ¢×ÊÔ´°ü
 
    // UI×é¼þÉùÃ÷
    private JButton btnStart, btnPause, btnClear, btnSend; // ¿ØÖư´Å¥£º¿ªÊ¼¡¢ÔÝÍ£¡¢Çå³ý¡¢·¢ËÍ
    private static JComboBox<String> cbDataType; // ÏÂÀ­¿ò£ºÊý¾ÝÀàÐÍ¡¢Ð­Òé¡¢É豸ID
    private JComboBox<String> cbProtocol;
    private static JComboBox<String> cbDeviceId;
    private static JComboBox<String> cbRemoteDevice; // Ô¶³ÌÉ豸ÏÂÀ­¿ò
    private static JCheckBox chkAutoSave; // ¸´Ñ¡¿ò£º×Ô¶¯±£´æ¡¢ÏÔʾÀ´Ô´¡¢Ê®Áù½øÖÆÏÔʾ¡¢ASCIIÏÔʾ
    private static JCheckBox chkShowSource;
    private static JCheckBox chkHexDisplay;
    private static JCheckBox chkAsciiDisplay;
    private static JTextArea txtDataView; // Êý¾ÝÏÔÊ¾ÇøÓò
    private JScrollPane scrollPane; // ´ø¹ö¶¯ÌõµÄÃæ°å
    private JTextField txtSendData; // ·¢ËÍÊý¾ÝÎı¾¿ò
    private JRadioButton rdoSendHex; // HEX·¢Ë͵¥Ñ¡°´Å¥
    private JRadioButton rdoSendAscii; // ASCII·¢Ë͵¥Ñ¡°´Å¥
    private JCheckBox chkAppendNewline; // »Ø³µ»»Ðи´Ñ¡¿ò
    // Ìí¼Ó¾²Ì¬±äÁ¿¸ú×Ù´°¿Ú¿É¼û״̬
    public static volatile boolean isWindowVisible = false;
    // ×´Ì¬±äÁ¿
    private static boolean isCapturing = false; // ÊÇ·ñÕýÔÚ²¶»ñÊý¾ÝµÄ±êÖ¾
 
    // ´æ´¢ËùÓÐÔ¶³ÌÉ豸(IP:¶Ë¿Ú)
    private static Set<String> remoteDevices = new HashSet<>();
       // ÐÞ¸ÄΪMap´æ´¢É豸¼°Æä±¾µØ¶Ë¿Ú
    private static Map<String, Integer> remoteDeviceLocalPortMap = new HashMap<>();
    // ¹¹Ô캯Êý
    public MessageViewPanel(ResourceBundle messages) {       
        this.messages = messages; // ³õʼ»¯ÏûÏ¢×ÊÔ´°ü        
        initializeUI(); // ³õʼ»¯Óû§½çÃæ
    }
 
    public void addNotify() {
        super.addNotify();
        Window window = SwingUtilities.getWindowAncestor(this);
        if (window != null) {
            isWindowVisible = true; // Ãæ°åÒÑÌí¼Óµ½¿É¼û´°¿Ú
            window.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    isWindowVisible = false;
                }
                
                @Override
                public void windowClosed(WindowEvent e) {
                    isWindowVisible = false;
                }
            });
        }
    }
 
    // ³õʼ»¯UI×é¼þ
    private void initializeUI() {
        setLayout(new GridBagLayout()); // Ê¹ÓÃÍø¸ñ´ü²¼¾Ö
        GridBagConstraints gbc = new GridBagConstraints(); // ²¼¾ÖÔ¼Êø¶ÔÏó
        gbc.insets = new Insets(5, 10, 5, 10); // ÉèÖÃ×é¼þ¼ä¾à£¨ÉÏ¡¢×ó¡¢Ï¡¢ÓÒ£©
        gbc.anchor = GridBagConstraints.WEST; // ×é¼þ¿¿×ó¶ÔÆë
        gbc.fill = GridBagConstraints.HORIZONTAL; // Ë®Æ½Ìî³ä
        setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
 
        // ÏÂÀ­¿òÇøÓò
        gbc.gridx = 0; // ÆðʼÁÐ
        gbc.gridy = 0; // ÆðʼÐÐ
        gbc.gridwidth = 1; // Õ¼ÓÃ1Áпí¶È
        gbc.weightx = 0.25; // Ë®Æ½È¨ÖØ
 
        // Êý¾ÝÀàÐÍÏÂÀ­¿òÃæ°å
        JPanel dataTypePanel = new JPanel(new BorderLayout(5, 0)); // Ê¹Óñ߽粼¾Ö
        dataTypePanel.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        dataTypePanel.add(createLabel(messages.getString("MESSAGE_VIEW_DATA_TYPE")), BorderLayout.WEST); // Ìí¼Ó±êÇ©
        cbDataType = new JComboBox<>(); // ´´½¨ÏÂÀ­¿ò
        loadtyps();// Ìí¼Ó"ËùÓÐÀàÐÍ"Ñ¡Ïî       
        dataTypePanel.add(cbDataType, BorderLayout.CENTER); // ½«ÏÂÀ­¿òÌí¼Óµ½Ãæ°åÖÐÑë
        add(dataTypePanel, gbc); // ½«Ãæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // Ð­ÒéÏÂÀ­¿ò
        gbc.gridx++; // Òƶ¯µ½ÏÂÒ»ÁÐ
        JPanel protocolPanel = new JPanel(new BorderLayout(5, 0)); // Ð­ÒéÑ¡ÔñÃæ°å
        protocolPanel.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        protocolPanel.add(createLabel(messages.getString("MESSAGE_VIEW_PROTOCOL")), BorderLayout.WEST); // Ìí¼ÓЭÒé±êÇ©
        cbProtocol = new JComboBox<>(new String[]{ // ´´½¨Ð­ÒéÏÂÀ­¿ò²¢³õʼ»¯Ñ¡Ïî
                messages.getString("MESSAGE_VIEW_ALL_PROTOCOLS"), // ËùÓÐЭÒé
                "UDP", // UDPЭÒé
                "TCP", // TCPЭÒé
                "MQTT", // MQTTЭÒé
                "HTTP" // HTTPЭÒé
        });
        protocolPanel.add(cbProtocol, BorderLayout.CENTER); // ½«ÏÂÀ­¿òÌí¼Óµ½Ãæ°åÖÐÑë
        add(protocolPanel, gbc); // ½«Ãæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // É豸IDÏÂÀ­¿ò
        gbc.gridx++; // Òƶ¯µ½ÏÂÒ»ÁÐ
        JPanel devicePanel = new JPanel(new BorderLayout(5, 0)); // É豸IDÑ¡ÔñÃæ°å
        devicePanel.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        devicePanel.add(createLabel(messages.getString("MESSAGE_VIEW_DEVICE_ID")), BorderLayout.WEST); // Ìí¼ÓÉ豸ID±êÇ©
        cbDeviceId = new JComboBox<>(); // ´´½¨É豸IDÏÂÀ­¿ò
        cbDeviceId.setEditable(true); // ÉèÖÃΪ¿É±à¼­£¬ÔÊÐíÊÖ¶¯ÊäÈëÉ豸ID
        cbDeviceId.addItem(messages.getString("MESSAGE_VIEW_ALL_DEVICES")); // Ìí¼Ó"ËùÓÐÉ豸"Ñ¡Ïî
        loadDeviceIds(); // ¼ÓÔØÉ豸IDÁбí
        devicePanel.add(cbDeviceId, BorderLayout.CENTER); // ½«ÏÂÀ­¿òÌí¼Óµ½Ãæ°åÖÐÑë
        add(devicePanel, gbc); // ½«Ãæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // Ô¶³ÌÉ豸ÏÂÀ­¿ò
        gbc.gridx++; // Òƶ¯µ½ÏÂÒ»ÁÐ
        JPanel remoteDevicePanel = new JPanel(new BorderLayout(5, 0)); // Ô¶³ÌÉ豸ѡÔñÃæ°å
        remoteDevicePanel.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        remoteDevicePanel.add(createLabel(messages.getString("MESSAGE_VIEW_REMOTE_DEVICE")), BorderLayout.WEST); // Ìí¼ÓÔ¶³ÌÉ豸±êÇ©
        cbRemoteDevice = new JComboBox<>(); // ´´½¨Ô¶³ÌÉ豸ÏÂÀ­¿ò
        cbRemoteDevice.setEditable(true); // ÉèÖÃΪ¿É±à¼­
        cbRemoteDevice.addItem(messages.getString("MESSAGE_VIEW_ALL_DEVICES")); // Ìí¼Ó"ËùÓÐÉ豸"Ñ¡Ïî
        remoteDevicePanel.add(cbRemoteDevice, BorderLayout.CENTER); // ½«ÏÂÀ­¿òÌí¼Óµ½Ãæ°åÖÐÑë
        add(remoteDevicePanel, gbc); // ½«Ãæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // ¸´Ñ¡¿òÇøÓò
        gbc.gridx = 0; // ÖØÖÃÁÐλÖÃ
        gbc.gridy++; // Òƶ¯µ½ÏÂÒ»ÐÐ
        gbc.gridwidth = 4; // ºá¿ç4ÁÐ
        gbc.weightx = 1.0; // Ë®Æ½È¨ÖØ
 
        JPanel checkboxPanel = new JPanel(new GridLayout(1, 4, 6, 0)); // ´´½¨Íø¸ñ²¼¾ÖÃæ°å£¨1ÐÐ4ÁУ©
        checkboxPanel.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        chkAutoSave = createStyledCheckbox(messages.getString("MESSAGE_VIEW_AUTO_SAVE")); // ´´½¨×Ô¶¯±£´æ¸´Ñ¡¿ò
        chkShowSource = createStyledCheckbox(messages.getString("MESSAGE_VIEW_SHOW_SOURCE")); // ´´½¨ÏÔʾÀ´Ô´¸´Ñ¡¿ò
        chkHexDisplay = createStyledCheckbox(messages.getString("MESSAGE_VIEW_HEX_DISPLAY")); // ´´½¨Ê®Áù½øÖÆÏÔʾ¸´Ñ¡¿ò
        chkAsciiDisplay = createStyledCheckbox(messages.getString("MESSAGE_VIEW_ASCII_DISPLAY")); // ´´½¨ASCIIÏÔʾ¸´Ñ¡¿ò
 
        // ½«¸´Ñ¡¿òÌí¼Óµ½Ãæ°å
        checkboxPanel.add(chkAutoSave);
        checkboxPanel.add(chkShowSource);
        checkboxPanel.add(chkHexDisplay);
        checkboxPanel.add(chkAsciiDisplay);
 
        add(checkboxPanel, gbc); // ½«¸´Ñ¡¿òÃæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // Êý¾ÝÏÔÊ¾ÇøÓò
        gbc.gridy++; // Òƶ¯µ½ÏÂÒ»ÐÐ
        gbc.weighty = 1.0; // ´¹Ö±È¨ÖØ
        gbc.fill = GridBagConstraints.BOTH; // Ë«ÏòÌî³ä
 
        txtDataView = new JTextArea(); // ´´½¨Îı¾ÇøÓò
        txtDataView.setFont(new Font("Monospaced", Font.PLAIN, 14)); // ÉèÖõȿí×ÖÌå
        txtDataView.setEditable(false); // ÉèÖÃΪ²»¿É±à¼­
        scrollPane = new JScrollPane(txtDataView); // ´´½¨´ø¹ö¶¯ÌõµÄÃæ°å
        scrollPane.setPreferredSize(new Dimension(800, 400));
        scrollPane.setBorder(BorderFactory.createTitledBorder( // ÉèÖôø±êÌâµÄ±ß¿ò
                BorderFactory.createLineBorder(new Color(180, 180, 220)), // ±ß¿òÑÕÉ«
                messages.getString("MESSAGE_VIEW_DATA_PANEL"), // ±êÌâÎı¾
                TitledBorder.LEFT, // ±êÌâλÖÃ
                TitledBorder.TOP, // ±êÌâλÖÃ
                new Font("΢ÈíÑźÚ", Font.BOLD, 12), // ±êÌâ×ÖÌå
                new Color(70, 130, 180) // ±êÌâÑÕÉ«
                ));
 
        add(scrollPane, gbc); // ½«¹ö¶¯Ãæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // °´Å¥Ãæ°åÇøÓò
        gbc.gridy++; // Òƶ¯µ½ÏÂÒ»ÐÐ
        gbc.weighty = 0; // ÖØÖô¹Ö±È¨ÖØ
        gbc.fill = GridBagConstraints.HORIZONTAL; // Ë®Æ½Ìî³ä
 
        // °´Å¥Ãæ°å
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5)); // Ê¹ÓÃÁ÷ʽ²¼¾Ö£¨×ó¶ÔÆë£©
        buttonPanel.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        btnStart = createStyledButton(messages.getString("MESSAGE_VIEW_START"), new Color(70, 130, 180)); // ´´½¨¿ªÊ¼°´Å¥£¨À¶É«£©
        btnPause = createStyledButton(messages.getString("MESSAGE_VIEW_PAUSE"), new Color(218, 165, 32)); // ´´½¨ÔÝÍ£°´Å¥£¨½ðÉ«£©
        btnClear = createStyledButton(messages.getString("MESSAGE_VIEW_CLEAR"), new Color(205, 92, 92)); // ´´½¨Çå³ý°´Å¥£¨ºìÉ«£©
 
        // ·¢ËÍÏà¹Ø¿Ø¼þ
        txtSendData = new JTextField(30); // ·¢ËÍÊý¾ÝÎı¾¿ò
        txtSendData.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå
        btnSend = createStyledButton(messages.getString("MESSAGE_VIEW_SEND"), new Color(60, 179, 113)); // ´´½¨·¢ËͰ´Å¥£¨ÂÌÉ«£©
 
        // ´´½¨·¢ËÍÑ¡ÏîÃæ°å
        JPanel sendOptionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        sendOptionsPanel.setBackground(new Color(240, 245, 249));
        
        // ´´½¨µ¥Ñ¡°´Å¥×é
        ButtonGroup sendFormatGroup = new ButtonGroup();
        
        // ´´½¨HEX·¢Ë͵¥Ñ¡°´Å¥
        rdoSendHex = createStyledRadioButton(messages.getString("MESSAGE_VIEW_SEND_HEX"));
        // ´´½¨ASCII·¢Ë͵¥Ñ¡°´Å¥
        rdoSendAscii = createStyledRadioButton(messages.getString("MESSAGE_VIEW_SEND_ASCII"));
        
        // ½«µ¥Ñ¡°´Å¥Ìí¼Óµ½°´Å¥×é
        sendFormatGroup.add(rdoSendHex);
        sendFormatGroup.add(rdoSendAscii);
        
        // Ä¬ÈÏÑ¡ÖÐASCII·¢ËÍ
        rdoSendAscii.setSelected(true);
        
        // »Ø³µ»»Ðи´Ñ¡¿ò
        chkAppendNewline = createStyledCheckbox(messages.getString("MESSAGE_VIEW_APPEND_NEWLINE"));
 
        sendOptionsPanel.add(rdoSendHex);
        sendOptionsPanel.add(rdoSendAscii);
        sendOptionsPanel.add(chkAppendNewline);
 
        // ½«°´Å¥Ìí¼Óµ½°´Å¥Ãæ°å
        buttonPanel.add(btnStart);
        buttonPanel.add(btnPause);
        buttonPanel.add(btnClear);
        buttonPanel.add(txtSendData);
        buttonPanel.add(btnSend);
        buttonPanel.add(sendOptionsPanel);
 
        add(buttonPanel, gbc); // ½«°´Å¥Ãæ°åÌí¼Óµ½Ö÷Ãæ°å
 
        // ÉèÖð´Å¥¶¯×÷
        setupActions();
    }
 
    // ´´½¨´øÑùʽµÄ±êÇ©
    private JLabel createLabel(String text) {
        JLabel label = new JLabel(text); // ´´½¨±êÇ©
        label.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 12)); // ÉèÖÃ×ÖÌå
        label.setForeground(new Color(70, 70, 70)); // ÉèÖÃǰ¾°É«
        return label;
    }
 
    // ´´½¨´øÑùʽµÄ°´Å¥
    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text); // ´´½¨°´Å¥
        button.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 12)); // ÉèÖÃ×ÖÌå
        button.setBackground(bgColor); // ÉèÖñ³¾°É«
        button.setForeground(Color.WHITE); // ÉèÖÃǰ¾°É«£¨°×É«£©
        button.setFocusPainted(false); // ½ûÓý¹µã±ß¿ò
        button.setBorder(BorderFactory.createCompoundBorder( // ÉèÖø´ºÏ±ß¿ò
                BorderFactory.createLineBorder(bgColor.darker(), 1), // Íⲿ±ß¿ò£¨ÉîÉ«£©
                BorderFactory.createEmptyBorder(5, 15, 5, 15) // ÄÚ²¿±ß¾à
                ));
        return button;
    }
 
    // ´´½¨´øÑùʽµÄ¸´Ñ¡¿ò
    private JCheckBox createStyledCheckbox(String text) {
        JCheckBox checkBox = new JCheckBox(text); // ´´½¨¸´Ñ¡¿ò
        checkBox.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå
        checkBox.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        checkBox.setForeground(new Color(70, 70, 70)); // ÉèÖÃǰ¾°É«
        return checkBox;
    }
    
    // ´´½¨´øÑùʽµÄµ¥Ñ¡°´Å¥
    private JRadioButton createStyledRadioButton(String text) {
        JRadioButton radioButton = new JRadioButton(text); // ´´½¨µ¥Ñ¡°´Å¥
        radioButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 12)); // ÉèÖÃ×ÖÌå
        radioButton.setBackground(new Color(240, 245, 249)); // ÉèÖñ³¾°É«
        radioButton.setForeground(new Color(70, 70, 70)); // ÉèÖÃǰ¾°É«
        
        // ÃÀ»¯Ñ¡ÖÐЧ¹û
        radioButton.setIcon(new RadioButtonIcon(false));
        radioButton.setSelectedIcon(new RadioButtonIcon(true));
        
        // Ìí¼ÓÊó±êÐüͣЧ¹û
        radioButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                radioButton.setForeground(new Color(30, 144, 255));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                radioButton.setForeground(new Color(70, 70, 70));
            }
        });
        
        return radioButton;
    }
 
    // ×Ô¶¨Ò嵥ѡ°´Å¥Í¼±ê
    private static class RadioButtonIcon implements Icon {
        private static final int SIZE = 14;
        private final boolean selected;
        
        public RadioButtonIcon(boolean selected) {
            this.selected = selected;
        }
        
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            if (selected) {
                // Ñ¡ÖÐ״̬ - À¶É«Ìî³ä
                g2d.setColor(new Color(70, 130, 180));
                g2d.fillOval(x, y, SIZE, SIZE);
                
                // ÄÚÔ² - °×É«
                g2d.setColor(Color.WHITE);
                g2d.fillOval(x + SIZE/4, y + SIZE/4, SIZE/2, SIZE/2);
            } else {
                // Î´Ñ¡ÖÐ״̬ - »ÒÉ«±ß¿ò
                g2d.setColor(new Color(150, 150, 150));
                g2d.drawOval(x, y, SIZE-1, SIZE-1);
            }
            
            g2d.dispose();
        }
        
        @Override
        public int getIconWidth() {
            return SIZE;
        }
        
        @Override
        public int getIconHeight() {
            return SIZE;
        }
    }
 
    // ¼ÓÔØÉ豸IDÁбí
    private void loadDeviceIds() {
        // ´ÓDell_tagÀà»ñÈ¡ËùÓÐÉ豸ID
        List<String>  tags = Dell_tag.getAlldeviceIds();
        // ±éÀúÉ豸IDÁÐ±í²¢Ìí¼Óµ½ÏÂÀ­¿ò
        for (String tag : tags) {
            cbDeviceId.addItem(tag);
        }
    }
 
    // ¼ÓÔØÊý¾ÝÀàÐÍ
    private void loadtyps() {
        // ´ÓDell_tagÀà»ñÈ¡ËùÓÐÉ豸ID
        List<String>  typs = Dell_MessageType.getAllMessageTypes();
        // ±éÀúÉ豸IDÁÐ±í²¢Ìí¼Óµ½ÏÂÀ­¿ò
        for (String typ : typs) {
            cbDataType.addItem(typ);
        }
    }
 
    // ÉèÖð´Å¥¶¯×÷
    private void setupActions() {
        btnStart.addActionListener(e -> startCapture()); // ¿ªÊ¼°´Å¥°ó¶¨¿ªÊ¼²¶»ñ·½·¨
        btnPause.addActionListener(e -> pauseCapture()); // ÔÝÍ£°´Å¥°ó¶¨ÔÝÍ£²¶»ñ·½·¨
        btnClear.addActionListener(e -> clearDisplay()); // Çå³ý°´Å¥°ó¶¨Çå³ýÏÔʾ·½·¨
        btnSend.addActionListener(e -> sendData()); // ·¢ËͰ´Å¥°ó¶¨·¢ËÍ·½·¨
    }
 
    // ¿ªÊ¼²¶»ñÊý¾Ý
    private void startCapture() {
        if (!isCapturing) { // Èç¹ûµ±Ç°Î´ÔÚ²¶»ñ״̬
            isCapturing = true; // ÉèÖò¶»ñ״̬Ϊtrue
            btnStart.setEnabled(false); // ½ûÓÿªÊ¼°´Å¥
            btnPause.setEnabled(true); // ÆôÓÃÔÝÍ£°´Å¥
            // Æô¶¯Êý¾Ý²¶»ñÏß³Ì
        }
    }
 
    // ÔÝÍ£²¶»ñÊý¾Ý
    private void pauseCapture() {
        isCapturing = false; // ÉèÖò¶»ñ״̬Ϊfalse
        btnStart.setEnabled(true); // ÆôÓÿªÊ¼°´Å¥
        btnPause.setEnabled(false); // ½ûÓÃÔÝÍ£°´Å¥
    }
 
    // Çå³ýÊý¾ÝÏÔʾ
    private void clearDisplay() {
        txtDataView.setText(""); // Çå¿ÕÎı¾ÇøÓò
    }
 
    // ·¢ËÍÊý¾Ýµ½Ô¶³ÌÉ豸
    private void sendData() {
        String selectedDevice = (String) cbRemoteDevice.getSelectedItem();
        if (selectedDevice == null || selectedDevice.equals(messages.getString("MESSAGE_VIEW_ALL_DEVICES"))) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("MESSAGE_VIEW_SELECT_DEVICE"), 
                    messages.getString("WARNING"), 
                    JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        // »ñÈ¡±¾µØ¶Ë¿Ú
        Integer localPort = remoteDeviceLocalPortMap.get(selectedDevice);
        if (localPort == null) {
            JOptionPane.showMessageDialog(this, 
                    "ÎÞ·¨È·¶¨É豸µÄ½ÓÊÕ¶Ë¿Ú",
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
 
        // ½âÎöIPºÍ¶Ë¿Ú
        String[] parts = selectedDevice.split(":");
        if (parts.length != 2) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("MESSAGE_VIEW_INVALID_DEVICE"), 
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
 
        String ip = parts[0];
        int port;
        try {
            port = Integer.parseInt(parts[1]);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("MESSAGE_VIEW_INVALID_PORT"), 
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
 
        String dataToSend = txtSendData.getText();
        if (dataToSend.isEmpty()) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("MESSAGE_VIEW_EMPTY_DATA"), 
                    messages.getString("WARNING"), 
                    JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        // Ìí¼Ó»Ø³µ»»ÐÐ
        if (chkAppendNewline.isSelected()) {
            dataToSend += "\r\n";
        }
 
        // ¼Ç¼·¢ËÍÊý¾Ýµ½½çÃæ
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = sdf.format(new Date());
 
        StringBuilder displayData = new StringBuilder();
        displayData.append("[SEND][").append(timestamp).append("][").append(ip).append(":").append(port).append("] ");
 
        if (rdoSendHex.isSelected()) {
            // HEX¸ñʽ·¢ËÍ
            String hexData = toHexString(dataToSend);
            displayData.append(hexData);
            if (localPort == UDPPortAReceiver.PORT) {
                // ´«ÈëÊÇ·ñHEX·¢Ë͵ıêÖ¾
                UDPPortAReceiver.sendData(ip, port, dataToSend, true);
            } else if (localPort == UDPPortBReceiver.PORT) {
                // ´«ÈëÊÇ·ñHEX·¢Ë͵ıêÖ¾
                UDPPortBReceiver.sendData(ip, port, dataToSend, true);
            }
            
        } else {
            // ASCII¸ñʽ·¢ËÍ
            displayData.append(dataToSend);
            // Êµ¼Ê·¢ËÍASCIIÊý¾Ý£¨ÕâÀïÐèÒªµ÷ÓÃUDP·¢ËÍ·½·¨£©
            if (localPort == UDPPortAReceiver.PORT) {
                UDPPortAReceiver.sendData(ip, port, dataToSend, false);
            } else if (localPort == UDPPortBReceiver.PORT) {
                UDPPortBReceiver.sendData(ip, port, dataToSend, false);
            }
        }
 
        // ÔÚEDTÏß³ÌÖиüÐÂUI
        SwingUtilities.invokeLater(() -> {
            txtDataView.append(displayData.toString() + "\n");
            // ×Ô¶¯¹ö¶¯µ½µ×²¿
            txtDataView.setCaretPosition(txtDataView.getDocument().getLength());
        });
    }
    
    // ÐÞ¸ÄÌí¼ÓÔ¶³ÌÉ豸·½·¨
    // ÔÚMessageViewPanelÀàÖÐÐÞ¸ÄaddRemoteDevice·½·¨
    public static void addRemoteDevice(String ip, int port, int localPort) {
        String device = ip + ":" + port;
        if (!remoteDeviceLocalPortMap.containsKey(device)) {
            remoteDeviceLocalPortMap.put(device, localPort);
            SwingUtilities.invokeLater(() -> {
                // ¼ì²éÏÂÀ­¿òÖÐÊÇ·ñÒÑ´æÔÚ¸ÃÉ豸
                boolean exists = false;
                ComboBoxModel<String> model = cbRemoteDevice.getModel();
                for (int i = 0; i < model.getSize(); i++) {
                    if (device.equals(model.getElementAt(i))) {
                        exists = true;
                        break;
                    }
                }
                
                if (!exists) {
                    cbRemoteDevice.addItem(device);
                }
            });
        }
    }
    
    // ²¶»ñÊý¾ÝµÄºËÐÄ·½·¨
    public  static void captureData(String rawData, String ip, int port, int localPort, String timestamp) {
         // ¼ì²é´°¿ÚÊÇ·ñ¿É¼û
        if (!isWindowVisible) {
            return;
        }
        String formattedData = formatData(rawData,ip,port,timestamp); // ¸ñʽ»¯Êý¾Ý
        if(isCapturing) {
            // Ìí¼ÓÔ¶³ÌÉ豸µ½ÏÂÀ­¿ò
            addRemoteDevice(ip, port, localPort); // È·±£µ÷Óôø¶Ë¿Ú²ÎÊýµÄ·½·¨
            // »ñÈ¡µ±Ç°Ñ¡ÔñµÄÉ豸IDºÍÊý¾ÝÀàÐÍ
            String deviceId = cbDeviceId.getSelectedItem().toString();
            String dataType = cbDataType.getSelectedItem().toString();
            
            // ÔÚEDTÏß³ÌÖиüÐÂUI
            SwingUtilities.invokeLater(() -> {
                txtDataView.append(formattedData + "\n"); // Ìí¼ÓÊý¾Ýµ½Îı¾ÇøÓò
                // ×Ô¶¯¹ö¶¯µ½µ×²¿
                txtDataView.setCaretPosition(txtDataView.getDocument().getLength());
            });
        }
        // Èç¹ûÆôÓÃÁË×Ô¶¯±£´æ£¬Ôò±£´æµ½Îļþ
        if (chkAutoSave.isSelected()) {
            saveToFile(formattedData);
        }       
    }
 
    // ¸ñʽ»¯Êý¾ÝÏÔʾ
    private static String formatData(String rawData, String ip,  int port,String timestamp) {
        StringBuilder formatted = new StringBuilder(); // Ê¹ÓÃStringBuilder¹¹½¨¸ñʽ»¯×Ö·û´®
 
        // Èç¹ûÆôÓÃÁËÏÔʾÀ´Ô´£¬Ìí¼Óʱ¼ä´ÁºÍÔ´µØÖ·
        if (chkShowSource.isSelected()) {
            formatted.append("["+timestamp+";"+ip+":"+port+"]"); // Ìí¼ÓÄ£ÄâÔ´µØÖ·
        }
 
        // ¸ù¾ÝÏÔʾѡÏî¸ñʽ»¯Êý¾Ý
        if (chkHexDisplay.isSelected() && chkAsciiDisplay.isSelected()) {
            // Í¬Ê±ÏÔʾʮÁù½øÖƺÍASCII
            formatted.append(toHexString(rawData)) // Ìí¼ÓÊ®Áù½øÖƱíʾ
            .append(" | ") // Ìí¼Ó·Ö¸ô·û
            .append(rawData); // Ìí¼ÓԭʼASCIIÊý¾Ý
        } else if (chkHexDisplay.isSelected()) {
            // ½öÏÔʾʮÁù½øÖÆ
            formatted.append(toHexString(rawData)); 
        } else {
            // Ä¬ÈÏÏÔʾԭʼASCIIÊý¾Ý
            formatted.append(rawData);
        }
 
        return formatted.toString(); // ·µ»Ø¸ñʽ»¯ºóµÄ×Ö·û´®
    }
 
    // ½«×Ö·û´®×ª»»ÎªÊ®Áù½øÖƱíʾ
    private static String toHexString(String input) {
        StringBuilder hex = new StringBuilder(); // Ê¹ÓÃStringBuilder¹¹½¨Ê®Áù½øÖÆ×Ö·û´®
        for (char c : input.toCharArray()) { // ±éÀúÿ¸ö×Ö·û
            hex.append(String.format("%02X ", (int) c)); // ¸ñʽ»¯ÎªÁ½Î»Ê®Áù½øÖÆ
        }
        return hex.toString().trim(); // ·µ»Ø²¢È¥³ýĩβ¿Õ¸ñ
    }
 
    // ±£´æÊý¾Ýµ½Îļþ
    private static void saveToFile(String data) {
        SimpleDateFormat fileFormat = new SimpleDateFormat("yyyyMMdd"); // ÎļþÈÕÆÚ¸ñʽ
        String fileName = "systemfile/baowen/messages_" + fileFormat.format(new Date()) + ".txt"; // ÎļþÃû°üº¬ÈÕÆÚ
 
        try (java.io.FileWriter writer = new java.io.FileWriter(fileName, true)) { // Ê¹ÓÃ×·¼Óģʽ
            writer.write(data + "\n"); // Ð´ÈëÊý¾Ý²¢»»ÐÐ
        } catch (java.io.IOException ex) {
            ex.printStackTrace(); // ´òÓ¡IOÒì³£
        }
    }
}