diff --git a/src/main/java/com/liuzhihang/toolkit/ui/Base64Form.java b/src/main/java/com/liuzhihang/toolkit/ui/Base64Form.java index 05982c9..a1b65a0 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/Base64Form.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/Base64Form.java @@ -2,27 +2,22 @@ import com.intellij.find.editorHeaderActions.Utils; import com.intellij.icons.AllIcons; -import com.intellij.ide.highlighter.HighlighterFactory; -import com.intellij.lang.Language; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.editor.EditorSettings; -import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.ex.EditorEx; -import com.intellij.openapi.editor.highlighter.EditorHighlighter; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopup; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; -import com.intellij.ui.JBColor; import com.intellij.ui.components.JBScrollPane; import com.intellij.util.ui.JBUI; import com.liuzhihang.toolkit.ToolkitBundle; +import com.liuzhihang.toolkit.utils.EditorExUtils; import com.liuzhihang.toolkit.utils.NotificationUtils; import org.jetbrains.annotations.NotNull; @@ -79,32 +74,14 @@ private void initUI() { private void initTextEditor() { - base64InputPanel.add(getJbScrollPane(inputDocument, false), BorderLayout.CENTER); + EditorEx inputEditorEx = EditorExUtils.createEditorEx(project, inputDocument, fileType, false); + base64InputPanel.add(new JBScrollPane(inputEditorEx.getComponent()), BorderLayout.CENTER); - base64OutputPanel.add(getJbScrollPane(outputDocument, true), BorderLayout.CENTER); + EditorEx outputEditorEx = EditorExUtils.createEditorEx(project, outputDocument, fileType, true); + base64OutputPanel.add(new JBScrollPane(outputEditorEx.getComponent()), BorderLayout.CENTER); } - private JBScrollPane getJbScrollPane(Document document, boolean onlyView) { - EditorHighlighter editorHighlighter = HighlighterFactory - .createHighlighter(fileType, EditorColorsManager.getInstance().getGlobalScheme(), project); - - EditorEx editorEx = (EditorEx) EditorFactory.getInstance().createEditor(document, project, fileType, onlyView); - EditorSettings editorSettings = editorEx.getSettings(); - editorSettings.setAdditionalLinesCount(0); - editorSettings.setAdditionalColumnsCount(0); - editorSettings.setLineMarkerAreaShown(false); - editorSettings.setLineNumbersShown(false); - editorSettings.setVirtualSpace(false); - editorSettings.setFoldingOutlineShown(false); - editorSettings.setTabSize(4); - editorSettings.setLanguageSupplier(() -> Language.findLanguageByID("Text")); - - editorEx.setHighlighter(editorHighlighter); - editorEx.setBorder(JBUI.Borders.emptyLeft(5)); - return new JBScrollPane(editorEx.getComponent()); - } - private void initTailLeftToolbar() { } diff --git a/src/main/java/com/liuzhihang/toolkit/ui/EntityJsonForm.java b/src/main/java/com/liuzhihang/toolkit/ui/EntityJsonForm.java index 1ee3d23..7d07da6 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/EntityJsonForm.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/EntityJsonForm.java @@ -4,25 +4,22 @@ import com.google.gson.GsonBuilder; import com.intellij.find.editorHeaderActions.Utils; import com.intellij.icons.AllIcons; -import com.intellij.ide.highlighter.HighlighterFactory; -import com.intellij.lang.Language; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.editor.EditorSettings; -import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.ex.EditorEx; -import com.intellij.openapi.editor.highlighter.EditorHighlighter; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopup; -import com.intellij.psi.*; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiFile; import com.intellij.ui.components.JBScrollPane; import com.intellij.util.ui.JBUI; import com.liuzhihang.toolkit.ToolkitBundle; +import com.liuzhihang.toolkit.utils.EditorExUtils; import com.liuzhihang.toolkit.utils.FieldsPsiUtils; import com.liuzhihang.toolkit.utils.GsonFormatUtil; import com.liuzhihang.toolkit.utils.NotificationUtils; @@ -79,28 +76,9 @@ private void initUI() { private void initJsonEditor() { - EditorHighlighter editorHighlighter = HighlighterFactory - .createHighlighter(fileType, EditorColorsManager.getInstance().getGlobalScheme(), project); - - EditorEx jsonEditor = (EditorEx) EditorFactory.getInstance().createEditor(jsonDocument, project, fileType, true); - - EditorSettings editorSettings = jsonEditor.getSettings(); - editorSettings.setAdditionalLinesCount(0); - editorSettings.setAdditionalColumnsCount(0); - editorSettings.setLineMarkerAreaShown(false); - editorSettings.setLineNumbersShown(false); - editorSettings.setVirtualSpace(false); - editorSettings.setFoldingOutlineShown(false); - editorSettings.setTabSize(4); - - editorSettings.setLanguageSupplier(() -> Language.findLanguageByID("Json")); - - jsonEditor.setHighlighter(editorHighlighter); - jsonEditor.setBorder(JBUI.Borders.emptyLeft(5)); - JBScrollPane templateScrollPane = new JBScrollPane(jsonEditor.getComponent()); - + EditorEx editorEx = EditorExUtils.createEditorEx(project, jsonDocument, fileType, true); + JBScrollPane templateScrollPane = new JBScrollPane(editorEx.getComponent()); jsonPane.add(templateScrollPane, BorderLayout.CENTER); - } private void initTailLeftToolbar() { diff --git a/src/main/java/com/liuzhihang/toolkit/ui/JsonFormatForm.java b/src/main/java/com/liuzhihang/toolkit/ui/JsonFormatForm.java index 6c87902..ab5061c 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/JsonFormatForm.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/JsonFormatForm.java @@ -3,17 +3,12 @@ import com.google.gson.*; import com.intellij.find.editorHeaderActions.Utils; import com.intellij.icons.AllIcons; -import com.intellij.ide.highlighter.HighlighterFactory; -import com.intellij.lang.Language; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.editor.EditorSettings; -import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.ex.EditorEx; -import com.intellij.openapi.editor.highlighter.EditorHighlighter; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.project.Project; @@ -23,6 +18,7 @@ import com.intellij.ui.components.JBScrollPane; import com.intellij.util.ui.JBUI; import com.liuzhihang.toolkit.ToolkitBundle; +import com.liuzhihang.toolkit.utils.EditorExUtils; import com.liuzhihang.toolkit.utils.GsonFormatUtil; import com.liuzhihang.toolkit.utils.NotificationUtils; import org.jetbrains.annotations.NotNull; @@ -88,24 +84,9 @@ private void initUI() { private void initJsonEditor() { - EditorHighlighter editorHighlighter = HighlighterFactory - .createHighlighter(fileType, EditorColorsManager.getInstance().getGlobalScheme(), project); + EditorEx editorEx = EditorExUtils.createEditorEx(project, jsonDocument, fileType, false); - EditorEx jsonEditor = (EditorEx) EditorFactory.getInstance().createEditor(jsonDocument, project, fileType, false); - - EditorSettings editorSettings = jsonEditor.getSettings(); - editorSettings.setAdditionalLinesCount(0); - editorSettings.setAdditionalColumnsCount(0); - editorSettings.setLineMarkerAreaShown(false); - editorSettings.setLineNumbersShown(false); - editorSettings.setVirtualSpace(false); - editorSettings.setFoldingOutlineShown(false); - editorSettings.setTabSize(4); - editorSettings.setLanguageSupplier(() -> Language.findLanguageByID("Json")); - - jsonEditor.setHighlighter(editorHighlighter); - jsonEditor.setBorder(JBUI.Borders.emptyLeft(5)); - JBScrollPane templateScrollPane = new JBScrollPane(jsonEditor.getComponent()); + JBScrollPane templateScrollPane = new JBScrollPane(editorEx.getComponent()); jsonFormatPane.add(templateScrollPane, BorderLayout.CENTER); @@ -132,9 +113,6 @@ public void actionPerformed(@NotNull AnActionEvent e) { rightGroup.add(new AnAction("Compress", "Compress one line", AllIcons.Actions.Collapseall) { @Override public void actionPerformed(@NotNull AnActionEvent e) { - - System.out.println("getComponentPopupMenu" + rootJPanel.getComponentPopupMenu()); - compressAction(); } }); diff --git a/src/main/java/com/liuzhihang/toolkit/ui/Toolkit.form b/src/main/java/com/liuzhihang/toolkit/ui/Toolkit.form index 451d336..9f360c3 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/Toolkit.form +++ b/src/main/java/com/liuzhihang/toolkit/ui/Toolkit.form @@ -19,40 +19,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java b/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java index 96e39c0..17f74be 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java @@ -36,17 +36,14 @@ public class ToolkitForm { private final PsiFile psiFile; private final PsiClass psiClass; private final PsiMethod psiMethod; + private JBPopup popup; private JPanel rootPanel; private JTabbedPane toolkitTabbedPane; private JPanel headToolbarPanel; - private JPanel jsonFormatTabPanel; - private JLabel fileReference; - private JPanel entityJsonTabPanel; - private JPanel base64TabPanel; - private JPanel urlTabPanel; + protected ToolkitForm(@NotNull Project project, PsiFile psiFile, PsiClass psiClass, PsiMethod psiMethod) { @@ -55,6 +52,8 @@ protected ToolkitForm(@NotNull Project project, PsiFile psiFile, this.psiMethod = psiMethod; this.psiFile = psiFile; + initPopup(); + initUI(); initTitle(psiFile, psiClass); @@ -64,28 +63,13 @@ protected ToolkitForm(@NotNull Project project, PsiFile psiFile, addMouseListeners(); tabPanelListener(); - } - - - @NotNull - public static ToolkitForm getInstance(@NotNull Project project, Editor editor, PsiFile psiFile) { - PsiClass psiClass = null; - PsiMethod targetMethod = null; - if (editor != null) { - // 获取Java类或者接口 - psiClass = CustomPsiUtils.getTargetClass(editor, psiFile); - - targetMethod = CustomPsiUtils.getTargetMethod(editor, psiFile); - } - - return new ToolkitForm(project, psiFile, psiClass, targetMethod); + initTabs(popup); } - public void popup() { - + private void initPopup() { // dialog 改成 popup, 第一个为根¸面板,第二个为焦点面板 - JBPopup popup = JBPopupFactory.getInstance().createComponentPopupBuilder(rootPanel, toolkitTabbedPane) + popup = JBPopupFactory.getInstance().createComponentPopupBuilder(rootPanel, toolkitTabbedPane) .setProject(project) .setResizable(true) .setMovable(true) @@ -104,9 +88,26 @@ public void popup() { .setCancelOnOtherWindowOpen(false) .setCancelOnWindowDeactivation(false) .createPopup(); + } + + @NotNull + public static ToolkitForm getInstance(@NotNull Project project, Editor editor, PsiFile psiFile) { + PsiClass psiClass = null; + PsiMethod targetMethod = null; + + if (editor != null) { + // 获取Java类或者接口 + psiClass = CustomPsiUtils.getTargetClass(editor, psiFile); + + targetMethod = CustomPsiUtils.getTargetMethod(editor, psiFile); + } + + return new ToolkitForm(project, psiFile, psiClass, targetMethod); + } + + public void popup() { popup.showCenteredInCurrentWindow(project); - initTabs(popup); } private void addMouseListeners() { @@ -180,21 +181,16 @@ private void initTitle(PsiFile psiFile, PsiClass psiClass) { */ private void initTabs(JBPopup popup) { // json tab - JPanel rootJPanel = JsonFormatForm.getInstance(project, psiFile, psiClass, popup).getRootJPanel(); - - System.out.println("rootJPanel = " + rootJPanel.getHeight()); - - jsonFormatTabPanel.add(rootJPanel); - - System.out.println("rootPanel = " + rootPanel.getHeight()); - + toolkitTabbedPane.addTab("Json Format", + JsonFormatForm.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); if (psiClass != null) { - entityJsonTabPanel.add(EntityJsonForm.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); - } else { - toolkitTabbedPane.removeTabAt(1); + toolkitTabbedPane.addTab("Entity Json", + EntityJsonForm.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); } - base64TabPanel.add(Base64Form.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); - urlTabPanel.add(UrlForm.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); + toolkitTabbedPane.addTab("Base64", + Base64Form.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); + toolkitTabbedPane.addTab("Url Encode/Decode", + UrlForm.getInstance(project, psiFile, psiClass, popup).getRootJPanel()); } private void tabPanelListener() { diff --git a/src/main/java/com/liuzhihang/toolkit/ui/UrlForm.java b/src/main/java/com/liuzhihang/toolkit/ui/UrlForm.java index aae6a22..e302823 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/UrlForm.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/UrlForm.java @@ -2,32 +2,24 @@ import com.intellij.find.editorHeaderActions.Utils; import com.intellij.icons.AllIcons; -import com.intellij.ide.highlighter.HighlighterFactory; -import com.intellij.lang.Language; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.editor.EditorSettings; -import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.ex.EditorEx; -import com.intellij.openapi.editor.highlighter.EditorHighlighter; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.popup.JBPopup; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; -import com.intellij.ui.JBColor; import com.intellij.ui.components.JBScrollPane; import com.intellij.util.ui.JBUI; import com.liuzhihang.toolkit.ToolkitBundle; +import com.liuzhihang.toolkit.utils.EditorExUtils; import com.liuzhihang.toolkit.utils.NotificationUtils; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; @@ -36,7 +28,6 @@ import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.Base64; /** * @author liuzhihang @@ -85,31 +76,15 @@ private void initUI() { private void initTextEditor() { - urlInputPanel.add(getJbScrollPane(inputDocument, false), BorderLayout.CENTER); + EditorEx inputEditorEx = EditorExUtils.createEditorEx(project, inputDocument, fileType, false); + urlInputPanel.add(new JBScrollPane(inputEditorEx.getComponent()), BorderLayout.CENTER); - urlOutputPanel.add(getJbScrollPane(outputDocument, true), BorderLayout.CENTER); + EditorEx outputEditorEx = EditorExUtils.createEditorEx(project, outputDocument, fileType, true); + urlOutputPanel.add(new JBScrollPane(outputEditorEx.getComponent()), BorderLayout.CENTER); } - private JBScrollPane getJbScrollPane(Document document, boolean onlyView) { - EditorHighlighter editorHighlighter = HighlighterFactory - .createHighlighter(fileType, EditorColorsManager.getInstance().getGlobalScheme(), project); - - EditorEx editorEx = (EditorEx) EditorFactory.getInstance().createEditor(document, project, fileType, onlyView); - EditorSettings editorSettings = editorEx.getSettings(); - editorSettings.setAdditionalLinesCount(0); - editorSettings.setAdditionalColumnsCount(0); - editorSettings.setLineMarkerAreaShown(false); - editorSettings.setLineNumbersShown(false); - editorSettings.setVirtualSpace(false); - editorSettings.setFoldingOutlineShown(false); - editorSettings.setTabSize(4); - editorSettings.setLanguageSupplier(() -> Language.findLanguageByID("Text")); - - editorEx.setHighlighter(editorHighlighter); - editorEx.setBorder(JBUI.Borders.emptyLeft(5)); - return new JBScrollPane(editorEx.getComponent()); - } + private void initTailLeftToolbar() { diff --git a/src/main/java/com/liuzhihang/toolkit/utils/EditorExUtils.java b/src/main/java/com/liuzhihang/toolkit/utils/EditorExUtils.java new file mode 100644 index 0000000..34593d9 --- /dev/null +++ b/src/main/java/com/liuzhihang/toolkit/utils/EditorExUtils.java @@ -0,0 +1,44 @@ +package com.liuzhihang.toolkit.utils; + +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.EditorFactory; +import com.intellij.openapi.editor.EditorSettings; +import com.intellij.openapi.editor.colors.EditorColors; +import com.intellij.openapi.editor.colors.EditorColorsManager; +import com.intellij.openapi.editor.colors.EditorColorsScheme; +import com.intellij.openapi.editor.ex.EditorEx; +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.openapi.project.Project; +import com.intellij.util.ui.JBUI; +import org.jetbrains.annotations.NotNull; + +/** + * @author liuzhihang + * @date 2021/4/17 13:33 + */ +public class EditorExUtils { + + private static final EditorColorsScheme EDITOR_COLORS_SCHEME = EditorColorsManager.getInstance().getGlobalScheme(); + + public static EditorEx createEditorEx(@NotNull Project project, @NotNull Document document, + @NotNull FileType fileType, @NotNull Boolean isViewer) { + + EditorEx editorEx = (EditorEx) EditorFactory.getInstance().createEditor(document, project, fileType, isViewer); + EditorSettings editorSettings = editorEx.getSettings(); + editorSettings.setAdditionalLinesCount(0); + editorSettings.setAdditionalColumnsCount(0); + editorSettings.setLineMarkerAreaShown(false); + editorSettings.setLineNumbersShown(false); + editorSettings.setVirtualSpace(false); + editorSettings.setFoldingOutlineShown(false); + editorSettings.setTabSize(4); + + // editorEx.setHighlighter(editorHighlighter); + editorEx.setBorder(JBUI.Borders.emptyLeft(5)); + + EditorColorsScheme colorsScheme = editorEx.getColorsScheme(); + colorsScheme.setColor(EditorColors.CARET_ROW_COLOR, null); + + return editorEx; + } +}