Skip to content

Commit 0ae297d

Browse files
committed
To optimize the ui ,some action process
1 parent 9ff381b commit 0ae297d

File tree

14 files changed

+457
-171
lines changed

14 files changed

+457
-171
lines changed
Lines changed: 169 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.longforus.apidebugger.ui;
22

3+
import com.google.gson.JsonArray;
34
import com.google.gson.JsonElement;
45
import com.google.gson.JsonObject;
56
import com.google.gson.JsonParser;
@@ -12,9 +13,9 @@
1213

1314
/**
1415
* <p>Provides the model for translating JsonElement into JTree data nodes. This class is not thread safe.</p>
15-
*
16+
*
1617
* @author Stephen Owens
17-
*
18+
*
1819
* <p>Provides the model for translating JsonElement into JTree data nodes.</p>
1920
*
2021
* <p>
@@ -26,7 +27,7 @@
2627
* You may obtain a copy of the License at
2728
* </p>
2829
* <p>
29-
* http://www.apache.org/licenses/LICENSE-2.0
30+
* http://www.apache.org/licenses/LICENSE-2.0
3031
* </p>
3132
* <p>
3233
* Unless required by applicable law or agreed to in writing, software
@@ -37,130 +38,170 @@
3738
* </p>
3839
*/
3940
public class JSONJTreeNode extends DefaultMutableTreeNode {
40-
/**
41-
* Using default serial id.
42-
*/
43-
private static final long serialVersionUID = 1L;
41+
/**
42+
* Using default serial id.
43+
*/
44+
private static final long serialVersionUID = 1L;
4445

45-
public enum DataType {ARRAY, OBJECT, VALUE};
46-
final DataType dataType;
47-
final int index;
48-
String fieldName;
49-
final String value;
50-
51-
/**
52-
* @param fieldName - name of field if applicable or null
53-
* @param index - index of element in the array or -1 if not part of an array
54-
* @param jsonElement - element to represent
55-
*/
56-
public JSONJTreeNode(String fieldName, int index, JsonElement jsonElement) {
57-
this.index = index;
58-
this.fieldName = fieldName;
59-
if(jsonElement.isJsonArray()) {
60-
this.dataType = DataType.ARRAY;
61-
this.value = jsonElement.toString();
62-
populateChildren(jsonElement);
63-
} else if(jsonElement.isJsonObject()) {
64-
this.dataType = DataType.OBJECT;
65-
this.value = jsonElement.toString();
66-
populateChildren(jsonElement);
67-
} else if(jsonElement.isJsonPrimitive()) {
68-
this.dataType = DataType.VALUE;
69-
this.value = jsonElement.toString();
70-
} else if(jsonElement.isJsonNull()) {
71-
this.dataType = DataType.VALUE;
72-
this.value = jsonElement.toString();
73-
} else {
74-
throw new IllegalArgumentException("jsonElement is an unknown element type.");
75-
}
76-
77-
}
78-
79-
private void populateChildren(JsonElement myJsonElement) {
80-
switch(dataType) {
81-
case ARRAY:
82-
int index = 0;
83-
Iterator<JsonElement> it = myJsonElement.getAsJsonArray().iterator();
84-
while(it.hasNext()) {
85-
JsonElement element = it.next();
86-
JSONJTreeNode childNode = new JSONJTreeNode(null, index, element);
87-
this.add(childNode);
88-
index++;
89-
}
90-
break;
91-
case OBJECT:
92-
for(Entry<String,JsonElement> entry : myJsonElement.getAsJsonObject().entrySet()) {
93-
JSONJTreeNode childNode = new JSONJTreeNode(entry.getKey(), -1, entry.getValue());
94-
this.add(childNode);
95-
}
96-
break;
97-
default:
98-
throw new IllegalStateException("Internal coding error this should never happen.");
99-
}
100-
}
101-
102-
public JsonElement asJsonElement() {
103-
StringBuilder sb = new StringBuilder();
104-
buildJsonString(sb);
105-
String json = sb.toString().trim();
106-
if(json.startsWith("{") || json.startsWith("["))
107-
return new JsonParser().parse(sb.toString());
108-
else {
109-
// Safety check the JSON, if it is of a named value object
110-
// We cheat a little if it is an orphan name value pair then
111-
// if we wrap it in {} chars it will parse if it isn't the parse
112-
// fails.
113-
String testValue = "{" + json + "}";
114-
try {
115-
JsonElement wrapperElt = new JsonParser().parse(testValue);
116-
JsonObject obj = (JsonObject) wrapperElt;
117-
Iterator<Entry<String,JsonElement>> it = obj.entrySet().iterator();
118-
Entry<String,JsonElement> entry = it.next();
119-
return entry.getValue();
120-
} catch(JsonSyntaxException jse) {
121-
JsonElement rawElement = new JsonParser().parse(json);
122-
return rawElement;
123-
}
124-
}
125-
}
126-
127-
@SuppressWarnings("unchecked")
128-
private void buildJsonString(StringBuilder sb) {
129-
if(!StringUtils.isEmpty(this.fieldName)) {
130-
sb.append("\"" + this.fieldName + "\":");
131-
}
132-
Enumeration children;
133-
switch(dataType) {
134-
case ARRAY:
135-
sb.append("[");
136-
children = this.children();
137-
while(children.hasMoreElements()) {
138-
JSONJTreeNode child = (JSONJTreeNode) children.nextElement();
139-
child.buildJsonString(sb);
140-
if(children.hasMoreElements())
141-
sb.append(",");
142-
}
143-
sb.append("]");
144-
break;
145-
case OBJECT:
146-
sb.append("{");
147-
children = this.children();
148-
while(children.hasMoreElements()) {
149-
JSONJTreeNode child = (JSONJTreeNode) children.nextElement();
150-
child.buildJsonString(sb);
151-
if(children.hasMoreElements())
152-
sb.append(",");
153-
}
154-
sb.append("}");
155-
break;
156-
default: {
157-
// Use the JSON parser to parse the value for safety
158-
JsonElement elt = new JsonParser().parse(this.value);
159-
sb.append(elt.toString());
160-
}
161-
}
162-
}
163-
46+
public enum DataType {
47+
ARRAY,
48+
OBJECT,
49+
VALUE
50+
}
51+
52+
;
53+
final DataType dataType;
54+
final int index;
55+
String fieldName;
56+
final String value;
57+
private int childCount = 0;
58+
59+
/**
60+
* @param fieldName - name of field if applicable or null
61+
* @param index - index of element in the array or -1 if not part of an array
62+
* @param jsonElement - element to represent
63+
*/
64+
public JSONJTreeNode(String fieldName, int index, JsonElement jsonElement) {
65+
this.index = index;
66+
this.fieldName = fieldName;
67+
if (jsonElement.isJsonArray()) {
68+
this.dataType = DataType.ARRAY;
69+
this.value = jsonElement.toString();
70+
populateChildren(jsonElement);
71+
} else if (jsonElement.isJsonObject()) {
72+
this.dataType = DataType.OBJECT;
73+
this.value = jsonElement.toString();
74+
populateChildren(jsonElement);
75+
} else if (jsonElement.isJsonPrimitive()) {
76+
this.dataType = DataType.VALUE;
77+
this.value = jsonElement.toString();
78+
} else if (jsonElement.isJsonNull()) {
79+
this.dataType = DataType.VALUE;
80+
this.value = jsonElement.toString();
81+
} else {
82+
throw new IllegalArgumentException("jsonElement is an unknown element type.");
83+
}
84+
}
85+
86+
private void populateChildren(JsonElement myJsonElement) {
87+
switch (dataType) {
88+
case ARRAY:
89+
int index = 0;
90+
JsonArray array = myJsonElement.getAsJsonArray();
91+
childCount = array.size();
92+
Iterator<JsonElement> it = array.iterator();
93+
while (it.hasNext()) {
94+
JsonElement element = it.next();
95+
JSONJTreeNode childNode = new JSONJTreeNode(null, index, element);
96+
this.add(childNode);
97+
index++;
98+
}
99+
break;
100+
case OBJECT:
101+
JsonObject object = myJsonElement.getAsJsonObject();
102+
childCount = object.size();
103+
for (Entry<String, JsonElement> entry : object.entrySet()) {
104+
JSONJTreeNode childNode = new JSONJTreeNode(entry.getKey(), -1, entry.getValue());
105+
this.add(childNode);
106+
}
107+
break;
108+
default:
109+
throw new IllegalStateException("Internal coding error this should never happen.");
110+
}
111+
}
112+
113+
public JsonElement asJsonElement() {
114+
StringBuilder sb = new StringBuilder();
115+
buildJsonString(sb);
116+
String json = sb.toString().trim();
117+
if (json.startsWith("{") || json.startsWith("[")) {
118+
return new JsonParser().parse(sb.toString());
119+
} else {
120+
// Safety check the JSON, if it is of a named value object
121+
// We cheat a little if it is an orphan name value pair then
122+
// if we wrap it in {} chars it will parse if it isn't the parse
123+
// fails.
124+
String testValue = "{" + json + "}";
125+
try {
126+
JsonElement wrapperElt = new JsonParser().parse(testValue);
127+
JsonObject obj = (JsonObject) wrapperElt;
128+
Iterator<Entry<String, JsonElement>> it = obj.entrySet().iterator();
129+
Entry<String, JsonElement> entry = it.next();
130+
return entry.getValue();
131+
} catch (JsonSyntaxException jse) {
132+
JsonElement rawElement = new JsonParser().parse(json);
133+
return rawElement;
134+
}
135+
}
136+
}
137+
138+
@SuppressWarnings("unchecked")
139+
private void buildJsonString(StringBuilder sb) {
140+
if (!StringUtils.isEmpty(this.fieldName)) {
141+
sb.append("\"" + this.fieldName + "\":");
142+
}
143+
Enumeration children;
144+
switch (dataType) {
145+
case ARRAY:
146+
sb.append("[");
147+
children = this.children();
148+
processNode(sb, children);
149+
sb.append("]");
150+
break;
151+
case OBJECT:
152+
sb.append("{");
153+
children = this.children();
154+
processNode(sb, children);
155+
sb.append("}");
156+
break;
157+
default: {
158+
// Use the JSON parser to parse the value for safety
159+
JsonElement elt = new JsonParser().parse(this.value);
160+
sb.append(elt.toString());
161+
}
162+
}
163+
}
164+
165+
private void processNode(StringBuilder sb, Enumeration children) {
166+
while (children.hasMoreElements()) {
167+
JSONJTreeNode child = (JSONJTreeNode) children.nextElement();
168+
child.buildJsonString(sb);
169+
if (children.hasMoreElements()) {
170+
sb.append(",");
171+
}
172+
}
173+
}
174+
175+
@Override
176+
public String toString() {
177+
switch (dataType) {
178+
case ARRAY:
179+
if (index >= 0) {
180+
return String.format("%d [%d]", index, childCount);
181+
}else if (fieldName != null) {
182+
return String.format("%s [%d]", fieldName, childCount);
183+
}else {
184+
return String.format("(%s)", dataType.name());
185+
}
186+
case OBJECT:
187+
if (index >= 0) {
188+
return String.format("%d {%d}", index, childCount);
189+
}else if (fieldName != null) {
190+
return String.format("%s {%d}", fieldName, childCount);
191+
} else {
192+
return String.format("(%s)", dataType.name());
193+
}
194+
default:
195+
if (index >= 0) {
196+
return String.format("[%d] %s", index, value);
197+
} else if (fieldName != null) {
198+
return String.format("%s: %s", fieldName, value);
199+
} else {
200+
return String.format("%s", value);
201+
}
202+
}
203+
}
204+
/*
164205
@Override
165206
public String toString() {
166207
switch(dataType) {
@@ -181,7 +222,8 @@ public String toString() {
181222
} else {
182223
return String.format("%s", value);
183224
}
184-
225+
185226
}
186227
}
228+
*/
187229
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.longforus.apidebugger.ui;
2+
3+
import java.awt.Component;
4+
import javax.swing.JTree;
5+
import javax.swing.tree.DefaultTreeCellRenderer;
6+
7+
/**
8+
* 自定义树描述类,将树的每个节点设置成不同的图标
9+
*
10+
* @author RuiLin.Xie - xKF24276
11+
*/
12+
public class JsonTreeCellRenderer extends DefaultTreeCellRenderer {
13+
/**
14+
* ID
15+
*/
16+
private static final long serialVersionUID = 1L;
17+
18+
/**
19+
* 重写父类DefaultTreeCellRenderer的方法
20+
*/
21+
@Override
22+
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
23+
24+
//执行父类原型操作
25+
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
26+
27+
setText(value.toString());
28+
29+
if (sel) {
30+
setForeground(getTextSelectionColor());
31+
} else {
32+
setForeground(getTextNonSelectionColor());
33+
}
34+
this.setIcon(null);
35+
return this;
36+
}
37+
}

src/main/java/com/longforus/apidebugger/ui/MainPanel.form

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@
8383
<grid row="9" column="0" row-span="1" col-span="11" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
8484
<forms defaultalign-horz="false" defaultalign-vert="false"/>
8585
</constraints>
86-
<properties/>
86+
<properties>
87+
<horizontalScrollBarPolicy value="31"/>
88+
</properties>
8789
<border type="none" title="Response"/>
8890
<children>
8991
<component id="368c1" class="javax.swing.JTextPane" binding="mTpResponse">

0 commit comments

Comments
 (0)