Skip to content

Commit

Permalink
完善查询界面
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryZy committed Dec 28, 2024
1 parent a666828 commit 2ee0044
Show file tree
Hide file tree
Showing 27 changed files with 590 additions and 34 deletions.
25 changes: 19 additions & 6 deletions src/main/java/cn/memoryzy/json/action/JsonStructureAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import cn.memoryzy.json.service.persistent.JsonAssistantPersistentState;
import cn.memoryzy.json.service.persistent.state.GeneralState;
import cn.memoryzy.json.toolwindow.AuxiliaryTreeToolWindowManager;
import cn.memoryzy.json.ui.JsonQueryComponentProvider;
import cn.memoryzy.json.ui.dialog.JsonStructureDialog;
import cn.memoryzy.json.ui.panel.JsonAssistantToolWindowPanel;
import cn.memoryzy.json.util.Json5Util;
import cn.memoryzy.json.util.JsonUtil;
import cn.memoryzy.json.util.PlatformUtil;
import cn.memoryzy.json.util.ToolWindowUtil;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -50,6 +52,10 @@ public JsonStructureAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
DataContext dataContext = event.getDataContext();
Editor editor = PlatformUtil.getEditor(dataContext);

// 如果是标记的编辑器,那么就用弹窗
boolean editorFlag = Boolean.TRUE.equals(editor.getUserData(JsonQueryComponentProvider.EDITOR_FLAG));
ToolWindow toolWindow = PlatformDataKeys.TOOL_WINDOW.getData(dataContext);
StructureActionSource source =
Objects.nonNull(toolWindow) && PluginConstant.JSON_ASSISTANT_TOOLWINDOW_ID.equals(toolWindow.getId())
Expand All @@ -58,21 +64,28 @@ public void actionPerformed(@NotNull AnActionEvent event) {

GlobalTextConversionProcessorContext context = new GlobalTextConversionProcessorContext();
String json = GlobalJsonConverter.parseJson(context, PlatformUtil.getEditor(dataContext));
show(event.getDataContext(), json, GlobalJsonConverter.isValidJson(context.getProcessor()), source);
show(event.getDataContext(), json, GlobalJsonConverter.isValidJson(context.getProcessor()), source, editorFlag);
}


