Skip to content

Commit 57e4ed5

Browse files
committed
To optimize tree node
1 parent 0ae297d commit 57e4ed5

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
lines changed

src/main/java/com/longforus/apidebugger/ui/JSONEditPanel.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import com.google.gson.JsonElement;
44
import com.google.gson.JsonNull;
55
import com.google.gson.JsonParser;
6+
import com.longforus.apidebugger.MyValueHandler;
67
import java.awt.BorderLayout;
8+
import java.awt.event.MouseEvent;
79
import java.util.ArrayList;
810
import java.util.Enumeration;
911
import java.util.List;
1012
import javax.swing.JOptionPane;
1113
import javax.swing.JPanel;
1214
import javax.swing.JScrollPane;
1315
import javax.swing.JTree;
16+
import javax.swing.event.MouseInputAdapter;
1417
import javax.swing.event.TreeSelectionListener;
1518
import javax.swing.tree.DefaultTreeModel;
1619
import javax.swing.tree.TreePath;
@@ -75,6 +78,16 @@ public JSONEditPanel() {
7578
*/
7679
public void addTreeSelectionListener(TreeSelectionListener tsl) {
7780
jTree.addTreeSelectionListener(tsl);
81+
jTree.addMouseListener(new MouseInputAdapter() {
82+
@Override
83+
public void mouseClicked(MouseEvent e) {
84+
super.mouseClicked(e);
85+
if (e.getButton() == MouseEvent.BUTTON3) {
86+
JSONJTreeNode component = (JSONJTreeNode) jTree.getLastSelectedPathComponent();
87+
MyValueHandler.INSTANCE.setSysClipboardText(component.toSSearchStr());
88+
}
89+
}
90+
});
7891
}
7992

8093
/**

src/main/java/com/longforus/apidebugger/ui/JSONJTreeNode.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public enum DataType {
5656
final String value;
5757
private int childCount = 0;
5858

59+
public String getFieldName() {
60+
return fieldName;
61+
}
62+
5963
/**
6064
* @param fieldName - name of field if applicable or null
6165
* @param index - index of element in the array or -1 if not part of an array
@@ -197,7 +201,35 @@ public String toString() {
197201
} else if (fieldName != null) {
198202
return String.format("%s: %s", fieldName, value);
199203
} else {
200-
return String.format("%s", value);
204+
return String.format("%s",value);
205+
}
206+
}
207+
}
208+
public String toSSearchStr() {
209+
switch (dataType) {
210+
case ARRAY:
211+
if (index >= 0) {
212+
return String.format("%d [%d]", index, childCount);
213+
}else if (fieldName != null) {
214+
return String.format("%s [%d]", fieldName, childCount);
215+
}else {
216+
return String.format("(%s)", dataType.name());
217+
}
218+
case OBJECT:
219+
if (index >= 0) {
220+
return String.format("%d {%d}", index, childCount);
221+
}else if (fieldName != null) {
222+
return String.format("%s {%d}", fieldName, childCount);
223+
} else {
224+
return String.format("(%s)", dataType.name());
225+
}
226+
default:
227+
if (index >= 0) {
228+
return String.format("[%d] %s", index, value);
229+
} else if (fieldName != null) {
230+
return String.format("\"%s\": %s", fieldName, value);
231+
} else {
232+
return String.format("%s",value);
201233
}
202234
}
203235
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public MainPanel(String title) throws HeadlessException {
119119
mBtnSend.addActionListener(e -> UIActionHandler.INSTANCE.onSend());
120120
mCbMethod.addItemListener(e -> UIActionHandler.INSTANCE.onMethodChanged(mCbMethod.getSelectedIndex()));
121121
mCbEncrypt.addItemListener(e -> UIActionHandler.INSTANCE.onEncryptTypeChanged(((IEncryptHandler) e.getItem()).getTypeCode()));
122-
123122
mTpResponse.addMouseListener(new MouseInputAdapter() {
124123
@Override
125124
public void mouseClicked(MouseEvent e) {

src/main/kotlin/com/longforus/apidebugger/HttpManage.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ object HttpManage {
4949
val body = response.body() ?: return
5050
val resStr = body.string()
5151
val json = mGson.fromJson<JsonObject>(resStr,JsonObject::class.java )
52-
mainPanel.tpResponse.append(mGson.toJson(json,JsonObject::class.java))
52+
val jsonStr = mGson.toJson(json, JsonObject::class.java)
53+
MyValueHandler.curShowJsonStr = jsonStr
54+
mainPanel.tpResponse.append(jsonStr)
5355
mainPanel.jep.setJson(resStr, JSONEditPanel.UpdateType.REPLACE)
5456
} else {
5557
mainPanel.tpInfo.append("on response but not success", Color.RED)

src/main/kotlin/com/longforus/apidebugger/JsonTreeActionHandler.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.longforus.apidebugger
22

3+
import com.longforus.apidebugger.ui.JSONJTreeNode
34
import javax.swing.event.TreeSelectionEvent
45
import javax.swing.event.TreeSelectionListener
56

@@ -10,7 +11,12 @@ import javax.swing.event.TreeSelectionListener
1011

1112
object JsonTreeActionHandler: TreeSelectionListener {
1213
override fun valueChanged(e: TreeSelectionEvent?) {
13-
println("on tree node selected")
14+
println(e.toString())
15+
e?.newLeadSelectionPath?.lastPathComponent?.let {
16+
val treeNode = it as JSONJTreeNode
17+
val start = MyValueHandler.curShowJsonStr.indexOf(treeNode.toSSearchStr())
18+
// mainPanel.tpResponse.select(start,start+treeNode.toSSearchStr().length)
19+
}
1420
}
1521

1622
}

src/main/kotlin/com/longforus/apidebugger/MyValueHandler.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import com.longforus.apidebugger.bean.ProjectBean
66
import com.longforus.apidebugger.encrypt.DefaultEncryptHandler
77
import com.longforus.apidebugger.encrypt.IEncryptHandler
88
import com.longforus.apidebugger.encrypt.kzEncryptHandler
9+
import java.awt.Toolkit
10+
import java.awt.datatransfer.StringSelection
11+
912

1013
/**
1114
* Created by XQ Yang on 8/30/2018 5:24 PM.
@@ -42,6 +45,7 @@ object MyValueHandler {
4245
}
4346

4447
var curBaseUrl = ""
48+
var curShowJsonStr = ""
4549

4650
fun encryptId2Index(id: Int): Int {
4751
encryptImplList.forEachIndexed { index, iEncryptHandler ->
@@ -54,19 +58,15 @@ object MyValueHandler {
5458
fun encryptIndex2Id(index: Int)=encryptImplList[index].typeCode
5559

5660

57-
// private fun formatJson(json: String): String {
58-
// var json = json
59-
// try {
60-
// if (json.startsWith("{")) {
61-
// json = JSONObject(json).toString(4)
62-
// } else if (json.startsWith("[")) {
63-
// json = JSONArray(json).toString(4)
64-
// }
65-
// } catch (e: JSONException) {
66-
// e.printStackTrace()
67-
// }
68-
//
69-
// return json
70-
// }
61+
62+
63+
/**
64+
* 将字符串复制到剪切板。
65+
*/
66+
fun setSysClipboardText(writeMe: String) {
67+
val clip = Toolkit.getDefaultToolkit().systemClipboard
68+
val tText = StringSelection(writeMe)
69+
clip.setContents(tText, null)
70+
}
7171

7272
}

0 commit comments

Comments
 (0)