ViewerMisc { @SuppressWarnings("unchecked") public static <T> void setLazyTreeContentProvider(TreeViewer viewer, TreeDef.Parented<T> treeDef) { Preconditions.checkArgument(SwtMisc.flagIsSet(SWT.VIRTUAL, viewer.getControl()), "The tree must have SWT.VIRTUAL set."); viewer.setUseHashlookup(true); viewer.setContentProvider(new ILazyTreeContentProvider() { @Override public void dispose() {} @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} @Override public void updateElement(Object parent, int index) { T child = treeDef.childrenOf((T) parent).get(index); viewer.replace(parent, index, child); updateChildCount(child, 0); } @Override public void updateChildCount(Object element, int currentChildCount) { viewer.setChildCount(element, treeDef.childrenOf((T) element).size()); } @Override public Object getParent(Object element) { return treeDef.parentOf((T) element); } }); }  static @SwtThread RxBox<Optional<T>> singleSelection(StructuredViewer viewer); static void singleSelection(StructuredViewer viewer, RxBox<Optional<T>> box); static @SwtThread RxBox<ImmutableSet<T>> multiSelectionSet(StructuredViewer viewer); static void multiSelectionSet(StructuredViewer viewer, RxBox<ImmutableSet<T>> box); static @SwtThread RxBox<ImmutableList<T>> multiSelectionList(StructuredViewer viewer); @SuppressWarnings("unchecked") static void multiSelectionList(StructuredViewer viewer, RxBox<ImmutableList<T>> box); @SuppressWarnings("unchecked") static void setTreeContentProvider(TreeViewer viewer, TreeDef.Parented<T> treeDef); @SuppressWarnings("unchecked") static void setLazyTreeContentProvider(TreeViewer viewer, TreeDef.Parented<T> treeDef);  }
@Test public void testLazyContentProviderFile() { InteractiveTest.testCoat("You should be able to browse the filesystem.", cmp -> { ColumnViewerFormat<File> format = ColumnViewerFormat.builder(); format.setStyle(SWT.VIRTUAL); format.addColumn().setText("Name") .setLabelProviderText(File::getName); format.addColumn().setText("Last Modified") .setLabelProviderText(file -> new Date(file.lastModified()).toString()) .setLayoutPixel(SwtMisc.systemFontWidth() * new Date().toString().length()); TreeViewer viewer = format.buildTree(cmp); ViewerMisc.setLazyTreeContentProvider(viewer, TreeDef.forFile(Errors.suppress())); File[] roots = File.listRoots(); if (roots.length == 1) { viewer.setInput(roots[0]); } else { Optional<File> cDrive = Arrays.asList(roots).stream().filter(file -> file.getAbsolutePath().contains("C")).findFirst(); viewer.setInput(cDrive.orElse(roots[0])); } }); }
@Test public void testLazyContentProviderPath() { InteractiveTest.testCoat("You should be able to browse the filesystem.", cmp -> { ColumnViewerFormat<Path> format = ColumnViewerFormat.builder(); format.setStyle(SWT.VIRTUAL); format.addColumn().setText("Name") .setLabelProviderText(path -> path.getFileName().toString()); format.addColumn().setText("Last Modified") .setLabelProviderText(path -> Errors.suppress().getWithDefault(() -> Files.getLastModifiedTime(path).toString(), "")) .setLayoutPixel(SwtMisc.systemFontWidth() * new Date().toString().length()); TreeViewer viewer = format.buildTree(cmp); ViewerMisc.setLazyTreeContentProvider(viewer, TreeDef.forPath(Errors.suppress())); File[] roots = File.listRoots(); if (roots.length == 1) { viewer.setInput(roots[0].toPath()); } else { Optional<File> cDrive = Arrays.asList(roots).stream().filter(file -> file.getAbsolutePath().contains("C")).findFirst(); viewer.setInput(cDrive.orElse(roots[0]).toPath()); } }); }