Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Apr 17, 2021
1 parent 07e9f28 commit a963163
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 182 deletions.
33 changes: 5 additions & 28 deletions src/main/java/com/liuzhihang/toolkit/ui/Base64Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {

}
Expand Down
32 changes: 5 additions & 27 deletions src/main/java/com/liuzhihang/toolkit/ui/EntityJsonForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
28 changes: 3 additions & 25 deletions src/main/java/com/liuzhihang/toolkit/ui/JsonFormatForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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();
}
});
Expand Down
35 changes: 1 addition & 34 deletions src/main/java/com/liuzhihang/toolkit/ui/Toolkit.form
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,7 @@
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="bce88" binding="jsonFormatTabPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<tabbedpane title="Json Format"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="7325a" binding="entityJsonTabPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<tabbedpane title="Entity Json"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="66a07" binding="base64TabPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<tabbedpane title="Base64"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="d5783" binding="urlTabPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<tabbedpane title="URL Decoder/Encoder"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
<children/>
</tabbedpane>
<grid id="dad49" binding="headToolbarPanel" layout-manager="BorderLayout" hgap="3" vgap="0">
<constraints>
Expand Down
70 changes: 33 additions & 37 deletions src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -55,6 +52,8 @@ protected ToolkitForm(@NotNull Project project, PsiFile psiFile,
this.psiMethod = psiMethod;
this.psiFile = psiFile;

initPopup();

initUI();

initTitle(psiFile, psiClass);
Expand All @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit a963163

Please sign in to comment.