Bug 575569 - cache the OS Editors

This speeds up repeated start of the Dialog:
"Preferences/General/Editors/File Associations/Add.../External programs"

It assumes OS Editor never changes.
You now need to restart Eclipse after adding a new OS Editor if you need
to add it in Eclipse. Like before the EditorRegistry does not know of
newly created OS Editors anyway.

Change-Id: I96623e7d65c38471a7ddff96f97bb36de573d102
Signed-off-by: Joerg Kubitz <jkubitz-eclipse@gmx.de>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/184308
Tested-by: Platform Bot <platform-bot@eclipse.org>
diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/registry/EditorRegistry.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/registry/EditorRegistry.java
index 3244c9b..af83d76 100644
--- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/registry/EditorRegistry.java
+++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/registry/EditorRegistry.java
@@ -147,6 +147,10 @@
 	 */
 	private List<IEditorDescriptor> sortedEditorsFromPlugins = new ArrayList<>();
 
+	/** cache of OS editors **/
+	private IEditorDescriptor[] sortedEditorsFromOS;
+	final Object sortedEditorsFromOSSynchronizer = new Object();
+
 	// Map of EditorDescriptor - map editor id to editor.
 	private Map<String, IEditorDescriptor> mapIDtoInternalEditor = initialIdToEditorMap(10);
 	// Map of EditorDescriptor - map editor id to OS editor.
@@ -304,8 +308,8 @@
 		}
 
 		// reset external editors from OS
-		synchronized(this){
-			mapIDtoOSEditors=null;
+		synchronized (this) {
+			mapIDtoOSEditors = null;
 		}
 	}
 
@@ -494,7 +498,24 @@
 	 * @return the editor descriptors
 	 */
 	public IEditorDescriptor[] getSortedEditorsFromOS() {
-		return getStaticSortedEditorsFromOS();
+		synchronized (sortedEditorsFromOSSynchronizer) {
+			if (sortedEditorsFromOS == null) {
+				loadEditorsFromOS();
+			}
+			return sortedEditorsFromOS;
+		}
+	}
+
+	/**
+	 * refreshes cache.
+	 *
+	 * @see #getSortedEditorsFromOS
+	 */
+	// public just in case someone wants to reload
+	public void loadEditorsFromOS() {
+		synchronized (sortedEditorsFromOSSynchronizer) {
+			sortedEditorsFromOS = getStaticSortedEditorsFromOS();
+		}
 	}
 
 	private static IEditorDescriptor[] getStaticSortedEditorsFromOS() {