Bug 501286 - Update EventBroker registration with annotations in
BindingProcessingAddon

Change-Id: Ib0d6fd491fc240e5daaddd0bd6af8afa97c738be
Signed-off-by: Rolf Theunissen <rolf.theunissen@gmail.com>
diff --git a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/workbench/swt/util/BindingProcessingAddon.java b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/workbench/swt/util/BindingProcessingAddon.java
index c937a14..7912a7d 100644
--- a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/workbench/swt/util/BindingProcessingAddon.java
+++ b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/workbench/swt/util/BindingProcessingAddon.java
@@ -22,7 +22,6 @@
 import java.util.List;
 import java.util.Map;
 import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import org.eclipse.core.commands.ParameterizedCommand;
 import org.eclipse.core.commands.common.NotDefinedException;
@@ -32,10 +31,10 @@
 import org.eclipse.e4.core.commands.ECommandService;
 import org.eclipse.e4.core.contexts.IEclipseContext;
 import org.eclipse.e4.core.di.annotations.Optional;
-import org.eclipse.e4.core.services.events.IEventBroker;
 import org.eclipse.e4.ui.bindings.EBindingService;
 import org.eclipse.e4.ui.bindings.internal.BindingTable;
 import org.eclipse.e4.ui.bindings.internal.BindingTableManager;
+import org.eclipse.e4.ui.di.UIEventTopic;
 import org.eclipse.e4.ui.internal.workbench.Activator;
 import org.eclipse.e4.ui.internal.workbench.swt.Policy;
 import org.eclipse.e4.ui.internal.workbench.swt.WorkbenchSWTActivator;
@@ -57,7 +56,7 @@
 import org.eclipse.jface.bindings.BindingManager;
 import org.eclipse.jface.bindings.Scheme;
 import org.eclipse.jface.bindings.TriggerSequence;
