张世豪
7 天以前 f784463ab019c1113cf0b31a249e8802b07a57fa
src/set/Sets.java
@@ -1,4 +1,12 @@
package set;
import baseStation.BaseStation;
import zhuye.MapRenderer;
import zhuye.Shouye;
import zhuye.buttonset;
import set.Setsys;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
@@ -24,23 +32,30 @@
    
    // 设置项组件
    private JLabel mowerIdLabel;
    private JLabel baseStationIdLabel;
    private JLabel handheldMarkerLabel;
    private JLabel simCardNumberLabel;
    private JLabel baseStationSimLabel;
    private JLabel firmwareVersionLabel;
    private JLabel appVersionLabel;
    private JLabel idleTrailDurationLabel;
    
    private JButton mowerIdEditBtn;
    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();
    }
@@ -49,6 +64,7 @@
        super(parent, "系统设置", true);
        this.THEME_COLOR = themeColor;
        this.setData = new Setsys();
        this.baseStation = new BaseStation();
        initializeUI();
        loadData();
    }
@@ -92,6 +108,11 @@
            setData.getMowerId() != null ? setData.getMowerId() : "未设置", true);
        mowerIdLabel = (JLabel) mowerIdPanel.getClientProperty("valueLabel");
        mowerIdEditBtn = (JButton) mowerIdPanel.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);
@@ -99,30 +120,50 @@
        handheldEditBtn = (JButton) handheldPanel.getClientProperty("editButton");
        // 物联卡号
        JPanel simCardPanel = createSettingItemPanel("物联卡号",
        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 feedbackPanel = createFeedbackPanel();
        
        // APP版本
        JPanel appVersionPanel = createAppVersionPanel();
        
        addRowWithSpacing(panel, mowerIdPanel);
    addRowWithSpacing(panel, mowerIdPanel);
    addRowWithSpacing(panel, baseStationIdPanel);
        addRowWithSpacing(panel, handheldPanel);
        addRowWithSpacing(panel, simCardPanel);
        addRowWithSpacing(panel, firmwarePanel);
        addRowWithSpacing(panel, feedbackPanel);
        panel.add(appVersionPanel);
    addRowWithSpacing(panel, simCardPanel);
    addRowWithSpacing(panel, baseStationSimPanel);
    addRowWithSpacing(panel, firmwarePanel);
    addRowWithSpacing(panel, idleTrailPanel);
    addRowWithSpacing(panel, feedbackPanel);
    addRowWithSpacing(panel, appVersionPanel);
        
        return panel;
    }
    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 + "秒";
    }
    private void addRowWithSpacing(JPanel container, JPanel row) {
        container.add(row);
        container.add(Box.createRigidArea(new Dimension(0, ROW_SPACING)));
@@ -209,28 +250,8 @@
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(appVersionLabel, gbc);
        checkUpdateBtn = new JButton("检查更新");
        checkUpdateBtn = buttonset.createStyledButton("检查更新", THEME_COLOR);
        checkUpdateBtn.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        checkUpdateBtn.setBackground(THEME_COLOR);
        checkUpdateBtn.setForeground(Color.WHITE);
        checkUpdateBtn.setBorder(BorderFactory.createEmptyBorder(0, 12, 0, 12));
        checkUpdateBtn.setPreferredSize(new Dimension(90, 25));
        checkUpdateBtn.setMinimumSize(new Dimension(90, 25));
        checkUpdateBtn.setMaximumSize(new Dimension(90, 25));
        checkUpdateBtn.setFocusPainted(false);
        checkUpdateBtn.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                checkUpdateBtn.setBackground(new Color(
                    Math.max(THEME_COLOR.getRed() - 20, 0),
                    Math.max(THEME_COLOR.getGreen() - 20, 0),
                    Math.max(THEME_COLOR.getBlue() - 20, 0)
                ));
            }
            public void mouseExited(MouseEvent e) {
                checkUpdateBtn.setBackground(THEME_COLOR);
            }
        });
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
@@ -263,30 +284,8 @@
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
        feedbackButton = new JButton("反馈");
        feedbackButton = buttonset.createStyledButton("反馈", THEME_COLOR);
        feedbackButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        feedbackButton.setBackground(THEME_COLOR);
        feedbackButton.setForeground(Color.WHITE);
        feedbackButton.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 20));
        feedbackButton.setPreferredSize(new Dimension(100, 28));
        feedbackButton.setMinimumSize(new Dimension(100, 28));
        feedbackButton.setMaximumSize(new Dimension(100, 28));
        feedbackButton.setFocusPainted(false);
        feedbackButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                feedbackButton.setBackground(new Color(
                        Math.max(THEME_COLOR.getRed() - 20, 0),
                        Math.max(THEME_COLOR.getGreen() - 20, 0),
                        Math.max(THEME_COLOR.getBlue() - 20, 0)));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                feedbackButton.setBackground(THEME_COLOR);
            }
        });
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
@@ -333,7 +332,50 @@
    private void loadData() {
        // 从Setsys类加载数据
        setData.initializeFromProperties();
        baseStation.load();
        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() {
@@ -342,6 +384,10 @@
            mowerIdLabel.setText(setData.getMowerId() != null ? setData.getMowerId() : "未设置");
        }
        if (baseStationIdLabel != null) {
            baseStationIdLabel.setText(resolveBaseStationId());
        }
        if (handheldMarkerLabel != null) {
            handheldMarkerLabel.setText(setData.getHandheldMarkerId() != null ? setData.getHandheldMarkerId() : "未设置");
        }
@@ -351,12 +397,20 @@
            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());
        }
        
        // 更新APP版本显示
        if (appVersionLabel != null) {
@@ -364,12 +418,46 @@
                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 (baseStationIdEditBtn != null) {
            baseStationIdEditBtn.addActionListener(e -> editBaseStationId());
        }
        
        // 检查更新按钮事件
        if (checkUpdateBtn != null) {
@@ -383,6 +471,10 @@
        if (feedbackButton != null) {
            feedbackButton.addActionListener(e -> showFeedbackDialog());
        }
        if (idleTrailEditBtn != null) {
            idleTrailEditBtn.addActionListener(e -> editIdleTrailDuration());
        }
        
    }
    
@@ -438,6 +530,97 @@
        }
    }
    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));
@@ -472,9 +655,8 @@
        photoControls.setAlignmentX(Component.LEFT_ALIGNMENT);
        JLabel photoLabel = new JLabel("选择照片(最多6张):");
        photoLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        JButton selectPhotosButton = new JButton("选择照片");
        selectPhotosButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        selectPhotosButton.setFocusPainted(false);
    JButton selectPhotosButton = buttonset.createStyledButton("选择照片", THEME_COLOR);
    selectPhotosButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        photoControls.add(photoLabel);
        photoControls.add(selectPhotosButton);
    content.add(photoControls);
@@ -494,13 +676,11 @@
        content.add(Box.createRigidArea(new Dimension(0, 20)));
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton cancelButton = new JButton("放弃");
        JButton submitButton = new JButton("提交");
        submitButton.setBackground(THEME_COLOR);
        submitButton.setForeground(Color.WHITE);
        submitButton.setFocusPainted(false);
        cancelButton.setFocusPainted(false);
    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);