张世豪
4 天以前 6700283f9103a45bc087838ebf3eeeeb9022dd98
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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
package set;
 
import baseStation.BaseStation;
import gecaoji.Device;
import gecaoji.MowerSafetyDistanceCalculator;
 
import zhuye.MapRenderer;
import zhuye.Shouye;
import zhuye.buttonset;
import zhuye.celiangmoshi;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
 
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 设置对话框 - 参考Shouye.java样式
 */
public class Sets extends JDialog {
    private static final long serialVersionUID = 1L;
    private static final int ROW_HEIGHT = 50;  // 增加行高以适应分割线
    private static final int ITEM_PADDING = 16;  // 列表项内边距
    
    // 主题颜色
    private final Color THEME_COLOR;
    private final Color BACKGROUND_COLOR = new Color(245, 247, 250);  // 更柔和的浅灰色背景
    private final Color PANEL_BACKGROUND = new Color(255, 255, 255);  // 白色面板
    private final Color BORDER_COLOR = new Color(233, 236, 239);  // 浅边框色
    private final Color DIVIDER_COLOR = new Color(233, 236, 239);  // 分割线颜色
    
    // 设置项组件
    private JLabel mowerIdLabel;
    private JLabel mowerSizeLabel;
    private JLabel mowingSafetyDistanceLabel;
    private JLabel baseStationIdLabel;
    private JLabel handheldMarkerLabel;
    private JLabel simCardNumberLabel;
    private JLabel baseStationSimLabel;
    private JLabel firmwareVersionLabel;
    private JLabel appVersionLabel;
    private JLabel idleTrailDurationLabel;
    private JLabel boundaryLengthVisibleLabel;
    private JLabel measurementModeEnabledLabel;
    
    private JButton mowerIdEditBtn;
    private JButton mowerSizeEditBtn;
    private JButton mowingSafetyDistanceEditBtn;
    private JButton baseStationIdEditBtn;
    private JButton handheldEditBtn;
    private JButton checkUpdateBtn;
    private JButton feedbackButton;
    private JButton idleTrailEditBtn;
    
    // 数据模型
    private Setsys setData;
    private final BaseStation baseStation;
    
    public Sets(JFrame parent, Color themeColor) {
        super(parent, "系统设置", true);
        this.THEME_COLOR = themeColor;
        this.setData = new Setsys();
        this.baseStation = new BaseStation();
        initializeUI();
        loadData();
    }
    
    public Sets(JDialog parent, Color themeColor) {
        super(parent, "系统设置", true);
        this.THEME_COLOR = themeColor;
        this.setData = new Setsys();
        this.baseStation = new BaseStation();
        initializeUI();
        loadData();
    }
    
    private void initializeUI() {
        setLayout(new BorderLayout());
        setBackground(BACKGROUND_COLOR);
    setSize(400, 800);
    setPreferredSize(new Dimension(400, 800));
    setMinimumSize(new Dimension(400, 800));
        setLocationRelativeTo(getParent());
        setResizable(false);
        
        // 创建主内容面板(使用更柔和的浅灰色背景)
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(BACKGROUND_COLOR);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 创建设置项面板(圆角白色面板)
        JPanel settingsPanel = createSettingsPanel();
        
        // 添加组件到主面板
        mainPanel.add(settingsPanel, BorderLayout.CENTER);
        
        add(mainPanel, BorderLayout.CENTER);
    }
    
