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 publicway.buttonset;
|
|
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;
|
}
|
}
|