■ ■ ■ ■ ■ ■
jadx-gui/src/main/java/jadx/gui/treemodel/JResource.java
1 | 1 | | package jadx.gui.treemodel; |
2 | 2 | | |
3 | | - | import java.util.ArrayList; |
| 3 | + | import java.util.Collections; |
4 | 4 | | import java.util.Comparator; |
5 | 5 | | import java.util.List; |
6 | 6 | | import java.util.Map; |
| skipped 7 lines |
14 | 14 | | import jadx.api.ICodeInfo; |
15 | 15 | | import jadx.api.ICodeWriter; |
16 | 16 | | import jadx.api.ResourceFile; |
17 | | - | import jadx.api.ResourceFileContent; |
18 | 17 | | import jadx.api.ResourceType; |
19 | 18 | | import jadx.api.ResourcesLoader; |
20 | 19 | | import jadx.api.impl.SimpleCodeInfo; |
| 20 | + | import jadx.core.utils.ListUtils; |
21 | 21 | | import jadx.core.utils.Utils; |
22 | 22 | | import jadx.core.xmlgen.ResContainer; |
23 | 23 | | import jadx.gui.ui.TabbedPane; |
| skipped 2 lines |
26 | 26 | | import jadx.gui.ui.panel.ImagePanel; |
27 | 27 | | import jadx.gui.utils.NLS; |
28 | 28 | | import jadx.gui.utils.UiUtils; |
| 29 | + | import jadx.gui.utils.res.ResTableHelper; |
29 | 30 | | |
30 | 31 | | public class JResource extends JLoadableNode { |
31 | 32 | | private static final long serialVersionUID = -201018424302612434L; |
| skipped 9 lines |
41 | 42 | | private static final ImageIcon JAVA_ICON = UiUtils.openSvgIcon("nodes/java"); |
42 | 43 | | private static final ImageIcon UNKNOWN_ICON = UiUtils.openSvgIcon("nodes/unknown"); |
43 | 44 | | |
| 45 | + | public static final Comparator<JResource> RESOURCES_COMPARATOR = |
| 46 | + | Comparator.<JResource>comparingInt(r -> r.type.ordinal()) |
| 47 | + | .thenComparing(JResource::getName, String.CASE_INSENSITIVE_ORDER); |
| 48 | + | |
44 | 49 | | public enum JResType { |
45 | 50 | | ROOT, |
46 | 51 | | DIR, |
| skipped 2 lines |
49 | 54 | | |
50 | 55 | | private final transient String name; |
51 | 56 | | private final transient String shortName; |
52 | | - | private final transient List<JResource> files = new ArrayList<>(1); |
53 | 57 | | private final transient JResType type; |
54 | 58 | | private final transient ResourceFile resFile; |
55 | 59 | | |
56 | | - | private transient boolean loaded; |
| 60 | + | private transient volatile boolean loaded; |
| 61 | + | private transient List<JResource> subNodes = Collections.emptyList(); |
57 | 62 | | private transient ICodeInfo content; |
58 | 63 | | |
59 | 64 | | public JResource(ResourceFile resFile, String name, JResType type) { |
| skipped 9 lines |
69 | 74 | | } |
70 | 75 | | |
71 | 76 | | public final void update() { |
72 | | - | if (files.isEmpty()) { |
| 77 | + | removeAllChildren(); |
| 78 | + | if (Utils.isEmpty(subNodes)) { |
73 | 79 | | if (type == JResType.DIR || type == JResType.ROOT |
74 | 80 | | || resFile.getType() == ResourceType.ARSC) { |
75 | 81 | | // fake leaf to force show expand button |
| skipped 1 lines |
77 | 83 | | add(new TextNode(NLS.str("tree.loading"))); |
78 | 84 | | } |
79 | 85 | | } else { |
80 | | - | removeAllChildren(); |
81 | | - | |
82 | | - | Comparator<JResource> typeComparator = Comparator.comparingInt(r -> r.type.ordinal()); |
83 | | - | Comparator<JResource> nameComparator = Comparator.comparing(JResource::getName, String.CASE_INSENSITIVE_ORDER); |
84 | | - | |
85 | | - | files.sort(typeComparator.thenComparing(nameComparator)); |
86 | | - | |
87 | | - | for (JResource res : files) { |
| 86 | + | for (JResource res : subNodes) { |
88 | 87 | | res.update(); |
89 | 88 | | add(res); |
90 | 89 | | } |
| skipped 15 lines |
106 | 105 | | return type; |
107 | 106 | | } |
108 | 107 | | |
109 | | - | public List<JResource> getFiles() { |
110 | | - | return files; |
| 108 | + | public List<JResource> getSubNodes() { |
| 109 | + | return subNodes; |
| 110 | + | } |
| 111 | + | |
| 112 | + | public void addSubNode(JResource node) { |
| 113 | + | subNodes = ListUtils.safeAdd(subNodes, node); |
| 114 | + | } |
| 115 | + | |
| 116 | + | public void sortSubNodes() { |
| 117 | + | sortResNodes(subNodes); |
| 118 | + | } |
| 119 | + | |
| 120 | + | private static void sortResNodes(List<JResource> nodes) { |
| 121 | + | if (Utils.notEmpty(nodes)) { |
| 122 | + | nodes.forEach(JResource::sortSubNodes); |
| 123 | + | nodes.sort(RESOURCES_COMPARATOR); |
| 124 | + | } |
111 | 125 | | } |
112 | 126 | | |
113 | 127 | | @Override |
| skipped 31 lines |
145 | 159 | | } |
146 | 160 | | if (rc.getDataType() == ResContainer.DataType.RES_TABLE) { |
147 | 161 | | ICodeInfo codeInfo = loadCurrentSingleRes(rc); |
148 | | - | for (ResContainer subFile : rc.getSubFiles()) { |
149 | | - | loadSubNodes(this, subFile, 1); |
150 | | - | } |
| 162 | + | List<JResource> nodes = ResTableHelper.buildTree(rc); |
| 163 | + | sortResNodes(nodes); |
| 164 | + | subNodes = nodes; |
151 | 165 | | return codeInfo; |
152 | 166 | | } |
153 | 167 | | // single node |
| skipped 22 lines |
176 | 190 | | default: |
177 | 191 | | return new SimpleCodeInfo("Unexpected resource type: " + rc); |
178 | 192 | | } |
179 | | - | } |
180 | | - | |
181 | | - | private void loadSubNodes(JResource root, ResContainer rc, int depth) { |
182 | | - | String resName = rc.getName(); |
183 | | - | String[] path = resName.split("/"); |
184 | | - | String resShortName = path.length == 0 ? resName : path[path.length - 1]; |
185 | | - | ICodeInfo code = rc.getText(); |
186 | | - | ResourceFileContent fileContent = new ResourceFileContent(resShortName, ResourceType.XML, code); |
187 | | - | addPath(path, root, new JResource(fileContent, resName, resShortName, JResType.FILE)); |
188 | | - | |
189 | | - | for (ResContainer subFile : rc.getSubFiles()) { |
190 | | - | loadSubNodes(root, subFile, depth + 1); |
191 | | - | } |
192 | | - | } |
193 | | - | |
194 | | - | private static void addPath(String[] path, JResource root, JResource jResource) { |
195 | | - | if (path.length == 1) { |
196 | | - | root.getFiles().add(jResource); |
197 | | - | return; |
198 | | - | } |
199 | | - | JResource currentRoot = root; |
200 | | - | int last = path.length - 1; |
201 | | - | for (int i = 0; i <= last; i++) { |
202 | | - | String f = path[i]; |
203 | | - | if (i == last) { |
204 | | - | currentRoot.getFiles().add(jResource); |
205 | | - | } else { |
206 | | - | currentRoot = getResDir(currentRoot, f); |
207 | | - | } |
208 | | - | } |
209 | | - | } |
210 | | - | |
211 | | - | private static JResource getResDir(JResource root, String dirName) { |
212 | | - | for (JResource file : root.getFiles()) { |
213 | | - | if (file.getName().equals(dirName)) { |
214 | | - | return file; |
215 | | - | } |
216 | | - | } |
217 | | - | JResource resDir = new JResource(null, dirName, JResType.DIR); |
218 | | - | root.getFiles().add(resDir); |
219 | | - | return resDir; |
220 | 193 | | } |
221 | 194 | | |
222 | 195 | | @Override |
| skipped 130 lines |