Enhanced RCP: Reuse of WritableList in JFace-Viewers

Just because I’ve been running into this today. I thought it might be interesting to others as well. The following is a shortened version of a code I’ve been using in an RCP-Application.

public class CreateItemsHandler {
   public void execute(IObservableList companies, 
                      @Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
       DialogImpl i = new DialogImpl(shell,companies);

   class DialogImpl extends TitleAreaDialog {
      private final IObservableList companies;

      // ....
      public Composite createDialogArea(Composite parent) {
        // ....
        TreeViewer v; 
        // ....
          new ObservableListTreeContentProvider(new ObservableFactoryImpl(),
          new StructureAdvisorImpl())

Does someone spot the error? I guess not because from it is not visible at all without looking into the implementation of ObservableListTreeContentProvider. The problem in there is that when the viewer is disposed it disposes also the IObservableList passed into it so when the handler is executed the second time you’ll run into problems.

The solution to the problem though is quite simple by making the input call look like this:

 v.setInput(new DecoratingObservableList(companies,false));

This way your original IObservableList is not disposed but only the decorator.

This entry was posted in Enhanced RCP. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.