似乎是沒有辦法用裝飾與編輯標題圖像(如3.7至少)。
我最終創建了一個EditorLabelUpdator,它實現了IResourceChangeListener接口(找出標記何時更改),基本上是resourceChanged()方法。然後它使用一個簡單的裝飾類型類,它是從OverlayImageIcon模式構建而成的(您可以在Google上找到很多示例)。
構造函數從編輯器的initalization中調用,並將編輯器作爲參數保存以獲取資源及其標題圖標。
編輯器還必須使用觸發標題圖標更新的回調方法進行修改(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;
}
這是我能想出的最簡單的解決方案。
我已經有了。它很好地裝飾導航器中的資源。但不是*編輯器* titel圖像。我已經編輯了問題標題來清除這個細節(這已經在問題中了)。 – thoni56 2012-07-20 14:58:28
你有自定義的EditorPart嗎?您可以重寫addPage()並通過創建一個新頁面給編輯器來將自定義圖像添加到CFolderItem。 (請參閱MultiPageEditorPart的實現)addPage()方法調用createItem(),它只創建一個CTabItem。 – aphex 2012-07-25 13:57:13