张世豪
2025-12-09 32524195d474b74e48916867b2a6c2f022a40d98
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
package zhuye;
 
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
 
/**
 * 自检提示工具类,集中管理割草机作业前的自检提示逻辑。
 */
public final class zijian {
    private static boolean selfCheckCompleted;
    private static boolean initialPromptShown;
 
    private zijian() {
        // Utility class
    }
 
    public static boolean ensureBeforeMowing(Component anchorComponent, Runnable onSelfCheckConfirmed) {
        if (selfCheckCompleted) {
            return true;
        }
        showSelfCheckDialog(anchorComponent, onSelfCheckConfirmed);
        return false;
    }
 
    public static void showInitialPromptIfNeeded(Component anchorComponent, Runnable onSelfCheckConfirmed) {
        if (selfCheckCompleted || initialPromptShown) {
            return;
        }
        initialPromptShown = true;
        showSelfCheckDialog(anchorComponent, onSelfCheckConfirmed);
    }
 
    public static void markSelfCheckCompleted() {
        selfCheckCompleted = true;
    }
 
    private static void showSelfCheckDialog(Component anchorComponent, Runnable onSelfCheckConfirmed) {
    Component parent = resolveDialogParent(anchorComponent);
    Object[] options = {"立即自检", "取消"};
    String message = "<html>割草前请先完成对割草机的自检操作。<br>遥控割草机向前开约2米,然后原地转圈完成自检功能。</html>";
 
    JOptionPane optionPane = new JOptionPane(
        message,
        JOptionPane.INFORMATION_MESSAGE,
        JOptionPane.DEFAULT_OPTION,
        null,
        options,
        options[0]);
 
    JDialog dialog = optionPane.createDialog(parent, "自检提示");
    dialog.pack();
    applyTargetWidth(anchorComponent, dialog);
    dialog.setLocationRelativeTo(parent instanceof Component ? (Component) parent : null);
    dialog.setVisible(true);
 
        Object selectedValue = optionPane.getValue();
        dialog.dispose();
 
        boolean confirmed = options[0].equals(selectedValue);
        if (confirmed) {
            selfCheckCompleted = true;
            if (onSelfCheckConfirmed != null) {
                SwingUtilities.invokeLater(onSelfCheckConfirmed);
            }
        }
    }
 
    private static void applyTargetWidth(Component anchorComponent, JDialog dialog) {
        if (dialog == null) {
            return;
        }
        int targetWidth = resolveTargetDialogWidth(anchorComponent);
        if (targetWidth <= 0) {
            return;
        }
        Dimension currentSize = dialog.getSize();
        if (currentSize == null) {
            currentSize = dialog.getPreferredSize();
        }
        if (currentSize == null) {
            currentSize = new Dimension(targetWidth, 0);
        }
        int width = Math.max(targetWidth, currentSize.width);
        int height = currentSize.height > 0 ? currentSize.height : dialog.getHeight();
        if (height <= 0) {
            height = 200;
        }
        dialog.setSize(new Dimension(width, height));
    }
 
    private static int resolveTargetDialogWidth(Component anchorComponent) {
        int baseWidth = 0;
        if (anchorComponent != null) {
            baseWidth = anchorComponent.getWidth();
            if (baseWidth <= 0) {
                Component parent = resolveDialogParent(anchorComponent);
                if (parent != null) {
                    baseWidth = parent.getWidth();
                }
            }
        }
        if (baseWidth <= 0) {
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            baseWidth = screenSize != null ? screenSize.width : 0;
        }
        if (baseWidth <= 0) {
            return 0;
        }
        return (int) Math.round(baseWidth * 0.8);
    }
 
    private static Component resolveDialogParent(Component anchorComponent) {
        if (anchorComponent == null) {
            return null;
        }
        if (anchorComponent instanceof Window) {
            return anchorComponent;
        }
        Window window = SwingUtilities.getWindowAncestor(anchorComponent);
        return window != null ? window : anchorComponent;
    }
}