@Override
	public void run(TaskMonitor monitor) {
		int max = 100; // Note: this used to be root.getNonLeafCount(), but that triggered a load
		UnknownProgressWrappingTaskMonitor monitorWrapper =
			new UnknownProgressWrappingTaskMonitor(monitor, max);

		monitorWrapper.initialize(max);
		monitorWrapper.setMessage("Collapsing nodes...");
		try {
			collapseNode(root, monitorWrapper);
		}
		catch (CancelledException e) {
			// Not everything expanded which is ok
		}
	}
A GTree task to fully collapse a tree
Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/tasks/GTreeCollapseAllTask.java