generateFlatTreeFromLabels now keeps selection and scroll position