From 48ee74129bb09a817a0bbbabe860c4007b74c66b Mon Sep 17 00:00:00 2001
From: 826220679@qq.com <826220679@qq.com>
Date: 星期日, 21 十二月 2025 12:37:44 +0800
Subject: [PATCH] 新增了往返路径

---
 src/baseStation/BaseStationDialog.java |  189 +++++++++++++++++++++++++++-------------------
 1 files changed, 110 insertions(+), 79 deletions(-)

diff --git a/src/baseStation/BaseStationDialog.java b/src/baseStation/BaseStationDialog.java
index 5ea81ea..a878c06 100644
--- a/src/baseStation/BaseStationDialog.java
+++ b/src/baseStation/BaseStationDialog.java
@@ -3,6 +3,7 @@
 import javax.swing.*;
 
 import gecaoji.Device;
+import publicway.buttonset;
 
 import java.awt.*;
 import java.awt.event.ActionListener;
@@ -13,6 +14,7 @@
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
 import java.util.Properties;
 
 public class BaseStationDialog extends JDialog {
@@ -180,26 +182,24 @@
         baseStation.load();
 
         String currentRaw = baseStation.getInstallationCoordinates();
-        String normalizedCurrent = currentRaw == null ? "-1" : currentRaw.trim();
-        if (normalizedCurrent.isEmpty()) {
-            normalizedCurrent = "-1";
-        }
+        String normalizedCurrent = canonicalizeCoordinateValue(currentRaw);
+        boolean hasExistingCoordinate = normalizedCurrent != null && !"-1".equals(normalizedCurrent);
 
-        JTextField inputField = new JTextField("-1".equals(normalizedCurrent) ? "" : normalizedCurrent);
+        JTextField inputField = new JTextField(hasExistingCoordinate ? normalizedCurrent : "");
         inputField.setColumns(28);
 
         JPanel dialogPanel = new JPanel(new BorderLayout(0, 8));
-        JLabel hintLabel = new JLabel("璇疯緭鍏ユ柊鐨勫熀鍑嗙珯鍧愭爣锛堟牸寮忕ず渚嬶細2324.194945,N,11330.938547,E锛�");
+        JLabel hintLabel = new JLabel("璇疯緭鍏ユ柊鐨勫熀鍑嗙珯鍧愭爣锛堢ず渚嬶細3949.90238860,N,11616.75692000,E锛�");
         hintLabel.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 12));
         dialogPanel.add(hintLabel, BorderLayout.NORTH);
         dialogPanel.add(inputField, BorderLayout.CENTER);
 
-    JOptionPane optionPane = new JOptionPane(dialogPanel,
-        JOptionPane.PLAIN_MESSAGE,
-        JOptionPane.OK_CANCEL_OPTION);
-    JDialog dialog = optionPane.createDialog(this, "淇敼鍩哄噯绔欎綅缃�");
-    dialog.setModal(true);
-    dialog.pack();
+        JOptionPane optionPane = new JOptionPane(dialogPanel,
+            JOptionPane.PLAIN_MESSAGE,
+            JOptionPane.OK_CANCEL_OPTION);
+        JDialog dialog = optionPane.createDialog(this, "淇敼鍩哄噯绔欎綅缃�");
+        dialog.setModal(true);
+        dialog.pack();
 
         Dimension packedSize = dialog.getSize();
         Window ownerWindow = getOwner();
@@ -208,12 +208,11 @@
             referenceWidth = 400;
         }
         dialog.setSize(new Dimension(referenceWidth, packedSize.height));
-    dialog.setResizable(false);
-    dialog.setLocationRelativeTo(this);
-    dialog.setVisible(true);
-
-    Object selectedValue = optionPane.getValue();
-    if (!(selectedValue instanceof Integer) || ((Integer) selectedValue) != JOptionPane.OK_OPTION) {
+        dialog.setResizable(false);
+        dialog.setLocationRelativeTo(this);
+        dialog.setVisible(true);
+        Object selectedValue = optionPane.getValue();
+        if (!(selectedValue instanceof Integer) || ((Integer) selectedValue) != JOptionPane.OK_OPTION) {
             return;
         }
 
@@ -234,7 +233,7 @@
             }
         }
 
