eclipse命令框架(二)

在代码中创建命令

 

1.获取serviceLocator,针对不同的控件进行获取

 

(1)获取工作台的serviceLocator

 

               IWorkbench serviceLocator = UIPlugin.getDefault().getWorkbench();
 

(2)获取视图的serviceLocator

 

               getSite()

 

2.创建Command,并为其绑定实现

 

 

               ICommandService service = (ICommandService) serviceLocator.getService(ICommandService.class);

		Category category = service.getCategory("com.leng.category");
		if (!category.isDefined()) {
			category.define("name", "description");
		}

		Command command = service.getCommand("com.leng.command");
		if (!command.isDefined()) {
			command.define("name", "description", category);
		}
		
		IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class); 
		handlerService.activateHandler(command.getId(), new AbstractHandler() {
		    public Object execute(ExecutionEvent event) throws ExecutionException { 
		        System.out.println("Command executed !"); 
		        return null; 
		    } 
		});

 

3.为command绑定图片

 

 ICommandImageService imageService = (ICommandImageService) serviceLocator.getService(ICommandImageService.class);
		imageService.getImageDescriptor(command.getId()).createFromImage(null);

 

4.绑定key,参考http://www.blogjava.net/baallee/articles/47049.html

 

你可能感兴趣的:(eclipse)