[英]Change Eclipse toolbar icon dynamically
我有一個帶有自己圖標的工具欄項,在plugin.xml文件中定義如下:
<action
class="MyActionClass"
id="MyActionID"
label="MyActionLabel"
menubarPath="MyActionMenuBarPath"
toolbarPath="MyActionToolBarPath"
icon="icon/myicon.png" <---- this one
...
</action>
如何在需要時動態更改此設置? 我的意思是從代碼中改變它
請改用org.eclipse.ui.menus
擴展點,並使用dynamic添加menuContribution 。 類 動態的子類應該ControlContribution
和實施createControl
方法來創建一個按鈕。
您應該在Handler
類中implements
IElementUpdater
。
請參考: https : //stackoverflow.com/a/23742598/2893073
處理程序類
import java.util.Map; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.commands.IElementUpdater; import org.eclipse.ui.menus.UIElement; import com.packpub.e4.menu.Activator; public class SampleHandler2 extends AbstractHandler implements IElementUpdater{ private static ImageDescriptor image_enable = Activator.getImageDescriptor("icons/btn_adapt_enable.png"); private static ImageDescriptor image_disable = Activator.getImageDescriptor("icons/btn_adapt_disable.png"); /** * The constructor. */ public SampleHandler2() { } /** * the command has been executed, so extract extract the needed information * from the application context. */ public Object execute(ExecutionEvent event) throws ExecutionException { //... return null; } @Override public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map map) { boolean condition = false; //... if( condition ) { element.setIcon(image_disable); }else{ element.setIcon(image_enable); } } }
使用ICommandService
調用此Handler:
IWorkbenchWindow window = part.getSite().getWorkbenchWindow(); ICommandService commandService = (ICommandService) window.getService(ICommandService.class); if (commandService != null) { commandService.refreshElements("com.packpub.e4.menu.commands.sampleCommand", null); }
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.