diff --git a/src/main/java/com/liuzhihang/doc/view/action/EditorAction.java b/src/main/java/com/liuzhihang/doc/view/action/EditorAction.java index eb4cf7f..2953a5d 100644 --- a/src/main/java/com/liuzhihang/doc/view/action/EditorAction.java +++ b/src/main/java/com/liuzhihang/doc/view/action/EditorAction.java @@ -3,6 +3,7 @@ import com.intellij.codeInsight.AnnotationUtil; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; @@ -37,10 +38,10 @@ public void actionPerformed(AnActionEvent e) { PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE); Editor editor = e.getData(CommonDataKeys.EDITOR); - - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { return; } + // 获取Java类或者接口 PsiClass targetClass = CustomPsiUtils.getTargetClass(editor, psiFile); @@ -84,7 +85,7 @@ public void update(@NotNull AnActionEvent e) { Presentation presentation = e.getPresentation(); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { presentation.setEnabledAndVisible(false); return; } diff --git a/src/main/java/com/liuzhihang/doc/view/action/PreviewAction.java b/src/main/java/com/liuzhihang/doc/view/action/PreviewAction.java index 82f3b2a..0fed453 100644 --- a/src/main/java/com/liuzhihang/doc/view/action/PreviewAction.java +++ b/src/main/java/com/liuzhihang/doc/view/action/PreviewAction.java @@ -3,6 +3,7 @@ import com.intellij.codeInsight.AnnotationUtil; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; @@ -38,9 +39,11 @@ public void actionPerformed(AnActionEvent e) { Editor editor = e.getData(CommonDataKeys.EDITOR); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { return; } + + // 获取Java类或者接口 PsiClass targetClass = CustomPsiUtils.getTargetClass(editor, psiFile); @@ -89,7 +92,7 @@ public void update(@NotNull AnActionEvent e) { Presentation presentation = e.getPresentation(); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { presentation.setEnabledAndVisible(false); return; } diff --git a/src/main/java/com/liuzhihang/doc/view/action/ShowDocUploadAction.java b/src/main/java/com/liuzhihang/doc/view/action/ShowDocUploadAction.java index 0ff6235..01bc3b5 100644 --- a/src/main/java/com/liuzhihang/doc/view/action/ShowDocUploadAction.java +++ b/src/main/java/com/liuzhihang/doc/view/action/ShowDocUploadAction.java @@ -5,6 +5,7 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.options.ShowSettingsUtil; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; @@ -45,9 +46,14 @@ public void actionPerformed(@NotNull AnActionEvent e) { Editor editor = e.getData(CommonDataKeys.EDITOR); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { return; } + + if (DumbService.isDumb(project)) { + return; + } + // 获取Java类或者接口 PsiClass targetClass = CustomPsiUtils.getTargetClass(editor, psiFile); @@ -106,7 +112,7 @@ public void update(@NotNull AnActionEvent e) { Presentation presentation = e.getPresentation(); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { presentation.setEnabledAndVisible(false); return; } diff --git a/src/main/java/com/liuzhihang/doc/view/action/YApiUploadAction.java b/src/main/java/com/liuzhihang/doc/view/action/YApiUploadAction.java index 0b0f77d..9af4799 100644 --- a/src/main/java/com/liuzhihang/doc/view/action/YApiUploadAction.java +++ b/src/main/java/com/liuzhihang/doc/view/action/YApiUploadAction.java @@ -5,6 +5,7 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.options.ShowSettingsUtil; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; @@ -45,9 +46,10 @@ public void actionPerformed(@NotNull AnActionEvent e) { Editor editor = e.getData(CommonDataKeys.EDITOR); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { return; } + // 获取Java类或者接口 PsiClass targetClass = CustomPsiUtils.getTargetClass(editor, psiFile); @@ -108,11 +110,12 @@ public void update(@NotNull AnActionEvent e) { Presentation presentation = e.getPresentation(); - if (editor == null || project == null || psiFile == null) { + if (editor == null || project == null || psiFile == null || DumbService.isDumb(project)) { presentation.setEnabledAndVisible(false); return; } + PsiClass targetClass = CustomPsiUtils.getTargetClass(editor, psiFile); if (targetClass == null || targetClass.isAnnotationType() || targetClass.isEnum()) { diff --git a/src/main/java/com/liuzhihang/doc/view/config/Settings.java b/src/main/java/com/liuzhihang/doc/view/config/Settings.java index af4b659..6aec0c2 100644 --- a/src/main/java/com/liuzhihang/doc/view/config/Settings.java +++ b/src/main/java/com/liuzhihang/doc/view/config/Settings.java @@ -6,6 +6,7 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import com.intellij.util.xmlb.XmlSerializerUtil; +import com.liuzhihang.doc.view.constant.Constant; import com.liuzhihang.doc.view.constant.LombokConstant; import com.liuzhihang.doc.view.constant.SpringConstant; import com.liuzhihang.doc.view.constant.ValidationConstant; @@ -26,7 +27,7 @@ * @date 2020/2/27 19:02 */ @Data -@State(name = "DocViewSettingsComponent", storages = {@Storage("DocViewSettings.xml")}) +@State(name = "DocViewSettingsComponent", storages = {@Storage(Constant.DOC_VIEW + "/settings.xml")}) public class Settings implements PersistentStateComponent { /** diff --git a/src/main/java/com/liuzhihang/doc/view/config/ShowDocSettings.java b/src/main/java/com/liuzhihang/doc/view/config/ShowDocSettings.java index 16655c9..8da5d2a 100644 --- a/src/main/java/com/liuzhihang/doc/view/config/ShowDocSettings.java +++ b/src/main/java/com/liuzhihang/doc/view/config/ShowDocSettings.java @@ -6,6 +6,7 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import com.intellij.util.xmlb.XmlSerializerUtil; +import com.liuzhihang.doc.view.constant.Constant; import lombok.Data; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,7 +18,7 @@ * @date 2020/11/22 13:51 */ @Data -@State(name = "DocViewShowDocSettingsComment", storages = {@Storage("DocViewShowDocSettings.xml")}) +@State(name = "DocViewShowDocSettingsComment", storages = {@Storage(Constant.DOC_VIEW + "/ShowDocSettings.xml")}) public class ShowDocSettings implements PersistentStateComponent { private String url; diff --git a/src/main/java/com/liuzhihang/doc/view/config/TemplateSettings.java b/src/main/java/com/liuzhihang/doc/view/config/TemplateSettings.java index e388b13..b5bf3c9 100644 --- a/src/main/java/com/liuzhihang/doc/view/config/TemplateSettings.java +++ b/src/main/java/com/liuzhihang/doc/view/config/TemplateSettings.java @@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project; import com.intellij.util.xmlb.XmlSerializerUtil; import com.liuzhihang.doc.view.DocViewBundle; +import com.liuzhihang.doc.view.constant.Constant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -16,7 +17,7 @@ * @author liuzhihang * @date 2020/11/22 13:51 */ -@State(name = "TemplateSettingsComponent", storages = {@Storage("DocViewTemplateSettings.xml")}) +@State(name = "TemplateSettingsComponent", storages = {@Storage(Constant.DOC_VIEW + "/TemplateSettings.xml")}) public class TemplateSettings implements PersistentStateComponent { /** diff --git a/src/main/java/com/liuzhihang/doc/view/config/YApiSettings.java b/src/main/java/com/liuzhihang/doc/view/config/YApiSettings.java index dd0108f..0bc3e35 100644 --- a/src/main/java/com/liuzhihang/doc/view/config/YApiSettings.java +++ b/src/main/java/com/liuzhihang/doc/view/config/YApiSettings.java @@ -6,6 +6,7 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import com.intellij.util.xmlb.XmlSerializerUtil; +import com.liuzhihang.doc.view.constant.Constant; import lombok.Data; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,7 +18,7 @@ * @date 2020/11/22 13:51 */ @Data -@State(name = "DocViewYApiSettingsComment", storages = {@Storage("DocViewYApiSettings.xml")}) +@State(name = "DocViewYApiSettingsComment", storages = {@Storage(Constant.DOC_VIEW + "/YApiSettings.xml")}) public class YApiSettings implements PersistentStateComponent { private String url; diff --git a/src/main/java/com/liuzhihang/doc/view/constant/Constant.java b/src/main/java/com/liuzhihang/doc/view/constant/Constant.java new file mode 100644 index 0000000..f7c2a63 --- /dev/null +++ b/src/main/java/com/liuzhihang/doc/view/constant/Constant.java @@ -0,0 +1,12 @@ +package com.liuzhihang.doc.view.constant; + +/** + * @author liuzhihang + * @date 2021/8/5 18:12 + */ +public class Constant { + private Constant() { + } + + public static final String DOC_VIEW = "DocView"; +} diff --git a/src/main/java/com/liuzhihang/doc/view/dto/DocViewData.java b/src/main/java/com/liuzhihang/doc/view/dto/DocViewData.java index 6c185a7..21fe53b 100644 --- a/src/main/java/com/liuzhihang/doc/view/dto/DocViewData.java +++ b/src/main/java/com/liuzhihang/doc/view/dto/DocViewData.java @@ -80,7 +80,7 @@ public class DocViewData { /** * 返回参数 */ - private final List responseDocViewParamDataList; + private final List responseParamDataList; private final String responseParam; @@ -109,8 +109,8 @@ public DocViewData(DocView docView) { this.requestBody = paramMarkdown(requestBodyDataList); this.requestExample = buildReqExample(docView.getReqExampleType(), docView.getReqExample()); - this.responseDocViewParamDataList = buildBodyDataList(docView.getRespRootBody().getChildList()); - this.responseParam = paramMarkdown(responseDocViewParamDataList); + this.responseParamDataList = buildBodyDataList(docView.getRespRootBody().getChildList()); + this.responseParam = paramMarkdown(responseParamDataList); this.responseExample = buildRespExample(docView.getReqExampleType(), docView.getRespExample()); } diff --git a/src/main/java/com/liuzhihang/doc/view/ui/DocEditor.form b/src/main/java/com/liuzhihang/doc/view/ui/DocEditor.form index f336317..0250c93 100644 --- a/src/main/java/com/liuzhihang/doc/view/ui/DocEditor.form +++ b/src/main/java/com/liuzhihang/doc/view/ui/DocEditor.form @@ -123,14 +123,7 @@ - - - - - - - - + @@ -148,14 +141,7 @@ - - - - - - - - + diff --git a/src/main/java/com/liuzhihang/doc/view/ui/DocEditorForm.java b/src/main/java/com/liuzhihang/doc/view/ui/DocEditorForm.java index b1cf151..a209e03 100644 --- a/src/main/java/com/liuzhihang/doc/view/ui/DocEditorForm.java +++ b/src/main/java/com/liuzhihang/doc/view/ui/DocEditorForm.java @@ -22,9 +22,14 @@ import com.liuzhihang.doc.view.dto.DocViewData; import com.liuzhihang.doc.view.dto.DocViewParamData; import com.liuzhihang.doc.view.service.impl.WriterService; +import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableModel; +import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableUtils; import com.liuzhihang.doc.view.utils.CustomPsiCommentUtils; +import com.liuzhihang.doc.view.utils.DocViewUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.jdesktop.swingx.JXTreeTable; +import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -76,10 +81,10 @@ public class DocEditorForm { private JTextArea methodTextArea; private JScrollPane requestParamScrollPane; - private JTable requestParamTable; + private JXTreeTable requestTreeTable; private JScrollPane responseParamScrollPane; - private JTable responseParamTable; + private JXTreeTable responseTreeTable; private JBPopup popup; @@ -175,13 +180,20 @@ private void initRequestParamTable() { // 边框 responseParamScrollPane.setBorder(JBUI.Borders.empty()); - responseParamTable.setBorder(JBUI.Borders.empty()); - ParamTableModel paramTableModel = new ParamTableModel(docViewData.getResponseDocViewParamDataList()); - responseParamTable.setModel(paramTableModel); - ParamTableUI.rowSetting(responseParamTable); - ParamTableUI.columnSetting(responseParamTable); + DocViewParamData paramData = new DocViewParamData(); + + DefaultMutableTreeTableNode rootNode = new DefaultMutableTreeTableNode(paramData); + ParamTreeTableUtils.createTreeData(rootNode, docViewData.getResponseParamDataList()); + + ParamTreeTableModel treeTableModel = new ParamTreeTableModel(rootNode); + + requestTreeTable = new JXTreeTable(treeTableModel); + + ParamTreeTableUtils.render(requestTreeTable); + + responseParamScrollPane.setViewportView(requestTreeTable); } @@ -189,13 +201,19 @@ private void initResponseParamTable() { // 边框 requestParamScrollPane.setBorder(JBUI.Borders.empty()); - requestParamTable.setBorder(JBUI.Borders.empty()); - ParamTableModel paramTableModel = new ParamTableModel(docViewData.getRequestBodyDataList()); - requestParamTable.setModel(paramTableModel); + DocViewParamData paramData = new DocViewParamData(); - ParamTableUI.rowSetting(requestParamTable); - ParamTableUI.columnSetting(requestParamTable); + DefaultMutableTreeTableNode rootNode = new DefaultMutableTreeTableNode(paramData); + ParamTreeTableUtils.createTreeData(rootNode, docViewData.getRequestBodyDataList()); + + ParamTreeTableModel treeTableModel = new ParamTreeTableModel(rootNode); + + responseTreeTable = new JXTreeTable(treeTableModel); + + ParamTreeTableUtils.render(responseTreeTable); + + requestParamScrollPane.setViewportView(responseTreeTable); } @@ -359,15 +377,15 @@ private void initTailRightToolbar() { @Override public void actionPerformed(@NotNull AnActionEvent e) { - if (requestParamTable.isEditing()) { - requestParamTable.getCellEditor().stopCellEditing(); + if (requestTreeTable.isEditing()) { + requestTreeTable.getCellEditor().stopCellEditing(); } - if (responseParamTable.isEditing()) { - responseParamTable.getCellEditor().stopCellEditing(); + if (responseTreeTable.isEditing()) { + responseTreeTable.getCellEditor().stopCellEditing(); } generateMethodComment(); - generateComment((ParamTableModel) requestParamTable.getModel()); - generateComment((ParamTableModel) responseParamTable.getModel()); + generateComment((ParamTreeTableModel) requestTreeTable.getTreeTableModel()); + generateComment((ParamTreeTableModel) requestTreeTable.getTreeTableModel()); popup.cancel(); } }); @@ -432,16 +450,19 @@ private void generateMethodComment() { /** * 变动的字段生成注释 + * + * @param paramTreeTableModel */ - private void generateComment(ParamTableModel paramTableModel) { + private void generateComment(ParamTreeTableModel paramTreeTableModel) { - Map modifyBodyMap = paramTableModel.getModifyBodyMap(); + Map modifyBodyMap = paramTreeTableModel.getModifiedMap(); for (PsiElement element : modifyBodyMap.keySet()) { DocViewParamData data = modifyBodyMap.get(element); String comment; - if (data.getRequired()) { + // 原来有注解, 则不添加注释 + if (!DocViewUtils.isRequired((PsiField) element) && data.getRequired()) { comment = "/** " + data.getDesc() + "\n" + "* @" + Settings.getInstance(project).getRequired() diff --git a/src/main/java/com/liuzhihang/doc/view/ui/ParamDocEditorForm.java b/src/main/java/com/liuzhihang/doc/view/ui/ParamDocEditorForm.java index 4966001..f65be0c 100644 --- a/src/main/java/com/liuzhihang/doc/view/ui/ParamDocEditorForm.java +++ b/src/main/java/com/liuzhihang/doc/view/ui/ParamDocEditorForm.java @@ -10,11 +10,12 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopup; import com.intellij.openapi.ui.popup.JBPopupFactory; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiFile; +import com.intellij.psi.*; +import com.intellij.psi.javadoc.PsiDocComment; import com.intellij.ui.WindowMoveListener; import com.intellij.util.ui.JBUI; import com.liuzhihang.doc.view.DocViewBundle; +import com.liuzhihang.doc.view.config.Settings; import com.liuzhihang.doc.view.config.SettingsConfigurable; import com.liuzhihang.doc.view.dto.Body; import com.liuzhihang.doc.view.dto.DocViewData; @@ -22,7 +23,8 @@ import com.liuzhihang.doc.view.notification.DocViewNotification; import com.liuzhihang.doc.view.service.impl.WriterService; import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableModel; -import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableUI; +import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableUtils; +import com.liuzhihang.doc.view.utils.DocViewUtils; import com.liuzhihang.doc.view.utils.GsonFormatUtil; import com.liuzhihang.doc.view.utils.ParamPsiUtils; import org.jdesktop.swingx.JXTreeTable; @@ -36,7 +38,6 @@ import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.event.MouseEvent; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -52,7 +53,7 @@ public class ParamDocEditorForm { private final Editor editor; private final PsiClass psiClass; - // private final ParamTableModel paramTableModel; + private ParamTreeTableModel treeTableModel; private JPanel rootPanel; private JPanel headToolbarPanel; @@ -62,8 +63,6 @@ public class ParamDocEditorForm { private JBPopup popup; - private ArrayList defaultMutableTreeTableNodeList; - @NonNls public static final String DOC_VIEW_POPUP = "com.intellij.docview.param.editor.popup"; private static final AtomicBoolean myIsPinned = new AtomicBoolean(true); @@ -133,6 +132,8 @@ public void popup() { private void initUI() { // 边框 + paramScrollPane.setBackground(null); + paramScrollPane.setOpaque(false); paramScrollPane.setBorder(JBUI.Borders.empty()); } @@ -235,28 +236,29 @@ public void actionPerformed(@NotNull AnActionEvent e) { private void generateComment() { - // Map modifyBodyMap = paramTableModel.getModifyBodyMap(); - // - // for (PsiElement element : modifyBodyMap.keySet()) { - // DocViewParamData data = modifyBodyMap.get(element); - // String comment; - // - // if (data.getRequired()) { - // comment = "/** " - // + data.getDesc() + "\n" - // + "* @" + Settings.getInstance(project).getRequired() - // + " */"; - // } else { - // comment = "/** " - // + data.getDesc() - // + " */"; - // } - // - // - // PsiElementFactory factory = PsiElementFactory.getInstance(project); - // PsiDocComment psiDocComment = factory.createDocCommentFromText(comment); - // writerService.write(project, element, psiDocComment); - // } + Map modifyBodyMap = treeTableModel.getModifiedMap(); + + for (PsiElement element : modifyBodyMap.keySet()) { + DocViewParamData data = modifyBodyMap.get(element); + String comment; + + // 不修改原有注解 + if (!DocViewUtils.isRequired((PsiField) element) && data.getRequired()) { + comment = "/** " + + data.getDesc() + "\n" + + "* @" + Settings.getInstance(project).getRequired() + + " */"; + } else { + comment = "/** " + + data.getDesc() + + " */"; + } + + + PsiElementFactory factory = PsiElementFactory.getInstance(project); + PsiDocComment psiDocComment = factory.createDocCommentFromText(comment); + writerService.write(project, element, psiDocComment); + } } private void initTailLeftToolbar() { @@ -274,31 +276,20 @@ private void initParamTable() { DocViewParamData paramData = new DocViewParamData(); - - defaultMutableTreeTableNodeList = new ArrayList<>(); - DefaultMutableTreeTableNode rootNode = new DefaultMutableTreeTableNode(paramData); - createTreeData(rootNode, dataList); + ParamTreeTableUtils.createTreeData(rootNode, dataList); + + treeTableModel = new ParamTreeTableModel(rootNode); - JXTreeTable treeTable = new JXTreeTable(new ParamTreeTableModel(rootNode)); + JXTreeTable treeTable = new JXTreeTable(treeTableModel); - ParamTreeTableUI.render(treeTable); + ParamTreeTableUtils.render(treeTable); paramScrollPane.setViewportView(treeTable); } - private void createTreeData(DefaultMutableTreeTableNode rootNode, @NotNull List dataList) { - for (DocViewParamData data : dataList) { - DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(data); - rootNode.add(node); - defaultMutableTreeTableNodeList.add(node); - if (data.getChildList() != null && data.getChildList().size() > 0) { - createTreeData(node, data.getChildList()); - } - } - } } diff --git a/src/main/java/com/liuzhihang/doc/view/ui/PreviewForm.java b/src/main/java/com/liuzhihang/doc/view/ui/PreviewForm.java index a6d6ef7..9596556 100644 --- a/src/main/java/com/liuzhihang/doc/view/ui/PreviewForm.java +++ b/src/main/java/com/liuzhihang/doc/view/ui/PreviewForm.java @@ -1,8 +1,5 @@ package com.liuzhihang.doc.view.ui; -import com.intellij.execution.ExecutionException; -import com.intellij.execution.configurations.GeneralCommandLine; -import com.intellij.execution.util.ExecUtil; import com.intellij.find.editorHeaderActions.Utils; import com.intellij.icons.AllIcons; import com.intellij.ide.highlighter.HighlighterFactory; @@ -62,8 +59,10 @@ import java.awt.datatransfer.StringSelection; import java.awt.event.MouseEvent; import java.time.LocalTime; +import java.util.HashMap; import java.util.List; -import java.util.*; +import java.util.Map; +import java.util.Vector; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -419,65 +418,8 @@ public void actionPerformed(@NotNull AnActionEvent e) { @Override public void actionPerformed(@NotNull AnActionEvent e) { - Point location = previewToolbarPanel.getLocationOnScreen(); - location.x = MouseInfo.getPointerInfo().getLocation().x; - location.y += previewToolbarPanel.getHeight(); - - myIsPinned.set(true); - - JBPopupFactory.getInstance() - .createListPopup(new BaseListPopupStep<>(null, "Markdown", "Docx", "PDF", "HTML") { - - @Override - public @NotNull String getTextFor(String value) { - return "Export to " + value; - } - - @Override - public @Nullable PopupStep onChosen(String selectedValue, boolean finalChoice) { - - if (selectedValue.equals("Markdown")) { - - ExportUtils.exportMarkdown(project, currentDocView.getName(), currentMarkdownText); - - } else if (selectedValue.equals("Docx")) { - - - // val commandLine = mutableListOf( - // "pandoc", - // srcFile.path, - // "-f", - // MarkdownFileType.INSTANCE.name.toLowerCase(), - // "-t", - // formatDescription.extension, - // "-o", - // targetFile - // ) - // if (FileUtil.exists(refFile)) { - // commandLine.add("--reference-doc=$refFile") - // } - - - List command = new ArrayList<>(); - command.add(""); - - GeneralCommandLine commandLine = new GeneralCommandLine(); - - try { - ExecUtil.execAndGetOutput(commandLine); - } catch (ExecutionException ex) { - ex.printStackTrace(); - } - - - } else if (selectedValue.equals("PDF")) { - - } else if (selectedValue.equals("HTML")) { - - } - return FINAL_CHOICE; - } - }).showInScreenCoordinates(previewToolbarPanel, location); + popup.cancel(); + ExportUtils.exportMarkdown(project, currentDocView.getName(), currentMarkdownText); } }); @@ -547,7 +489,7 @@ private void initMenuToolbarPanelToolbar() { public void actionPerformed(@NotNull AnActionEvent e) { popup.cancel(); - ExportUtils.allExportMarkdown(project, currentDocView.getPsiClass().getName(), docViewList); + ExportUtils.batchExportMarkdown(project, currentDocView.getPsiClass().getName(), docViewList); } }); menuGroup.addSeparator(); diff --git a/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableModel.java b/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableModel.java index 8af1d65..a8a7ffd 100644 --- a/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableModel.java +++ b/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableModel.java @@ -1,11 +1,15 @@ package com.liuzhihang.doc.view.ui.treetable; +import com.intellij.psi.PsiElement; import com.liuzhihang.doc.view.dto.DocViewParamData; import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode; import org.jdesktop.swingx.treetable.DefaultTreeTableModel; import org.jdesktop.swingx.treetable.TreeTableNode; +import java.util.HashMap; +import java.util.Map; + /** * 字段树列表 * @@ -16,6 +20,11 @@ public class ParamTreeTableModel extends DefaultTreeTableModel { public static final String[] names = {"参数名", "类型", "必选", "描述"}; + /** + * 修改集合 + */ + private Map modifiedMap = new HashMap<>(); + public ParamTreeTableModel(TreeTableNode node) { super(node); @@ -82,12 +91,14 @@ public void setValueAt(Object value, Object node, int column) { Object userObject = ((DefaultMutableTreeTableNode) node).getUserObject(); if (userObject instanceof DocViewParamData) { // "参数名", "类型", "必选", "描述" - DocViewParamData docViewParamData = (DocViewParamData) userObject; + DocViewParamData data = (DocViewParamData) userObject; if (column == 2) { - docViewParamData.setRequired(String.valueOf(value).equalsIgnoreCase("true")); + data.setRequired(String.valueOf(value).equalsIgnoreCase("true")); + modifiedMap.put(data.getPsiElement(), data); } else if (column == 3) { - docViewParamData.setDesc(String.valueOf(value)); + data.setDesc(String.valueOf(value)); + modifiedMap.put(data.getPsiElement(), data); } } @@ -110,4 +121,8 @@ public boolean isCellEditable(Object node, int column) { } + public Map getModifiedMap() { + + return modifiedMap; + } } diff --git a/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableUI.java b/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableUtils.java similarity index 58% rename from src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableUI.java rename to src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableUtils.java index dea7bf1..b844148 100644 --- a/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableUI.java +++ b/src/main/java/com/liuzhihang/doc/view/ui/treetable/ParamTreeTableUtils.java @@ -3,12 +3,17 @@ import com.intellij.ui.Gray; import com.intellij.ui.JBColor; import com.intellij.util.ui.UIUtil; +import com.liuzhihang.doc.view.dto.DocViewParamData; import org.jdesktop.swingx.JXTreeTable; +import org.jdesktop.swingx.decorator.ColorHighlighter; +import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode; +import org.jetbrains.annotations.NotNull; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableColumn; import java.awt.*; +import java.util.List; import static javax.swing.ListSelectionModel.SINGLE_SELECTION; @@ -16,13 +21,13 @@ * @author liuzhihang * @date 2021/3/30 10:16 */ -public class ParamTreeTableUI { +public class ParamTreeTableUtils { public static final DefaultTableCellRenderer RENDERER = new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - + setOpaque(true); /** * 隔行变色 */ @@ -51,12 +56,6 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole */ public static void render(JXTreeTable treeTable) { - treeTable.getColumnModel().getColumn(0).setPreferredWidth(150); - treeTable.setRowHeight(30); - treeTable.setLeafIcon(null); - treeTable.setOpenIcon(null); - treeTable.setClosedIcon(null); - final DefaultListSelectionModel defaultListSelectionModel = new DefaultListSelectionModel(); treeTable.setSelectionModel(defaultListSelectionModel); @@ -65,17 +64,40 @@ public static void render(JXTreeTable treeTable) { for (int i = 0; i < treeTable.getColumnCount(); i++) { - treeTable.getColumn(i).setCellRenderer(RENDERER); + + TableColumn column = treeTable.getColumn(i); + + if (column.getIdentifier().equals("必选")) { + // 设置 表格列 的 单元格编辑器 + column.setCellEditor(new DefaultCellEditor(new JComboBox<>(new Boolean[]{true, false}))); + } + } + treeTable.setRowHeight(30); + treeTable.setLeafIcon(null); + treeTable.setOpenIcon(null); + treeTable.setClosedIcon(null); - for (int i = 0; i < treeTable.getColumnCount(); i++) { + if (UIUtil.isUnderDarcula()) { + treeTable.addHighlighter(new ColorHighlighter((renderer, adapter) -> adapter.row % 2 == 1, Gray._45, null)); + } else { + treeTable.addHighlighter(new ColorHighlighter((renderer, adapter) -> adapter.row % 2 == 1, Gray._245, null)); + } - // 根据 列名 获取 表格列 - TableColumn tableColumn = treeTable.getColumn("必选"); - // 设置 表格列 的 单元格编辑器 - tableColumn.setCellEditor(new DefaultCellEditor(new JComboBox<>(new Boolean[]{true, false}))); + treeTable.setSelectionForeground(JBColor.WHITE); + } + + + public static void createTreeData(DefaultMutableTreeTableNode rootNode, @NotNull List dataList) { + for (DocViewParamData data : dataList) { + DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(data); + rootNode.add(node); + + if (data.getChildList() != null && data.getChildList().size() > 0) { + createTreeData(node, data.getChildList()); + } } } diff --git a/src/main/java/com/liuzhihang/doc/view/utils/ExportUtils.java b/src/main/java/com/liuzhihang/doc/view/utils/ExportUtils.java index 89f247e..a836425 100644 --- a/src/main/java/com/liuzhihang/doc/view/utils/ExportUtils.java +++ b/src/main/java/com/liuzhihang/doc/view/utils/ExportUtils.java @@ -7,7 +7,6 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import com.liuzhihang.doc.view.DocViewBundle; -import com.liuzhihang.doc.view.config.TemplateSettings; import com.liuzhihang.doc.view.dto.DocView; import com.liuzhihang.doc.view.dto.DocViewData; import com.liuzhihang.doc.view.notification.DocViewNotification; @@ -15,7 +14,6 @@ import java.io.File; import java.io.IOException; import java.util.List; -import java.util.Map; /** * @author liuzhihang @@ -23,50 +21,6 @@ */ public class ExportUtils { - /** - * 批量导出接口文档 - * - * @param project 当前项目 - * @param generatedDocView 批量生成的接口文档 - */ - public static void bathExportMarkdown(Project project, Map> generatedDocView) { - // 选择路径 - FileChooserDescriptor fileChooserDescriptor = - new FileChooserDescriptor(false, true, false, false, false, false); - VirtualFile chooser = FileChooser.chooseFile(fileChooserDescriptor, project, null); - if (chooser != null) { - if (chooser.getChildren().length > 0) { - DocViewNotification.notifyError(project, DocViewBundle.message("notify.export.batch.file.use.empty")); - return; - } - // 文件夹路径 - StringBuilder path = new StringBuilder(chooser.getPath()); - // 逐个写入磁盘 - int success = 0; - int fail = 0; - for (String folder : generatedDocView.keySet()) { - Map docViewMap = generatedDocView.get(folder); - if (new File(path + File.separator + folder).mkdir()) { - path.append(File.separator).append(folder); - } - for (Map.Entry docViewEntry : docViewMap.entrySet()) { - // 将 docView 按照模版转换 - DocViewData docViewData = new DocViewData(docViewEntry.getValue()); - String markdownText = VelocityUtils.convert(TemplateSettings.getInstance(project).getSpringTemplate(), docViewData); - File file = new File(path + "/" + docViewEntry.getKey() + ".md"); - try { - FileUtil.writeToFile(file, markdownText); - success++; - } catch (IOException ioException) { - fail++; - } - } - } - DocViewNotification.notifyInfo(project, DocViewBundle.message("notify.export.batch.count", success, fail)); - } - } - - public static void exportMarkdown(Project project, String fileName, String markdownText) { // 选择路径 @@ -86,17 +40,17 @@ public static void exportMarkdown(Project project, String fileName, String markd } try { FileUtil.writeToFile(file, markdownText); + DocViewNotification.notifyInfo(project, DocViewBundle.message("notify.export.success")); } catch (IOException ioException) { DocViewNotification.notifyError(project, DocViewBundle.message("notify.export.fail")); } } - DocViewNotification.notifyInfo(project, DocViewBundle.message("notify.export.success")); } - public static void allExportMarkdown(Project project, String className, List docViewList) { + public static void batchExportMarkdown(Project project, String className, List docViewList) { // 选择路径 FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); @@ -114,17 +68,18 @@ public static void allExportMarkdown(Project project, String className, List