package com.redhat.ceylon.eclipse.imp.editorActionContributions;

import org.eclipse.imp.editor.UniversalEditor;
import org.eclipse.imp.services.ILanguageActionsContributor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.ui.IFileEditorInput;

import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.eclipse.imp.core.CeylonReferenceResolver;
import com.redhat.ceylon.eclipse.imp.parser.CeylonParseController;
import com.redhat.ceylon.eclipse.imp.refactoring.RefactoringContributor;

public class CeylonEditorActionContributions implements
		ILanguageActionsContributor {

	public void contributeToEditorMenu(final UniversalEditor editor,
			IMenuManager menuManager) {
		//IMenuManager languageMenu = new MenuManager("Search");
		menuManager.add(new Action("Find References") {
			@Override
			public void run() {
				CeylonParseController cpc = (CeylonParseController) editor.getParseController();
				Node node = cpc.getSourcePositionLocator().findNode(cpc.getRootNode(), 
						editor.getSelection().x, editor.getSelection().x+editor.getSelection().y);
				Declaration referencedDeclaration = CeylonReferenceResolver.getReferencedDeclaration(node);
				NewSearchUI.runQueryInBackground(new FindReferencesSearchQuery(cpc, referencedDeclaration, 
						((IFileEditorInput) editor.getEditorInput()).getFile()));
			}
		});
	}

	public void contributeToMenuBar(UniversalEditor editor, IMenuManager menu) {
		//languageMenu = new MenuManager("ceylon");
		IMenuManager refactor = /*editor.getEditorSite().getActionBars()
				.getMenuManager()*/menu.findMenuUsingPath("refactorMenuId");
		if (refactor.getItems().length==0) {
			for (IAction action: RefactoringContributor.getActions(editor)) {
				refactor.add(action);
			}
		}
	}

	public void contributeToStatusLine(final UniversalEditor editor,
			IStatusLineManager statusLineManager) {
		// TODO add ControlContribution objects to the statusLineManager
	}

	public void contributeToToolBar(UniversalEditor editor,
			IToolBarManager toolbarManager) {
		// add ControlContribution objects to the toolbarManager
	}
}

@@ -418,29 +418,100 @@
 pse.
-search.ui.NewSearchUI
+jface.action.Separator;%0Aimport org.eclipse.search.ui.NewSearchUI;%0Aimport org.eclipse.swt.SWT
 ;%0Aim
@@ -974,213 +974,264 @@
 %0A%0A%09p
-ublic void contributeToEditorMenu(final UniversalEditor editor,%0A%09%09%09IMenuManager menuManager) %7B%0A%09%09//IMenuManager languageMenu = new MenuManager(%22Search%22);%0A%09%09menuManager.add(new Action(%22Find References%22) %7B%0A%09
+rivate class FindReferencesAction extends Action %7B%0A%09%09private final UniversalEditor editor;%0A%0A%09%09private FindReferencesAction(UniversalEditor editor) %7B%0A%09%09%09super(%22Find References%22);%0A%09%09%09this.editor = editor;%0A%09%09%09setAccelerator(SWT.CONTROL %7C SWT.ALT %7C 'G');%0A%09%09%7D%0A%0A
 %09%09@O
@@ -1238,17 +1238,16 @@
 verride%0A
-%09
 %09%09public
@@ -1260,17 +1260,16 @@
 run() %7B%0A
-%09
 %09%09%09Ceylo
@@ -1344,17 +1344,16 @@
 ller();%0A
-%09
 %09%09%09Node 
@@ -1424,17 +1424,16 @@
 , %0A%09%09%09%09%09
-%09
 editor.g
@@ -1499,17 +1499,16 @@
 n().y);%0A
-%09
 %09%09%09Decla
@@ -1597,17 +1597,16 @@
 de);%0A%09%09%09
-%09
 NewSearc
@@ -1689,17 +1689,16 @@
 ation, %0A
-%09
 %09%09%09%09%09((I
@@ -1754,22 +1754,228 @@
 ()));%0A%09%09
+%7D%0A
 %09%7D%0A
-%09%09%7D
+%0A%09public void contributeToEditorMenu(final UniversalEditor editor,%0A%09%09%09IMenuManager menuManager) %7B%0A%09%09//IMenuManager languageMenu = new MenuManager(%22Search%22);%0A%09%09menuManager.add(new FindReferencesAction(editor)
 );%0A%09%7D%0A%0A%09
@@ -2368,24 +2368,228 @@
 );%0A%09%09%09%7D%0A%09%09%7D%0A
+%09%09IMenuManager search = /*editor.getEditorSite().getActionBars()%0A%09%09%09%09.getMenuManager()*/menu.findMenuUsingPath(%22navigate%22);%0A%09%09search.add(new Separator());%0A%09%09search.add(new FindReferencesAction(editor));%0A%0A
 %09%7D%0A%0A%09public 

put Find References in the menu