public static void show(DataContext dataContext, String text, boolean isJson, StructureActionSource source) {
public static void show(DataContext dataContext, String text, boolean isJson, StructureActionSource source, boolean editorFlag) {
Project project = dataContext.getData(CommonDataKeys.PROJECT);
JsonWrapper jsonWrapper = isJson ? JsonUtil.parse(JsonUtil.ensureJson(text)) : Json5Util.parse(text);
JsonAssistantPersistentState persistentState = JsonAssistantPersistentState.getInstance();
GeneralState generalState = persistentState.generalState;

if (generalState.treeDisplayMode == TreeDisplayMode.POPUP) {
TreeDisplayMode treeDisplayMode;
if (editorFlag) {
treeDisplayMode = TreeDisplayMode.POPUP;
} else {
JsonAssistantPersistentState persistentState = JsonAssistantPersistentState.getInstance();
GeneralState generalState = persistentState.generalState;
treeDisplayMode = generalState.treeDisplayMode;
}

if (treeDisplayMode == TreeDisplayMode.POPUP) {
// 弹窗展示
new JsonStructureDialog(jsonWrapper).show();

} else if (generalState.treeDisplayMode == TreeDisplayMode.ORIGINAL_TOOLWINDOW) {
} else if (treeDisplayMode == TreeDisplayMode.ORIGINAL_TOOLWINDOW) {
// 在旧窗口展示
showInOriginalToolWindow(project, jsonWrapper, source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@
import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.service.persistent.JsonAssistantPersistentState;
import cn.memoryzy.json.service.persistent.state.DeserializerState;
import cn.memoryzy.json.ui.component.FastJson2OptionsCheckBox;
import cn.memoryzy.json.ui.component.FastJsonOptionsCheckBox;
import cn.memoryzy.json.ui.component.JacksonOptionsCheckBox;
import cn.memoryzy.json.ui.component.KeepCamelOptionsCheckBox;
import cn.memoryzy.json.util.JavaUtil;
import cn.memoryzy.json.util.UIManager;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.ui.CheckBoxList;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBList;
import com.intellij.ui.popup.KeepingPopupOpenAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -48,6 +59,28 @@ public void actionPerformed(@NotNull AnActionEvent e) {
DataContext dataContext = e.getDataContext();

// IdeEventQueue.invokeLater(() -> {

// JBPopupFactory.getInstance().createComponentPopupBuilder()
boolean hasFastJsonLib = JavaUtil.hasFastJsonLib(module);
boolean hasFastJson2Lib = JavaUtil.hasFastJson2Lib(module);
boolean hasJacksonLib = JavaUtil.hasJacksonLib(module);

DefaultListModel<JCheckBox> listModel = createListModel(hasFastJsonLib, hasFastJson2Lib, hasJacksonLib);
CheckBoxList<JBCheckBox> checkBoxList = new CheckBoxList<>(listModel);

JBPopup popup1 = JBPopupFactory.getInstance()
.createComponentPopupBuilder(checkBoxList, null)
.setFocusable(true)
.setShowShadow(true)
.setShowBorder(true)
.setLocateByContent(true)
.setNormalWindowLevel(true)
// .addListener(new SaveOptionsListener(listModel))
.createPopup();

popup1.showInBestPositionFor(e.getDataContext());


ListPopup popup = JBPopupFactory.getInstance()
.createActionGroupPopup(null,
this, dataContext, JBPopupFactory.ActionSelectionAid.MNEMONICS, true);
Expand All @@ -61,6 +94,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
// }
// });


Runnable disposeCallback = () -> {
System.out.println("callback...");
};
Expand All @@ -74,15 +108,58 @@ public void actionPerformed(@NotNull AnActionEvent e) {

JBList myList = (JBList) ReflectUtil.getFieldValue(popup, "myList");

// myList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

ListSelectionListener[] listSelectionListeners = myList.getListSelectionListeners();

popup.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Object source = e.getSource();
UIManager.repaintComponent((JComponent) source);

System.out.println("sync list " + source);
}
});

popup.showInBestPositionFor(dataContext);
// });


}

private DefaultListModel<JCheckBox> createListModel(boolean hasFastJsonLib, boolean hasFastJson2Lib, boolean hasJacksonLib) {
JBCheckBox fastJsonCheckBox = new FastJsonOptionsCheckBox(module, deserializerState);
JBCheckBox fastJson2CheckBox = new FastJson2OptionsCheckBox(module, deserializerState);
JBCheckBox jacksonCheckBox = new JacksonOptionsCheckBox(module, deserializerState);
JBCheckBox keepCamelCheckBox = new KeepCamelOptionsCheckBox(deserializerState);
DefaultListModel<JCheckBox> listModel = new DefaultListModel<>();

// 存在某种依赖,就添加某个选项
if (hasFastJsonLib && hasFastJson2Lib) {
listModel.addElement(fastJsonCheckBox);
listModel.addElement(fastJson2CheckBox);

// 限制单选
fastJsonCheckBox.addItemListener(new OptionsGroup.RestrictedRadioItemListener(fastJsonCheckBox, fastJson2CheckBox));
fastJson2CheckBox.addItemListener(new OptionsGroup.RestrictedRadioItemListener(fastJsonCheckBox, fastJson2CheckBox));

} else if (hasFastJsonLib) {
listModel.addElement(fastJsonCheckBox);

} else if (hasFastJson2Lib) {
listModel.addElement(fastJson2CheckBox);
}

if (hasJacksonLib) {
listModel.addElement(jacksonCheckBox);
}

listModel.addElement(keepCamelCheckBox);
return listModel;
}


@Override
public AnAction @NotNull [] getChildren(@Nullable AnActionEvent e) {
List<AnAction> actions = new ArrayList<>();
Expand All @@ -94,7 +171,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
}


public class FastJsonToggleAction extends ToggleAction {
public class FastJsonToggleAction extends ToggleAction implements KeepingPopupOpenAction{

private final DeserializerState deserializerState;

Expand All @@ -114,7 +191,7 @@ public void setSelected(@NotNull AnActionEvent e, boolean state) {
}
}

public class JacksonToggleAction extends ToggleAction {
public class JacksonToggleAction extends ToggleAction implements KeepingPopupOpenAction {

private final DeserializerState deserializerState;

Expand All @@ -134,7 +211,7 @@ public void setSelected(@NotNull AnActionEvent e, boolean state) {
}
}

public class KeepCamelToggleAction extends ToggleAction {
public class KeepCamelToggleAction extends ToggleAction implements KeepingPopupOpenAction {

private final DeserializerState deserializerState;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.memoryzy.json.action.query;

import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.enums.JsonQuerySchema;
import cn.memoryzy.json.service.persistent.state.QueryState;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareToggleAction;
import org.jetbrains.annotations.NotNull;

/**
* @author Memory
* @since 2024/12/27
*/
public class JmesPathToggleAction extends DumbAwareToggleAction {

private final QueryState queryState;

public JmesPathToggleAction(QueryState queryState) {
super();
this.queryState = queryState;
setEnabledInModalContext(true);
Presentation presentation = getTemplatePresentation();
presentation.setText(JsonAssistantBundle.messageOnSystem("action.toggle.jmes.text"));
presentation.setDescription(JsonAssistantBundle.messageOnSystem("action.toggle.jmes.description"));
}

@Override
public boolean isSelected(@NotNull AnActionEvent e) {
return queryState.querySchema == JsonQuerySchema.JMESPath;
}

@Override
public void setSelected(@NotNull AnActionEvent e, boolean state) {
// 限制单选
queryState.querySchema = state ? JsonQuerySchema.JMESPath : JsonQuerySchema.JSONPath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.memoryzy.json.action.query;

import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.enums.JsonQuerySchema;
import cn.memoryzy.json.service.persistent.state.QueryState;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareToggleAction;
import org.jetbrains.annotations.NotNull;

/**
* @author Memory
* @since 2024/12/27
*/
public class JsonPathToggleAction extends DumbAwareToggleAction {

private final QueryState queryState;

public JsonPathToggleAction(QueryState queryState) {
super();
this.queryState = queryState;
setEnabledInModalContext(true);
Presentation presentation = getTemplatePresentation();
presentation.setText(JsonAssistantBundle.messageOnSystem("action.toggle.jsonpath.text"));
presentation.setDescription(JsonAssistantBundle.messageOnSystem("action.toggle.jsonpath.description"));
}

@Override
public boolean isSelected(@NotNull AnActionEvent e) {
return queryState.querySchema == JsonQuerySchema.JSONPath;
}

@Override
public void setSelected(@NotNull AnActionEvent e, boolean state) {
// 限制单选
queryState.querySchema = state ? JsonQuerySchema.JSONPath : JsonQuerySchema.JMESPath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cn.memoryzy.json.action.query;

import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.service.persistent.state.QueryState;
import cn.memoryzy.json.ui.JsonQueryComponentProvider;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareToggleAction;
import com.intellij.util.ui.components.BorderLayoutPanel;
import icons.JsonAssistantIcons;
import org.jetbrains.annotations.NotNull;

/**
* @author Memory
* @since 2024/12/27
*/
public class ShowOriginalTextAction extends DumbAwareToggleAction {

private final QueryState queryState;
private final JsonQueryComponentProvider queryComponentProvider;

public ShowOriginalTextAction(QueryState queryState, JsonQueryComponentProvider queryComponentProvider) {
super();
this.queryState = queryState;
this.queryComponentProvider = queryComponentProvider;
setEnabledInModalContext(true);
Presentation presentation = getTemplatePresentation();
presentation.setText(JsonAssistantBundle.messageOnSystem("action.show.original.text.text"));
presentation.setDescription(JsonAssistantBundle.messageOnSystem("action.show.original.text.description"));
presentation.setIcon(JsonAssistantIcons.ToolWindow.TEXT);
}


@Override
public boolean isSelected(@NotNull AnActionEvent e) {
return queryState.showOriginalText;
}

@Override
public void setSelected(@NotNull AnActionEvent e, boolean state) {
queryState.showOriginalText = state;
BorderLayoutPanel docPanel = queryComponentProvider.getDocPanel();
if (state) {
// 展示
if (!docPanel.isVisible()) {
docPanel.setVisible(true);
}

} else {
// 关闭
if (docPanel.isVisible()) {
docPanel.setVisible(false);
}
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/cn/memoryzy/json/action/query/SwitchAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cn.memoryzy.json.action.query;

import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.service.persistent.state.QueryState;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.tools.SimpleActionGroup;
import icons.JsonAssistantIcons;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

/**
* @author Memory
* @since 2024/12/27
*/
public class SwitchAction extends DumbAwareAction {

private final QueryState queryState;

public SwitchAction(QueryState queryState) {
super();
this.queryState = queryState;
setEnabledInModalContext(true);
Presentation presentation = getTemplatePresentation();
presentation.setText(JsonAssistantBundle.messageOnSystem("action.switch.ql.text"));
presentation.setDescription(JsonAssistantBundle.messageOnSystem("action.switch.ql.description"));
presentation.setIcon(JsonAssistantIcons.ToolWindow.SWITCH);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
SimpleActionGroup actionGroup = new SimpleActionGroup();
actionGroup.add(new JsonPathToggleAction(queryState));
actionGroup.add(new JmesPathToggleAction(queryState));

JBPopupFactory.getInstance()
.createActionGroupPopup(null, actionGroup, e.getDataContext(), JBPopupFactory.ActionSelectionAid.MNEMONICS, true)
.showUnderneathOf((Component) e.getInputEvent().getSource());
}


}
Loading

0 comments on commit 2ee0044

Please sign in to comment.