2011-07-09 27 views
3

我想創建一個簡單的eclipse插件,它的確如下:當你右鍵點擊一個java項目時,它會顯示一個彈出式菜單,其中有一個項目有標籤「N java files found在這個項目中「,其中」N「是文件數量。eclipse插件開發中彈出動作的動態標籤

我有一個想法,我可以「的SelectionChanged」更新標籤:

public class CountAction implements IObjectActionDelegate { 
    public void selectionChanged(IAction action, ISelection selection) { 
     action.setText(countJavaFiles()); 
    } 
} 

但是,如果我不點擊該菜單項,因爲CountAction尚未加載它不工作,當您右鍵單擊該項目時,將不會調用selectionChanged

我在這方面花了很多時間,但沒有解決。請幫幫我。

回答

0

最後,我找到了實現這個一個非常簡單的方法:

我不需要改變我的代碼(有問題的代碼示例),但我需要添加一個小startup類:

import org.eclipse.ui.IStartup; 

public class MyStartUp implements IStartup { 

    @Override 
    public void earlyStartup() { 
     // Initial the action 
     new CountAction(); 
    } 
} 

,並添加以下內容plugin.xml

<extension 
    point="org.eclipse.ui.startup"> 
    <startup 
     class="myplugin.MyStartUp"> 
    </startup> 

這個MyStartUp將在啓動時加載該動作的一個實例,然後當我右鍵單擊項目或文件時,將會調用selectionChanged

2

由@kett_chup建議的文章的替代,是使用IElementUpdater。只要

  • handler必須實現IElementUpdater
  • handler.updateElement((UIElement element, Map parameters)必須設置使用element.setText("new text")想要的文字 - 這一新文本將在菜單和工具欄顯示
  • 只要你需要/想更新的命令文本使用ICommandService.refreshElements(String commandId, Map filter)與您的特定命令ID - 全局命令服務通常就好

IElementUpdater接口也可用於更改檢查狀態 - 對於帶有style=toggle的命令 - 以及圖標和工具提示。

+0

我跟着@ kett_chup的答案,它的工作原理。我對你的回答很感興趣,但我不明白如何實現它。例如,這裏的'處理程序'是'MyHandler'嗎?還有其他的東西需要嗎(CompoundContributionItem,命令)? – Freewind

+0

我會盡力在今晚晚些時候寫一個例子... –

+0

非常感謝你! – Freewind