在Eclipse编辑器中装饰编辑器标题图像的简单方法?

时间:2012-07-20 09:29:58

标签: eclipse eclipse-rcp eclipse-plugin

Eclipse的JDT和其他插件用问题状态(编译错误等)装饰编辑器标题图像。在我的插件中,我想模仿这种行为。

然而,从资料来看,JDT似乎做了很多额外的处理来进行装饰。

Decorators,特别是轻量级的同上,是一种在图标上进行装饰的便捷方式,但是我找不到以编程方式为编辑器的标题图像编程的方法。 (而且我不想在我的插件中引入所有JDT UI ...)

是否有这样的方式或者我需要实现我自己的ILabelProvider然后

public void updatedTitleImage(Image image) {
    setTitleImage(image);
}

就像JavaEditor一样吗?

2 个答案:

答案 0 :(得分:1)

似乎无法在编辑器标题图像中使用装饰器(至少从3.7开始)。

我最终创建了一个EditorLabelUpdator,它实现了IResourceChangeListener接口(以找出更改标记的时间),基本上是 resourceChanged()方法。然后它使用一个简单的装饰器类,它是从OverlayImageIcon模式构建的(你可以在Google上找到很多例子)。

从编辑器的初始化调用构造函数,将编辑器作为参数发送,保存该参数以获取资源及其标题图标。

编辑器也必须使用回调方法进行修改,触发标题图标更新( updatedTitleImage(Image image))。

这是我得到的代码的核心:

public void resourceChanged(IResourceChangeEvent event) {
    if (isMarkerChangeForResource(event, editor)) {
        /* Changes in markers on this resource, so re-decorate title image */
        decorate();
    }
}

private boolean isMarkerChangeForResource(IResourceChangeEvent event, AlanEditor editor) {
    boolean isMarkerChangeForThisResource;
    final IResource resource = ResourceUtil.getResource(editor.getEditorInput());
    final IPath path = resource.getFullPath();
    IResourceDelta delta = event.getDelta().findMember(path);
    isMarkerChangeForThisResource = (delta != null) && ((delta.getFlags() & IResourceDelta.MARKERS) != 0);
    return isMarkerChangeForThisResource;
}

public void decorate() {
    Shell shell = editor.getEditorSite().getShell();
    if (shell != null && !shell.isDisposed()) {
        shell.getDisplay().syncExec(new Runnable() {
            public void run() {
                Image decoratedImage = decorateImage(editor.getTitleImage(), getSeverity());
                editor.updatedTitleImage(decoratedImage);
            }
        });
    }
}

private Image decorateImage(Image titleImage, int severity) {
    final ImageRegistry registry = AlanIDEPlugin.getDefault().getImageRegistry();
    String key = createKey(severity);
    ImageDescriptor descriptor = AlanIDEPlugin.getImageDescriptor(key);
    if (descriptor != null)
        return descriptor.createImage();

    OverlayImageDescriptor overlayImageDescriptor = buildDecoratedImage(severity, key);
    registry.put(key, overlayImageDescriptor);
    return overlayImageDescriptor.createImage();
}

private String createKey(int severity) {
    String key;
    switch (severity) {
    case IMarker.SEVERITY_ERROR: key = EDITOR_TITLE_ICON + ".error"; break;
    case IMarker.SEVERITY_WARNING: key = EDITOR_TITLE_ICON + ".warning"; break;
    default: key = EDITOR_TITLE_ICON; break;
    }
    return key;
}

private OverlayImageDescriptor buildDecoratedImage(int severity, String key) {
    ImageDescriptor overlay = null;
    if (severity >= IMarker.SEVERITY_ERROR)
        overlay = AlanIDEPlugin.getImageDescriptor("ovr16.error_ovr");
    else if (severity == IMarker.SEVERITY_WARNING)
        overlay = AlanIDEPlugin.getImageDescriptor("ovr16.warning_ovr");
    ImageDescriptor baseImage = AlanIDEPlugin.getImageDescriptor(EDITOR_TITLE_ICON);
    OverlayImageDescriptor overlayIcon = new OverlayImageDescriptor(baseImage);

    if (overlay != null)
        overlayIcon.addOverlay(overlay, IDecoration.BOTTOM_LEFT);
    return overlayIcon;
}

private int getSeverity() {
    int severity = 0;
    try {
        final IResource resource = ResourceUtil.getResource(editor.getEditorInput());
        severity = resource.findMaxProblemSeverity(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
    } catch (CoreException e) {
        // Might be a project that is not open
    }
    return severity;
}

这是我能想到的最简单的解决方案。

答案 1 :(得分:0)

是的,有一个扩展点可供您实现。请参阅以下代码:

 <!--define the decorator -->
<extension point="org.eclipse.ui.decorators">
    <decorator
    adaptable="true"
    class="org.example.PackageExplorerDecorator"
    id="org.example.PackageExplorerDecorator"
    label="File Decorator"
    lightweight="true"
    state="true">
 <enablement>
    <or>
       <objectClass
             name="org.eclipse.jdt.core.IMethod">
       </objectClass>
       <objectClass
             name="org.eclipse.core.resources.IResource">
       </objectClass>
    </or>
 </enablement>

对于PackageExplorerDecorator:extends LabelProvider implements ILightweightLabelDecorator