Search code examples
swteclipse-rcprcp

RCP overrided Composite dispose method not called


I have created my own compound composite by extending the Composite class in a e4 application.

I have then override the dispose method but it never gets called. However if i add a dispose listener on my compound composite or any of the widgets that it contains the listener do get called.

Could somebody explain my why?

Here would be an example.

public class MyComposite extends Composite{
public MyComposite(){
    this.addDisposeListener(new DisposeListener(){

        @Override
        public void widgetDisposed(DisposeEvent e) {
            System.out.println("This get printed");
        }
    });
}

@Override
public void dispose(){
    System.out.println("This is not printed");
    super.dispose();
}
}

I have read the javadoc for the dispose method and it says:

NOTE: This method is not called recursively on the descendants of the receiver. This means that, widget implementers can not detect when a widget is being disposed of by re-implementing this method, but should instead listen for the Dispose event.

For the behaviour i am seeing and the javadoc above, seems that the dispose method can not be override because it is ignored. Is this interpretation correct?


Solution

  • For the behaviour i am seeing and the javadoc above, seems that the dispose method can not be override because it is ignored. Is this interpretation correct?

    "Can not be overridden" is too strong, since technically you can override it, but basically you are correct. Overriding it does not have the desired effect as you have seen.

    To handle disposal in a Composite, the DisposeListener is the correct way. See also this question: Eclipse RCP: How and when to correctly unsubscribe a Composite from EventBroker?