Динамическое изменение значка панели инструментов Eclipse - PullRequest
3 голосов
/ 05 октября 2011

У меня есть элемент панели инструментов с собственным значком, определенным в файле plugin.xml, например:

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

Как мне изменить это динамически при необходимости? Я имею в виду изменение его из кода

Ответы [ 2 ]

5 голосов
/ 05 октября 2011

Вместо этого используйте org.eclipse.ui.menus точку расширения и добавьте menuContribution с dynamic . Класс из динамический должен подкласс ControlContribution и реализовать метод createControl для создания кнопки.

3 голосов
/ 19 февраля 2016

Вы должны implements IElementUpdater в вашем Handler классе.
Пожалуйста, обратитесь к: 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);
        }
    

Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...