简体   繁体   English

动态更改Eclipse工具栏图标

[英]Change Eclipse toolbar icon dynamically

I've got a toolbar item with its own icon, defined in the plugin.xml file like: 我有一个带有自己图标的工具栏项,在plugin.xml文件中定义如下:

<action
class="MyActionClass"
id="MyActionID"
label="MyActionLabel"
menubarPath="MyActionMenuBarPath"
toolbarPath="MyActionToolBarPath"
icon="icon/myicon.png" <---- this one
     ...
</action>

How do I change this dynamically when needed? 如何在需要时动态更改此设置? I mean changing it from code 我的意思是从代码中改变它

Use org.eclipse.ui.menus extension point instead and add menuContribution with dynamic . 请改用org.eclipse.ui.menus扩展点,并使用dynamic添加menuContribution The class of dynamic should subclass ControlContribution and implement createControl method to create a button. 动态的子类应该ControlContribution和实施createControl方法来创建一个按钮。

You should implements IElementUpdater in your Handler class. 您应该在Handler类中implements IElementUpdater
Please refer to : https://stackoverflow.com/a/23742598/2893073 请参考: https//stackoverflow.com/a/23742598/2893073

  1. Handler class 处理程序类

     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); } } } 
  2. invoke this Handler using ICommandService : 使用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); } 

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM