统一处理对话框的兼容性问题

This commit is contained in:
makejava 2021-02-01 17:55:51 +08:00
parent d27143956f
commit 55dfd1654c
8 changed files with 109 additions and 26 deletions

View File

@ -6,15 +6,14 @@ import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.testFramework.LightVirtualFile;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.tool.CompareFileUtils;
import com.sjhy.plugin.tool.FileUtils;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.ProjectUtils;
import lombok.Data;
import lombok.NonNull;
@ -183,7 +182,7 @@ public class SaveFile {
}
// 尝试创建目录
String msg = String.format("Directory %s Not Found, Confirm Create?", this.path);
if (this.operateTitle && !MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, msg).isYes()) {
if (this.operateTitle && !MessageDialogUtils.yesNo(project, msg)) {
return null;
}
saveDir = fileUtils.createChildDirectory(project, baseDir, savePath);
@ -210,11 +209,7 @@ public class SaveFile {
// 提示覆盖文件
if (operateTitle) {
String msg = String.format("File %s Exists, Select Operate Mode?", file.getPath());
MessageDialogBuilder.YesNoCancel yesNoCancel = MessageDialogBuilder.yesNoCancel(MsgValue.TITLE_INFO, msg);
yesNoCancel.yesText("Cover");
yesNoCancel.noText("Compare");
yesNoCancel.cancelText("Cancel");
int result = yesNoCancel.show();
int result = MessageDialogUtils.yesNoCancel(project, msg, "Conver", "Compare", "Cancel");
switch (result) {
case Messages.YES:
// 覆盖文件

View File

@ -9,7 +9,6 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ExceptionUtil;
@ -263,7 +262,7 @@ public class TableInfoServiceImpl implements TableInfoService {
}
}
// 没找到类型引导用户去添加类型
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, String.format("数据库类型%s没有找到映射关系是否去添加", typeName)).isYes()) {
if (MessageDialogUtils.yesNo(project, String.format("数据库类型%s没有找到映射关系是否去添加", typeName))) {
ShowSettingsUtil.getInstance().showSettingsDialog(project, "Type Mapper");
return false;
}

View File

@ -0,0 +1,92 @@
package com.sjhy.plugin.tool;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ExceptionUtil;
import com.sjhy.plugin.constants.MsgValue;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 消息弹框工具类
* 对message dialog弹框进行兼容处理
*
* @author makejava
* @version 1.0.0
* @since 2021/02/01 15:36
*/
public class MessageDialogUtils {
/**
* yes no确认框
*
* @param msg 消息
* @return 是否确认
*/
public static boolean yesNo(String msg) {
return yesNo(null, msg);
}
/**
* yes no确认框
*
* @param project 项目对象
* @param msg 消息
* @return 是否确认
*/
public static boolean yesNo(Project project, String msg) {
MessageDialogBuilder builder = MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, msg);
Method method;
// 新版本兼容
try {
method = builder.getClass().getMethod("ask", Project.class);
return (boolean) method.invoke(builder, project);
} 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;
}
/**
* 显示确认框
*
* @param msg 确认框消息
* @param project 项目
* @return 点击按钮
*/
public static int yesNoCancel(Project project, String msg, String yesText, String noText, String cancelText) {
MessageDialogBuilder builder = MessageDialogBuilder
.yesNoCancel(MsgValue.TITLE_INFO, msg)
.yesText(yesText)
.noText(noText)
.cancelText(cancelText);
Method method;
// 新版本兼容
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;
}
}

View File

@ -1,7 +1,6 @@
package com.sjhy.plugin.ui;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.BooleanTableCellEditor;
import com.intellij.util.ui.ComboBoxCellEditor;
@ -10,6 +9,7 @@ import com.sjhy.plugin.entity.AbstractGroup;
import com.sjhy.plugin.entity.ColumnConfig;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.StringUtils;
import javax.swing.*;
@ -204,7 +204,7 @@ public abstract class AbstractTableGroupPanel<T extends AbstractGroup<E>, E> {
if (!initFlag) {
return;
}
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, "Confirm Delete Group " + currGroupName + "?").isYes()) {
if (MessageDialogUtils.yesNo( "Confirm Delete Group " + currGroupName + "?")) {
if (Settings.DEFAULT_NAME.equals(currGroupName)) {
Messages.showWarningDialog("Can't Delete Default Group!", MsgValue.TITLE_INFO);
return;
@ -257,7 +257,7 @@ public abstract class AbstractTableGroupPanel<T extends AbstractGroup<E>, E> {
if (itemList.isEmpty()) {
return;
}
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, "Confirm Delete Selected Item?").isYes()) {
if (MessageDialogUtils.yesNo( "Confirm Delete Selected Item?")) {
int[] rows = table.getSelectedRows();
for (int i = rows.length - 1; i >= 0; i--) {
tableModel.removeRow(rows[i]);

View File

@ -14,10 +14,7 @@ import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.constants.StrState;
import com.sjhy.plugin.entity.*;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.HttpUtils;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.tool.*;
import com.sjhy.plugin.ui.base.ListCheckboxPanel;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@ -95,7 +92,7 @@ public class MainSetting implements Configurable, Configurable.Composite {
Settings settings = Settings.getInstance();
//重置配置信息
resetBtn.addActionListener(e -> {
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, MsgValue.RESET_DEFAULT_SETTING_MSG).isYes()) {
if (MessageDialogUtils.yesNo(project, MsgValue.RESET_DEFAULT_SETTING_MSG)) {
if (CollectionUtil.isEmpty(resetList)) {
return;
}
@ -283,7 +280,7 @@ public class MainSetting implements Configurable, Configurable.Composite {
String value = node.get(key).toString();
T group = objectMapper.readValue(value, cls);
if (srcGroup.containsKey(key)) {
if (!MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, String.format("是否覆盖%s配置中的%s分组", name, key)).isYes()) {
if (!MessageDialogUtils.yesNo(String.format("是否覆盖%s配置中的%s分组", name, key))) {
continue;
}
}

View File

@ -3,7 +3,6 @@ package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.entity.TypeMapper;
@ -11,6 +10,7 @@ import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.entity.TypeMapperModel;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
@ -91,7 +91,7 @@ public class TypeMapperSetting implements Configurable {
if (selectRows == null || selectRows.length == 0) {
return;
}
if (!MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, "Confirm Delete Selected Item?").isYes()) {
if (!MessageDialogUtils.yesNo("Confirm Delete Selected Item?")) {
return;
}
// 从后面往前面移除防止下标错位问题
@ -142,7 +142,7 @@ public class TypeMapperSetting implements Configurable {
//删除分组
deleteButton.addActionListener(e -> {
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, "Confirm Delete Group " + typeMapperComboBox.getSelectedItem() + "?").isYes()) {
if (MessageDialogUtils.yesNo( "Confirm Delete Group " + typeMapperComboBox.getSelectedItem() + "?")) {
if (Settings.DEFAULT_NAME.equals(currGroupName)) {
Messages.showWarningDialog("Can't Delete Default Group!", MsgValue.TITLE_INFO);
return;

View File

@ -4,12 +4,12 @@ import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.CollectionComboBoxModel;
import com.intellij.util.ui.JBUI;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
@ -201,7 +201,7 @@ public abstract class BaseGroupPanel extends JPanel {
return;
}
// 确认删除
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, String.format(MsgValue.CONFIRM_DELETE_GROUP, groupName)).isYes()) {
if (MessageDialogUtils.yesNo(String.format(MsgValue.CONFIRM_DELETE_GROUP, groupName))) {
deleteGroup(groupName);
}
}

View File

@ -3,7 +3,6 @@ package com.sjhy.plugin.ui.base;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.Splitter;
import com.intellij.ui.CollectionListModel;
@ -11,6 +10,7 @@ import com.intellij.ui.border.CustomLineBorder;
import com.intellij.ui.components.JBList;
import com.intellij.util.ui.JBUI;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
@ -228,7 +228,7 @@ public abstract class BaseItemSelectPanel<T extends Item> {
public void actionPerformed(AnActionEvent e) {
T selectedItem = getSelectedItem();
// 确认删除
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, String.format(MsgValue.CONFIRM_DELETE_MESSAGE, selectedItem.getName())).isYes()) {
if (MessageDialogUtils.yesNo(String.format(MsgValue.CONFIRM_DELETE_MESSAGE, selectedItem.getName()))) {
deleteItem(selectedItem);
}
}