Bug 161805 [Viewers] JFace TreeViewer collapses node when sibbling removed
3.2 1. Use the Eclipse Wizard to produce a plugin with a tree viewer view. 2. Paste the modified SampleView.java - this looks like the left part of the attached screenshot 3. Click on the top node Parent1 and type - 4. TreeViewer.remove is called to remove Parent 1. TreeViewer.refresh is called - this looks like the right part of the attached screenshot Wrong: node Parent 4 is now collapsed. Expectation Parent 4 is still expanded. Is there any workaround? Am I misusing your API? Note. The snippet is almost identical to the code generated by PDE for plugin / view / Tree Viewer. Here is the extra code in createPartControl: viewer.getTree().addKeyListener(new KeyListener() { public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } public void keyPressed(KeyEvent e) { if (e.character == '-') { TreeParent parent = (TreeParent) ((IStructuredSelection)viewer.getSelection()).toArray()[0]; parent.getParent().removeChild(parent); viewer.refresh(); } if (e.character == '+') { TreeParent parent = (TreeParent) ((IStructuredSelection)viewer.getSelection()).toArray()[0]; parent.addChild(new TreeParent(new Date().toString())); } } });
bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java
/**
	 * This implementation of setSelectionToWidget accepts a list of elements or
	 * a list of tree paths.
	 */
	protected void setSelectionToWidget(List v, boolean reveal) {
		if (v == null) {
			setSelection(new ArrayList(0));
			return;
		}
		int size = v.size();
		List newSelection = new ArrayList(size);
		for (int i = 0; i < size; ++i) {
			Object elementOrTreePath = v.get(i);
			
			
			Widget w = internalExpand(elementOrTreePath, false);
			if (w instanceof Item) {
				newSelection.add(w);
			} else if (w == null && elementOrTreePath instanceof TreePath) {
				TreePath treePath = (TreePath) elementOrTreePath;
				Object element = treePath.getLastSegment();
				if (element != null) {
					w = internalExpand(element, false);
					if (w instanceof Item) {
						newSelection.add(w);
					}
				}
			}
		}
		setSelection(newSelection);