修复mac版本对话框兼容BUG

This commit is contained in:
makejava 2021-07-15 17:03:04 +08:00
parent c9f686c072
commit c526aec25e
1 changed files with 14 additions and 45 deletions

View File

@ -1,13 +1,10 @@
package com.sjhy.plugin.tool; package com.sjhy.plugin.tool;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder; import com.intellij.util.ui.UIUtil;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ExceptionUtil;
import com.sjhy.plugin.constants.MsgValue; import com.sjhy.plugin.constants.MsgValue;
import java.lang.reflect.InvocationTargetException; import javax.swing.*;
import java.lang.reflect.Method;
/** /**
* 消息弹框工具类 * 消息弹框工具类
@ -37,24 +34,12 @@ public class MessageDialogUtils {
* @return 是否确认 * @return 是否确认
*/ */
public static boolean yesNo(Project project, String msg) { public static boolean yesNo(Project project, String msg) {
MessageDialogBuilder builder = MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, msg); Object[] options = new Object[]{"Yes", "No"};
Method method; return JOptionPane.showOptionDialog(null,
// 新版本兼容 msg, MsgValue.TITLE_INFO,
try { JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
method = builder.getClass().getMethod("ask", Project.class); UIUtil.getQuestionIcon(),
return (boolean) method.invoke(builder, project); options, options[0]) == 0;
} catch (NoSuchMethodException e) {
// 旧版本兼容
try {
method = builder.getClass().getMethod("isYes");
return (boolean) method.invoke(builder);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e1) {
ExceptionUtil.rethrow(e1);
}
} catch (IllegalAccessException | InvocationTargetException e) {
ExceptionUtil.rethrow(e);
}
return false;
} }
/** /**
@ -65,28 +50,12 @@ public class MessageDialogUtils {
* @return 点击按钮 * @return 点击按钮
*/ */
public static int yesNoCancel(Project project, String msg, String yesText, String noText, String cancelText) { public static int yesNoCancel(Project project, String msg, String yesText, String noText, String cancelText) {
MessageDialogBuilder builder = MessageDialogBuilder Object[] options = new Object[]{"Yes", "No", "Cancel"};
.yesNoCancel(MsgValue.TITLE_INFO, msg) return JOptionPane.showOptionDialog(null,
.yesText(yesText) msg, MsgValue.TITLE_INFO,
.noText(noText) JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
.cancelText(cancelText); UIUtil.getQuestionIcon(),
Method method; options, options[0]);
// 新版本兼容
try {
method = builder.getClass().getMethod("show", Project.class);
return (int) method.invoke(builder, project);
} catch (NoSuchMethodException e) {
// 旧版本兼容
try {
method = builder.getClass().getMethod("show");
return (int) method.invoke(builder);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e1) {
ExceptionUtil.rethrow(e1);
}
} catch (IllegalAccessException | InvocationTargetException e) {
ExceptionUtil.rethrow(e);
}
return Messages.NO;
} }
} }