Bug 382118 - [Tools] Multiple keybindings for Show Live model
diff --git a/bundles/org.eclipse.e4.tools.emf.liveeditor/src/org/eclipse/e4/tools/emf/liveeditor/ModelProcessor.java b/bundles/org.eclipse.e4.tools.emf.liveeditor/src/org/eclipse/e4/tools/emf/liveeditor/ModelProcessor.java
index 97c7df7..3ddbbad 100644
--- a/bundles/org.eclipse.e4.tools.emf.liveeditor/src/org/eclipse/e4/tools/emf/liveeditor/ModelProcessor.java
+++ b/bundles/org.eclipse.e4.tools.emf.liveeditor/src/org/eclipse/e4/tools/emf/liveeditor/ModelProcessor.java
@@ -10,46 +10,88 @@
  ******************************************************************************/
 package org.eclipse.e4.tools.emf.liveeditor;
 
+import java.util.List;
+
 import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.MApplicationElement;
 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;
 import org.eclipse.e4.ui.model.application.descriptor.basic.MPartDescriptor;
 
+@SuppressWarnings("restriction")
 public class ModelProcessor {
 	
 	@Execute
 	public void process(MApplication application) {
-		MCommand command = MCommandsFactory.INSTANCE.createCommand();
-		command.setElementId("e4.tooling.livemodel");
-		command.setCommandName("Show running app model");
-		command.setDescription("Show the running application model");
-		application.getCommands().add(command);
+		MCommand command = findElementInstance(application.getCommands(), "e4.tooling.livemodel"); 
+				
 		
-		MHandler handler = MCommandsFactory.INSTANCE.createHandler();
-		handler.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor/org.eclipse.e4.tools.emf.liveeditor.OpenLiveDialogHandler");
-		handler.setCommand(command);
-		application.getHandlers().add(handler);
-		
-		MKeyBinding binding = MCommandsFactory.INSTANCE.createKeyBinding();
-		binding.setKeySequence("ALT+SHIFT+F9");
-		binding.setCommand(command);
-		if( application.getBindingTables().size() > 0 ) {
-			application.getBindingTables().get(0).getBindings().add(binding);	
+		if( command == null ) {
+			command = MCommandsFactory.INSTANCE.createCommand();
+			command.setElementId("e4.tooling.livemodel");
+			command.setCommandName("Show running app model");
+			command.setDescription("Show the running application model");
+			application.getCommands().add(command);						
 		}
 		
-		MPartDescriptor descriptor = org.eclipse.e4.ui.model.application.descriptor.basic.MBasicFactory.INSTANCE.createPartDescriptor();
-		descriptor.setCategory("org.eclipse.e4.secondaryDataStack");
-		descriptor.setElementId("org.eclipse.e4.tools.emf.liveeditor.view");
-		descriptor.getTags().add("View");
-		descriptor.getTags().add("categoryTag:General");
+		MHandler handler = findElementInstance(application.getHandlers(), "e4.tooling.livemodel.handler");
 		
-		descriptor.setLabel("Live Application Model");
-		descriptor.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor/org.eclipse.e4.tools.emf.liveeditor.LivePartDelegator");
-		descriptor.setContributorURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor");
-		descriptor.setIconURI("platform:/plugin/org.eclipse.e4.tools.emf.liveeditor/icons/full/obj16/application_lightning.png");
-		application.getDescriptors().add(descriptor);
+		if( handler == null ) {
+			handler = MCommandsFactory.INSTANCE.createHandler();
+			handler.setElementId("e4.tooling.livemodel.handler");
+			handler.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor/org.eclipse.e4.tools.emf.liveeditor.OpenLiveDialogHandler");
+			handler.setCommand(command);
+			application.getHandlers().add(handler);			
+		} else {
+			handler.setCommand(command);
+		}
+
+		
+		MKeyBinding binding = null;
+		
+		if( application.getBindingTables().size() > 0 ) {
+			binding = findElementInstance(application.getBindingTables().get(0).getBindings(), "e4.tooling.livemodel.binding");	
+		}
+		
+		if( binding == null ) {
+			binding = MCommandsFactory.INSTANCE.createKeyBinding();
+			binding.setElementId("e4.tooling.livemodel.binding");
+			binding.setKeySequence("ALT+SHIFT+F9");
+			binding.setCommand(command);
+			if( application.getBindingTables().size() > 0 ) {
+				application.getBindingTables().get(0).getBindings().add(binding);	
+			}			
+		} else {
+			binding.setCommand(command);
+		}
+		
+		MPartDescriptor descriptor = findElementInstance(application.getDescriptors(), "org.eclipse.e4.tools.emf.liveeditor.view");
+
+		if( descriptor == null ) {
+			descriptor = org.eclipse.e4.ui.model.application.descriptor.basic.MBasicFactory.INSTANCE.createPartDescriptor();
+			descriptor.setCategory("org.eclipse.e4.secondaryDataStack");
+			descriptor.setElementId("org.eclipse.e4.tools.emf.liveeditor.view");
+			descriptor.getTags().add("View");
+			descriptor.getTags().add("categoryTag:General");
+			
+			descriptor.setLabel("Live Application Model");
+			descriptor.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor/org.eclipse.e4.tools.emf.liveeditor.LivePartDelegator");
+			descriptor.setContributorURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor");
+			descriptor.setIconURI("platform:/plugin/org.eclipse.e4.tools.emf.liveeditor/icons/full/obj16/application_lightning.png");
+			application.getDescriptors().add(descriptor);			
+		}
+	}
+		
+	private <O> O findElementInstance(List<? extends MApplicationElement> l, String id) {
+		for( MApplicationElement e : l ) {
+			if( id.equals(e.getElementId()) ) {
+				return (O) e;
+			}
+		}
+		
+		return null;
 	}
 }