zsh_root
2025-12-10 8d662de2fd262b3a485f16e197cb4d0ca2a61cdf
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
package home;
 
import javax.swing.*;
import java.awt.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.util.PropertyResourceBundle;
import javax.imageio.ImageIO;
 
public class MainFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    private ResourceBundle messages;
    private Locale currentLocale;
    private CardLayout cardLayout;
    private JPanel mainPanel;
    private JButton serialCommBtn, networkCommBtn, networkConfigBtn;
    private JComboBox<String> languageCombo;
    // 新增:卫星数据按钮与面板
    private JButton satelliteBtn;
    private SatelliteDataPanel satellitePanel;
 
    // 各功能面板
    private SerialCommunicationPanel serialPanel;
    private NetworkCommunicationPanel networkPanel;
    private NetworkConfigPanel configPanel;
    private DataLogPanel dataLogPanel;
    private SendPanel sendPanel;
    private JSplitPane contentSplitPane;
    private JSplitPane rightSplitPane;
 
    // 新增:快捷计算面板相关
    private JButton quickCalcBtn;
    private JDialog quickCalcDialog;
    private QuickCalculationPanel quickCalcPanel;
 
    public MainFrame() {
        this.currentLocale = Locale.SIMPLIFIED_CHINESE;
        this.messages = loadResourceBundle(currentLocale);
        initializeUI();
    }
 
    private ResourceBundle loadResourceBundle(Locale locale) {
        String fileName = locale.equals(Locale.ENGLISH) ?
                "Messages_en.properties" : "Messages_zh.properties";
 
        File langFile = new File("systemfile/" + fileName);
 
        if (!langFile.exists()) {
            System.err.println("默认资源文件未找到: " + langFile.getAbsolutePath());
            // 备用方案:尝试从类路径加载
            try {
                return ResourceBundle.getBundle("Messages", locale);
            } catch (Exception e) {
                System.err.println("无法加载备用资源文件");
                return createDefaultResourceBundle();
            }
        }
 
        try (InputStream inputStream = new FileInputStream(langFile)) {
            return new PropertyResourceBundle(inputStream);
        } catch (IOException e) {
            System.err.println("无法加载资源文件: " + e.getMessage());
            // 备用方案
            try {
                return ResourceBundle.getBundle("Messages", locale);
            } catch (Exception ex) {
                System.err.println("无法加载备用资源文件");
                return createDefaultResourceBundle();
            }
        }
    }
 
    private ResourceBundle createDefaultResourceBundle() {
        // 创建默认资源包(中文)
        java.util.Properties properties = new java.util.Properties();
        properties.setProperty("app.title", "配置软件");
        properties.setProperty("serial.communication", "串口通信");
        properties.setProperty("network.communication", "网络通信");
        properties.setProperty("network.config", "网络配置");
        properties.setProperty("language.switch", "语言切换");
        properties.setProperty("select.serial", "选择串口");
        properties.setProperty("baud.rate", "波特率");
        properties.setProperty("open", "打开");
        properties.setProperty("close", "关闭");
        // 新增:快捷计算相关资源
        properties.setProperty("quick.calculation", "快捷计算");
        properties.setProperty("quick.calc.title", "坐标计算工具");
 
        try {
            return new PropertyResourceBundle(new java.io.ByteArrayInputStream(
                    properties.toString().replace("=", ": ").getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return messages;
    }
 
    public String getString(String key) {
        if (messages != null) {
            try {
                return messages.getString(key);
            } catch (Exception e) {
                // 如果资源文件中没有找到对应的key,返回默认值
                return getDefaultString(key);
            }
        }
        return getDefaultString(key);
    }
 
    private String getDefaultString(String key) {
        // 默认文本(中文)
        switch (key) {
            case "app.title":
                return "配置软件";
            case "serial.communication":
                return "串口通信";
            case "network.communication":
                return "网络通信";
            case "network.config":
                return "网络配置";
            case "language.switch":
                return "语言切换";
            case "select.serial":
                return "选择串口";
            case "baud.rate":
                return "波特率";
            case "open":
                return "打开";
            case "close":
                return "关闭";
            case "base.parameters":
                return "基础参数";
            case "base.station":
                return "基站";
            case "tag":
                return "标签";
            case "anti.collision":
                return "防撞";
            case "upgrade":
                return "升级";
            case "device.version":
                return "设备版本";
            case "device.id":
                return "设备编号";
            case "comm.frequency":
                return "通信频率";
            case "base.stations.per.comm":
                return "单次通信基站数";
            case "group.id":
                return "分组编号";
            case "distance.calibration":
                return "距离校准值";
            case "device.type":
                return "设备类型";
            case "base.station.ranging":
                return "基站主动测距";
            case "alarm.device":
                return "报警设备";
            case "pairing.id":
                return "配对编号";
            case "heartbeat.switch":
                return "心跳包开关";
            case "modbus.mode":
                return "MODBUS模式";
            case "rf.power":
                return "射频发射功率";
            case "read.parameters":
                return "读取参数";
            case "save.parameters":
                return "保存参数";
            case "restart.device":
                return "重启设备";
            case "reset.factory":
                return "恢复出厂设置";
            case "protocol.type":
                return "协议类型";
            case "local.host.address":
                return "本地主机地址";
            case "local.host.port":
                return "本地主机端口";
            case "remote.host.address":
                return "远程主机地址";
            case "remote.host.port":
                return "远程主机端口";
            case "data.log":
                return "数据日志";
            case "ascii.display":
                return "ASCII显示";
            case "auto.save":
                return "自动保存";
            case "show.time":
                return "显示时间";
            case "start":
                return "开始";
            case "pause":
                return "暂停";
            case "clear":
                return "清空";
            case "send.data":
                return "发送数据";
            case "hex.send":
                return "HEX发送";
            case "loop.send":
                return "循环发送";
            case "loop.time":
                return "循环时间(ms)";
            case "send":
                return "发送";
            case "extension":
                return "扩展";
            case "LANGUAGE":
                return "Language";
            case "external.control":
                return "外部控制";
            case "adjacent.stations.count":
                return "相邻基站数量";
            case "adjacent.station1":
                return "相邻基站1";
            case "adjacent.station2":
                return "相邻基站2";
            case "adjacent.station3":
                return "相邻基站3";
            case "adjacent.station4":
                return "相邻基站4";
            case "adjacent.station5":
                return "相邻基站5";
            case "adjacent.station6":
                return "相邻基站6";
            case "adjacent.station7":
                return "相邻基站7";
            case "adjacent.station8":
                return "相邻基站8";
            case "adjacent.station9":
                return "相邻基站9";
            case "adjacent.station10":
                return "相邻基站10";
            case "sync.station.id":
                return "同步基站ID";
            case "sync.station.type":
                return "同步基站类型";
            case "tag.id":
                return "标签ID";
            case "tag.type":
                return "标签类型";
            case "tag.mode":
                return "标签模式";
            case "tag.interval":
                return "标签间隔";
            case "tag.power":
                return "标签功率";
            case "tag.sensitivity":
                return "标签灵敏度";
            case "anti.collision.enable":
                return "防撞使能";
            case "safety.distance":
                return "安全距离";
            case "warning.distance":
                return "警告距离";
            case "alarm.distance":
                return "报警距离";
            case "alarm.type":
                return "报警类型";
            case "upgrade.description":
                return "固件升级功能\n\n请选择要升级的固件文件,然后点击开始升级按钮。升级过程中请勿断开电源。";
            case "start.upgrade":
                return "开始升级";
 
            // 新增:快捷计算相关键值对
            case "quick.calculation":
                return "快捷计算";
            case "quick.calc.title":
                return "坐标计算工具";
            // 新增:卫星数据相关键值对
            case "satellite.data":
                return "卫星数据";
            case "sat.table.title":
                return "卫星数据";
            case "sat.col.prn":
                return "PRN";
            case "sat.col.snr":
                return "信噪比(SNR)";
            case "sat.col.azimuth":
                return "方位角";
            case "sat.col.elevation":
                return "仰角";
            case "sat.col.used":
                return "是否被使用";
            // 升级相关键值对
            case "upgrade.instructions":
                return "升级说明";
            case "select.folder":
                return "选择文件夹";
            case "browse":
                return "浏览";
            case "select.bin.file":
                return "选择BIN文件";
            case "selected.file":
                return "已选择文件";
            case "file.selected":
                return "文件已选择";
            case "please.select.folder.first":
                return "请先选择文件夹";
            case "warning":
                return "警告";
            case "upgrade.completed":
                return "升级完成";
            case "success":
                return "成功";
            case "upgrade.in.progress":
                return "升级进行中...";
            case "select.upgrade.file":
                return "选择升级文件";
            case "upgrade.file":
                return "升级文件";
            case "upgrade.progress":
                return "升级进度";
            case "cancel.upgrade":
                return "取消升级";
            case "upgrade.success":
                return "升级成功";
            case "upgrade.failed":
                return "升级失败";
            case "confirm.upgrade":
                return "确认升级";
            case "upgrade.confirmation":
                return "升级确认";
            case "upgrade.confirm.message":
                return "确定要开始升级吗?升级过程中请勿断开电源。";
            case "select.file":
                return "选择文件";
            case "no.file.selected":
                return "未选择文件";
            case "file.not.found":
                return "文件未找到";
            case "invalid.file":
                return "无效文件";
            case "upgrade.ready":
                return "准备升级";
            case "preparing.upgrade":
                return "准备升级环境...";
            case "writing.firmware":
                return "写入固件...";
            case "verifying.firmware":
                return "验证固件...";
            case "upgrade.cancelled":
                return "升级已取消";
 
            // 扩展面板相关键值对
            case "common.commands":
                return "常用指令";
            case "command.string":
                return "指令字符串";
            case "save.common.commands":
                return "保存常用指令";
            case "modify.common.commands":
                return "修改常用指令";
            case "load.commands.failed":
                return "加载指令失败:";
            case "save.commands.success":
                return "保存指令成功!";
            case "save.commands.failed":
                return "保存指令失败:";
            case "command.cannot.empty":
                return "指令不能为空!";
            case "now.can.modify.commands":
                return "现在可以修改表格中的指令内容,修改完成后点击保存常用指令按钮";
            case "prompt":
                return "提示";
            case "error":
                return "错误";
 
            // 扩展命令相关键值对
            case "extension.command":
                return "扩展命令";
 
            default:
                return key;
        }
    }
 
    private void initializeUI() {
        setTitle(getString("app.title"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1250, 800);
        setLocationRelativeTo(null);
 
        // 设置窗口图标
        setWindowIcon();
 
        // 创建顶部按钮面板
        JPanel topPanel = createTopPanel();
 
        // 创建主内容面板
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
 
        // 初始化各功能面板
        serialPanel = new SerialCommunicationPanel(this);
        networkPanel = new NetworkCommunicationPanel(this);
        configPanel = new NetworkConfigPanel(this);
 
        mainPanel.add(serialPanel, "SERIAL");
        mainPanel.add(networkPanel, "NETWORK");
        mainPanel.add(configPanel, "CONFIG");
        // 新增:卫星数据面板
        satellitePanel = new SatelliteDataPanel(this);
        mainPanel.add(satellitePanel, "SATELLITE");
 
        // 创建右侧面板(数据日志和发送面板)
        JPanel rightPanel = createRightPanel();
 
        // 设置布局
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
 
        contentSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, mainPanel, rightPanel);
        contentSplitPane.setResizeWeight(0.6); // 修改:设置主面板占60%
        contentSplitPane.setOneTouchExpandable(true);
        contentSplitPane.setContinuousLayout(true);
        contentSplitPane.setDividerLocation(0.6); // 修改:分割条位置在60%处
        add(contentSplitPane, BorderLayout.CENTER);
 
        // 初始化快捷计算对话框
        initQuickCalculationDialog();
 
        // 添加事件监听
        setupEventListeners();
 
        // 关键修改:连接串口面板和数据日志面板
        connectPanels();
    }
 
    private JPanel createTopPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
 
        // 使用ButtonUtils创建导航按钮
        serialCommBtn = ButtonUtils.createBlueButton(getString("serial.communication"));
        networkCommBtn = ButtonUtils.createBlueButton(getString("network.communication"));
        networkConfigBtn = ButtonUtils.createBlueButton(getString("network.config"));
 
        // 新增:快捷计算按钮
        quickCalcBtn = ButtonUtils.createBlueButton(getString("quick.calculation"));
        // 新增:卫星数据按钮
        satelliteBtn = ButtonUtils.createBlueButton(getString("satellite.data"));
 
        languageCombo = new JComboBox<>(new String[]{"中文", "English"});
        languageCombo.setSelectedIndex(0);
        languageCombo.addActionListener(e -> {
            Locale newLocale = languageCombo.getSelectedIndex() == 0 ?
                    Locale.SIMPLIFIED_CHINESE : Locale.ENGLISH;
            switchLanguage(newLocale);
        });
 
        panel.add(serialCommBtn);
        panel.add(networkCommBtn);
        panel.add(networkConfigBtn);
        panel.add(quickCalcBtn); // 添加快捷计算按钮
        panel.add(satelliteBtn); // 添加卫星数据按钮
 
        JLabel languageLabel = new JLabel(getString("LANGUAGE") + ":");
        panel.add(Box.createHorizontalStrut(20));
        panel.add(languageLabel);
        panel.add(languageCombo);
 
        return panel;
    }
 
    private JPanel createRightPanel() {
        dataLogPanel = new DataLogPanel(this);
        sendPanel = new SendPanel(this);
 
        rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, dataLogPanel, sendPanel);
        rightSplitPane.setResizeWeight(0.75); // 3:1 高度比例
        rightSplitPane.setContinuousLayout(true);
        rightSplitPane.setDividerSize(6);
        rightSplitPane.setBorder(null);
        SwingUtilities.invokeLater(() -> rightSplitPane.setDividerLocation(0.75));
 
        JPanel container = new JPanel(new BorderLayout());
        // 修改:设置右侧面板宽度为主窗口宽度的40%(4:6比例)
        container.setPreferredSize(new Dimension((int)(getWidth() * 0.4), getHeight()));
        container.add(rightSplitPane, BorderLayout.CENTER);
        return container;
    }
 
    /**
     * 初始化快捷计算对话框
     */
    private void initQuickCalculationDialog() {
        quickCalcDialog = new JDialog(this, getString("quick.calc.title"), false);
        quickCalcDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        quickCalcDialog.setSize(900, 700);
        quickCalcDialog.setLocationRelativeTo(this);
 
        // 创建快捷计算面板
        quickCalcPanel = new QuickCalculationPanel(messages);
        quickCalcDialog.add(quickCalcPanel);
    }
 
    private void setupEventListeners() {
        serialCommBtn.addActionListener(e -> cardLayout.show(mainPanel, "SERIAL"));
        networkCommBtn.addActionListener(e -> cardLayout.show(mainPanel, "NETWORK"));
        networkConfigBtn.addActionListener(e -> cardLayout.show(mainPanel, "CONFIG"));
 
        // 新增:快捷计算按钮事件监听
        quickCalcBtn.addActionListener(e -> {
            quickCalcDialog.setVisible(true);
        });
 
        // 新增:卫星数据按钮事件监听
        satelliteBtn.addActionListener(e -> {
            cardLayout.show(mainPanel, "SATELLITE");
        });
    }
 
    /**
     * 关键修改:连接串口面板和数据日志面板
     */
    private void connectPanels() {
        if (serialPanel != null && dataLogPanel != null) {
            serialPanel.setDataLogPanel(dataLogPanel);
        }
        if (networkPanel != null && dataLogPanel != null) {
            networkPanel.setDataLogPanel(dataLogPanel);
        }
        if (satellitePanel != null && dataLogPanel != null) {
            satellitePanel.setDataLogPanel(dataLogPanel);
        }
 
    }
 
    private void switchLanguage(Locale newLocale) {
        this.currentLocale = newLocale;
        this.messages = loadResourceBundle(currentLocale);
 
        // 更新界面文本
        updateUILanguage();
 
        // 更新语言选择框
        languageCombo.setSelectedIndex(newLocale.equals(Locale.SIMPLIFIED_CHINESE) ? 0 : 1);
 
        revalidate();
        repaint();
    }
 
    private void updateUILanguage() {
        // 更新导航按钮文本
        serialCommBtn.setText(getString("serial.communication"));
        networkCommBtn.setText(getString("network.communication"));
        networkConfigBtn.setText(getString("network.config"));
        quickCalcBtn.setText(getString("quick.calculation")); // 更新快捷计算按钮文本
        if (satelliteBtn != null) satelliteBtn.setText(getString("satellite.data")); // 更新卫星按钮文本
 
        // 更新语言标签
        setTitle(getString("app.title"));
 
        // 更新对话框标题
        if (quickCalcDialog != null) {
            quickCalcDialog.setTitle(getString("quick.calc.title"));
        }
 
        // 更新各面板文本
        if (serialPanel != null) serialPanel.updateLanguage();
        if (networkPanel != null) networkPanel.updateLanguage();
        if (configPanel != null) configPanel.updateLanguage();
        if (dataLogPanel != null) dataLogPanel.updateLanguage();
        if (satellitePanel != null) satellitePanel.updateLanguage();
        if (sendPanel != null) sendPanel.updateLanguage();
 
        // 更新快捷计算面板
        if (quickCalcPanel != null) {
            // 重新创建快捷计算面板以更新语言
            quickCalcDialog.getContentPane().removeAll();
            quickCalcPanel = new QuickCalculationPanel(messages);
            quickCalcDialog.add(quickCalcPanel);
            quickCalcDialog.revalidate();
            quickCalcDialog.repaint();
        }
    }
 
    public Locale getCurrentLocale() {
        return currentLocale;
    }
 
    /**
     * 获取数据日志面板实例
     *
     * @return DataLogPanel实例
     */
    public DataLogPanel getDataLogPanel() {
        return dataLogPanel;
    }
 
    /**
     * 从扩展面板发送数据
     *
     * @param data  要发送的数据
     * @param isHex 是否为HEX格式
     */
    public void sendDataFromExtension(String data, boolean isHex) {
        if (data == null || data.trim().isEmpty()) {
            return;
        }
 
        // 根据当前激活的面板决定发送方式
        Component currentPanel = null;
        for (Component comp : mainPanel.getComponents()) {
            if (comp.isVisible()) {
                currentPanel = comp;
                break;
            }
        }
 
        if (currentPanel instanceof SerialCommunicationPanel) {
            // 串口通信面板激活,通过串口发送
            sendViaSerial(data, isHex);
        } else if (currentPanel instanceof NetworkCommunicationPanel) {
            // 网络通信面板激活,通过网络发送
            sendViaNetwork(data, isHex);
        } else {
            // 默认尝试通过串口发送
            sendViaSerial(data, isHex);
        }
    }
 
    /**
     * 通过串口发送数据
     */
    private void sendViaSerial(String data, boolean isHex) {
        if (serialPanel != null) {
            // 这里调用串口面板的发送方法
            // 需要根据您的SerialCommunicationPanel实现来调用相应方法
 
            // 示例:如果串口面板有发送数据的方法
            // serialPanel.sendData(data, isHex);
        } else {
            System.err.println("串口面板未初始化");
        }
    }
 
    /**
     * 通过网络发送数据
     */
    private void sendViaNetwork(String data, boolean isHex) {
        if (networkPanel != null) {
            if (isHex) {
                // networkPanel.sendHexNetworkData(data);
            } else {
                //networkPanel.sendNetworkData(data.getBytes(java.nio.charset.StandardCharsets.UTF_8));
            }
        } else {
            System.err.println("网络面板未初始化");
        }
    }
 
    public void addToDataLog(String logMessage) {
        if (dataLogPanel != null) {
            // 通过DataLogPanel的公共方法添加日志
            dataLogPanel.addLog(logMessage);
        }
    }
 
 
    /**
     * 获取网络通信面板实例
     */
    public NetworkCommunicationPanel getNetworkCommunicationPanel() {
        // 根据您的实际实现返回网络通信面板
        // 例如,如果网络通信面板是选项卡中的一个,可以这样获取:
        return networkPanel;
    }
 
    /**
     * 设置窗口图标
     */
    private void setWindowIcon() {
        try {
            File iconFile = new File("image/setting.png");
            if (iconFile.exists()) {
                Image icon = ImageIO.read(iconFile);
                if (icon != null) {
                    setIconImage(icon);
                }
            } else {
                System.err.println("图标文件不存在: " + iconFile.getAbsolutePath());
            }
        } catch (IOException e) {
            System.err.println("加载图标文件失败: " + e.getMessage());
        }
    }
}