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;
|
}
|
}
|