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 {
   @Execute
   public void execute(IObservableList companies, 
                      @Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
       DialogImpl i = new DialogImpl(shell,companies);
       i.open();
   }

   class DialogImpl extends TitleAreaDialog {
      private final IObservableList companies;

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

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.

Advertisements
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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s