-import org.osgi.service.event.EventHandler;
+import org.osgi.service.event.Event;
 import org.osgi.service.log.LogService;
 
 /**
@@ -70,9 +69,6 @@
 	private MApplication application;
 
 	@Inject
-	private IEventBroker broker;
-
-	@Inject
 	private ContextManager contextManager;
 
 	@Inject
@@ -88,10 +84,6 @@
 	@Inject
 	private EBindingService bindingService;
 
-	private EventHandler additionHandler;
-
-	private EventHandler contextHandler;
-
 	@PostConstruct
 	public void init() {
 		String[] schemes = DEFAULT_SCHEMES;
@@ -104,7 +96,6 @@
 		bindingTables.setActiveSchemes(schemes);
 		defineBindingTables();
 		activateContexts(application);
-		registerModelListeners();
 	}
 
 	private final String[] getSchemeIds(String schemeId) {
@@ -295,164 +286,166 @@
 		}
 	}
 
-	@PreDestroy
-	public void dispose() {
-		unregisterModelListeners();
-	}
+	@Inject
+	@Optional
+	private void subscribeBindingTableContainerTopicBindingTables(
+			@UIEventTopic(UIEvents.BindingTableContainer.TOPIC_BINDINGTABLES) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		if (!(elementObj instanceof MApplication)) {
+			return;
+		}
 
-	private void registerModelListeners() {
-		additionHandler = event -> {
-			Object elementObj = event
-					.getProperty(UIEvents.EventTags.ELEMENT);
-			if (elementObj instanceof MApplication) {
-				if (UIEvents.isADD(event)) {
-					for (Object newObj1 : UIEvents.asIterable(event,
-							UIEvents.EventTags.NEW_VALUE)) {
-						if (newObj1 instanceof MBindingTable) {
-							MBindingTable bt = (MBindingTable) newObj1;
-							final Context bindingContext = contextManager
-									.getContext(bt.getBindingContext()
-											.getElementId());
-							final BindingTable table = new BindingTable(
-									bindingContext);
-							bindingTables.addTable(table);
-							List<MKeyBinding> bindings = bt.getBindings();
-							for (MKeyBinding binding1 : bindings) {
-								Binding keyBinding = createBinding(
-										bindingContext,
-										binding1.getCommand(),
-										binding1.getParameters(),
-										binding1.getKeySequence(), binding1);
-								if (keyBinding != null) {
-									table.addBinding(keyBinding);
-								}
-							}
+		if (UIEvents.isADD(event)) {
+			for (Object newObj : UIEvents.asIterable(event, UIEvents.EventTags.NEW_VALUE)) {
+				if (newObj instanceof MBindingTable) {
+					MBindingTable bt = (MBindingTable) newObj;
+					final Context bindingContext = contextManager.getContext(bt.getBindingContext().getElementId());
+					final BindingTable table = new BindingTable(bindingContext);
+					bindingTables.addTable(table);
+					List<MKeyBinding> bindings = bt.getBindings();
+					for (MKeyBinding binding : bindings) {
+						Binding keyBinding = createBinding(bindingContext, binding.getCommand(),
+								binding.getParameters(), binding.getKeySequence(), binding);
+						if (keyBinding != null) {
+							table.addBinding(keyBinding);
 						}
 					}
 				}
-			} else if (elementObj instanceof MBindingTable) {
-				// adding a binding
-				if (UIEvents.isADD(event)) {
-					for (Object newObj2 : UIEvents.asIterable(event,
-							UIEvents.EventTags.NEW_VALUE)) {
-						if (newObj2 instanceof MKeyBinding) {
-							MKeyBinding binding2 = (MKeyBinding) newObj2;
-							updateBinding(binding2, true, elementObj);
-						}
-					}
-				}
-				// removing a binding
-				else if (UIEvents.isREMOVE(event)) {
-					for (Object oldObj1 : UIEvents.asIterable(event,
-							UIEvents.EventTags.OLD_VALUE)) {
-						if (oldObj1 instanceof MKeyBinding) {
-							MKeyBinding binding3 = (MKeyBinding) oldObj1;
-							updateBinding(binding3, false, elementObj);
-						}
-					}
-				}
-			} else if (elementObj instanceof MKeyBinding) {
-				MKeyBinding binding4 = (MKeyBinding) elementObj;
-
-				String attrName = (String) event
-						.getProperty(UIEvents.EventTags.ATTNAME);
-
-				// System.out.println("MKeyBinding." + attrName + ": "
-				// + event.getProperty(UIEvents.EventTags.TYPE));
-				if (UIEvents.isSET(event)) {
-					Object oldObj2 = event
-							.getProperty(UIEvents.EventTags.OLD_VALUE);
-					if (UIEvents.KeyBinding.COMMAND.equals(attrName)) {
-						MKeyBinding oldBinding1 = (MKeyBinding) EcoreUtil
-								.copy((EObject) binding4);
-						oldBinding1.setCommand((MCommand) oldObj2);
-						updateBinding(oldBinding1, false,
-								((EObject) binding4).eContainer());
-						updateBinding(binding4, true, null);
-					} else if (UIEvents.KeySequence.KEYSEQUENCE
-							.equals(attrName)) {
-						MKeyBinding oldBinding2 = (MKeyBinding) EcoreUtil
-								.copy((EObject) binding4);
-						oldBinding2.setKeySequence((String) oldObj2);
-						updateBinding(oldBinding2, false,
-								((EObject) binding4).eContainer());
-						updateBinding(binding4, true, null);
-					}
-				} else if (UIEvents.KeyBinding.PARAMETERS.equals(attrName)) {
-					if (UIEvents.isADD(event)) {
-						Object newObj3 = event
-								.getProperty(UIEvents.EventTags.NEW_VALUE);
-						MKeyBinding oldBinding3 = (MKeyBinding) EcoreUtil
-								.copy((EObject) binding4);
-						if (UIEvents.EventTypes.ADD_MANY.equals(event
-								.getProperty(UIEvents.EventTags.TYPE))) {
-							oldBinding3.getParameters().removeAll(
-									(Collection<?>) newObj3);
-						} else {
-							oldBinding3.getParameters().remove(newObj3);
-						}
-						updateBinding(oldBinding3, false,
-								((EObject) binding4).eContainer());
-						updateBinding(binding4, true, null);
-					} else if (UIEvents.isREMOVE(event)) {
-						Object oldObj3 = event
-								.getProperty(UIEvents.EventTags.OLD_VALUE);
-						MKeyBinding oldBinding4 = (MKeyBinding) EcoreUtil
-								.copy((EObject) binding4);
-						if (UIEvents.EventTypes.REMOVE_MANY.equals(event
-								.getProperty(UIEvents.EventTags.TYPE))) {
-							@SuppressWarnings("unchecked")
-							Collection<MParameter> parms = (Collection<MParameter>) oldObj3;
-							oldBinding4.getParameters().addAll(parms);
-						} else {
-							oldBinding4.getParameters().add(
-									(MParameter) oldObj3);
-						}
-
-						updateBinding(oldBinding4, false,
-								((EObject) binding4).eContainer());
-						updateBinding(binding4, true, null);
-					}
-				}
-				// if we've updated the tags for an MKeyBinding
-				else if (UIEvents.ApplicationElement.TAGS.equals(attrName)) {
-					List<String> tags = binding4.getTags();
-					// if we added a deleted tag to the MKeyBinding, then
-					// remove it from the runtime binding tables
-					if (tags.contains(EBindingService.DELETED_BINDING_TAG)) {
-						updateBinding(binding4, false, elementObj);
-					}
-					// else we're adding the binding to the runtime tables
-					else {
-						updateBinding(binding4, true, elementObj);
-					}
-				}
 			}
-		};
-		broker.subscribe(UIEvents.BindingTableContainer.TOPIC_BINDINGTABLES, additionHandler);
-		broker.subscribe(UIEvents.BindingTable.TOPIC_BINDINGS, additionHandler);
-		broker.subscribe(UIEvents.KeyBinding.TOPIC_COMMAND, additionHandler);
-		broker.subscribe(UIEvents.KeyBinding.TOPIC_PARAMETERS, additionHandler);
-		broker.subscribe(UIEvents.KeySequence.TOPIC_KEYSEQUENCE, additionHandler);
-		broker.subscribe(UIEvents.ApplicationElement.TOPIC_TAGS, additionHandler);
+		}
+	}
 
-		contextHandler = event -> {
-			Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+	@Inject
+	@Optional
+	private void subscribeTopicBinding(@UIEventTopic(UIEvents.BindingTable.TOPIC_BINDINGS) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		if (!(elementObj instanceof MBindingTable)) {
+			return;
+		}
+
+		// adding a binding
+		if (UIEvents.isADD(event)) {
+			for (Object newObj : UIEvents.asIterable(event, UIEvents.EventTags.NEW_VALUE)) {
+				if (newObj instanceof MKeyBinding) {
+					MKeyBinding binding = (MKeyBinding) newObj;
+					updateBinding(binding, true, elementObj);
+				}
+			}
+		}
+		// removing a binding
+		else if (UIEvents.isREMOVE(event)) {
+			for (Object oldObj : UIEvents.asIterable(event, UIEvents.EventTags.OLD_VALUE)) {
+				if (oldObj instanceof MKeyBinding) {
+					MKeyBinding binding = (MKeyBinding) oldObj;
+					updateBinding(binding, false, elementObj);
+				}
+			}
+		}
+	}
+
+	@Inject
+	@Optional
+	private void subscribeTopicCommand(@UIEventTopic(UIEvents.KeyBinding.TOPIC_COMMAND) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		if (!(elementObj instanceof MKeyBinding)) {
+			return;
+		}
+
+		MKeyBinding binding = (MKeyBinding) elementObj;
+		Object oldObj = event.getProperty(UIEvents.EventTags.OLD_VALUE);
+
+		MKeyBinding oldBinding = (MKeyBinding) EcoreUtil.copy((EObject) binding);
+		oldBinding.setCommand((MCommand) oldObj);
+		updateBinding(oldBinding, false, ((EObject) binding).eContainer());
+		updateBinding(binding, true, null);
+	}
+
+	@Inject
+	@Optional
+	private void subscribeTopicKeySequence(@UIEventTopic(UIEvents.KeySequence.TOPIC_KEYSEQUENCE) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		if (!(elementObj instanceof MKeyBinding)) {
+			return;
+		}
+
+		MKeyBinding binding = (MKeyBinding) elementObj;
+		Object oldObj = event.getProperty(UIEvents.EventTags.OLD_VALUE);
+
+		MKeyBinding oldBinding = (MKeyBinding) EcoreUtil.copy((EObject) binding);
+		oldBinding.setKeySequence((String) oldObj);
+		updateBinding(oldBinding, false, ((EObject) binding).eContainer());
+		updateBinding(binding, true, null);
+	}
+
+	@Inject
+	@Optional
+	private void subscribeTopicParameters(@UIEventTopic(UIEvents.KeyBinding.TOPIC_PARAMETERS) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		if (!(elementObj instanceof MKeyBinding)) {
+			return;
+		}
+
+		MKeyBinding binding = (MKeyBinding) elementObj;
+
+		if (UIEvents.isADD(event)) {
 			Object newObj = event.getProperty(UIEvents.EventTags.NEW_VALUE);
-			if (UIEvents.EventTypes.SET.equals(event.getProperty(UIEvents.EventTags.TYPE))
-					&& newObj instanceof IEclipseContext) {
-				activateContexts(elementObj);
+			MKeyBinding oldBinding = (MKeyBinding) EcoreUtil.copy((EObject) binding);
+			if (UIEvents.EventTypes.ADD_MANY.equals(event.getProperty(UIEvents.EventTags.TYPE))) {
+				oldBinding.getParameters().removeAll((Collection<?>) newObj);
+			} else {
+				oldBinding.getParameters().remove(newObj);
 			}
-		};
-		broker.subscribe(UIEvents.Context.TOPIC_CONTEXT, contextHandler);
+			updateBinding(oldBinding, false, ((EObject) binding).eContainer());
+			updateBinding(binding, true, null);
+		} else if (UIEvents.isREMOVE(event)) {
+			Object oldObj = event.getProperty(UIEvents.EventTags.OLD_VALUE);
+			MKeyBinding oldBinding = (MKeyBinding) EcoreUtil.copy((EObject) binding);
+			if (UIEvents.EventTypes.REMOVE_MANY.equals(event.getProperty(UIEvents.EventTags.TYPE))) {
+				@SuppressWarnings("unchecked")
+				Collection<MParameter> parms = (Collection<MParameter>) oldObj;
+				oldBinding.getParameters().addAll(parms);
+			} else {
+				oldBinding.getParameters().add((MParameter) oldObj);
+			}
+
+			updateBinding(oldBinding, false, ((EObject) binding).eContainer());
+			updateBinding(binding, true, null);
+		}
 	}
 
-	private void unregisterModelListeners() {
-		broker.unsubscribe(additionHandler);
-		broker.unsubscribe(additionHandler);
-		broker.unsubscribe(additionHandler);
-		broker.unsubscribe(additionHandler);
-		broker.unsubscribe(additionHandler);
-		broker.unsubscribe(contextHandler);
+	@Inject
+	@Optional
+	private void subscribeAppElemTopicTags(@UIEventTopic(UIEvents.ApplicationElement.TOPIC_TAGS) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		if (!(elementObj instanceof MKeyBinding)) {
+			return;
+		}
+
+		MKeyBinding binding = (MKeyBinding) elementObj;
+
+		List<String> tags = binding.getTags();
+		// if we added a deleted tag to the MKeyBinding, then
+		// remove it from the runtime binding tables
+		if (tags.contains(EBindingService.DELETED_BINDING_TAG)) {
+			updateBinding(binding, false, elementObj);
+		}
+		// else we're adding the binding to the runtime tables
+		else {
+			updateBinding(binding, true, elementObj);
+		}
 	}
+
+	@Inject
+	@Optional
+	private void subscribeContextTopicContext(@UIEventTopic(UIEvents.Context.TOPIC_CONTEXT) Event event) {
+		Object elementObj = event.getProperty(UIEvents.EventTags.ELEMENT);
+		Object newObj = event.getProperty(UIEvents.EventTags.NEW_VALUE);
+
+		if (!(newObj instanceof IEclipseContext)) {
+			return;
+		}
+
+		activateContexts(elementObj);
+	}
+
 }