【问题标题】:Change Eclipse toolbar icon dynamically动态更改 Eclipse 工具栏图标
【发布时间】:2011-10-04 20:46:26
【问题描述】:

我有一个带有自己图标的工具栏项,在 plugin.xml 文件中定义,例如:

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

如何在需要时动态更改它?我的意思是从代码中改变它

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

    改用org.eclipse.ui.menus 扩展点并添加menuContributiondynamicdynamicclass 应该继承ControlContribution 并实现createControl 方法来创建按钮。

    【讨论】:

      【解决方案2】:

      你应该在 Handler 班级中 implements IElementUpdater
      请参考:https://stackoverflow.com/a/23742598/2893073

      1. 处理类

        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. 使用ICommandService调用这个处理程序:

            IWorkbenchWindow window = part.getSite().getWorkbenchWindow();
            ICommandService commandService = (ICommandService) window.getService(ICommandService.class);
            if (commandService != null) {
                commandService.refreshElements("com.packpub.e4.menu.commands.sampleCommand", null);
            }
        

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        相关资源
        最近更新 更多