Search code examples
javaswteclipse-rcpjfacetreeviewer

How to set selection for treeviewer without firing selectionChanged()?


I am working on an eclipse RCP project. It has several views with TreeViewer and I use selectionChanged() method to handle selection events. However, at times I need to set selection programmatically.For this, in selectionChanged() method, I invoke setSelection() method of TreeViewer to set the desired selection. This method fires selectionChanged() method of all views thus resulting in cyclic calls to selectionChanged().

How can I select an item from the TreeViewer or StructuredViewer without firing selectionChanged() for other views?


Solution

  • Well, creating an event that triggers the Listener you're currently in is always a tricky situation. What I usually do is something along those lines:

    Listener listener = new Listener()
    {
        private boolean ignore = false;
    
        @Override
        public void handleEvent(Event e)
        {
            if(ignore)
                return;
    
            ignore = true;
            doPotentiallyCyclicStuff();
            ignore = false;
        }
    };
    

    It's not a very pretty solution, but it does work.


    Looking forward to alternative solutions here, since this has been bothering me for a while now.