 /*******************************************************************************
 * Copyright (c) 2019 OPCoach.
 * 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:
 *     OPCoach - initial API and implementation
 *******************************************************************************/

package com.opcoach.e4.preferences;

import javax.inject.Inject;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.jface.preference.PreferenceManager;

import com.opcoach.e4.preferences.internal.E4PrefManager;
import com.opcoach.e4.preferences.internal.E4PreferenceRegistry;

/** This addon is used to ensure that current PreferenceManager in the context is updated with both : 
 * 
 *   - pure e4 preference pages
 *   - legacy E3 preference pages
 *   
 *   With this addon it will be possible to use the E4 preferences either in a pure E4 application or in a 
 *   mixed E3/E4 application without any change. 
 *   
 * @author olivier
 *
 */
public class E4PreferencesAddon {

//	/** 
//	 * Here we must ensure that PreferenceManager is initialized correctly (see comment inside). 
//	 * after this method, the PreferenceManager can contain both E3 and E4 pref page or only E4 pages.
//	 * @param ctx current application context.
//	 */
//	@PostConstruct
//	public void initializePreferenceManager(IEclipseContext ctx) {
//		
//		// First of all ... is there a PreferenceManager in the context ? 
//		//  Yes -> we are in a E3 application that initialized it from class org.eclipse.ui.internal.WorkbenchPlugin (around line 1356)
//		//       in this case we must complete this existing preference manager with addition pure E4 pref page. 
//		//  No -> we are running in a pure E4 application... let's manage ours
//		
//		PreferenceManager pm = ctx.get(PreferenceManager.class);
//		if (pm == null)
//		{
//			// we are in a pure E4 application without any initialization... Must init it using E4PreferenceRegistry
//			pm = new PreferenceManager();
//			ctx.set(PreferenceManager.class, pm);
//		}
//		
//			
//	}
//	
	/**
	 * This method is invoked when the preference manager changes in the context. 
	 * It can be possible in 2 cases : 
	 *   <ul>
	 *   
	 *   <li> E3 mixed mode with E4 pref plugins -> the org.eclipse.ui.internal.WorkbenchPlugin set it after the creation of this addon. 
	 *        In this case we must complete the E3 init with the E4 preferences extensions.  </li>
	 *   <li> pure E4 mode : in this case, the com.opcoach.e4.preferences.handlers.E4PreferencesHandler was called to initialize the Preference manager if it is not preset. In this case
	 *   nothing special to do...</li> 
	 *    </ul>
	 * @param ctx
	 * @param pm
	 */
	@Inject @Optional
	public void initializePreferenceManager(IEclipseContext ctx, PreferenceManager pm) {
		// First time it can be null because init is not finished but we register that we are interesting in
		if (pm == null || pm instanceof E4PrefManager)
			return;
		
		// Here we got a E3 preference manager, we must complete its content. 
		// If we enter here, it is because Fill this context with the additional E4 pref pages. 
		E4PreferenceRegistry registry = new E4PreferenceRegistry();
		registry.populatePrefManagerWithE4Extensions(pm, ctx);	
		
	}
		
	

}

@@ -1262,1050 +1262,8 @@
  %7B%0A%0A
-//%09/** %0A//%09 * Here we must ensure that PreferenceManager is initialized correctly (see comment inside). %0A//%09 * after this method, the PreferenceManager can contain both E3 and E4 pref page or only E4 pages.%0A//%09 * @param ctx current application context.%0A//%09 */%0A//%09@PostConstruct%0A//%09public void initializePreferenceManager(IEclipseContext ctx) %7B%0A//%09%09%0A//%09%09// First of all ... is there a PreferenceManager in the context ? %0A//%09%09//  Yes -%3E we are in a E3 application that initialized it from class org.eclipse.ui.internal.WorkbenchPlugin (around line 1356)%0A//%09%09//       in this case we must complete this existing preference manager with addition pure E4 pref page. %0A//%09%09//  No -%3E we are running in a pure E4 application... let's manage ours%0A//%09%09%0A//%09%09PreferenceManager pm = ctx.get(PreferenceManager.class);%0A//%09%09if (pm == null)%0A//%09%09%7B%0A//%09%09%09// we are in a pure E4 application without any initialization... Must init it using E4PreferenceRegistry%0A//%09%09%09pm = new PreferenceManager();%0A//%09%09%09ctx.set(PreferenceManager.class, pm);%0A//%09%09%7D%0A//%09%09%0A//%09%09%09%0A//%09%7D%0A//%09
 %0A%09/*

Remove useless comments