<commit_before>package jadx.gui.treemodel;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.jetbrains.annotations.Nullable;

import jadx.api.ResourceFile;
import jadx.gui.JadxWrapper;
import jadx.gui.treemodel.JResource.JResType;
import jadx.gui.utils.NLS;
import jadx.gui.utils.UiUtils;

public class JRoot extends JNode {
	private static final long serialVersionUID = 8888495789773527342L;

	private static final ImageIcon ROOT_ICON = UiUtils.openSvgIcon("nodes/rootPackageFolder");

	private final transient JadxWrapper wrapper;

	private transient boolean flatPackages = false;

	private final List<JNode> customNodes = new ArrayList<>();

	public JRoot(JadxWrapper wrapper) {
		this.wrapper = wrapper;
	}

	public final void update() {
		removeAllChildren();
		add(new JSources(this, wrapper));

		List<ResourceFile> resources = wrapper.getResources();
		if (!resources.isEmpty()) {
			add(getHierarchyResources(resources));
		}
		for (JNode customNode : customNodes) {
			add(customNode);
		}
	}

	private JResource getHierarchyResources(List<ResourceFile> resources) {
		JResource root = new JResource(null, NLS.str("tree.resources_title"), JResType.ROOT);
		String splitPathStr = Pattern.quote(File.separator);
		for (ResourceFile rf : resources) {
			String rfName;
			if (rf.getZipRef() != null) {
				rfName = rf.getDeobfName();
			} else {
				rfName = new File(rf.getDeobfName()).getName();
			}
			String[] parts = new File(rfName).getPath().split(splitPathStr);
			JResource curRf = root;
			int count = parts.length;
			for (int i = 0; i < count; i++) {
				String name = parts[i];
				JResource subRF = getResourceByName(curRf, name);
				if (subRF == null) {
					if (i != count - 1) {
						subRF = new JResource(null, name, JResType.DIR);
					} else {
						subRF = new JResource(rf, rf.getOriginalName(), name, JResType.FILE);
					}
					curRf.getFiles().add(subRF);<commit_msg>fix(gui): sort resources by deobfuscated name (#1595)(PR #1598)<commit_after>package jadx.gui.treemodel;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.jetbrains.annotations.Nullable;

import jadx.api.ResourceFile;
import jadx.gui.JadxWrapper;
import jadx.gui.treemodel.JResource.JResType;
import jadx.gui.utils.NLS;
import jadx.gui.utils.UiUtils;

public class JRoot extends JNode {
	private static final long serialVersionUID = 8888495789773527342L;

	private static final ImageIcon ROOT_ICON = UiUtils.openSvgIcon("nodes/rootPackageFolder");

	private final transient JadxWrapper wrapper;

	private transient boolean flatPackages = false;

	private final List<JNode> customNodes = new ArrayList<>();

	public JRoot(JadxWrapper wrapper) {
		this.wrapper = wrapper;
	}

	public final void update() {
		removeAllChildren();
		add(new JSources(this, wrapper));

		List<ResourceFile> resources = wrapper.getResources();
		if (!resources.isEmpty()) {
			add(getHierarchyResources(resources));
		}
		for (JNode customNode : customNodes) {
			add(customNode);
		}
	}

	private JResource getHierarchyResources(List<ResourceFile> resources) {
		JResource root = new JResource(null, NLS.str("tree.resources_title"), JResType.ROOT);
		String splitPathStr = Pattern.quote(File.separator);
		for (ResourceFile rf : resources) {
			String rfName;
			if (rf.getZipRef() != null) {
				rfName = rf.getDeobfName();
			} else {
				rfName = new File(rf.getDeobfName()).getName();
			}
			String[] parts = new File(rfName).getPath().split(splitPathStr);
			JResource curRf = root;
			int count = parts.length;
			for (int i = 0; i < count; i++) {
				String name = parts[i];
				JResource subRF = getResourceByName(curRf, name);
				if (subRF == null) {
					if (i != count - 1) {
						subRF = new JResource(null, name, JResType.DIR);
					} else {
						subRF = new JResource(rf, rf.getDeobfName(), name, JResType.FILE);
					}
					curRf.getFiles().add(subRF);
{'fraction_non_alphanumeric': 0.12277368796010449, 'fraction_numerical': 0.011873664212776063, 'mean_word_length': 4.331645569620253, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7654829', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1400, 'n_words': 324}