2012-07-20 86 views
0

用於Eclipse的JDT和其他插件用問題狀態(編譯錯誤等)修飾編輯器標題圖像。在我的插件中,我想模仿這種行爲。在Eclipse編輯器中修飾編輯器標題圖像的簡單方法?

不過,看着資料來源,JDT似乎做了很多額外的處理來做裝飾。

Decorators,尤其是輕量級的同上,是一種在圖標上進行裝飾的方便方式,但我無法以編程方式爲編輯器的標題圖像啓用它們。 (我不希望在所有的JDT UI來拉我的插件...)

有這樣或者我需要實現我自己ILabelProvider然後

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

像JavaEditor呢?

回答

1

似乎是沒有辦法用裝飾與編輯標題圖像(如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; 
} 

這是我能想出的最簡單的解決方案。

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

+0

我已經有了。它很好地裝飾導航器中的資源。但不是*編輯器* titel圖像。我已經編輯了問題標題來清除這個細節(這已經在問題中了)。 – thoni56 2012-07-20 14:58:28

+0

你有自定義的EditorPart嗎?您可以重寫addPage()並通過創建一個新頁面給編輯器來將自定義圖像添加到CFolderItem。 (請參閱MultiPageEditorPart的實現)addPage()方法調用createItem(),它只創建一個CTabItem。 – aphex 2012-07-25 13:57:13