Skip to content

Commit

Permalink
JSON树菜单增加提取路径的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryZy committed Jan 20, 2025
1 parent 6910c3b commit 7322b02
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@ public class OptionsGroup2 extends DefaultActionGroup {
private final DeserializerState deserializerState;

public OptionsGroup2(DeserializerState deserializerState, Module module) {
super(JsonAssistantBundle.messageOnSystem("dialog.deserialize.options.text"), true);
super(JsonAssistantBundle.messageOnSystem("dialog.deserialize.options.text"), false);
this.module = module;
this.deserializerState = deserializerState;
// setEnabledInModalContext(false);
Presentation presentation = getTemplatePresentation();
presentation.setIcon(AllIcons.General.Settings);
// presentation.setMultipleChoice(true);
// presentation.setPopupGroup(true);

// 使用本身的 actionPerformed 执行
// presentation.setPerformGroup(true);


presentation.setMultipleChoice(true);
}


@Override
public AnAction @NotNull [] getChildren(@Nullable AnActionEvent e) {
List<AnAction> actions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cn.memoryzy.json.action.structure;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.memoryzy.json.bundle.JsonAssistantBundle;
import cn.memoryzy.json.enums.JsonTreeNodeType;
import cn.memoryzy.json.ui.node.JsonTreeNode;
import cn.memoryzy.json.util.PlatformUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.ui.treeStructure.Tree;
import org.jetbrains.annotations.NotNull;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.List;

/**
* @author Memory
* @since 2025/1/20
*/
public class CopyNodePathAction extends DumbAwareAction {

private final Tree tree;

public CopyNodePathAction(Tree tree) {
super(JsonAssistantBundle.message("action.structure.copy.node.path.text"),
JsonAssistantBundle.messageOnSystem("action.structure.copy.node.path.description"),
null);
this.tree = tree;
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
TreePath[] paths = tree.getSelectionPaths();
if (ArrayUtil.isNotEmpty(paths)) {
List<String> pathList = new ArrayList<>();
for (TreePath path : paths) {
StringBuilder pathString = new StringBuilder();
for (Object element : path.getPath()) {
JsonTreeNode node = (JsonTreeNode) element;
JsonTreeNodeType nodeType = node.getNodeType();
if (JsonTreeNodeType.JSONArrayElement == nodeType) {
// 获取父节点,并得知当前节点在父节点的索引位置
TreeNode parent = node.getParent();
int index = parent.getIndex(node);
pathString.append("[").append(index).append("]");
} else {
pathString.append(pathString.length() > 0 ? " -> " : "").append(node.getUserObject());
}
}

pathList.add(pathString.toString());
}

PlatformUtil.setClipboard(StrUtil.join(", \n", pathList));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ private JPopupMenu buildRightMousePopupMenu() {
group.addSeparator();
group.add(new CopyKeyValueAction(tree));
group.addSeparator();
group.add(new CopyNodePathAction(tree));
group.addSeparator();
group.add(new ExpandMultiAction(tree));
group.addSeparator();
group.add(new CollapseMultiAction(tree));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
Expand All @@ -70,7 +69,6 @@
*/
public class JsonToJavaBeanDialog extends DialogWrapper {
private static final Logger LOG = Logger.getInstance(JsonToJavaBeanDialog.class);
private static final org.slf4j.Logger log = LoggerFactory.getLogger(JsonToJavaBeanDialog.class);

private JBTextField classNameTextField;
private EditorTextField jsonTextField;
Expand All @@ -88,7 +86,8 @@ public JsonToJavaBeanDialog(@Nullable Project project, PsiDirectory directory, M
this.project = project;
this.directory = directory;
this.module = module;
this.deserializerState = JsonAssistantPersistentState.getInstance().deserializerState;
JsonAssistantPersistentState persistentState = JsonAssistantPersistentState.getInstance();
this.deserializerState = persistentState.deserializerState;

setTitle(JsonAssistantBundle.messageOnSystem("dialog.deserialize.title"));
setOKButtonText(JsonAssistantBundle.messageOnSystem("dialog.deserialize.ok"));
Expand Down Expand Up @@ -556,7 +555,6 @@ private ObjectWrapper resolveJson(String jsonText) {


private String getFieldName(String originalKey) {
String fieldName;
// 包含下划线或空格,那就转为驼峰格式
if (deserializerState.keepCamelCase && (originalKey.contains("_") || originalKey.contains(" "))) {
return JsonAssistantUtil.toCamel(originalKey);
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 @@ -41,6 +41,7 @@ public class JsonUtil {
.enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS)
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.disable(JsonWriteFeature.WRITE_NAN_AS_STRINGS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/JsonAssistantBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ action.structure.collapse.multi.text=Collapse
action.structure.collapse.multi.description=Collapses the node and its children (Json Assistant)
action.structure.remove.text=Remove\u2026
action.structure.remove.description=Remove the node (Json Assistant)
action.structure.copy.node.path.text=Copy Node Path
action.structure.copy.node.path.description=Copy the path of the current node (Json Assistant)
action.serialize.text=_Convert to JSON
action.serialize.description=Serialize Java attribute to Json (Json Assistant)
action.serialize.json5.text=_Convert to JSON5
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/JsonAssistantBundle_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ action.structure.collapse.multi.text=\u6298\u53e0
action.structure.collapse.multi.description=\u6298\u53e0\u8be5\u8282\u70b9\u53ca\u5b50\u8282\u70b9 (Json Assistant)
action.structure.remove.text=\u5220\u9664\u2026
action.structure.remove.description=\u79fb\u9664\u8be5\u8282\u70b9 (Json Assistant)
action.structure.copy.node.path.text=\u62f7\u8d1d\u8282\u70b9\u8def\u5f84
action.structure.copy.node.path.description=\u62f7\u8d1d\u5f53\u524d\u8282\u70b9\u7684\u8def\u5f84 (Json Assistant)
action.serialize.text=\u004a\u0061\u0076\u0061\u0020\u5c5e\u6027\u8f6c\u4e3a\u0020\u004a\u0073\u006f\u006e
action.serialize.description=\u5c06\u0020\u004a\u0061\u0076\u0061\u0042\u0065\u0061\u006e\u0020\u5e8f\u5217\u5316\u4e3a\u0020\u004a\u0073\u006f\u006e (Json Assistant)
action.serialize.json5.text=\u004a\u0061\u0076\u0061\u0020\u5c5e\u6027\u8f6c\u4e3a\u0020\u004a\u0073\u006f\u006e\u0035
Expand Down

0 comments on commit 7322b02

Please sign in to comment.