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

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.IEditorMatchAdapter;
import org.eclipse.search.ui.text.IFileMatchAdapter;
import org.eclipse.search.ui.text.Match;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;

import com.redhat.ceylon.eclipse.imp.treeModelBuilder.CeylonLabelProvider;
import com.redhat.ceylon.eclipse.ui.ICeylonResources;

public class CeylonSearchResult extends AbstractTextSearchResult
		implements IEditorMatchAdapter, IFileMatchAdapter {
	ISearchQuery query;
	
	CeylonSearchResult(ISearchQuery query) {
		this.query = query;
	}
	
	@Override
	public String getTooltip() {
		return getLabel();
	}

	@Override
	public ISearchQuery getQuery() {
		return query;
	}

	@Override
	public String getLabel() {
		return query.getLabel();
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		return CeylonLabelProvider.imageRegistry.getDescriptor(ICeylonResources.CEYLON_FILE);
	}

	@Override
	public IFileMatchAdapter getFileMatchAdapter() {
		return this;
	}

	@Override
	public IEditorMatchAdapter getEditorMatchAdapter() {
		return this;
	}

	@Override
	public Match[] computeContainedMatches(AbstractTextSearchResult atsr,
			IFile file) {
		return getMatches(file);
	}

	@Override
	public IFile getFile(Object element) {
		if (element instanceof IFile)
			return (IFile) element;
		else if (element instanceof CeylonElement)
			return ((CeylonElement) element).getFile();
		else 
			throw new RuntimeException();
	}

	@Override
	public Match[] computeContainedMatches(AbstractTextSearchResult atsr,
			IEditorPart editor) {
		IEditorInput ei= editor.getEditorInput();
		if (ei instanceof IFileEditorInput) {
			IFileEditorInput fi= (IFileEditorInput) ei;
			return getMatches(fi.getFile());
		}
		else {
			return new Match[0];
		}
	}

	@Override
	public boolean isShownInEditor(Match match, IEditorPart editor) {
		IEditorInput ei= editor.getEditorInput();
		if (ei instanceof IFileEditorInput) {
			IFileEditorInput fi= (IFileEditorInput) ei;
			return ((CeylonElement) match.getElement()).getFile().equals(fi.getFile());
		}
		else {
			return false;
		}
	}
	
}
@@ -59,16 +59,93 @@
 tions;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import o
@@ -1544,25 +1544,28 @@
 le file) %7B%0A%09
-%09
+    
 return getMa
@@ -1569,16 +1569,23 @@
 tMatches
+ForFile
 (file);%0A
@@ -1584,24 +1584,368 @@
 (file);%0A%09%7D%0A%0A
+    public Match%5B%5D getMatchesForFile(IFile file) %7B%0A        List%3CMatch%3E matches = new ArrayList%3CMatch%3E();%0A%09    for (Object element: this.getElements()) %7B%0A%09        if ( getFile(element).equals(file) ) %7B%0A%09            matches.addAll(Arrays.asList(getMatches(element)));%0A%09        %7D%0A%09    %7D%0A%09%09return matches.toArray(new Match%5Bmatches.size()%5D);%0A    %7D%0A%0A
 %09@Override%0A%09
@@ -2437,16 +2437,23 @@
 tMatches
+ForFile
 (fi.getF
@@ -2724,33 +2724,24 @@
 %09return 
-((CeylonElement) 
+getFile(
 match.ge
@@ -2756,18 +2756,8 @@
 ()).
-getFile().
 equa

Fix bug where little arrows indicating search results disappeared from the editor after switching to diff file