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

import static com.redhat.ceylon.eclipse.imp.core.CeylonReferenceResolver.getReferencedDeclaration;

import org.eclipse.core.resources.IProject;
import org.eclipse.imp.editor.UniversalEditor;
import org.eclipse.jface.action.Action;
import org.eclipse.search.ui.NewSearchUI;

import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.eclipse.imp.parser.CeylonParseController;
import com.redhat.ceylon.eclipse.imp.parser.CeylonSourcePositionLocator;
import com.redhat.ceylon.eclipse.util.Util;

abstract class FindAction extends Action {
    
    private final UniversalEditor editor;

    FindAction(String text, UniversalEditor editor) {
        super(text);
        this.editor = editor;
    }
    
    @Override
    public void run() {
        CeylonParseController cpc = (CeylonParseController) editor.getParseController();
        Node node = CeylonSourcePositionLocator.findNode(cpc.getRootNode(), 
                editor.getSelection().x, editor.getSelection().x+editor.getSelection().y);
        NewSearchUI.runQueryInBackground(createSearchQuery(
                getReferencedDeclaration(node), 
                Util.getProject(editor.getEditorInput())));
    }

    public abstract FindSearchQuery createSearchQuery(Declaration declaration, IProject project);
    
}

@@ -294,29 +294,115 @@
 pse.
-search.ui.NewSearchUI
+jface.dialogs.MessageDialog;%0Aimport org.eclipse.search.ui.NewSearchUI;%0Aimport org.eclipse.swt.widgets.Shell
 ;%0A%0Ai
@@ -658,24 +658,74 @@
 ionLocator;%0A
+import com.redhat.ceylon.eclipse.ui.CeylonPlugin;%0A
 import com.r
@@ -1259,16 +1259,141 @@
 n().y);%0A
+        if (node!=null) %7B%0A            Declaration dec = getReferencedDeclaration(node);%0A            if (dec!=null) %7B%0A        
         
@@ -1443,17 +1443,29 @@
 chQuery(
-%0A
+dec, %0A       
         
@@ -1476,98 +1476,329 @@
     
-getReferencedDeclaration(node), %0A                Util.getProject(editor.getEditorInput()))
+ Util.getProject(editor.getEditorInput())));%0A                return;%0A            %7D%0A        %7D%0A        Shell shell= CeylonPlugin.getInstance().getWorkbench()%0A                .getActiveWorkbenchWindow().getShell();%0A        MessageDialog.openWarning(shell, %22Ceylon Find Error%22, %0A                %22No declaration name selected%22
 );%0A 

handle case where no appropriate node is selected