Skip to content

Commit

Permalink
优化json格式化逻辑,增加json反序列化可选参数
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryZy committed Dec 20, 2024
1 parent 7acc2fc commit 7c62a2d
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cn.memoryzy.json.action.toolwindow;

import cn.hutool.core.util.StrUtil;
import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.ui.panel.JsonAssistantToolWindowPanel;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actions.ScrollToTheEndToolbarAction;
Expand All @@ -27,6 +30,12 @@ public ScrollToTheEndAction(@NotNull Editor editor, SimpleToolWindowPanel simple
presentation.setIcon(JsonAssistantIcons.ToolWindow.SCROLL_DOWN);
}


@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(
getEventProject(e) != null
&& StrUtil.isNotBlank(editor.getDocument().getText())
&& JsonAssistantToolWindowPanel.isEditorCardDisplayed(simpleToolWindowPanel));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import cn.hutool.core.util.StrUtil;
import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.constant.PluginConstant;
import cn.memoryzy.json.ui.panel.JsonAssistantToolWindowPanel;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.Toggleable;
import com.intellij.openapi.actionSystem.UpdateInBackground;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actions.AbstractToggleUseSoftWrapsAction;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.impl.softwrap.SoftWrapAppliancePlaces;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import icons.JsonAssistantIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author Memory
Expand All @@ -22,13 +27,6 @@ public class ToggleUseSoftWrapsAction extends AbstractToggleUseSoftWrapsAction i
private final EditorEx editor;
private final SimpleToolWindowPanel simpleToolWindowPanel;

/**
* Creates new {@code AbstractToggleUseSoftWrapsAction} object.
*
* @param appliancePlace defines type of the place where soft wraps are applied
* @param global indicates if soft wraps should be changed for the current editor only or for the all editors
* used at the target appliance place
*/
public ToggleUseSoftWrapsAction(EditorEx editor, SimpleToolWindowPanel simpleToolWindowPanel) {
super(SoftWrapAppliancePlaces.MAIN_EDITOR, false);
this.editor = editor;
Expand All @@ -40,11 +38,35 @@ public ToggleUseSoftWrapsAction(EditorEx editor, SimpleToolWindowPanel simpleToo
presentation.setIcon(JsonAssistantIcons.ToolWindow.SOFT_WRAP);
}

@Override
public void setSelected(@NotNull AnActionEvent e, boolean state) {
super.setSelected(e, state);
// 存储状态
saveToPropertiesComponent(state);
}

@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(
getEventProject(e) != null
&& StrUtil.isNotBlank(editor.getDocument().getText())
&& JsonAssistantToolWindowPanel.isEditorCardDisplayed(simpleToolWindowPanel));
boolean enabled = getEventProject(e) != null
&& StrUtil.isNotBlank(editor.getDocument().getText())
&& JsonAssistantToolWindowPanel.isEditorCardDisplayed(simpleToolWindowPanel);

final Presentation presentation = e.getPresentation();
if (enabled) {
boolean selected = isSelected(e);
Toggleable.setSelected(presentation, selected);
}

presentation.setEnabled(enabled);
}

@Override
protected @Nullable Editor getEditor(@NotNull AnActionEvent e) {
return editor;
}

