张世豪
6 天以前 9d171a3c3a57ea54454d7e9d64dec213aa885a2c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package zhuye;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Dialog;
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);
        Window owner = parent instanceof Window
                ? (Window) parent
                : SwingUtilities.getWindowAncestor(parent);
 
        final JDialog dialog = owner != null
                ? new JDialog(owner, "自检提示", Dialog.ModalityType.APPLICATION_MODAL)
                : new JDialog((Window) null, "自检提示", Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setResizable(false);
 
        String message = "<html>操作割草前请先完成对割草机的自检操作。<br>遥控割草机向前开约2米,然后原地转圈完成自检功能。</html>";
 
        JPanel contentPanel = new JPanel(new BorderLayout(0, 20));
        contentPanel.setBorder(BorderFactory.createEmptyBorder(24, 24, 24, 24));
        contentPanel.setBackground(Color.WHITE);
 
        JLabel messageLabel = new JLabel(message);
        messageLabel.setForeground(new Color(60, 60, 60));
        contentPanel.add(messageLabel, BorderLayout.CENTER);
 
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 18, 0));
        buttonPanel.setOpaque(false);
 
        JButton confirmButton = buttonset.createStyledButton("立即自检", new Color(46, 139, 87));
        JButton cancelButton = buttonset.createStyledButton("取消", new Color(128, 128, 128));
 
        final boolean[] confirmedHolder = {false};
 
        confirmButton.addActionListener(e -> {
            confirmedHolder[0] = true;
            dialog.dispose();
        });
        cancelButton.addActionListener(e -> {
            confirmedHolder[0] = false;
            dialog.dispose();
        });
 
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
 
        contentPanel.add(buttonPanel, BorderLayout.SOUTH);
 
        dialog.setContentPane(contentPanel);
        dialog.pack();
        applyTargetWidth(anchorComponent, dialog);
        dialog.setLocationRelativeTo(owner);
        dialog.setVisible(true);
        dialog.dispose();
 
        boolean confirmed = confirmedHolder[0];
        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;
    }
}