1515 * 自定义树节点渲染器,用于美化 JTree 的节点显示
1616 */
1717public class RequestTreeCellRenderer extends DefaultTreeCellRenderer {
18+
19+ private static final int ICON_SIZE = 16 ;
20+ private static final int METHOD_FONT_PX = 7 ;
21+ private static final int NAME_FONT_PX = 8 ; // using 8 instead of 8.5 for simplicity
22+
1823 @ Override
1924 public Component getTreeCellRendererComponent (JTree tree , Object value , boolean sel , boolean expanded ,
2025 boolean leaf , int row , boolean hasFocus ) {
@@ -23,33 +28,65 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
2328 Object userObject = node .getUserObject ();
2429 if (userObject instanceof Object [] obj ) {
2530 if (RequestCollectionsLeftPanel .GROUP .equals (obj [0 ])) {
26- setIcon (new FlatSVGIcon ("icons/group.svg" , 16 , 16 ));
31+ setIcon (new FlatSVGIcon ("icons/group.svg" , ICON_SIZE , ICON_SIZE ));
2732 setText ((String ) obj [1 ]);
2833 } else if (RequestCollectionsLeftPanel .REQUEST .equals (obj [0 ])) {
29- // 直接用彩色文本显示 method + name,method 彩色,name 默认色
3034 HttpRequestItem item = (HttpRequestItem ) obj [1 ];
31- String method = item .getMethod ();
32- String name = item .getName ();
33- RequestItemProtocolEnum protocol = item .getProtocol ();
34- String methodColor = HttpUtil .getMethodColor (method );
35-
36- // 根据协议类型设置不同的图标和显示样式
37- if (protocol .isWebSocketProtocol ()) {
38- setIcon (new FlatSVGIcon ("icons/websocket.svg" , 16 , 16 ));
39- setText ("<html><span style='color:#1976D2;font-weight:bold;font-size:7px'>WS</span> <span style='font-size:8.5px'>" + name + "</span></html>" );
40- } else {
41- if (protocol .isSseProtocol ()) {
42- setIcon (new FlatSVGIcon ("icons/sse.svg" , 16 , 16 ));
43- } else {
44- setIcon (new FlatSVGIcon ("icons/http.svg" , 16 , 16 ));
45- }
46- setText ("<html><span style='color:" + methodColor + ";font-weight:bold;font-size:7px'>" +
47- (method == null ? "" : method ) + "</span> <span style='font-size:8.5px'>" + name + "</span></html>" );
48- }
35+ applyRequestRendering (item );
4936 }
5037 }
5138 if (sel ) setBackgroundSelectionColor (new Color (255 , 230 , 180 ));
5239 else setBackgroundNonSelectionColor (getBackground ());
5340 return this ;
5441 }
42+
43+ // Extracted to reduce cognitive complexity of the main method
44+ private void applyRequestRendering (HttpRequestItem item ) {
45+ String method = item .getMethod ();
46+ String name = item .getName ();
47+ RequestItemProtocolEnum protocol = item .getProtocol ();
48+ String methodColor = HttpUtil .getMethodColor (method );
49+
50+ if (protocol .isWebSocketProtocol ()) {
51+ method = "WS" ;
52+ methodColor = "#29cea5" ;
53+ } else if (protocol .isSseProtocol ()) {
54+ method = "SSE" ;
55+ methodColor = "#7fbee3" ;
56+ }
57+
58+ setText (buildStyledText (method , methodColor , name ));
59+ }
60+
61+ // Build HTML with escaped content and consistent font sizes
62+ private static String buildStyledText (String method , String methodColor , String name ) {
63+ String safeMethod = method == null ? "" : escapeHtml (method );
64+ String safeName = name == null ? "" : escapeHtml (name );
65+ String color = methodColor == null ? "#000" : methodColor ;
66+ // simple concatenation is clearer for this short html fragment
67+ return "<html>" +
68+ "<span style='color:" + color + ";font-weight:bold;font-size:" + METHOD_FONT_PX + "px'>" +
69+ safeMethod +
70+ "</span> " +
71+ "<span style='font-size:" + NAME_FONT_PX + "px'>" +
72+ safeName +
73+ "</span></html>" ;
74+ }
75+
76+ // Minimal HTML escape to avoid broken rendering or injection
77+ private static String escapeHtml (String s ) {
78+ if (s == null ) return null ;
79+ StringBuilder out = new StringBuilder (Math .max (16 , s .length ()));
80+ for (char c : s .toCharArray ()) {
81+ switch (c ) {
82+ case '<' -> out .append ("<" );
83+ case '>' -> out .append (">" );
84+ case '&' -> out .append ("&" );
85+ case '"' -> out .append (""" );
86+ case '\'' -> out .append ("'" );
87+ default -> out .append (c );
88+ }
89+ }
90+ return out .toString ();
91+ }
5592}
0 commit comments