From 32524195d474b74e48916867b2a6c2f022a40d98 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期二, 09 十二月 2025 19:36:32 +0800
Subject: [PATCH] 20251209
---
src/zhangaiwu/AddDikuai.java | 392 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 355 insertions(+), 37 deletions(-)
diff --git a/src/zhangaiwu/AddDikuai.java b/src/zhangaiwu/AddDikuai.java
index ee81258..263b539 100644
--- a/src/zhangaiwu/AddDikuai.java
+++ b/src/zhangaiwu/AddDikuai.java
@@ -14,16 +14,17 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Comparator;
import java.awt.geom.Point2D;
+import baseStation.BaseStation;
import bianjie.jisuanmianjie;
import dikuai.Dikuai;
import dikuai.Dikuaiguanli;
-import gecaoji.Device;
import bianjie.bianjieguihua2;
import lujing.Lunjingguihua;
import ui.UIConfig;
@@ -49,6 +50,9 @@
private final Color LIGHT_TEXT = new Color(108, 117, 125);
private final Color BORDER_COLOR = new Color(222, 226, 230);
private final Color SUCCESS_COLOR = new Color(40, 167, 69);
+ private final Color ERROR_COLOR = new Color(220, 53, 69);
+ private static final String KEY_PATH_MESSAGE_TEXT = "__pathMessageText";
+ private static final String KEY_PATH_MESSAGE_SUCCESS = "__pathMessageSuccess";
// 姝ラ闈㈡澘
private JPanel mainPanel;
@@ -68,8 +72,12 @@
private JButton prevButton;
private JButton nextButton;
private JButton createButton;
+ private JButton previewButton;
+ private Component previewButtonSpacer;
private JLabel boundaryCountLabel;
private JPanel obstacleListContainer;
+ private JTextArea pathGenerationMessageArea;
+ private JPanel pathMessageWrapper;
// 鍦板潡鏁版嵁
private Map<String, String> dikuaiData = new HashMap<>();
@@ -967,6 +975,29 @@
stepPanel.add(Box.createRigidArea(new Dimension(0, 20)));
stepPanel.add(generatePathButton);
+ stepPanel.add(Box.createRigidArea(new Dimension(0, 12)));
+
+ pathMessageWrapper = new JPanel(new BorderLayout());
+ pathMessageWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
+ pathMessageWrapper.setBackground(PRIMARY_LIGHT);
+ pathMessageWrapper.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(PRIMARY_COLOR, 1),
+ BorderFactory.createEmptyBorder(12, 12, 12, 12)
+ ));
+ pathMessageWrapper.setVisible(false);
+
+ pathGenerationMessageArea = new JTextArea();
+ pathGenerationMessageArea.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 14));
+ pathGenerationMessageArea.setForeground(TEXT_COLOR);
+ pathGenerationMessageArea.setOpaque(false);
+ pathGenerationMessageArea.setEditable(false);
+ pathGenerationMessageArea.setLineWrap(true);
+ pathGenerationMessageArea.setWrapStyleWord(true);
+ pathGenerationMessageArea.setFocusable(false);
+ pathGenerationMessageArea.setBorder(null);
+
+ pathMessageWrapper.add(pathGenerationMessageArea, BorderLayout.CENTER);
+ stepPanel.add(pathMessageWrapper);
stepPanel.add(Box.createVerticalGlue());
return stepPanel;
@@ -1026,6 +1057,9 @@
private void generateMowingPath() {
if (!dikuaiData.containsKey("boundaryDrawn")) {
JOptionPane.showMessageDialog(this, "璇峰厛瀹屾垚杈圭晫缁樺埗鍚庡啀鐢熸垚璺緞", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("璇峰厛瀹屾垚杈圭晫缁樺埗鍚庡啀鐢熸垚璺緞銆�", false);
+ setPathAvailability(false);
showStep(2);
return;
}
@@ -1039,9 +1073,9 @@
}
if (boundaryCoords == null) {
JOptionPane.showMessageDialog(this, "鏈壘鍒版湁鏁堢殑鍦板潡杈圭晫鍧愭爣锛屾棤娉曠敓鎴愯矾寰�", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
- if (createButton != null) {
- createButton.setEnabled(false);
- }
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("鏈壘鍒版湁鏁堢殑鍦板潡杈圭晫鍧愭爣锛屾棤娉曠敓鎴愯矾寰勩��", false);
+ setPathAvailability(false);
return;
}
@@ -1060,17 +1094,17 @@
Object widthObj = mowingWidthSpinner.getValue();
if (!(widthObj instanceof Number)) {
JOptionPane.showMessageDialog(this, "鍓茶崏瀹藉害杈撳叆鏃犳晥", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
- if (createButton != null) {
- createButton.setEnabled(false);
- }
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("鍓茶崏瀹藉害杈撳叆鏃犳晥锛岃閲嶆柊杈撳叆銆�", false);
+ setPathAvailability(false);
return;
}
double widthCm = ((Number) widthObj).doubleValue();
if (widthCm <= 0) {
JOptionPane.showMessageDialog(this, "鍓茶崏瀹藉害蹇呴』澶т簬0", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
- if (createButton != null) {
- createButton.setEnabled(false);
- }
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("鍓茶崏瀹藉害蹇呴』澶т簬0锛岃閲嶆柊璁剧疆銆�", false);
+ setPathAvailability(false);
return;
}
dikuaiData.put("mowingWidth", widthObj.toString());
@@ -1088,31 +1122,141 @@
String plannedPath = Lunjingguihua.formatPathSegments(segments);
if (!isMeaningfulValue(plannedPath)) {
JOptionPane.showMessageDialog(this, "鐢熸垚鍓茶崏璺緞澶辫触: 鐢熸垚缁撴灉涓虹┖", "閿欒", JOptionPane.ERROR_MESSAGE);
- if (createButton != null) {
- createButton.setEnabled(false);
- }
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("鐢熸垚鍓茶崏璺緞澶辫触锛氱敓鎴愮粨鏋滀负绌恒��", false);
+ setPathAvailability(false);
return;
}
- dikuaiData.put("plannedPath", plannedPath);
- if (createButton != null) {
- createButton.setEnabled(true);
+ if (isMeaningfulValue(boundaryCoords)) {
+ dikuaiData.put("boundaryCoordinates", boundaryCoords);
}
- JOptionPane.showMessageDialog(this,
- "宸叉牴鎹綋鍓嶈缃敓鎴愬壊鑽夎矾寰勶紝鍏辩敓鎴� " + segments.size() + " 娈点��",
- "鎴愬姛",
- JOptionPane.INFORMATION_MESSAGE);
+ if (isMeaningfulValue(obstacleCoords)) {
+ dikuaiData.put("obstacleCoordinates", obstacleCoords);
+ }
+ dikuaiData.put("plannedPath", plannedPath);
+ setPathAvailability(true);
+ showPathGenerationMessage(
+ "宸叉牴鎹綋鍓嶈缃敓鎴愬壊鑽夎矾寰勶紝鍏辩敓鎴� " + segments.size() + " 娈点�俓n鐐瑰嚮鈥滈瑙堚�濇寜閽彲鍦ㄤ富椤甸潰鏌ョ湅鏁堟灉銆�",
+ true);
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, "鐢熸垚鍓茶崏璺緞澶辫触: " + ex.getMessage(), "閿欒", JOptionPane.ERROR_MESSAGE);
- if (createButton != null) {
- createButton.setEnabled(false);
- }
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("鐢熸垚鍓茶崏璺緞澶辫触锛�" + ex.getMessage(), false);
+ setPathAvailability(false);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "鐢熸垚鍓茶崏璺緞鏃跺彂鐢熷紓甯�: " + ex.getMessage(), "閿欒", JOptionPane.ERROR_MESSAGE);
- if (createButton != null) {
- createButton.setEnabled(false);
+ dikuaiData.remove("plannedPath");
+ showPathGenerationMessage("鐢熸垚鍓茶崏璺緞鏃跺彂鐢熷紓甯革細" + ex.getMessage(), false);
+ setPathAvailability(false);
+ }
+ }
+
+ private void previewMowingPath() {
+ if (!hasGeneratedPath()) {
+ showPathGenerationMessage("璇峰厛鐢熸垚鍓茶崏璺緞鍚庡啀棰勮銆�", false);
+ setPathAvailability(false);
+ return;
+ }
+
+ persistStep3Inputs();
+
+ String landNumber = getPendingLandNumber();
+ String trimmedAreaName = areaNameField.getText() != null ? areaNameField.getText().trim() : "";
+ String displayAreaName = isMeaningfulValue(trimmedAreaName) ? trimmedAreaName : landNumber;
+
+ String plannedPath = dikuaiData.get("plannedPath");
+ if (!isMeaningfulValue(plannedPath)) {
+ showPathGenerationMessage("璇峰厛鐢熸垚鍓茶崏璺緞鍚庡啀棰勮銆�", false);
+ setPathAvailability(false);
+ return;
+ }
+
+ String boundary = null;
+ Dikuai pending = getOrCreatePendingDikuai();
+ if (pending != null) {
+ boundary = normalizeCoordinateValue(pending.getBoundaryCoordinates());
+ }
+ if (boundary == null) {
+ boundary = normalizeCoordinateValue(dikuaiData.get("boundaryCoordinates"));
+ }
+
+ String obstacles = normalizeCoordinateValue(dikuaiData.get("obstacleCoordinates"));
+ if (!isMeaningfulValue(obstacles)) {
+ obstacles = resolveObstaclePayloadFromConfig(landNumber);
+ if (isMeaningfulValue(obstacles)) {
+ dikuaiData.put("obstacleCoordinates", obstacles);
}
}
+
+ Shouye shouye = Shouye.getInstance();
+ if (shouye == null) {
+ JOptionPane.showMessageDialog(this, "鏃犳硶鎵撳紑涓婚〉闈紝璇风◢鍚庨噸璇�", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
+ return;
+ }
+
+ dikuaiData.put("areaName", trimmedAreaName);
+ if (isMeaningfulValue(boundary)) {
+ dikuaiData.put("boundaryCoordinates", boundary);
+ }
+
+ pendingLandNumber = landNumber;
+ captureSessionSnapshot();
+
+ resumeRequested = true;
+ boolean started = shouye.startMowingPathPreview(
+ landNumber,
+ displayAreaName,
+ boundary,
+ obstacles,
+ plannedPath,
+ AddDikuai::resumeFromPreview
+ );
+ if (!started) {
+ resumeRequested = false;
+ JOptionPane.showMessageDialog(this, "鏃犳硶鍚姩棰勮锛岃绋嶅悗鍐嶈瘯", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
+ return;
+ }
+
+ closePreviewAndDispose();
+ }
+
+ private void persistStep3Inputs() {
+ String trimmedName = areaNameField.getText() != null ? areaNameField.getText().trim() : "";
+ dikuaiData.put("areaName", trimmedName);
+
+ if (mowingPatternCombo != null) {
+ Object selection = mowingPatternCombo.getSelectedItem();
+ if (selection != null) {
+ dikuaiData.put("mowingPattern", selection.toString());
+ }
+ }
+
+ if (mowingWidthSpinner != null) {
+ Object widthValue = mowingWidthSpinner.getValue();
+ if (widthValue instanceof Number) {
+ int widthInt = ((Number) widthValue).intValue();
+ dikuaiData.put("mowingWidth", Integer.toString(widthInt));
+ } else if (widthValue != null) {
+ dikuaiData.put("mowingWidth", widthValue.toString());
+ }
+ }
+ }
+
+ private void captureSessionSnapshot() {
+ if (activeSession == null) {
+ activeSession = new DrawingSession();
+ }
+ String landNumber = getPendingLandNumber();
+ activeSession.landNumber = landNumber;
+ activeSession.areaName = areaNameField.getText() != null ? areaNameField.getText().trim() : "";
+ activeSession.drawingCompleted = true;
+ activeSession.data = new HashMap<>(dikuaiData);
+ }
+
+ private void closePreviewAndDispose() {
+ setVisible(false);
+ dispose();
}
private JButton createPrimaryButton(String text, int fontSize) {
@@ -1146,6 +1290,44 @@
return button;
}
+
+ private void showPathGenerationMessage(String message, boolean success) {
+ if (pathGenerationMessageArea == null || pathMessageWrapper == null) {
+ return;
+ }
+ String display = message == null ? "" : message.trim();
+ if (display.isEmpty()) {
+ dikuaiData.remove(KEY_PATH_MESSAGE_TEXT);
+ dikuaiData.remove(KEY_PATH_MESSAGE_SUCCESS);
+ } else {
+ dikuaiData.put(KEY_PATH_MESSAGE_TEXT, display);
+ dikuaiData.put(KEY_PATH_MESSAGE_SUCCESS, success ? "true" : "false");
+ }
+ pathGenerationMessageArea.setText(display);
+ Color borderColor = success ? PRIMARY_COLOR : ERROR_COLOR;
+ Color textColor = success ? PRIMARY_DARK : ERROR_COLOR;
+ Color backgroundColor = success ? PRIMARY_LIGHT : new Color(255, 235, 238);
+ pathGenerationMessageArea.setForeground(textColor);
+ pathMessageWrapper.setBackground(backgroundColor);
+ pathMessageWrapper.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(borderColor, 1),
+ BorderFactory.createEmptyBorder(12, 12, 12, 12)
+ ));
+ pathMessageWrapper.setVisible(!display.isEmpty());
+ pathMessageWrapper.revalidate();
+ pathMessageWrapper.repaint();
+ }
+
+ private void setPathAvailability(boolean available) {
+ boolean effective = available && currentStep == 3;
+ if (createButton != null) {
+ createButton.setEnabled(effective);
+ }
+ if (previewButton != null) {
+ boolean visible = previewButton.isVisible();
+ previewButton.setEnabled(effective && visible);
+ }
+ }
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel();
@@ -1167,11 +1349,20 @@
nextButton = createPrimaryButton("涓嬩竴姝�", 16);
createButton = createPrimaryButton("淇濆瓨", 16);
createButton.setVisible(false);
- createButton.setEnabled(false);
+ createButton.setEnabled(false);
+
+ previewButton = createPrimaryButton("棰勮", 16);
+ previewButton.setVisible(false);
+ previewButton.setEnabled(false);
+
+ previewButtonSpacer = Box.createHorizontalStrut(15);
+ previewButtonSpacer.setVisible(false);
buttonPanel.add(prevButton);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(nextButton);
+ buttonPanel.add(previewButtonSpacer);
+ buttonPanel.add(previewButton);
buttonPanel.add(Box.createHorizontalStrut(15));
buttonPanel.add(createButton);
@@ -1223,14 +1414,40 @@
return true;
}
- private static String buildOriginalBoundaryString() {
- if (Coordinate.coordinates == null || Coordinate.coordinates.isEmpty()) {
+ private static List<Coordinate> sanitizeCoordinateList(List<Coordinate> source) {
+ if (source == null || source.isEmpty()) {
+ return Collections.emptyList();
+ }
+
+ List<Coordinate> snapshot = new ArrayList<>();
+ for (Coordinate coordinate : source) {
+ if (coordinate != null) {
+ snapshot.add(coordinate);
+ }
+ }
+ if (snapshot.isEmpty()) {
+ return Collections.emptyList();
+ }
+
+ DecimalFormat latLonFormat = new DecimalFormat("0.000000");
+ LinkedHashMap<String, Coordinate> unique = new LinkedHashMap<>();
+ for (Coordinate coord : snapshot) {
+ double lat = convertToDecimalDegree(coord.getLatitude(), coord.getLatDirection());
+ double lon = convertToDecimalDegree(coord.getLongitude(), coord.getLonDirection());
+ String key = latLonFormat.format(lat) + "," + latLonFormat.format(lon);
+ unique.putIfAbsent(key, coord);
+ }
+ return new ArrayList<>(unique.values());
+ }
+
+ private static String buildOriginalBoundaryString(List<Coordinate> coordinates) {
+ if (coordinates == null || coordinates.isEmpty()) {
return "-1";
}
StringBuilder sb = new StringBuilder();
DecimalFormat latLonFormat = new DecimalFormat("0.000000");
DecimalFormat elevationFormat = new DecimalFormat("0.00");
- for (Coordinate coord : Coordinate.coordinates) {
+ for (Coordinate coord : coordinates) {
double lat = convertToDecimalDegree(coord.getLatitude(), coord.getLatDirection());
double lon = convertToDecimalDegree(coord.getLongitude(), coord.getLonDirection());
double elevation = coord.getElevation();
@@ -1334,8 +1551,14 @@
}
private static BoundarySnapshotResult computeBoundarySnapshot() {
- int count = Coordinate.coordinates != null ? Coordinate.coordinates.size() : 0;
- if (count < 3) {
+ List<Coordinate> uniqueCoordinates;
+ synchronized (Coordinate.coordinates) {
+ uniqueCoordinates = sanitizeCoordinateList(Coordinate.coordinates);
+ Coordinate.coordinates.clear();
+ Coordinate.coordinates.addAll(uniqueCoordinates);
+ }
+
+ if (uniqueCoordinates.size() < 3) {
return BoundarySnapshotResult.failure("閲囬泦鐨勮竟鐣岀偣涓嶈冻锛屾棤娉曠敓鎴愬湴鍧楄竟鐣�", JOptionPane.WARNING_MESSAGE);
}
@@ -1344,9 +1567,9 @@
return BoundarySnapshotResult.failure("褰撳墠鍦板潡闈㈢Н涓�0锛屾棤娉曠户缁�", JOptionPane.WARNING_MESSAGE);
}
- Device device = new Device();
- device.initFromProperties();
- String baseStationCoordinates = normalizeCoordinateValue(device.getBaseStationCoordinates());
+ BaseStation baseStation = new BaseStation();
+ baseStation.load();
+ String baseStationCoordinates = normalizeCoordinateValue(baseStation.getInstallationCoordinates());
if (!isMeaningfulValue(baseStationCoordinates)) {
return BoundarySnapshotResult.failure("鏈幏鍙栧埌鏈夋晥鐨勫熀鍑嗙珯鍧愭爣锛岃鍏堝湪鍩哄噯绔欑鐞嗕腑璁剧疆", JOptionPane.WARNING_MESSAGE);
}
@@ -1359,7 +1582,7 @@
return BoundarySnapshotResult.failure("鐢熸垚鍦板潡杈圭晫澶辫触: " + ex.getMessage(), JOptionPane.ERROR_MESSAGE);
}
- String originalBoundary = buildOriginalBoundaryString();
+ String originalBoundary = buildOriginalBoundaryString(uniqueCoordinates);
DecimalFormat areaFormat = new DecimalFormat("0.00");
String areaString = areaFormat.format(area);
@@ -1407,6 +1630,10 @@
// 鍒涘缓鍦板潡鎸夐挳
createButton.addActionListener(e -> createDikuai());
+
+ if (previewButton != null) {
+ previewButton.addActionListener(e -> previewMowingPath());
+ }
// 鍏抽棴瀵硅瘽妗�
addWindowListener(new WindowAdapter() {
@@ -1435,11 +1662,24 @@
if (step < 3) {
nextButton.setVisible(true);
createButton.setVisible(false);
- createButton.setEnabled(false);
+ setPathAvailability(false);
+ if (previewButton != null) {
+ previewButton.setVisible(false);
+ previewButton.setEnabled(false);
+ }
+ if (previewButtonSpacer != null) {
+ previewButtonSpacer.setVisible(false);
+ }
} else {
nextButton.setVisible(false);
createButton.setVisible(true);
- createButton.setEnabled(hasGeneratedPath());
+ if (previewButton != null) {
+ previewButton.setVisible(true);
+ }
+ if (previewButtonSpacer != null) {
+ previewButtonSpacer.setVisible(true);
+ }
+ setPathAvailability(hasGeneratedPath());
}
Container parent = prevButton.getParent();
@@ -1736,6 +1976,71 @@
showStep(1);
hideBoundaryPointSummary();
}
+
+ restoreGeneratedPathState(session);
+ }
+
+ private void restoreGeneratedPathState(DrawingSession session) {
+ if (session == null || session.data == null) {
+ showPathGenerationMessage("", true);
+ return;
+ }
+
+ Map<String, String> data = session.data;
+
+ if (mowingPatternCombo != null) {
+ String pattern = data.get("mowingPattern");
+ if (pattern != null) {
+ ComboBoxModel<String> model = mowingPatternCombo.getModel();
+ for (int i = 0; i < model.getSize(); i++) {
+ String candidate = model.getElementAt(i);
+ if (pattern.equals(candidate)) {
+ mowingPatternCombo.setSelectedIndex(i);
+ break;
+ }
+ }
+ }
+ }
+
+ if (mowingWidthSpinner != null) {
+ String width = data.get("mowingWidth");
+ if (isMeaningfulValue(width)) {
+ try {
+ double parsed = Double.parseDouble(width.trim());
+ SpinnerNumberModel model = (SpinnerNumberModel) mowingWidthSpinner.getModel();
+ int min = ((Number) model.getMinimum()).intValue();
+ int max = ((Number) model.getMaximum()).intValue();
+ int rounded = (int) Math.round(parsed);
+ if (rounded < min) {
+ rounded = min;
+ } else if (rounded > max) {
+ rounded = max;
+ }
+ mowingWidthSpinner.setValue(rounded);
+ } catch (NumberFormatException ignored) {
+ // 淇濇寔褰撳墠鍊�
+ }
+ }
+ }
+
+ boolean hasPath = isMeaningfulValue(data.get("plannedPath"));
+ if (!hasPath) {
+ showPathGenerationMessage("", true);
+ if (currentStep == 3) {
+ setPathAvailability(false);
+ }
+ return;
+ }
+
+ String message = data.get(KEY_PATH_MESSAGE_TEXT);
+ boolean success = !"false".equalsIgnoreCase(data.get(KEY_PATH_MESSAGE_SUCCESS));
+ showStep(3);
+ if (isMeaningfulValue(message)) {
+ showPathGenerationMessage(message, success);
+ } else {
+ showPathGenerationMessage("宸茬敓鎴愬壊鑽夎矾寰勶紝鍙偣鍑烩�滈瑙堚�濇寜閽煡鐪嬫晥鏋溿��", true);
+ }
+ setPathAvailability(true);
}
public static void finishDrawingSession() {
@@ -1769,6 +2074,19 @@
Component parent = shouye != null ? shouye : null;
showAddDikuaiDialog(parent);
}
+
+ public static void resumeFromPreview() {
+ Shouye shouye = Shouye.getInstance();
+ if (shouye != null) {
+ shouye.exitMowingPathPreview();
+ }
+ if (activeSession == null) {
+ return;
+ }
+ resumeRequested = true;
+ Component parent = shouye != null ? shouye : null;
+ SwingUtilities.invokeLater(() -> showAddDikuaiDialog(parent));
+ }
private void createDikuai() {
if (!validateCurrentStep()) {
--
Gitblit v1.10.0