diff options
Diffstat (limited to 'bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/Installer.java')
-rw-r--r-- | bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/Installer.java | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/Installer.java b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/Installer.java new file mode 100644 index 00000000..d175ba4b --- /dev/null +++ b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/Installer.java @@ -0,0 +1,137 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.e4.tools.event.spy; + + +import javax.inject.Inject; + +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.tools.event.spy.handlers.OpenSpyDialogHandler; +import org.eclipse.e4.tools.event.spy.util.LoggerWrapper; +import org.eclipse.e4.tools.event.spy.util.PluginUtils; +import org.eclipse.e4.ui.model.application.MApplication; +import org.eclipse.e4.ui.model.application.commands.MBindingTable; +import org.eclipse.e4.ui.model.application.commands.MCommand; +import org.eclipse.e4.ui.model.application.commands.MCommandsFactory; +import org.eclipse.e4.ui.model.application.commands.MHandler; +import org.eclipse.e4.ui.model.application.commands.MKeyBinding; + +@SuppressWarnings("restriction") +public class Installer { + @Inject + private MApplication application; + + @Inject + private LoggerWrapper logger; + + @Execute + public void execute() { + logger.info("installing ..."); + + registerCommand(CommandDescriptor.OpenSpyDialog, BindingDescriptor.OpenSpyDialogInDialogAndWindow, OpenSpyDialogHandler.class); + + logger.info("installed"); + } + + private void registerCommand(CommandDescriptor commandDesc, BindingDescriptor bindingDesc, Class<?> handlerCls) { + MCommand command = getCommand(application, commandDesc); + if (command == null) { + command = createCommand(commandDesc); + application.getCommands().add(command); + } + + Object binding = getBindingOrBindingTable(application, command, bindingDesc); + if (binding == null) { + logger.warn("binding context ''{0}'' for command ''{1}'' not found", + bindingDesc.getBindingContextId(), commandDesc.getName()); + return; + } + + if (binding instanceof MKeyBinding) { + MKeyBinding keyBinding = (MKeyBinding) binding; + if (keyBinding.getTags().contains(Constants.BINDING_MODIFIED_BY_USER_TAG)) { + logger.info("key binding for command ''{0}'' changed to {1}", commandDesc.getName(), keyBinding.getKeySequence()); + + } else if (keyBinding.getTags().contains(Constants.BINDING_DELETED_BY_USER_TAG)) { + logger.info("key binding for command ''{0}'' has been deleted. The command is disabled", commandDesc.getName()); + } + return; //command already processed + } + + ((MBindingTable) binding).getBindings().add(createKeyBinding(command, bindingDesc)); + logger.info("key binding for command ''{0}'' is {1}", commandDesc.getName(), bindingDesc.getKeySequence()); + + MHandler handler = getHandler(application, commandDesc); + if (handler == null) { + handler = createHandler(command, handlerCls); + application.getHandlers().add(handler); + } + } + + private MCommand getCommand(MApplication application, CommandDescriptor descriptor) { + for (MCommand command: application.getCommands()) { + if (descriptor.getId().equals(command.getElementId())) { + return command; + } + } + return null; + } + + private MCommand createCommand(CommandDescriptor descriptor) { + MCommand command = MCommandsFactory.INSTANCE.createCommand(); + command.setElementId(descriptor.getId()); + command.setCommandName(descriptor.getName()); + command.setContributorURI(PluginUtils.getContributorURI()); + return command; + } + + private MHandler getHandler(MApplication application, CommandDescriptor descriptor) { + for (MHandler handler: application.getHandlers()) { + if (descriptor.getId().equals(handler.getElementId())) { + return handler; + } + } + return null; + } + + private MHandler createHandler(MCommand command, Class<?> handlerCls) { + MHandler handler = MCommandsFactory.INSTANCE.createHandler(); + handler.setElementId(command.getElementId()); + handler.setCommand(command); + handler.setContributionURI(PluginUtils.getContributionURI(handlerCls)); + handler.setContributorURI(PluginUtils.getContributorURI()); + return handler; + } + + private Object getBindingOrBindingTable(MApplication application, MCommand command, BindingDescriptor descriptor) { + MBindingTable result = null; + for (MBindingTable bindingTable: application.getBindingTables()) { + for (MKeyBinding keyBinding : bindingTable.getBindings()) { + if (keyBinding.getCommand() == command) { + return bindingTable; + } + } + if (descriptor.getBindingContextId().equals(bindingTable.getBindingContext().getElementId())) { + result = bindingTable; + } + } + return result; + } + + private MKeyBinding createKeyBinding(MCommand command, BindingDescriptor descriptor) { + MKeyBinding keyBinding = MCommandsFactory.INSTANCE.createKeyBinding(); + keyBinding.setElementId(command.getElementId()); + keyBinding.setCommand(command); + keyBinding.setKeySequence(descriptor.getKeySequence()); + keyBinding.setContributorURI(PluginUtils.getContributorURI()); + return keyBinding; + } +} |