Eclipse插件之動态重新整理ToolBar的圖檔
擴充Eclipse的快捷工具欄的時候,經常會碰到一些類似于開關的問題,比如需要根據某個狀态或開關量來顯示不同的圖檔,但是IHandler或AbstractHandler隻給出了定義回調的執行函數,并沒有給出操作UI相關的方法,其實隻需要實作一個接口IElementUpdater即可,閑話少說,直接給出代碼。
public class TestAction extends AbstractHandler implements IElementUpdater{
public static boolean isOn = false;
private static ImageDescriptor IMAGE_ON = UIActivator.getImageDescriptor("icons/on.gif");
private static ImageDescriptor IMAGE_OFF = UIActivator.getImageDescriptor("icons/off.gif");
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
if(!isOn){
System.out.println("on");
isOn = true;
}else{
System.out.println("off");
isOn = false;
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
ICommandService commandService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
if(commandService != null){
commandService.refreshElements("com.zj.test.ui.commands.on", null);
}
}
});
return null;
}
@Override
public void updateElement(UIElement element, Map parameters) {
if(!isOn){
element.setIcon(IMAGE_ON);
element.setTooltip("on");
}else{
element.setIcon(IMAGE_OFF);
element.setTooltip("off");
}
}
}