private void saveToPropertiesComponent(boolean state) {
PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
propertiesComponent.setValue(PluginConstant.SOFT_WRAPS_SELECT_STATE, state + "");
}
}
5 changes: 5 additions & 0 deletions src/main/java/cn/memoryzy/json/constant/PluginConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public interface PluginConstant {
*/
String AUXILIARY_TREE_TOOLWINDOW_ID = "JsonAssistant.ToolWindow.AuxiliaryTree";

/**
* 选择的软换行状态 Key
*/
String SOFT_WRAPS_SELECT_STATE = JsonAssistantPlugin.PLUGIN_ID_NAME + ".SOFT_WRAPS_SELECT_STATE";

/**
* Json 编辑器卡片名称
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import cn.memoryzy.json.ui.panel.JsonAssistantToolWindowPanel;
import cn.memoryzy.json.util.UIManager;
import cn.memoryzy.json.util.*;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
Expand All @@ -35,6 +36,7 @@
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actions.AbstractToggleUseSoftWrapsAction;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.DocumentEvent;
Expand Down Expand Up @@ -196,6 +198,13 @@ private void changeEditorAppearance(EditorEx editor, boolean hasText) {
JComponent component = editor.getComponent();
component.setFont(UIManager.consolasFont(15));
component.setBorder(JBUI.Borders.customLine(editor.getBackgroundColor(), 0, 4, 0, 0));

// 切换软换行状态
PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
String value = propertiesComponent.getValue(PluginConstant.SOFT_WRAPS_SELECT_STATE);
if (null != value) {
AbstractToggleUseSoftWrapsAction.toggleSoftWraps(editor, null, Boolean.parseBoolean(value));
}
}


Expand All @@ -207,11 +216,11 @@ public JComponent createToolbar(SimpleToolWindowPanel simpleToolWindowPanel) {
actionGroup.add(new JsonStructureToolWindowAction(editor, simpleToolWindowPanel));
actionGroup.add(new JsonQueryAction(editor, simpleToolWindowPanel));
actionGroup.add(Separator.create());
actionGroup.add(new SaveToDiskAction(editor, simpleToolWindowPanel));
actionGroup.add(new ClearEditorAction(editor, simpleToolWindowPanel));
actionGroup.add(Separator.create());
actionGroup.add(new ToggleUseSoftWrapsAction(editor, simpleToolWindowPanel));
actionGroup.add(new ScrollToTheEndAction(editor, simpleToolWindowPanel));
actionGroup.add(Separator.create());
actionGroup.add(new SaveToDiskAction(editor, simpleToolWindowPanel));
actionGroup.add(new ClearEditorAction(editor, simpleToolWindowPanel));

ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.TOOLBAR, actionGroup, false);
return toolbar.getComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
import com.intellij.psi.util.PsiUtil;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.JBSplitter;
import com.intellij.ui.TitledSeparator;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.SwingHelper;
import com.intellij.util.ui.components.BorderLayoutPanel;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -84,6 +87,23 @@ public JsonToJavaBeanDialog(@Nullable Project project, PsiDirectory directory, M
JPanel firstPanel = SwingHelper.newHorizontalPanel(Component.CENTER_ALIGNMENT, label, classNameTextField);
firstPanel.setBorder(JBUI.Borders.emptyLeft(4));

// TODO 添加注解、功能选项
TitledSeparator titledSeparator = new TitledSeparator("可选参数");
JBCheckBox fastJsonCb = new JBCheckBox("FastJson 注解");
JBCheckBox jacksonCb = new JBCheckBox("Jackson 注解");
JBCheckBox toCamelCb = new JBCheckBox("下划线转驼峰");

JPanel checkBoxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 13,5));
checkBoxPanel.add(fastJsonCb);
checkBoxPanel.add(jacksonCb);
checkBoxPanel.add(toCamelCb);

BorderLayoutPanel centerPanel = new BorderLayoutPanel().addToTop(titledSeparator).addToCenter(checkBoxPanel);
centerPanel.setBorder(JBUI.Borders.empty(10, 4, 10, 0));

BorderLayoutPanel borderLayoutPanel = new BorderLayoutPanel().addToTop(firstPanel).addToCenter(centerPanel);


jsonTextField = new CustomizedLanguageTextEditor(LanguageHolder.JSON5, project, "", true);
jsonTextField.setFont(UIManager.consolasFont(15));
jsonTextField.setPlaceholder(JsonAssistantBundle.messageOnSystem("dialog.deserialize.placeholder.text") + PluginConstant.JSON_EXAMPLE);
Expand All @@ -95,7 +115,7 @@ public JsonToJavaBeanDialog(@Nullable Project project, PsiDirectory directory, M
jsonErrorDecorator = new TextEditorErrorPopupDecorator(getRootPane(), jsonTextField);

JBSplitter splitter = new JBSplitter(true, 0.06f);
splitter.setFirstComponent(firstPanel);
splitter.setFirstComponent(borderLayoutPanel);
splitter.setSecondComponent(jsonTextField);
splitter.setResizeEnabled(false);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/memoryzy/json/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public static String formatJson(Object data) {
.serializeSpecialFloatingPointValues()
.serializeNulls()
.setPrettyPrinting()
.disableHtmlEscaping()
.create()
.toJson(data);
} catch (Exception e) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/JsonAssistantIconMappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"search.svg": "icons/toolwindow/search.svg",
"structure.svg": "icons/toolwindow/structure.svg",
"delete.svg": "icons/toolwindow/delete.svg",
"magic.svg": "icons/toolwindow/magic.svg"
"magic.svg": "icons/toolwindow/magic.svg",
"scrollDown.svg": "icons/toolwindow/scrollDown.svg",
"softWrap.svg": "icons/toolwindow/softWrap.svg"
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/icons/expui/toolwindow/scrollDown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/main/resources/icons/expui/toolwindow/scrollDown_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/main/resources/icons/expui/toolwindow/softWrap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/main/resources/icons/expui/toolwindow/softWrap_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7c62a2d

Please sign in to comment.