张世豪
6 天以前 b315a6943e6c0d6bdf0d5f7565c570d719154d6c
src/lujing/MowingPathGenerationPage.java
@@ -1,6 +1,7 @@
package lujing;
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -143,14 +144,17 @@
        buttonPanel.setBackground(BACKGROUND_COLOR);
        
        JButton generateBtn = createPrimaryFooterButton("生成割草路径");
        JButton previewBtn = createPrimaryFooterButton("预览");
        JButton saveBtn = createPrimaryFooterButton("保存路径");
        JButton cancelBtn = createPrimaryFooterButton("取消");
        
        generateBtn.addActionListener(e -> generatePath(modeValue));
        previewBtn.addActionListener(e -> previewPath());
        saveBtn.addActionListener(e -> savePath());
        cancelBtn.addActionListener(e -> dispose());
        
        buttonPanel.add(generateBtn);
        buttonPanel.add(previewBtn);
        buttonPanel.add(saveBtn);
        buttonPanel.add(cancelBtn);
        add(buttonPanel, BorderLayout.SOUTH);
@@ -187,6 +191,123 @@
    }
    
    /**
     * 预览路径
     */
    private void previewPath() {
        // 先保存当前路径到地块(临时保存,用于预览)
        String pathNormalized = normalizeCoordinateInput(pathArea.getText());
        if (!"-1".equals(pathNormalized)) {
            pathNormalized = pathNormalized
                .replace("\r\n", ";")
                .replace('\r', ';')
                .replace('\n', ';')
                .replaceAll(";+", ";")
                .replaceAll("\\s*;\\s*", ";")
                .trim();
            if (pathNormalized.isEmpty()) {
                pathNormalized = "-1";
            }
        }
        if ("-1".equals(pathNormalized)) {
            JOptionPane.showMessageDialog(this, "请先生成割草路径", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        // 临时保存路径到地块对象(不持久化)
        if (saveCallback != null) {
            saveCallback.savePlannedPath(dikuai, pathNormalized);
        }
        // 保存当前页面状态,用于返回时恢复
        String currentBaseStation = baseStationField.getText();
        String currentBoundary = boundaryArea.getText();
        String currentObstacle = obstacleArea.getText();
        String currentWidth = widthField.getText();
        String currentPath = pathArea.getText();
        // 获取地块信息
        String landNumber = dikuai.getLandNumber();
        String landName = dikuai.getLandName();
        // 处理边界坐标,确保变量是 effectively final
        String boundaryInput = normalizeCoordinateInput(boundaryArea.getText());
        final String boundary;
        if (!"-1".equals(boundaryInput)) {
            String processed = boundaryInput.replace("\r\n", ";")
                .replace('\r', ';')
                .replace('\n', ';')
                .replaceAll(";+", ";")
                .replaceAll("\\s*;\\s*", ";")
                .trim();
            if (processed.isEmpty()) {
                boundary = dikuai.getBoundaryCoordinates();
            } else {
                boundary = processed;
            }
        } else {
            boundary = dikuai.getBoundaryCoordinates();
        }
        // 处理障碍物坐标,确保变量是 effectively final
        String obstaclesInput = normalizeCoordinateInput(obstacleArea.getText());
        final String obstacles;
        if (!"-1".equals(obstaclesInput)) {
            String processed = obstaclesInput.replace("\r\n", " ")
                .replace('\r', ' ')
                .replace('\n', ' ')
                .replaceAll("\\s{2,}", " ")
                .trim();
            if (processed.isEmpty()) {
                obstacles = null;
            } else {
                obstacles = processed;
            }
        } else {
            obstacles = null;
        }
        // 保存最终值到 final 变量,以便在 lambda 中使用
        final String finalPathNormalized = pathNormalized;
        final String finalLandNumber = landNumber;
        final String finalLandName = landName;
        // 关闭路径规划页面
        setVisible(false);
        // 打开主页面并显示路径预览
        SwingUtilities.invokeLater(() -> {
            zhuye.Shouye shouye = zhuye.Shouye.getInstance();
            if (shouye != null) {
                // 显示路径预览,并设置返回回调
                shouye.startMowingPathPreview(
                    finalLandNumber,
                    finalLandName,
                    boundary,
                    obstacles,
                    finalPathNormalized,
                    () -> {
                        // 返回回调:重新打开路径规划页面
                        SwingUtilities.invokeLater(() -> {
                            setVisible(true);
                            // 恢复之前的状态
                            baseStationField.setText(currentBaseStation);
                            boundaryArea.setText(currentBoundary);
                            obstacleArea.setText(currentObstacle);
                            widthField.setText(currentWidth);
                            pathArea.setText(currentPath);
                        });
                    }
                );
            } else {
                // 如果主页面不存在,提示用户并重新显示路径规划页面
                JOptionPane.showMessageDialog(null, "无法打开主页面进行预览", "提示", JOptionPane.WARNING_MESSAGE);
                setVisible(true);
            }
        });
    }
    /**
     * 保存路径
     */
    private void savePath() {