Source:
<code>public Snippet064TreeViewerReplacingElements(Shell shell) { final TreeViewer v = new TreeViewer(shell); Object[] rootElements = new Object[] { "root 1", "root 2", "root 3" }; Object input = new Object(); AtomicReference<IObservableList<String>> recentlyCreatedChildList = new AtomicReference<>(); final IObservableList<String> rootElementList = new WritableList(DisplayRealm.getRealm(shell.getDisplay()), Arrays.asList(rootElements), String.class); <START> ITreeContentProvider contentProvider = new ObservableListTreeContentProvider(new IObservableFactory() { <END> @Override public IObservable createObservable(Object target) { if (target == input) return rootElementList; if (target.toString().startsWith("root")) { recentlyCreatedChildList.set(new WritableList<>(DisplayRealm.getRealm(shell.getDisplay()), Arrays.asList(new String[] { "child 1", "child 2" }), String.class)); return recentlyCreatedChildList.get(); } return null; } }, null); v.setContentProvider(contentProvider); v.setComparator(new ViewerComparator()); v.setInput(input); v.getTree().addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.character == 'q') { rootElementList.set(0, "root " + new Random().nextInt()); } if (e.character == 'w') { IObservableList<String> childElementsList = recentlyCreatedChildList.get(); if (childElementsList != null) { childElementsList.set(0, "child " + new Random().nextInt()); } else { System.out.println("no children list present"); } } } }); }</code><technical_language>lambda</technical_language>

Target:
public Snippet064TreeViewerReplacingElements(Shell shell) { final Composite c = new Composite(shell, SWT.NONE); c.setLayout(new FillLayout(SWT.VERTICAL)); Label l = new Label(c, SWT.NONE); l.setText( "The elements are ordere lexicografically, i.e. 11 comes before 2,\nPress q, to rename one root element.\nPress q, to rename one child element."); final TreeViewer v = new TreeViewer(c); Object[] rootElements = new Object[] { "root 1", "root 2", "root 3" }; Object input = new Object(); AtomicReference<IObservableList<String>> recentlyCreatedChildList = new AtomicReference<>(); final IObservableList<String> rootElementList = new WritableList(DisplayRealm.getRealm(shell.getDisplay()), Arrays.asList(rootElements), String.class); ITreeContentProvider contentProvider = new ObservableListTreeContentProvider(target -> { if (target == input) return rootElementList; if (target.toString().startsWith("root")) { recentlyCreatedChildList.set(new WritableList<>(DisplayRealm.getRealm(shell.getDisplay()), Arrays.asList(new String[] { "child 1", "child 2" }), String.class)); return recentlyCreatedChildList.get(); } return null; }, null); v.setContentProvider(contentProvider); v.setComparator(new ViewerComparator()); v.setInput(input); v.getTree().addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.character == 'q') { rootElementList.set(0, "root " + new Random().nextInt()); } if (e.character == 'w') { IObservableList<String> childElementsList = recentlyCreatedChildList.get(); if (childElementsList != null) { childElementsList.set(0, "child " + new Random().nextInt()); } else { System.out.println("no children list present"); } } } }); }