-        String valueToSave = trimmed.isEmpty() ? "-1" : trimmed;
+        String valueToSave = trimmed.isEmpty() ? "-1" : canonicalizeCoordinateValue(trimmed);
 
         if (!"-1".equals(valueToSave) && !looksLikeCoordinateFormat(valueToSave)) {
             int confirm = JOptionPane.showConfirmDialog(this,
@@ -247,7 +246,7 @@
             }
         }
 
-        if (valueToSave.equals(normalizedCurrent)) {
+        if (normalizedCurrent.equals(valueToSave)) {
             JOptionPane.showMessageDialog(this,
                     "鍩哄噯绔欎綅缃湭鍙戠敓鍙樺寲銆�",
                     "鎻愮ず",
@@ -298,7 +297,7 @@
         titleLabel.setFont(new Font("寰蒋闆呴粦", Font.BOLD, 14));
         titleLabel.setForeground(Color.DARK_GRAY);
 
-        JButton actionButton = createInlineButton(buttonText, buttonColor);
+        JButton actionButton = buttonset.createStyledButton(buttonText, buttonColor);
         actionButton.addActionListener(actionListener);
 
         panel.add(titleLabel);
@@ -307,30 +306,6 @@
 
         return panel;
     }
-
-    private JButton createInlineButton(String text, Color bgColor) {
-        JButton button = new JButton(text);
-        button.setFont(new Font("寰蒋闆呴粦", Font.BOLD, 14));
-        button.setBackground(bgColor);
-        button.setForeground(Color.WHITE);
-        button.setFocusPainted(false);
-        button.setBorder(BorderFactory.createEmptyBorder(8, 18, 8, 18));
-        Dimension size = new Dimension(90, 36);
-        button.setPreferredSize(size);
-        button.setMinimumSize(size);
-        button.setMaximumSize(size);
-
-        button.addMouseListener(new java.awt.event.MouseAdapter() {
-            public void mouseEntered(java.awt.event.MouseEvent evt) {
-                button.setBackground(brightenColor(bgColor));
-            }
-            public void mouseExited(java.awt.event.MouseEvent evt) {
-                button.setBackground(bgColor);
-            }
-        });
-
-        return button;
-    }
     
     private Color brightenColor(Color color) {
         int r = Math.min(255, color.getRed() + 30);
@@ -425,26 +400,11 @@
             return "璁惧娌℃湁瀹夎鍥哄畾";
         }
         String coordinates = baseStation.getInstallationCoordinates();
-        if (coordinates == null) {
+        String canonical = canonicalizeCoordinateValue(coordinates);
+        if (canonical == null || "-1".equals(canonical)) {
             return "璁惧娌℃湁瀹夎鍥哄畾";
         }
-        String trimmed = coordinates.trim();
-        if (trimmed.isEmpty() || "-1".equals(trimmed)) {
-            return "璁惧娌℃湁瀹夎鍥哄畾";
-        }
-        try {
-            String[] parts = trimmed.split(",");
-            if (parts.length == 4) {
-                String lat = formatCoordinate(parts[0].trim(), true);
-                String latDir = parts[1].trim();
-                String lon = formatCoordinate(parts[2].trim(), false);
-                String lonDir = parts[3].trim();
-                return String.format("%s掳%s, %s掳%s", lat, latDir, lon, lonDir);
-            }
-        } catch (Exception e) {
-            // ignore formatting errors and fall back to raw value
-        }
-        return trimmed;
+        return canonical;
     }
 
     private String getSimCardDisplay() {
@@ -497,22 +457,93 @@
         return !trimmed.isEmpty() && !"-1".equals(trimmed);
     }
     
-    private String formatCoordinate(String coordinate, boolean isLatitude) {
-        // 鏍煎紡鍖栧潗鏍囷細2324.194945 -> 23掳24.1949'
-        try {
-            // 搴︽暟鏄墠2浣嶏紙绾害锛夋垨3浣嶏紙缁忓害锛�
-            int degreeDigits = isLatitude ? 2 : 3;
-            String degreeStr = coordinate.substring(0, degreeDigits);
-            String minuteStr = coordinate.substring(degreeDigits);
-            
-            // 淇濈暀4浣嶅皬鏁�
-            double minutes = Double.parseDouble(minuteStr);
-            String formattedMinutes = String.format("%.4f", minutes);
-            
-            return degreeStr + "掳" + formattedMinutes + "'";
-        } catch (Exception e) {
-            return coordinate;
+    // Normalizes coordinate strings into degree-minute format when possible for consistent display/storage
+    private String canonicalizeCoordinateValue(String value) {
+        if (value == null) {
+            return "-1";
         }
+        String trimmed = value.trim();
+        if (trimmed.isEmpty() || "-1".equals(trimmed)) {
+            return "-1";
+        }
+        String[] parts = trimmed.split(",");
+        if (parts.length != 4) {
+            return trimmed;
+        }
+        String latDirRaw = parts[1] == null ? "" : parts[1].trim();
+        String lonDirRaw = parts[3] == null ? "" : parts[3].trim();
+        if (latDirRaw.isEmpty() || lonDirRaw.isEmpty()) {
+            return trimmed;
+        }
+        String latDir = latDirRaw.toUpperCase(Locale.ROOT);
+        String lonDir = lonDirRaw.toUpperCase(Locale.ROOT);
+        if (!isValidDirection(latDir, true) || !isValidDirection(lonDir, false)) {
+            return trimmed;
+        }
+        String formattedLat = convertToDegreeMinuteString(parts[0], latDir, true);
+        String formattedLon = convertToDegreeMinuteString(parts[2], lonDir, false);
+        if (formattedLat == null || formattedLon == null) {
+            return trimmed;
+        }
+        return formattedLat + "," + latDir + "," + formattedLon + "," + lonDir;
+    }
+
+    private boolean isValidDirection(String direction, boolean isLatitude) {
+        if (direction == null) {
+            return false;
+        }
+        if (isLatitude) {
+            return "N".equals(direction) || "S".equals(direction);
+        }
+        return "E".equals(direction) || "W".equals(direction);
+    }
+
+    // Converts decimal degrees or degree-minute input into a canonical degree-minute string (8 decimal places)
+    private String convertToDegreeMinuteString(String rawValue, String direction, boolean isLatitude) {
+        double decimal = parseCoordinateToDecimalDegrees(rawValue, direction, isLatitude);
+        if (!Double.isFinite(decimal)) {
+            return null;
+        }
+        double absDecimal = Math.abs(decimal);
+        int degrees = (int) Math.floor(absDecimal);
+        double minutes = (absDecimal - degrees) * 60.0d;
+        double degreeMinutes = degrees * 100.0d + minutes;
+        return String.format(Locale.US, "%.8f", degreeMinutes);
+    }
+
+    private double parseCoordinateToDecimalDegrees(String rawValue, String direction, boolean isLatitude) {
+        if (rawValue == null) {
+            return Double.NaN;
+        }
+        String trimmed = rawValue.trim();
+        if (trimmed.isEmpty()) {
+            return Double.NaN;
+        }
+        double numeric;
+        try {
+            numeric = Double.parseDouble(trimmed);
+        } catch (NumberFormatException ex) {
+            return Double.NaN;
+        }
+
+        double abs = Math.abs(numeric);
+        double boundary = isLatitude ? 90d : 180d;
+        double decimal;
+        if (abs <= boundary) {
+            decimal = abs;
+        } else {
+            double degrees = Math.floor(abs / 100d);
+            double minutes = abs - degrees * 100d;
+            decimal = degrees + minutes / 60d;
+        }
+
+        String dirUpper = direction == null ? "" : direction.trim().toUpperCase(Locale.ROOT);
+        if ("S".equals(dirUpper) || "W".equals(dirUpper)) {
+            decimal = -decimal;
+        } else if (!"N".equals(dirUpper) && !"E".equals(dirUpper) && numeric < 0d) {
+            decimal = -decimal;
+        }
+        return decimal;
     }
     
     private void lockBaseStationPosition() {
@@ -542,7 +573,7 @@
                 JOptionPane.INFORMATION_MESSAGE);
             
             // 鏇存柊鍩哄噯绔欎綅缃紙杩欓噷浣跨敤妯℃嫙鏁版嵁锛屽疄闄呭簲浠嶨PS鑾峰彇锛�
-            String newPosition = "2324.194945,N,11330.938547,E";
+            String newPosition = canonicalizeCoordinateValue("2324.194945,N,11330.938547,E");
             String timestamp = String.valueOf(System.currentTimeMillis());
 
             if (!hasBaseStationId()) {

--
Gitblit v1.10.0