Bug 429854 - Stacktrace at start when org.eclipse.e4.tools.context.spy
(0.10.v20140303-1452) is included

Occured when no binding table nor context is present in model. In this
case, the created binding context should be created but not added in
application. 

Change-Id: I8074fda2f52229ae611624ea78b42bd5fb591b94
Signed-off-by: Olivier Prouvost <olivier.prouvost@opcoach.com>
diff --git a/bundles/org.eclipse.e4.tools.context.spy/src/org/eclipse/e4/tools/context/spy/ContextSpyProcessor.java b/bundles/org.eclipse.e4.tools.context.spy/src/org/eclipse/e4/tools/context/spy/ContextSpyProcessor.java
index 7bd6b84..8e4798f 100644
--- a/bundles/org.eclipse.e4.tools.context.spy/src/org/eclipse/e4/tools/context/spy/ContextSpyProcessor.java
+++ b/bundles/org.eclipse.e4.tools.context.spy/src/org/eclipse/e4/tools/context/spy/ContextSpyProcessor.java
@@ -10,6 +10,8 @@
  *******************************************************************************/
 package org.eclipse.e4.tools.context.spy;
 
+import java.util.List;
+
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 
@@ -164,26 +166,32 @@
 		// If this context does not yet exist, create it also.
 		if (spyBindingTable == null)
 		{
-			MBindingContext dialAndWindowContext = null;
-			for (MBindingContext bc : application.getBindingContexts())
-				if (ORG_ECLIPSE_UI_CONTEXTS_DIALOG_AND_WINDOW.equals(bc.getElementId()))
-				{
-					dialAndWindowContext = bc;
-					break;
+			
+			MBindingContext bc = null;
+			final List<MBindingContext> bindingContexts = application
+					.getBindingContexts();
+			if (bindingContexts.size() == 0) {
+				bc = modelService.createModelElement(MBindingContext.class);
+				bc.setElementId("org.eclipse.ui.contexts.window");
+			} else {
+				// Prefer org.eclipse.ui.contexts.dialogAndWindow but randomly
+				// select another one
+				// if org.eclipse.ui.contexts.dialogAndWindow cannot be found
+				for (MBindingContext aBindingContext : bindingContexts) {
+					bc = aBindingContext;
+					if ("org.eclipse.ui.contexts.dialogAndWindow"
+							.equals(aBindingContext.getElementId())) {
+						break;
+					}
 				}
-
-			if (dialAndWindowContext == null)
-			{
-				// This context has not yet been created... Application model
-				// must be very poor....
-				dialAndWindowContext = modelService.createModelElement(MBindingContext.class);
-				dialAndWindowContext.setElementId(ORG_ECLIPSE_UI_CONTEXTS_DIALOG_AND_WINDOW);
 			}
-
+			
+			
+	
 			// Can now create the binding table and bind it to this context...
 			spyBindingTable = modelService.createModelElement(MBindingTable.class);
 			spyBindingTable.setElementId(E4_SPIES_BINDING_TABLE);
-			spyBindingTable.setBindingContext(dialAndWindowContext);
+			spyBindingTable.setBindingContext(bc);
 			application.getBindingTables().add(spyBindingTable);
 		}