// Path: org.eclipse.ec4e/src/main/java/org/eclipse/ec4e/EditorConfigPlugin.java
// public class EditorConfigPlugin extends AbstractUIPlugin {
// 
// 	// The plug-in ID
// 	public static final String PLUGIN_ID = "org.eclipse.ec4e"; //$NON-NLS-1$
// 
// 	// The shared instance
// 	private static EditorConfigPlugin plugin;
// 
// 	/**
// 	 * The constructor
// 	 */
// 	public EditorConfigPlugin() {
// 	}
// 
// 	@Override
// 	public void start(BundleContext context) throws Exception {
// 		super.start(context);
// 		plugin = this;
// 		IDEEditorConfigManager.getInstance().init();
// 	}
// 
// 	@Override
// 	public void stop(BundleContext context) throws Exception {
// 		IDEEditorConfigManager.getInstance().dispose();
// 		plugin = null;
// 		super.stop(context);
// 	}
// 
// 	/**
// 	 * Returns the shared instance
// 	 *
// 	 * @return the shared instance
// 	 */
// 	public static EditorConfigPlugin getDefault() {
// 		return plugin;
// 	}
// 
// 	/**
// 	 * Utility method to log errors.
// 	 *
// 	 * @param thr
// 	 *            The exception through which we noticed the error
// 	 */
// 	public static void logError(final Throwable thr) {
// 		getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, thr.getMessage(), thr));
// 	}
// 
// 	/**
// 	 * Utility method to log errors.
// 	 *
// 	 * @param message
// 	 *            User comprehensible message
// 	 * @param thr
// 	 *            The exception through which we noticed the error
// 	 */
// 	public static void logError(final String message, final Throwable thr) {
// 		getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, message, thr));
// 	}
// 
// 	@Override
// 	protected void initializeImageRegistry(ImageRegistry registry) {
// 		EditorConfigImages.initalize(registry);
// 	}
// }


import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
import org.eclipse.ec4e.EditorConfigPlugin;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.AbstractTextEditor;

/**
 *  Copyright (c) 2017 Angelo ZERR.
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */
package org.eclipse.ec4e.internal;



/**
 * Editor tracker used to
 * 
 * <ul>
 * <li>update {@link IPreferenceStore} of the opened text editor by adding the
 * {@link EditorConfigPreferenceStore}.</li>
 * <li>call {@link EditorConfigPreferenceStore#applyConfig()} when editor has
 * focus to apply properties coming from .editorconfig files.</li>
 * </ul>
 *
 */
public class EditorTracker implements IWindowListener, IPageListener, IPartListener {

	private static EditorTracker INSTANCE;

	private Map<AbstractTextEditor, ApplyEditorConfig> applies = new HashMap<>();

	private EditorTracker() {
		init();
	}

	public static EditorTracker getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new EditorTracker();
		}
		return INSTANCE;
	}

	private void init() {
		if (PlatformUI.isWorkbenchRunning()) {

// Path: org.eclipse.ec4e/src/main/java/org/eclipse/ec4e/EditorConfigPlugin.java
// public class EditorConfigPlugin extends AbstractUIPlugin {
// 
// 	// The plug-in ID
// 	public static final String PLUGIN_ID = "org.eclipse.ec4e"; //$NON-NLS-1$
// 
// 	// The shared instance
// 	private static EditorConfigPlugin plugin;
// 
// 	/**
// 	 * The constructor
// 	 */
// 	public EditorConfigPlugin() {
// 	}
// 
// 	@Override
// 	public void start(BundleContext context) throws Exception {
// 		super.start(context);
// 		plugin = this;
// 		IDEEditorConfigManager.getInstance().init();
// 	}
// 
// 	@Override
// 	public void stop(BundleContext context) throws Exception {
// 		IDEEditorConfigManager.getInstance().dispose();
// 		plugin = null;
// 		super.stop(context);
// 	}
// 
// 	/**
// 	 * Returns the shared instance
// 	 *
// 	 * @return the shared instance
// 	 */
// 	public static EditorConfigPlugin getDefault() {
// 		return plugin;
// 	}
// 
// 	/**
// 	 * Utility method to log errors.
// 	 *
// 	 * @param thr
// 	 *            The exception through which we noticed the error
// 	 */
// 	public static void logError(final Throwable thr) {
// 		getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, thr.getMessage(), thr));
// 	}
// 
// 	/**
// 	 * Utility method to log errors.
// 	 *
// 	 * @param message
// 	 *            User comprehensible message
// 	 * @param thr
// 	 *            The exception through which we noticed the error
// 	 */
// 	public static void logError(final String message, final Throwable thr) {
// 		getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, message, thr));
// 	}
// 
// 	@Override
// 	protected void initializeImageRegistry(ImageRegistry registry) {
// 		EditorConfigImages.initalize(registry);
// 	}
// }


// Path: org.eclipse.ec4e/src/main/java/org/eclipse/ec4e/internal/EditorTracker.java
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
import org.eclipse.ec4e.EditorConfigPlugin;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.AbstractTextEditor;

/**
 *  Copyright (c) 2017 Angelo ZERR.
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */
package org.eclipse.ec4e.internal;



/**
 * Editor tracker used to
 * 
 * <ul>
 * <li>update {@link IPreferenceStore} of the opened text editor by adding the
 * {@link EditorConfigPreferenceStore}.</li>
 * <li>call {@link EditorConfigPreferenceStore#applyConfig()} when editor has
 * focus to apply properties coming from .editorconfig files.</li>
 * </ul>
 *
 */
public class EditorTracker implements IWindowListener, IPageListener, IPartListener {

	private static EditorTracker INSTANCE;

	private Map<AbstractTextEditor, ApplyEditorConfig> applies = new HashMap<>();

	private EditorTracker() {
		init();
	}

	public static EditorTracker getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new EditorTracker();
		}
		return INSTANCE;
	}

	private void init() {
		if (PlatformUI.isWorkbenchRunning()) {

			IWorkbench workbench = EditorConfigPlugin.getDefault().getWorkbench();