    private JPanel createSettingsPanel() {
        // 创建圆角白色面板容器
        JPanel container = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(PANEL_BACKGROUND);
                // 绘制圆角矩形背景
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
                g2d.dispose();
            }
        };
        container.setLayout(new BorderLayout());
        container.setOpaque(false);
        
        // 内容面板
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setOpaque(false);
        panel.setBorder(BorderFactory.createEmptyBorder(8, 0, 8, 0));
        
        // 割草机编号
        JPanel mowerIdPanel = createSettingItemPanel("割草机编号", 
            setData.getMowerId() != null ? setData.getMowerId() : "未设置", true);
        mowerIdLabel = (JLabel) mowerIdPanel.getClientProperty("valueLabel");
        mowerIdEditBtn = (JButton) mowerIdPanel.getClientProperty("editButton");
 
        // 割草机长宽
        JPanel mowerSizePanel = createSettingItemPanel("割草机长宽",
            formatMowerSize(), true);
        mowerSizeLabel = (JLabel) mowerSizePanel.getClientProperty("valueLabel");
        mowerSizeEditBtn = (JButton) mowerSizePanel.getClientProperty("editButton");
 
        // 割草安全距离
        JPanel mowingSafetyDistancePanel = createSettingItemPanel("割草安全距离",
            formatMowingSafetyDistance(), true);
        mowingSafetyDistanceLabel = (JLabel) mowingSafetyDistancePanel.getClientProperty("valueLabel");
        mowingSafetyDistanceEditBtn = (JButton) mowingSafetyDistancePanel.getClientProperty("editButton");
 
        JPanel baseStationIdPanel = createSettingItemPanel("差分基准站编号",
            resolveBaseStationId(), true);
        baseStationIdLabel = (JLabel) baseStationIdPanel.getClientProperty("valueLabel");
        baseStationIdEditBtn = (JButton) baseStationIdPanel.getClientProperty("editButton");
        
        JPanel handheldPanel = createSettingItemPanel("便携打点器编号",
            setData.getHandheldMarkerId() != null ? setData.getHandheldMarkerId() : "未设置", true);
        handheldMarkerLabel = (JLabel) handheldPanel.getClientProperty("valueLabel");
        handheldEditBtn = (JButton) handheldPanel.getClientProperty("editButton");
 
        // 物联卡号
        JPanel simCardPanel = createSettingItemPanel("割草机物联网卡号", 
            setData.getSimCardNumber() != null ? setData.getSimCardNumber() : "未设置", false);
        simCardNumberLabel = (JLabel) simCardPanel.getClientProperty("valueLabel");
 
        JPanel baseStationSimPanel = createSettingItemPanel("基准站物联网卡号",
            resolveBaseStationSimCard(), false);
        baseStationSimLabel = (JLabel) baseStationSimPanel.getClientProperty("valueLabel");
        
        // 固件版本
        JPanel firmwarePanel = createSettingItemPanel("固件版本", 
            setData.getFirmwareVersion() != null ? setData.getFirmwareVersion() : "未设置", false);
        firmwareVersionLabel = (JLabel) firmwarePanel.getClientProperty("valueLabel");
 
        JPanel idleTrailPanel = createSettingItemPanel("轨迹拖尾时长",
            formatIdleTrailDurationValue(), true);
        idleTrailDurationLabel = (JLabel) idleTrailPanel.getClientProperty("valueLabel");
        idleTrailEditBtn = (JButton) idleTrailPanel.getClientProperty("editButton");
        
        // 显示边界距离设置项
        JPanel boundaryLengthPanel = createBoundaryLengthPanel();
        boundaryLengthVisibleLabel = (JLabel) boundaryLengthPanel.getClientProperty("valueLabel");
        
        // 开启测量模式设置项
        JPanel measurementModePanel = createMeasurementModePanel();
        measurementModeEnabledLabel = (JLabel) measurementModePanel.getClientProperty("valueLabel");
 
        JPanel feedbackPanel = createFeedbackPanel();
        
        // APP版本
        JPanel appVersionPanel = createAppVersionPanel();
        
        // 添加设置项,使用分割线分隔
        addSettingItem(panel, mowerIdPanel, true);
        addSettingItem(panel, mowerSizePanel, true);
        addSettingItem(panel, mowingSafetyDistancePanel, true);
        addSettingItem(panel, baseStationIdPanel, true);
        addSettingItem(panel, handheldPanel, true);
        addSettingItem(panel, simCardPanel, true);
        addSettingItem(panel, baseStationSimPanel, true);
        addSettingItem(panel, firmwarePanel, true);
        addSettingItem(panel, idleTrailPanel, true);
        addSettingItem(panel, boundaryLengthPanel, true);
        addSettingItem(panel, measurementModePanel, true);
        addSettingItem(panel, feedbackPanel, true);
        addSettingItem(panel, appVersionPanel, false);  // 最后一项不加分割线
        
        container.add(panel, BorderLayout.CENTER);
        return container;
    }
    
    /**
     * 添加设置项(带分割线)
     */
    private void addSettingItem(JPanel container, JPanel itemPanel, boolean showDivider) {
        container.add(itemPanel);
        if (showDivider) {
            // 添加分割线
            JSeparator divider = new JSeparator();
            divider.setForeground(DIVIDER_COLOR);
            divider.setMaximumSize(new Dimension(Integer.MAX_VALUE, 1));
            divider.setAlignmentX(Component.LEFT_ALIGNMENT);
            container.add(divider);
        }
    }
 
    private String formatIdleTrailDurationValue() {
        int seconds = setData != null ? setData.getIdleTrailDurationSeconds() : Setsys.DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        if (seconds <= 0) {
            seconds = Setsys.DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        }
        return seconds + "秒";
    }
 
    /**
     * 将字符串值转换为米(兼容旧数据,如果值大于100认为是厘米,需要除以100)
     */
    private double convertToMeters(String value) {
        if (value == null || value.trim().isEmpty() || "-1".equals(value.trim())) {
            return -1;
        }
        try {
            double val = Double.parseDouble(value.trim());
            // 如果值大于100,认为是厘米,需要转换为米
            if (val > 100) {
                return val / 100.0;
            }
            return val;
        } catch (NumberFormatException e) {
            return -1;
        }
    }
 
    /**
     * 格式化数值为米,保留2位小数
     */
    private String formatMeters(double value) {
        if (value < 0) {
            return "未设置";
        }
        return String.format("%.2f", value) + "m";
    }
 
    private String formatMowerSize() {
        Device device = Device.getActiveDevice();
        if (device == null) {
            return "未设置";
        }
        String width = device.getMowerWidth();
        String length = device.getMowerLength();
        double widthMeters = convertToMeters(width);
        double lengthMeters = convertToMeters(length);
        
        if (widthMeters < 0 && lengthMeters < 0) {
            return "未设置";
        }
        
        String widthStr = widthMeters >= 0 ? formatMeters(widthMeters) : "未设置";
        String lengthStr = lengthMeters >= 0 ? formatMeters(lengthMeters) : "未设置";
        return lengthStr + " × " + widthStr;
    }
 
    private String formatMowingSafetyDistance() {
        Device device = Device.getActiveDevice();
        if (device == null) {
            return "未设置";
        }
        String distance = device.getMowingSafetyDistance();
        double distanceMeters = convertToMeters(distance);
        if (distanceMeters < 0) {
            return "未设置";
        }
        return formatMeters(distanceMeters);
    }
 
    
    private JPanel createSettingItemPanel(String title, String value, boolean editable) {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);  // 透明背景,由容器绘制
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
        panel.setBorder(BorderFactory.createEmptyBorder(ITEM_PADDING, ITEM_PADDING, ITEM_PADDING, ITEM_PADDING));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        JLabel valueLabel = new JLabel(value);
        valueLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        valueLabel.setForeground(Color.DARK_GRAY);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(valueLabel, gbc);
 
        panel.putClientProperty("valueLabel", valueLabel);
 
        if (editable) {
            JButton editBtn = createEditButton();
            editBtn.setPreferredSize(new Dimension(30, 30));
            editBtn.setMinimumSize(new Dimension(30, 30));
            editBtn.setMaximumSize(new Dimension(30, 30));
            gbc = new GridBagConstraints();
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.weightx = 0;
            gbc.anchor = GridBagConstraints.EAST;
            panel.add(editBtn, gbc);
            panel.putClientProperty("editButton", editBtn);
        }
 
        return panel;
    }
    
    private JPanel createAppVersionPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);  // 透明背景
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
        panel.setBorder(BorderFactory.createEmptyBorder(ITEM_PADDING, ITEM_PADDING, ITEM_PADDING, ITEM_PADDING));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel("APP版本");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        appVersionLabel = new JLabel(setData.getAppVersion() != null ? setData.getAppVersion() : "未设置");
        appVersionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        appVersionLabel.setForeground(Color.DARK_GRAY);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(appVersionLabel, gbc);
 
        checkUpdateBtn = buttonset.createStyledButton("检查更新", THEME_COLOR);
        checkUpdateBtn.setFont(new Font("微软雅黑", Font.PLAIN, 12));
 
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(checkUpdateBtn, gbc);
 
        return panel;
    }
 
    private JPanel createFeedbackPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);  // 透明背景
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
        panel.setBorder(BorderFactory.createEmptyBorder(ITEM_PADDING, ITEM_PADDING, ITEM_PADDING, ITEM_PADDING));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel("问题反馈咨询");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        feedbackButton = buttonset.createStyledButton("反馈", THEME_COLOR);
        feedbackButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
 
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(feedbackButton, gbc);
 
        return panel;
    }
    
    /**
     * 创建显示边界距离设置面板
     */
    private JPanel createBoundaryLengthPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);  // 透明背景
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
        panel.setBorder(BorderFactory.createEmptyBorder(ITEM_PADDING, ITEM_PADDING, ITEM_PADDING, ITEM_PADDING));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel("显示边界距离");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        boundaryLengthVisibleLabel = new JLabel(setData.isBoundaryLengthVisible() ? "已开启" : "已关闭");
        boundaryLengthVisibleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        boundaryLengthVisibleLabel.setForeground(Color.DARK_GRAY);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(boundaryLengthVisibleLabel, gbc);
 
        panel.putClientProperty("valueLabel", boundaryLengthVisibleLabel);
 
        // 创建切换按钮(使用图标)
        JButton toggleBtn = createBoundaryLengthToggleButton();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(toggleBtn, gbc);
        panel.putClientProperty("toggleButton", toggleBtn);
 
        return panel;
    }
    
    /**
     * 创建开启测量模式设置面板
     */
    private JPanel createMeasurementModePanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);  // 透明背景
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
        panel.setBorder(BorderFactory.createEmptyBorder(ITEM_PADDING, ITEM_PADDING, ITEM_PADDING, ITEM_PADDING));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel("开启测量模式");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        measurementModeEnabledLabel = new JLabel(setData.isMeasurementModeEnabled() ? "已开启" : "已关闭");
        measurementModeEnabledLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        measurementModeEnabledLabel.setForeground(Color.DARK_GRAY);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(measurementModeEnabledLabel, gbc);
 
        panel.putClientProperty("valueLabel", measurementModeEnabledLabel);
 
        // 创建切换按钮(使用图标)
        JButton toggleBtn = createMeasurementModeToggleButton();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(toggleBtn, gbc);
        panel.putClientProperty("toggleButton", toggleBtn);
 
        return panel;
    }
    
    /**
     * 创建测量模式切换按钮
     */
    private JButton createMeasurementModeToggleButton() {
        JButton button = new JButton();
        button.setContentAreaFilled(false);
        button.setBorder(null);
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setPreferredSize(new Dimension(32, 32));
        button.setMinimumSize(new Dimension(32, 32));
        button.setMaximumSize(new Dimension(32, 32));
        
        updateMeasurementModeToggleButton(button);
        
        button.addActionListener(e -> toggleMeasurementMode(button));
        
        return button;
    }
    
    /**
     * 更新测量模式切换按钮图标
     */
    private void updateMeasurementModeToggleButton(JButton button) {
        boolean isEnabled = setData.isMeasurementModeEnabled();
        try {
            String iconPath = isEnabled ? "image/open.png" : "image/close.png";
            ImageIcon icon = new ImageIcon(iconPath);
            if (icon.getIconWidth() > 0) {
                Image scaledImage = icon.getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH);
                button.setIcon(new ImageIcon(scaledImage));
                button.setText(null);
            } else {
                button.setIcon(null);
                button.setText(isEnabled ? "开" : "关");
            }
        } catch (Exception e) {
            button.setIcon(null);
            button.setText(isEnabled ? "开" : "关");
            System.err.println("无法加载测量模式图标: " + e.getMessage());
        }
    }
    
    /**
     * 切换测量模式状态
     */
    private void toggleMeasurementMode(JButton button) {
        boolean newValue = !setData.isMeasurementModeEnabled();
        setData.setMeasurementModeEnabled(newValue);
        
        // 保存到配置文件
        setData.updateProperty("measurementModeEnabled", String.valueOf(newValue));
        
        // 更新UI
        if (measurementModeEnabledLabel != null) {
            measurementModeEnabledLabel.setText(newValue ? "已开启" : "已关闭");
        }
        updateMeasurementModeToggleButton(button);
        
        // 通知MapRenderer更新
        Shouye shouye = Shouye.getInstance();
        if (shouye != null) {
            MapRenderer renderer = shouye.getMapRenderer();
            if (renderer != null) {
                renderer.setMeasurementMode(newValue);
            }
            if (newValue) {
                celiangmoshi.start();
            } else {
                celiangmoshi.stop();
            }
            // 刷新地图显示(通过MapRenderer触发重绘)
            if (renderer != null) {
                renderer.repaint();
            }
        }
    }
    
    /**
     * 创建边界距离显示切换按钮
     */
    private JButton createBoundaryLengthToggleButton() {
        JButton button = new JButton();
        button.setContentAreaFilled(false);
        button.setBorder(null);
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setPreferredSize(new Dimension(32, 32));
        button.setMinimumSize(new Dimension(32, 32));
        button.setMaximumSize(new Dimension(32, 32));
        
        updateBoundaryLengthToggleButton(button);
        
        button.addActionListener(e -> toggleBoundaryLengthVisible(button));
        
        return button;
    }
    
    /**
     * 更新边界距离切换按钮图标
     */
    private void updateBoundaryLengthToggleButton(JButton button) {
        boolean isVisible = setData.isBoundaryLengthVisible();
        try {
            String iconPath = isVisible ? "image/open.png" : "image/close.png";
            ImageIcon icon = new ImageIcon(iconPath);
            if (icon.getIconWidth() > 0) {
                Image scaledImage = icon.getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH);
                button.setIcon(new ImageIcon(scaledImage));
                button.setText(null);
            } else {
                button.setIcon(null);
                button.setText(isVisible ? "开" : "关");
            }
        } catch (Exception e) {
            button.setIcon(null);
            button.setText(isVisible ? "开" : "关");
            System.err.println("无法加载边界距离图标: " + e.getMessage());
        }
    }
    
    /**
     * 切换边界距离显示状态
     */
    private void toggleBoundaryLengthVisible(JButton button) {
        boolean newValue = !setData.isBoundaryLengthVisible();
        setData.setBoundaryLengthVisible(newValue);
        
        // 保存到配置文件
        setData.updateProperty("boundaryLengthVisible", String.valueOf(newValue));
        
        // 更新UI
        if (boundaryLengthVisibleLabel != null) {
            boundaryLengthVisibleLabel.setText(newValue ? "已开启" : "已关闭");
        }
        updateBoundaryLengthToggleButton(button);
        
        // 通知MapRenderer更新
        Shouye shouye = Shouye.getInstance();
        if (shouye != null) {
            MapRenderer renderer = shouye.getMapRenderer();
            if (renderer != null) {
                renderer.setBoundaryLengthVisible(newValue);
            }
        }
    }
    
    private JButton createEditButton() {
        JButton button = new JButton();
        try {
            // 加载编辑图标
            ImageIcon editIcon = new ImageIcon("image/bianjie.png");
            // 调整图片大小
            Image scaledImage = editIcon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
            button.setIcon(new ImageIcon(scaledImage));
        } catch (Exception e) {
            // 如果图片加载失败,使用文本
            button.setText("编辑");
            System.err.println("无法加载编辑图标: " + e.getMessage());
        }
        
        button.setPreferredSize(new Dimension(30, 30));
        button.setBackground(PANEL_BACKGROUND);
        button.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        button.setFocusPainted(false);
        
        // 悬停效果
        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                button.setBackground(new Color(240, 240, 240));
            }
            public void mouseExited(MouseEvent e) {
                button.setBackground(PANEL_BACKGROUND);
            }
        });
        
        return button;
    }
    
    private void loadData() {
        // 从Setsys类加载数据
        setData.initializeFromProperties();
        baseStation.load();
        // 从device.properties加载设备数据
        Device device = Device.getActiveDevice();
        if (device != null) {
            device.initFromProperties();
        }
        updateDisplay();
        // 加载并应用上次保存的视图中心坐标
        loadViewCenterFromProperties();
    }
    
    /**
     * 从配置文件加载视图中心坐标并应用到MapRenderer
     */
    private void loadViewCenterFromProperties() {
        Shouye shouye = Shouye.getInstance();
        if (shouye == null) {
            return;
        }
        MapRenderer renderer = shouye.getMapRenderer();
        if (renderer == null) {
            return;
        }
        
        // 从配置文件读取视图中心坐标
        String viewCenterXValue = Setsys.getPropertyValue("viewCenterX");
        String viewCenterYValue = Setsys.getPropertyValue("viewCenterY");
        
        double savedTranslateX = 0.0;
        double savedTranslateY = 0.0;
        
        if (viewCenterXValue != null && !viewCenterXValue.trim().isEmpty()) {
            try {
                savedTranslateX = Double.parseDouble(viewCenterXValue.trim());
            } catch (NumberFormatException e) {
                savedTranslateX = 0.0;
            }
        }
        if (viewCenterYValue != null && !viewCenterYValue.trim().isEmpty()) {
            try {
                savedTranslateY = Double.parseDouble(viewCenterYValue.trim());
            } catch (NumberFormatException e) {
                savedTranslateY = 0.0;
            }
        }
        
        // 应用视图中心坐标(保持当前缩放比例)
        double currentScale = renderer.getScale();
        renderer.setViewTransform(currentScale, savedTranslateX, savedTranslateY);
    }
    
    private void updateDisplay() {
        // 更新割草机编号显示
        if (mowerIdLabel != null) {
            mowerIdLabel.setText(setData.getMowerId() != null ? setData.getMowerId() : "未设置");
        }
 
        // 更新割草机长宽显示
        if (mowerSizeLabel != null) {
            mowerSizeLabel.setText(formatMowerSize());
        }
 
        // 更新割草安全距离显示
        if (mowingSafetyDistanceLabel != null) {
            mowingSafetyDistanceLabel.setText(formatMowingSafetyDistance());
        }
 
        if (baseStationIdLabel != null) {
            baseStationIdLabel.setText(resolveBaseStationId());
        }
 
        if (handheldMarkerLabel != null) {
            handheldMarkerLabel.setText(setData.getHandheldMarkerId() != null ? setData.getHandheldMarkerId() : "未设置");
        }
        
        // 更新物联卡号显示
        if (simCardNumberLabel != null) {
            simCardNumberLabel.setText(setData.getSimCardNumber() != null ? 
                setData.getSimCardNumber() : "未设置");
        }
 
        if (baseStationSimLabel != null) {
            baseStationSimLabel.setText(resolveBaseStationSimCard());
        }
        
        // 更新固件版本显示
        if (firmwareVersionLabel != null) {
            firmwareVersionLabel.setText(setData.getFirmwareVersion() != null ? 
                setData.getFirmwareVersion() : "未设置");
        }
 
        if (idleTrailDurationLabel != null) {
            idleTrailDurationLabel.setText(formatIdleTrailDurationValue());
        }
        
        // 更新显示边界距离状态
        if (boundaryLengthVisibleLabel != null) {
            boundaryLengthVisibleLabel.setText(setData.isBoundaryLengthVisible() ? "已开启" : "已关闭");
        }
        // 更新切换按钮图标
        JPanel boundaryLengthPanel = (JPanel) boundaryLengthVisibleLabel.getParent();
        if (boundaryLengthPanel != null) {
            JButton toggleBtn = (JButton) boundaryLengthPanel.getClientProperty("toggleButton");
            if (toggleBtn != null) {
                updateBoundaryLengthToggleButton(toggleBtn);
            }
        }
        
        // 更新测量模式状态
        if (measurementModeEnabledLabel != null) {
            measurementModeEnabledLabel.setText(setData.isMeasurementModeEnabled() ? "已开启" : "已关闭");
        }
        // 更新测量模式切换按钮图标
        JPanel measurementModePanel = (JPanel) measurementModeEnabledLabel.getParent();
        if (measurementModePanel != null) {
            JButton toggleBtn = (JButton) measurementModePanel.getClientProperty("toggleButton");
            if (toggleBtn != null) {
                updateMeasurementModeToggleButton(toggleBtn);
            }
        }
        
        // 更新APP版本显示
        if (appVersionLabel != null) {
            appVersionLabel.setText(setData.getAppVersion() != null ? 
                setData.getAppVersion() : "未设置");
        }
    }
 
    private String resolveBaseStationSimCard() {
        if (baseStation == null) {
            return "未设置";
        }
        String value = baseStation.getIotSimCardNumber();
        if (value == null) {
            return "未设置";
        }
        String trimmed = value.trim();
        if (trimmed.isEmpty() || "-1".equals(trimmed)) {
            return "未设置";
        }
        return trimmed;
    }
 
    private String resolveBaseStationId() {
        if (baseStation == null) {
            return "未设置";
        }
        String value = baseStation.getDeviceId();
        if (value == null) {
            return "未设置";
        }
        String trimmed = value.trim();
        if (trimmed.isEmpty() || "-1".equals(trimmed)) {
            return "未设置";
        }
        return trimmed;
    }
    
    private void setupEventHandlers() {
        // 割草机编号编辑按钮事件
        if (mowerIdEditBtn != null) {
            mowerIdEditBtn.addActionListener(e -> editMowerId());
        }
 
        // 割草机长宽编辑按钮事件
        if (mowerSizeEditBtn != null) {
            mowerSizeEditBtn.addActionListener(e -> editMowerSize());
        }
 
        // 割草安全距离编辑按钮事件
        if (mowingSafetyDistanceEditBtn != null) {
            mowingSafetyDistanceEditBtn.addActionListener(e -> editMowingSafetyDistance());
        }
 
        if (baseStationIdEditBtn != null) {
            baseStationIdEditBtn.addActionListener(e -> editBaseStationId());
        }
        
        // 检查更新按钮事件
        if (checkUpdateBtn != null) {
            checkUpdateBtn.addActionListener(e -> checkForUpdates());
        }
 
        if (handheldEditBtn != null) {
            handheldEditBtn.addActionListener(e -> editHandheldMarkerId());
        }
 
        if (feedbackButton != null) {
            feedbackButton.addActionListener(e -> showFeedbackDialog());
        }
 
        if (idleTrailEditBtn != null) {
            idleTrailEditBtn.addActionListener(e -> editIdleTrailDuration());
        }
        
    }
    
    private void editMowerId() {
        String currentValue = setData.getMowerId() != null ? setData.getMowerId() : "";
        String newValue = (String) JOptionPane.showInputDialog(this,
            "请输入割草机编号:",
            "修改割草机编号",
            JOptionPane.QUESTION_MESSAGE,
            null,
            null,
            currentValue);
 
        if (newValue == null) {
            return; // 用户取消
        }
 
        newValue = newValue.trim();
        if (!newValue.isEmpty()) {
            if (setData.updateProperty("mowerId", newValue)) {
                mowerIdLabel.setText(newValue);
                JOptionPane.showMessageDialog(this, "割草机编号更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "割草机编号更新失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
 
    private void editMowerSize() {
        Device device = Device.getActiveDevice();
        if (device == null) {
            JOptionPane.showMessageDialog(this, "设备未初始化", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
 
        // 获取当前值并转换为米(兼容旧数据)
        double currentLengthMeters = convertToMeters(device.getMowerLength());
        double currentWidthMeters = convertToMeters(device.getMowerWidth());
        
        String currentLengthStr = currentLengthMeters >= 0 ? String.format("%.2f", currentLengthMeters) : "";
        String currentWidthStr = currentWidthMeters >= 0 ? String.format("%.2f", currentWidthMeters) : "";
 
        // 创建输入对话框
        JPanel inputPanel = new JPanel(new GridLayout(2, 2, 5, 5));
        inputPanel.add(new JLabel("长度(m):"));
        JTextField lengthField = new JTextField(currentLengthStr);
        inputPanel.add(lengthField);
        inputPanel.add(new JLabel("宽度(m):"));
        JTextField widthField = new JTextField(currentWidthStr);
        inputPanel.add(widthField);
 
        int result = JOptionPane.showConfirmDialog(this,
            inputPanel,
            "修改割草机长宽",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);
 
        if (result == JOptionPane.OK_OPTION) {
            String newLength = lengthField.getText().trim();
            String newWidth = widthField.getText().trim();
 
            // 验证输入
            if (newLength.isEmpty() && newWidth.isEmpty()) {
                JOptionPane.showMessageDialog(this, "长度和宽度不能同时为空", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
 
            double lengthValue = -1;
            double widthValue = -1;
 
            if (!newLength.isEmpty()) {
                try {
                    lengthValue = Double.parseDouble(newLength);
                    if (lengthValue < 0) {
                        JOptionPane.showMessageDialog(this, "长度必须大于等于0", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(this, "长度必须是有效的数字", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
 
            if (!newWidth.isEmpty()) {
                try {
                    widthValue = Double.parseDouble(newWidth);
                    if (widthValue < 0) {
                        JOptionPane.showMessageDialog(this, "宽度必须大于等于0", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(this, "宽度必须是有效的数字", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
 
            // 更新设备属性(保存为米,保留2位小数)
            if (lengthValue >= 0) {
                String lengthStr = String.format("%.2f", lengthValue);
                device.setMowerLength(lengthStr);
                device.updateField("mowerLength", lengthStr);
            } else {
                device.setMowerLength("-1");
                device.updateField("mowerLength", "-1");
            }
 
            if (widthValue >= 0) {
                String widthStr = String.format("%.2f", widthValue);
                device.setMowerWidth(widthStr);
                device.updateField("mowerWidth", widthStr);
            } else {
                device.setMowerWidth("-1");
                device.updateField("mowerWidth", "-1");
            }
 
            // 如果长度和宽度都有效,自动计算安全距离
            if (lengthValue > 0 && widthValue > 0) {
                try {
                    double safetyDistance = MowerSafetyDistanceCalculator.calculateSafetyDistance(lengthValue, widthValue);
                    String safetyDistanceStr = String.format("%.2f", safetyDistance);
                    device.setMowingSafetyDistance(safetyDistanceStr);
                    device.updateField("mowingSafetyDistance", safetyDistanceStr);
                    
                    // 更新安全距离显示
                    if (mowingSafetyDistanceLabel != null) {
                        mowingSafetyDistanceLabel.setText(formatMowingSafetyDistance());
                    }
                } catch (IllegalArgumentException e) {
                    // 如果计算失败,不更新安全距离
                    System.err.println("计算安全距离失败: " + e.getMessage());
                }
            }
 
            // 保存到device.properties
            device.saveToProperties();
 
            // 更新显示
            if (mowerSizeLabel != null) {
                mowerSizeLabel.setText(formatMowerSize());
            }
            JOptionPane.showMessageDialog(this, "割草机长宽更新成功" + 
                (lengthValue > 0 && widthValue > 0 ? ",安全距离已自动计算" : ""), 
                "成功", JOptionPane.INFORMATION_MESSAGE);
        }
    }
 
    private void editMowingSafetyDistance() {
        Device device = Device.getActiveDevice();
        if (device == null) {
            JOptionPane.showMessageDialog(this, "设备未初始化", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
 
        // 获取当前值并转换为米(兼容旧数据)
        double currentDistanceMeters = convertToMeters(device.getMowingSafetyDistance());
        String currentValueStr = currentDistanceMeters >= 0 ? String.format("%.2f", currentDistanceMeters) : "";
 
        String newValue = (String) JOptionPane.showInputDialog(this,
            "请输入割草安全距离(单位:m):",
            "修改割草安全距离",
            JOptionPane.QUESTION_MESSAGE,
            null,
            null,
            currentValueStr);
 
        if (newValue == null) {
            return; // 用户取消
        }
 
        newValue = newValue.trim();
        if (newValue.isEmpty()) {
            JOptionPane.showMessageDialog(this, "割草安全距离不能为空", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        // 验证输入
        double distanceValue;
        try {
            distanceValue = Double.parseDouble(newValue);
            if (distanceValue < 0) {
                JOptionPane.showMessageDialog(this, "割草安全距离必须大于等于0", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "割草安全距离必须是有效的数字", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        // 更新设备属性(保存为米,保留2位小数)
        String distanceStr = String.format("%.2f", distanceValue);
        device.setMowingSafetyDistance(distanceStr);
        device.updateField("mowingSafetyDistance", distanceStr);
 
        // 保存到device.properties
        device.saveToProperties();
 
        // 更新显示
        if (mowingSafetyDistanceLabel != null) {
            mowingSafetyDistanceLabel.setText(formatMowingSafetyDistance());
        }
        JOptionPane.showMessageDialog(this, "割草安全距离更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
    }
 
    private void editHandheldMarkerId() {
        String currentValue = setData.getHandheldMarkerId() != null ? setData.getHandheldMarkerId() : "";
        String newValue = (String) JOptionPane.showInputDialog(this,
                "请输入便携打点器编号:",
                "修改便携打点器编号",
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                currentValue);
 
        if (newValue == null) {
            return;
        }
 
        newValue = newValue.trim();
        if (!newValue.isEmpty()) {
            if (setData.updateProperty("handheldMarkerId", newValue)) {
                if (handheldMarkerLabel != null) {
                    handheldMarkerLabel.setText(newValue);
                }
                JOptionPane.showMessageDialog(this, "便携打点器编号更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "便携打点器编号更新失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
 
    private void editBaseStationId() {
        String currentValue = "未设置".equals(resolveBaseStationId()) ? "" : resolveBaseStationId();
        String newValue = (String) JOptionPane.showInputDialog(this,
                "请输入差分基准站编号:",
                "修改差分基准站编号",
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                currentValue);
 
        if (newValue == null) {
            return;
        }
 
        newValue = newValue.trim();
        if (newValue.isEmpty()) {
            JOptionPane.showMessageDialog(this, "差分基准站编号不能为空", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        try {
            baseStation.updateByDeviceId(newValue,
                    baseStation.getInstallationCoordinates(),
                    baseStation.getIotSimCardNumber(),
                    baseStation.getDeviceActivationTime(),
                    baseStation.getDataUpdateTime());
            baseStation.load();
            if (baseStationIdLabel != null) {
                baseStationIdLabel.setText(resolveBaseStationId());
            }
            JOptionPane.showMessageDialog(this, "差分基准站编号更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
        } catch (IllegalArgumentException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "输入错误", JOptionPane.WARNING_MESSAGE);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "差分基准站编号更新失败", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
 
    private void editIdleTrailDuration() {
        int currentSeconds = setData != null ? setData.getIdleTrailDurationSeconds() : Setsys.DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        if (currentSeconds <= 0) {
            currentSeconds = Setsys.DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        }
 
        String input = JOptionPane.showInputDialog(this,
            "请输入轨迹拖尾时长(单位:秒)",
            currentSeconds);
 
        if (input == null) {
            return;
        }
 
        String trimmed = input.trim();
        if (trimmed.isEmpty()) {
            JOptionPane.showMessageDialog(this, "轨迹拖尾时长不能为空", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        int parsedSeconds;
        try {
            parsedSeconds = Integer.parseInt(trimmed);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入有效的整数秒数", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        if (parsedSeconds < 5 || parsedSeconds > 600) {
            JOptionPane.showMessageDialog(this, "请输入5到600之间的秒数", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        if (setData.updateProperty("idleTrailDurationSeconds", String.valueOf(parsedSeconds))) {
            int appliedSeconds = setData.getIdleTrailDurationSeconds();
            if (idleTrailDurationLabel != null) {
                idleTrailDurationLabel.setText(appliedSeconds + "秒");
            }
            MapRenderer renderer = null;
            Shouye shouye = Shouye.getInstance();
            if (shouye != null) {
                renderer = shouye.getMapRenderer();
            }
            if (renderer != null) {
                renderer.setIdleTrailDurationSeconds(appliedSeconds);
            }
            JOptionPane.showMessageDialog(this, "轨迹拖尾时长已更新为 " + appliedSeconds + " 秒", "成功", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "轨迹拖尾时长更新失败", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
 
    private void showFeedbackDialog() {
    JDialog dialog = new JDialog(this, "问题反馈咨询", true);
    dialog.setLayout(new BorderLayout(0, 12));
    dialog.setResizable(false);
    dialog.setPreferredSize(new Dimension(400, 800));
    dialog.setSize(new Dimension(400, 800));
    dialog.setMinimumSize(new Dimension(400, 800));
 
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.setBorder(BorderFactory.createEmptyBorder(20, 24, 20, 24));
    dialog.add(content, BorderLayout.CENTER);
 
        JLabel descriptionLabel = new JLabel("问题描述:");
        descriptionLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        descriptionLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        content.add(descriptionLabel);
 
        content.add(Box.createRigidArea(new Dimension(0, 8)));
 
        JTextArea descriptionArea = new JTextArea(5, 30);
        descriptionArea.setLineWrap(true);
        descriptionArea.setWrapStyleWord(true);
        descriptionArea.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        JScrollPane scrollPane = new JScrollPane(descriptionArea);
        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
        content.add(scrollPane);
 
        content.add(Box.createRigidArea(new Dimension(0, 16)));
 
        JPanel photoControls = new JPanel(new FlowLayout(FlowLayout.LEFT, 12, 0));
        photoControls.setAlignmentX(Component.LEFT_ALIGNMENT);
        JLabel photoLabel = new JLabel("选择照片(最多6张):");
        photoLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
    JButton selectPhotosButton = buttonset.createStyledButton("选择照片", THEME_COLOR);
    selectPhotosButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        photoControls.add(photoLabel);
        photoControls.add(selectPhotosButton);
    content.add(photoControls);
 
    content.add(Box.createRigidArea(new Dimension(0, 20)));
 
        JPanel photoGrid = new JPanel(new GridLayout(2, 3, 12, 12));
        photoGrid.setAlignmentX(Component.LEFT_ALIGNMENT);
        photoGrid.setMaximumSize(new Dimension(Integer.MAX_VALUE, 220));
        List<JLabel> photoSlots = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            JLabel slot = buildPhotoSlot();
            photoGrid.add(slot);
            photoSlots.add(slot);
        }
        content.add(photoGrid);
 
        content.add(Box.createRigidArea(new Dimension(0, 20)));
 
    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton cancelButton = buttonset.createStyledButton("放弃", new Color(128, 128, 128));
    cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
    JButton submitButton = buttonset.createStyledButton("提交", THEME_COLOR);
    submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
 
        buttonPanel.add(cancelButton);
        buttonPanel.add(submitButton);
        dialog.add(buttonPanel, BorderLayout.SOUTH);
 
        List<File> selectedPhotos = new ArrayList<>();
 
        selectPhotosButton.addActionListener(e -> {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setFileFilter(new FileNameExtensionFilter("图片文件", "jpg", "jpeg", "png", "bmp", "gif", "webp"));
 
            if (chooser.showOpenDialog(dialog) == JFileChooser.APPROVE_OPTION) {
                File[] files = chooser.getSelectedFiles();
                if (files != null) {
                    if (selectedPhotos.size() >= 6) {
                        JOptionPane.showMessageDialog(dialog, "已达到6张照片上限", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    for (File file : files) {
                        if (selectedPhotos.size() >= 6) {
                            break;
                        }
                        if (file != null && file.exists() && file.isFile()) {
                            selectedPhotos.add(file);
                        }
                    }
                    updatePhotoPreview(photoSlots, selectedPhotos);
                }
            }
        });
 
        cancelButton.addActionListener(e -> dialog.dispose());
 
        submitButton.addActionListener(e -> {
            String description = descriptionArea.getText() != null ? descriptionArea.getText().trim() : "";
            if (description.isEmpty()) {
                JOptionPane.showMessageDialog(dialog, "请填写问题描述", "提示", JOptionPane.WARNING_MESSAGE);
                descriptionArea.requestFocusInWindow();
                return;
            }
 
            // 提交逻辑占位
            JOptionPane.showMessageDialog(dialog,
                    "反馈已提交,感谢您的支持!\n描述字数:" + description.length() + ",照片数量:" + selectedPhotos.size(),
                    "提交成功",
                    JOptionPane.INFORMATION_MESSAGE);
            dialog.dispose();
        });
 
        dialog.pack();
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }
 
    private JLabel buildPhotoSlot() {
        JLabel slot = new JLabel();
        slot.setPreferredSize(new Dimension(100, 100));
        slot.setMinimumSize(new Dimension(100, 100));
        slot.setOpaque(true);
        slot.setBackground(new Color(245, 245, 245));
        slot.setHorizontalAlignment(SwingConstants.CENTER);
        slot.setVerticalAlignment(SwingConstants.CENTER);
        slot.setBorder(BorderFactory.createLineBorder(new Color(220, 220, 220), 1));
        slot.setText("+");
        slot.setFont(new Font("微软雅黑", Font.BOLD, 18));
        slot.setForeground(new Color(180, 180, 180));
        return slot;
    }
 
    private void updatePhotoPreview(List<JLabel> slots, List<File> photos) {
        for (int i = 0; i < slots.size(); i++) {
            JLabel slot = slots.get(i);
            if (i < photos.size()) {
                File photo = photos.get(i);
                ImageIcon icon = new ImageIcon(photo.getAbsolutePath());
                Image scaled = icon.getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
                slot.setIcon(new ImageIcon(scaled));
                slot.setText(null);
            } else {
                slot.setIcon(null);
                slot.setText("+");
            }
        }
    }
    
    private void checkForUpdates() {
        // 模拟检查更新过程
        checkUpdateBtn.setEnabled(false);
        checkUpdateBtn.setText("检查中...");
        
        // 使用定时器模拟网络请求
        Timer timer = new Timer(2000, e -> {
            // 这里应该是实际的检查更新逻辑
            // 目前只是模拟
            checkUpdateBtn.setEnabled(true);
            checkUpdateBtn.setText("检查更新");
            
            // 假设当前已是最新版本
            JOptionPane.showMessageDialog(this, 
                "当前已是最新版本: " + (setData.getAppVersion() != null ? setData.getAppVersion() : "未知"), 
                "检查更新", 
                JOptionPane.INFORMATION_MESSAGE);
        });
        timer.setRepeats(false);
        timer.start();
    }
    
    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            setupEventHandlers();
            loadData(); // 每次显示时重新加载数据
        }
        super.setVisible(visible);
    }
       
}