Saving TreeViewer state before setInput()

eclipse, java, jface, treeviewer, user-interface

Solution

You need to make sure that your TreeViewer's content provider provides objects that have their `hashCode` and `equals` methods properly defined. `AbstractTreeViewer` needs to be able to compare the old and new objects to determine their expansion state. If `hashCode` and `equals` aren't provided, it's a simple reference check, which won't work if you've recreated your contents.

Problem

I'm trying to save JFace `TreeViewer` expansion state to refresh it after calling `setInput()` method. I tried `getExpandedElements`, `setExpandedElements`, `getExpandedTreePaths`, `setExpandedTreePaths` methods of `TreeViewer` but it doesn't work. ``` Object[] expandedElements = viewer.getExpandedElements(); TreePath[] expandedTreePaths = viewer.getExpandedTreePaths(); viewer.setInput(); viewer.setExpandedElements(expandedElements); viewer.setExpandedTreePaths(expandedTreePaths); ```

Original source