如何更新文件保存装饰器?

时间:2016-07-31 07:32:52

标签: eclipse eclipse-plugin eclipse-rcp decorator

我创建了一个带有装饰器的插件,它将当前文件作为输入并显示适当的装饰器。

这是正常工作,并且当日食首次加载时装饰器正确显示。

但是,当文件更改时,装饰器不会更新。

每次修改文件时如何更新装饰器?即如何在文件保存事件上更新装饰器?

这是示例代码

public class Decorator implements ILightweightLabelDecorator {

    private final ImageDescriptor OVERLAY1 = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/1.png");
    private final ImageDescriptor OVERLAY2 = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/2.png");

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
     */
    @Override
    public void addListener(ILabelProviderListener listener) {
        // TODO Auto-generated method stub
        // IResourceChangeEvent.POST_CHANGE

    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
     */
    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
     */
    @Override
    public boolean isLabelProperty(Object element, String property) {
        // TODO Auto-generated method stub
        return false;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
 */
    @Override
    public void removeListener(ILabelProviderListener listener) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.ILightweightLabelDecorator#decorate(java.lang.Object, org.eclipse.jface.viewers.IDecoration)
     */
    @Override
    public void decorate(Object element, IDecoration decoration) {
        if (some_condition)
            decoration.addOverlay(OVERLAY1);
        else
            decoration.addOverlay(OVERLAY2);
    }
}

1 个答案:

答案 0 :(得分:2)

您可以要求IDecoratorManager使用以下内容更新具有特定ID的装饰品:

IDecoratorManager decoratorManager = PlatformUI.getWorkbench().getDecoratorManager();

decoratorManager.update("decorator id");

要在文件保存上执行此操作,您必须使用IResourceChangeListener来监听工作区资源更改,并在看到文件发生更改时做出反应。

使用以下命令设置监听器:

ResourcesPlugin.getWorkspace().addResourceChangeListener(listener);

IResourceChangeListener中,您可以执行以下操作:

public void resourceChanged(IResourceChangeEvent event) {
    IResourceDelta delta = event.getDelta();

    IResourceDelta fileDelta = delta.findMember(IPath of file you are interested in);

    if (fileDelta != null) {
       // TODO handle the delta
    }
}