From 55dfd1654cee5df31e9c0594944d1d20dd52d0a3 Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Mon, 1 Feb 2021 17:55:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=A1=86=E7=9A=84=E5=85=BC=E5=AE=B9=E6=80=A7=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sjhy/plugin/entity/SaveFile.java | 11 +-- .../service/impl/TableInfoServiceImpl.java | 3 +- .../sjhy/plugin/tool/MessageDialogUtils.java | 92 +++++++++++++++++++ .../plugin/ui/AbstractTableGroupPanel.java | 6 +- .../java/com/sjhy/plugin/ui/MainSetting.java | 9 +- .../com/sjhy/plugin/ui/TypeMapperSetting.java | 6 +- .../sjhy/plugin/ui/base/BaseGroupPanel.java | 4 +- .../plugin/ui/base/BaseItemSelectPanel.java | 4 +- 8 files changed, 109 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/sjhy/plugin/tool/MessageDialogUtils.java diff --git a/src/main/java/com/sjhy/plugin/entity/SaveFile.java b/src/main/java/com/sjhy/plugin/entity/SaveFile.java index 1b4ab96..d1c3e66 100644 --- a/src/main/java/com/sjhy/plugin/entity/SaveFile.java +++ b/src/main/java/com/sjhy/plugin/entity/SaveFile.java @@ -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: // 覆盖文件 diff --git a/src/main/java/com/sjhy/plugin/service/impl/TableInfoServiceImpl.java b/src/main/java/com/sjhy/plugin/service/impl/TableInfoServiceImpl.java index 42a0e6b..7d20360 100644 --- a/src/main/java/com/sjhy/plugin/service/impl/TableInfoServiceImpl.java +++ b/src/main/java/com/sjhy/plugin/service/impl/TableInfoServiceImpl.java @@ -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; } diff --git a/src/main/java/com/sjhy/plugin/tool/MessageDialogUtils.java b/src/main/java/com/sjhy/plugin/tool/MessageDialogUtils.java new file mode 100644 index 0000000..2bebbaf --- /dev/null +++ b/src/main/java/com/sjhy/plugin/tool/MessageDialogUtils.java @@ -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; + } + +} diff --git a/src/main/java/com/sjhy/plugin/ui/AbstractTableGroupPanel.java b/src/main/java/com/sjhy/plugin/ui/AbstractTableGroupPanel.java index df79428..7f4b112 100644 --- a/src/main/java/com/sjhy/plugin/ui/AbstractTableGroupPanel.java +++ b/src/main/java/com/sjhy/plugin/ui/AbstractTableGroupPanel.java @@ -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, 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, 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]); diff --git a/src/main/java/com/sjhy/plugin/ui/MainSetting.java b/src/main/java/com/sjhy/plugin/ui/MainSetting.java index 5a02743..aaf9712 100644 --- a/src/main/java/com/sjhy/plugin/ui/MainSetting.java +++ b/src/main/java/com/sjhy/plugin/ui/MainSetting.java @@ -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; } } diff --git a/src/main/java/com/sjhy/plugin/ui/TypeMapperSetting.java b/src/main/java/com/sjhy/plugin/ui/TypeMapperSetting.java index 6b1e21d..bd83554 100644 --- a/src/main/java/com/sjhy/plugin/ui/TypeMapperSetting.java +++ b/src/main/java/com/sjhy/plugin/ui/TypeMapperSetting.java @@ -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; diff --git a/src/main/java/com/sjhy/plugin/ui/base/BaseGroupPanel.java b/src/main/java/com/sjhy/plugin/ui/base/BaseGroupPanel.java index c9026b9..be4c9f0 100644 --- a/src/main/java/com/sjhy/plugin/ui/base/BaseGroupPanel.java +++ b/src/main/java/com/sjhy/plugin/ui/base/BaseGroupPanel.java @@ -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); } } diff --git a/src/main/java/com/sjhy/plugin/ui/base/BaseItemSelectPanel.java b/src/main/java/com/sjhy/plugin/ui/base/BaseItemSelectPanel.java index f52b1e8..6319664 100644 --- a/src/main/java/com/sjhy/plugin/ui/base/BaseItemSelectPanel.java +++ b/src/main/java/com/sjhy/plugin/ui/base/BaseItemSelectPanel.java @@ -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 { 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); } }