Skip to content

Commit f963233

Browse files
author
linyajun
committed
添加搜索功能,并且搜索关键字高亮
1 parent f8edb52 commit f963233

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

hight-1.png

99.1 KB
Loading

hight-2.png

99.9 KB
Loading

src/com/zuicoding/platform/plugins/jsonformater/JsonUtils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class JsonUtils {
1010
private JsonUtils(){}
1111
private static void addIndentBlank(StringBuilder sb, int indent) {
1212
for (int i = 0; i < indent; i++) {
13-
sb.append('\t');
13+
sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
1414
}
1515
}
1616
public static String formatJson(String json){
@@ -34,15 +34,15 @@ public static String formatJson(String json){
3434
case '[':
3535
sb.append(current);
3636
if (!isInQuotationMarks) {
37-
sb.append('\n');
37+
sb.append("<br/>");
3838
indent++;
3939
addIndentBlank(sb, indent);
4040
}
4141
break;
4242
case '}':
4343
case ']':
4444
if (!isInQuotationMarks) {
45-
sb.append('\n');
45+
sb.append("<br/>");
4646
indent--;
4747
addIndentBlank(sb, indent);
4848
}
@@ -51,7 +51,7 @@ public static String formatJson(String json){
5151
case ',':
5252
sb.append(current);
5353
if (last != '\\' && !isInQuotationMarks) {
54-
sb.append('\n');
54+
sb.append("<br/>");
5555
addIndentBlank(sb, indent);
5656
}
5757
break;
@@ -63,4 +63,10 @@ public static String formatJson(String json){
6363
return sb.toString();
6464
}
6565

66+
public static boolean isBank(String str){
67+
68+
return str == null || str.trim().isEmpty();
69+
}
70+
71+
6672
}

src/com/zuicoding/platform/plugins/ui/JSONViewer.form

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.zuicoding.platform.plugins.ui.JSONViewer">
3-
<grid id="27dc6" binding="container" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="27dc6" binding="container" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
66
<xy x="20" y="20" width="800" height="600"/>
@@ -12,17 +12,33 @@
1212
<children>
1313
<scrollpane id="dbe2d">
1414
<constraints>
15-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
15+
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
1616
</constraints>
1717
<properties/>
1818
<border type="none"/>
1919
<children>
20-
<component id="ad571" class="javax.swing.JTextArea" binding="jsonText">
20+
<component id="21d7" class="javax.swing.JEditorPane" binding="jsonArea">
2121
<constraints/>
2222
<properties/>
2323
</component>
2424
</children>
2525
</scrollpane>
26+
<component id="6aadf" class="javax.swing.JTextField" binding="queryField">
27+
<constraints>
28+
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
29+
<preferred-size width="150" height="-1"/>
30+
</grid>
31+
</constraints>
32+
<properties/>
33+
</component>
34+
<component id="9d6ea" class="javax.swing.JButton" binding="queryBtn">
35+
<constraints>
36+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
37+
</constraints>
38+
<properties>
39+
<text value="查找"/>
40+
</properties>
41+
</component>
2642
</children>
2743
</grid>
2844
<inspectionSuppressions>

src/com/zuicoding/platform/plugins/ui/JSONViewer.java

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import com.zuicoding.platform.plugins.jsonformater.JsonUtils;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
7+
import org.jsoup.Jsoup;
8+
import org.jsoup.nodes.Document;
9+
import org.jsoup.nodes.Element;
10+
import org.jsoup.select.Elements;
711

812
import javax.swing.*;
13+
import javax.swing.text.html.HTMLEditorKit;
914
import java.awt.event.ActionEvent;
15+
import java.awt.event.ActionListener;
1016

1117
/**
1218
* Created by <a href="mailto:[email protected]">Stephen.lin</a> on 2017/12/7
@@ -15,19 +21,62 @@
1521
*/
1622
public class JSONViewer extends DialogWrapper{
1723
private JPanel container;
18-
private JTextArea jsonText;
24+
private JTextField queryField;
25+
private JEditorPane jsonArea;
26+
private JButton queryBtn;
27+
1928
public JSONViewer() {
2029
super(false);
2130
init();
2231
setTitle("JSON 格式化");
2332
setOKButtonText("格式化");
33+
HTMLEditorKit kit = new HTMLEditorKit();
34+
jsonArea.setEditorKit(kit);
35+
queryBtn.addActionListener(new ActionListener() {
36+
@Override
37+
public void actionPerformed(ActionEvent e) {
38+
String text = jsonArea.getText();
39+
if (JsonUtils.isBank(text)) return;
40+
Document doc = Jsoup.parse(text);
41+
Element element = doc.body().selectFirst("p");
42+
//已经格式化好了
43+
if (element == null){
44+
element = doc.body();
45+
element.html("<p>" + element.html() + "</p>");
46+
element = element.selectFirst("p");
47+
}
48+
49+
text = element.html();
50+
51+
text = wrapStyle(text);
2452

53+
element.html(text);
54+
55+
setJsonArea(element.toString());
56+
}
57+
});
2558
}
2659

2760
public JPanel getContainer() {
2861
return container;
2962
}
3063

64+
65+
66+
private String wrapStyle(String html){
67+
if (html == null || html.trim().isEmpty())return html;
68+
String value = queryField.getText();
69+
if (value == null || value.trim().isEmpty()) return html;
70+
Document doc = Jsoup.parse(html);
71+
doc.select("font[name=\"keyWrapper\"]").unwrap();
72+
html = doc.body().html().replaceAll(value,"<font name=\"keyWrapper\" style=\"background-color:yellow;\">" + value + "</font>");
73+
74+
return html;
75+
}
76+
77+
private void setJsonArea(String html){
78+
jsonArea.setText(html);
79+
}
3180
@NotNull
3281
@Override
3382
protected Action[] createActions() {
@@ -36,10 +85,30 @@ protected Action[] createActions() {
3685

3786
@Override
3887
protected void doAction(ActionEvent actionEvent) {
39-
String text = jsonText.getText();
88+
String text = jsonArea.getText();
4089
if (text == null || text.trim().isEmpty()) return;
4190
System.err.println(text);
42-
jsonText.setText(JsonUtils.formatJson(text));
91+
92+
try {
93+
Document doc = Jsoup.parse(text);
94+
95+
Element element = doc.body().selectFirst("p");
96+
if (element == null) {
97+
element = doc.body();
98+
element.html("<p>" + element.html() + "</p>");
99+
element = element.selectFirst("p");
100+
}
101+
text = element.html();
102+
System.err.println(text);
103+
text = text.replaceAll("(&nbsp;|<br/>|\0|<br>)","");
104+
text = JsonUtils.formatJson(text);
105+
text = wrapStyle(text);
106+
element.html(text);
107+
setJsonArea(element.toString());
108+
}catch (Exception e){
109+
e.printStackTrace();
110+
}
111+
43112
}
44113
};
45114

0 commit comments

Comments
 (0)