eclipseswteclipse-rcpjfaceeclipse-databinding

Can ViewerSupport.bind() be used in conjunction with images?


In my Eclipse RCP View, I use JFace databinding to bind model data to a org.eclipse.jface.viewers.TreeViewer. I use ViewerSupport.bind() to bind the data as shown in the snippets, and it works just fine.

ViewerSupport.bind(viewer, model, BeanProperties.list("children",
          ModelObject.class), BeanProperties.value(ModelObject.class, "name"));

Now I'd really like to display an image along with the text on the nodes. Without databinding, I'd just override a LabelProvider's getImage(Object element) method, but the content + label provider method and the use of ViewerSupport don't work together. I haven't found a solution to my problem in the ViewerSupport API. Is there a solution, perhaps using org.eclipse.core.databinding.property.value.IValuePropertys?


Solution

  • ViewerSupport uses ObservableMapLabelProvider as the label provider. Unfortunately the getImage method for this just returns null.

    You could perhaps uses a sub-class of ObservableMapLabelProvider to provide the images. You can't use ViewerSupport to do that so you would have to duplicate some of the code.

    This is how ViewerSupport.bind sets up the tree:

    public static void bind(AbstractTreeViewer viewer, Object input,
            IListProperty childrenProperty, IValueProperty[] labelProperties) {
      Realm realm = DisplayRealm.getRealm(viewer.getControl().getDisplay());
    
      ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
                childrenProperty.listFactory(realm), null);
    
      if (viewer.getInput() != null)
        viewer.setInput(null);
    
      viewer.setContentProvider(contentProvider);
    
      viewer.setLabelProvider(new ObservableMapLabelProvider(Properties
                .observeEach(contentProvider.getKnownElements(),
                        labelProperties)));
    
      if (input != null)
        viewer.setInput(input);
    }