package org.strategoxt.imp.runtime.services;

import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.imp.ui.DefaultPartListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.spoofax.interpreter.library.index.IndexManager;
import org.strategoxt.imp.runtime.EditorState;

/**
 * Activates the observer when an editor gets focus.
 * Basically, a kludge.
 * 
 * Tries to install one listener per part.
 * 
 * @author Lennart Kats <lennart add lclnet.nl>
 */
public class StrategoObserverPartListener extends DefaultPartListener {
	
	private static Map<IWorkbenchPage, StrategoObserverPartListener> asyncListeners =
		new WeakHashMap<IWorkbenchPage, StrategoObserverPartListener>();
	
	private StrategoObserverPartListener() {
		// Private constructor
	}
	
	public static synchronized void register(EditorState editor) {
		if (editor == null) return;
		
		IWorkbenchPage page = editor.getEditor().getSite().getPage();

		StrategoObserverPartListener oldListener = asyncListeners.get(page);
		if (oldListener != null) {
			page.removePartListener(oldListener);
		}
		
		StrategoObserverPartListener newListener = new StrategoObserverPartListener();
		page.addPartListener(newListener);
		asyncListeners.put(page, newListener);
	}
	
	@Override
	public void partBroughtToTop(IWorkbenchPart part) {
		EditorState editor = EditorState.getEditorFor(part);
		if (editor != null && !IndexManager.isKnownIndexingLanguage(editor.getLanguage().getName()))
		{
			editor.scheduleAnalysis();
			org.strategoxt.imp.runtime.Environment.logWarning("Analyze " + part.getTitle());
		}
	}
}

@@ -1491,19 +1491,17 @@
 Name()))
-%0A%09%09
+ 
 %7B%0A%09%09%09edi
@@ -1528,92 +1528,8 @@
 ();%0A
-%09%09%09org.strategoxt.imp.runtime.Environment.logWarning(%22Analyze %22 + part.getTitle());%0A
 %09%09%7D%0A

Remove leftover debug logging.