[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform] Re: Action accelerators

A description of the new framework can be found in the EclipseCon 2006 presentation at http://www.magma.ca/~pollockd/despumate.html#eclipsecon2006

We're going to work this summer on updating the "Contributing Actions to the Workbench" article, but it hasn't been updated yet.

This code works for me. It defines a command, then a handler for that command. Then it defines a context and a keybinding for that command+context.

The keybinding works for me within my sample view, and is disabled outside it (which is what I wanted).

It's not the most robust piece of code, so be careful. Amongst other things, there's no protection for adding the same keybinding->same command over and over again. This code comes with no warranty to its fitness or sanity :-)

		try {
			ICommandService commandService = (ICommandService) getSite()
					.getService(ICommandService.class);
			IHandlerService handlerService = (IHandlerService) getSite()
					.getService(IHandlerService.class);
			IBindingService bindingService = (IBindingService) getSite()
					.getService(IBindingService.class);
			IContextService contextService = (IContextService) getSite()
					.getService(IContextService.class);

			Category editCat = commandService
					.getCategory("org.eclipse.ui.category.edit");
			String commandId = "z.ex.view.SCommand";
			Command scmd = commandService.getCommand(commandId);
			if (!scmd.isDefined()) {
				scmd.define("SCommand", "Run the SCommand", editCat);
			}

			IHandler handler = new AbstractHandler() {
				public Object execute(ExecutionEvent event)
						throws ExecutionException {
					System.out.println("The Handler has landed!");
					return null;
				}
			};

			handlerService.activateHandler(commandId, handler);

			// now set up the keybindings
			String sampleContextId = "sampleViewContext";
			String parentContextId = "org.eclipse.ui.contexts.window";

			Context sampleContext = contextService.getContext(sampleContextId);
			if (!sampleContext.isDefined()) {
				sampleContext.define("Sample Context", "My Sample Context",
						parentContextId);
			}
			contextService.activateContext(sampleContextId);

String defaultSchemeId = "org.eclipse.ui.defaultAcceleratorConfiguration";
Scheme defaultScheme = bindingService.getScheme(defaultSchemeId);


			ParameterizedCommand pscmd = new ParameterizedCommand(scmd, null);

			KeySequence keySequence = KeySequence.getInstance("CTRL+ALT+.");
			Binding newKey = new KeyBinding(keySequence, pscmd,
					defaultSchemeId, sampleContextId, null, null, null,
					Binding.USER);

			Binding[] bindings = bindingService.getBindings();
			Binding[] newBindings = new Binding[bindings.length + 1];
			newBindings[0] = newKey;
			System.arraycopy(bindings, 0, newBindings, 1, bindings.length);
			bindingService.savePreferences(defaultScheme, newBindings);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

Later,
PW