package me.coley.recaf.ui.controls.tree;

import me.coley.recaf.Recaf;
import me.coley.recaf.workspace.JavaResource;

import java.util.*;

/**
 * Tree item to contain class sub-items.
 *
 * @author Matt
 */
public class ClassFolderItem extends DirectoryItem {
	private final JavaResource resource = resource();

	/**
	 * @param resource
	 * 		The resource associated with the item.
	 */
	public ClassFolderItem(JavaResource resource) {
		super(resource, "classes");
		// Add class sub-items in sorted order
		new TreeSet<>(resource.getClasses().keySet()).forEach(this::addClass);
	}

	protected void addClass(String name) {
		DirectoryItem item = this;
		List<String> parts = new ArrayList<>(Arrays.asList(name.split("/")));
		// Prune tree directory middle section if it is obnoxiously long
		int maxDepth = Recaf.getController().config().display().maxTreeDepth;
		if (parts.size() > maxDepth) {
			while (parts.size() > maxDepth) {
				parts.remove(maxDepth - 1);
			}
			parts.add(maxDepth - 1, "...");
		}
		// Build directory structure
		StringBuilder sb = new StringBuilder();
		while(!parts.isEmpty()) {
			String part = parts.remove(0);
			int keep = sb.length();
			sb.append(part);
			boolean isLeaf = parts.isEmpty();
			if (!isLeaf) {
				sb.append('/');
			}
			DirectoryItem child = item.getChild(part, isLeaf);
			if(child == null) {
				child = isLeaf ?
						new ClassItem(resource, part, name) :
						new PackageItem(resource, part, sb.substring(keep, sb.length() - 1));
				item.addChild(part, child, isLeaf);
			}
			item = child;
		}
	}
}
@@ -897,59 +897,175 @@
 %0A%09%09%09
-while (parts.size() %3E maxDepth) %7B%0A%09%09%09%09parts.remove(
+String lastPart = parts.get(parts.size() - 1);%0A%09%09%09// We keep only elements between%0A%09%09%09// %5B0..maxDepth-1%5D and the last part%0A%09%09%09parts = new ArrayList%3C%3E(parts.subList(0, 
 maxD
@@ -1077,47 +1077,53 @@
 - 1)
+)
 ;%0A%09%09%09
-%7D%0A%09%09%09parts.add(maxDepth - 1, %22...%22
+parts.add(%22...%22);%0A%09%09%09parts.add(lastPart
 );%0A%09
@@ -1267,45 +1267,31 @@
 %0A%09%09%09
-int keep = sb.length();%0A%09%09%09sb
+sb.append(part)
 .append(
 part
@@ -1286,20 +1286,19 @@
 .append(
-part
+'.'
 );%0A%09%09%09bo
@@ -1333,51 +1333,8 @@
 ();%0A
-%09%09%09if (!isLeaf) %7B%0A%09%09%09%09sb.append('/');%0A%09%09%09%7D%0A
 %09%09%09D
@@ -1522,20 +1522,17 @@
 bstring(
-keep
+0
 , sb.len

Optimize prune algorithm for paths