Search code examples
javaeclipseeclipse-plugineclipse-rcprcp

Eclipse RCP view will not update when editor data is modified


I'm attempting to do something that seems like it should be quite common, so I'm surprised I'm having a hard time finding a solution.

I have a simple Eclipse RCP application. It consists of a view, containing a treeViewer that displays elements of an xml file hierarchically. The other side is an editor, which contains various fields such as textboxes, etc, for displaying and also modifying the xml values. The treeviewer displays icons alongside the element name, and what I'm trying to do is change the icon to a "modified" version of the icon whenever a change is made in the editor - signifying that a value of that element has been changed. This is very similar to how Eclipse, when integrated with subversion, shows that a file has been modified from the base revision in the Package Explorer.

I'll try to just show the parts of the code relevant to this specific issue and hope I don't leave anything out. This is the editor class:

public class XmlEditor extends EditorPart
{
    protected boolean dirty = false;

    public void setDirty(boolean value)
    {
        dirty = value;
        firePropertyChange(PROP_DIRTY);
    }
}

and this is the view with the tree:

public class TreeView extends ViewPart implements IPropertyChangeListener {
    public void createPartControl(Composite parent) {
        treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        getSite().setSelectionProvider(treeViewer);

        treeViewer.setLabelProvider(new TreeObjLabelProvider());
        treeViewer.setContentProvider(new TreeObjContentProvider());

        PlatformUI.getWorkbench().getWorkingSetManager().addPropertyChangeListener(this);
    }

    public void propertyChange(PropertyChangeEvent event) {
        if (event.getProperty().equals(IWorkbenchPartConstants.PROP_DIRTY)) {
            treeViewer.refresh();
        }
    }
}

In this scenario, TreeView::propertyChange() is not getting called, even though firePropertyChange() is getting fired. Any ideas why? I'm also open to any other ideas that don't involve PropertyChangeListener, it just seemed like this would be the easiest way at the time. Thank you.


Solution

  • Here's how I ended up solving the problem:

    1. Changed TreeView to implement IPropertyListener instead of IPropertyChangeListener
    2. Implemented the propertyChanged() method to perform a treeViewer.refresh()
    3. In the XmlEditor::createPartControl() method I got a reference to the Treeview part and then added it to the property listeners like so:
      • TreeView treeView = (TreeView) getSite().getPage().findView(TreeView.ID);
      • addPropertyListener(treeView);

    Now, TreeView::propertyChanged() gets called after firePropertyChanged(), just like I needed. It took quite a bit of experimentation to learn the difference between IPropertyListener and IPropertyChangeListener, addPropertyListener() and addPartPropertyListener().

    Thanks to nitind for giving me a new perspective, and for showing me about Decorators, which is definitely the right way to go as opposed to changing the tree icon to a modified version.