1 11 12 package org.eclipse.jdt.internal.ui.search; 13 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IResource; 16 17 import org.eclipse.jface.resource.ImageDescriptor; 18 19 import org.eclipse.ui.IEditorInput; 20 import org.eclipse.ui.IEditorPart; 21 import org.eclipse.ui.IFileEditorInput; 22 23 import org.eclipse.search.ui.ISearchQuery; 24 import org.eclipse.search.ui.text.AbstractTextSearchResult; 25 import org.eclipse.search.ui.text.IEditorMatchAdapter; 26 import org.eclipse.search.ui.text.IFileMatchAdapter; 27 import org.eclipse.search.ui.text.Match; 28 29 import org.eclipse.jdt.core.IClassFile; 30 import org.eclipse.jdt.core.IJavaElement; 31 import org.eclipse.jdt.core.JavaModelException; 32 33 import org.eclipse.jdt.internal.ui.JavaPluginImages; 34 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 35 36 37 public class OccurrencesSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter { 38 39 protected static final Match[] NO_MATCHES= new Match[0]; 40 private OccurrencesSearchQuery fQuery; 41 42 public OccurrencesSearchResult(OccurrencesSearchQuery query) { 43 fQuery= query; 44 } 45 46 49 public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) { 50 Object [] elements= getElements(); 51 if (elements.length == 0) 52 return NO_MATCHES; 53 JavaElementLine jel= (JavaElementLine) elements[0]; 55 try { 56 if (file.equals(jel.getJavaElement().getCorrespondingResource())) 57 return collectMatches(elements); 58 } catch (JavaModelException e) { 59 } 61 return NO_MATCHES; 62 } 63 64 67 public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) { 68 IEditorInput editorInput= editor.getEditorInput(); 70 if (editorInput instanceof IFileEditorInput) { 71 IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput; 72 return computeContainedMatches(result, fileEditorInput.getFile()); 73 74 } else if (editorInput instanceof IClassFileEditorInput) { 75 IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput; 76 IClassFile classFile= classFileEditorInput.getClassFile(); 77 78 Object [] elements= getElements(); 79 if (elements.length == 0) 80 return NO_MATCHES; 81 JavaElementLine jel= (JavaElementLine) elements[0]; 83 if (jel.getJavaElement().equals(classFile)) 84 return collectMatches(elements); 85 } 86 return NO_MATCHES; 87 } 88 89 92 public IFile getFile(Object element) { 93 JavaElementLine jel= (JavaElementLine) element; 94 IResource resource= null; 95 try { 96 resource= jel.getJavaElement().getCorrespondingResource(); 97 } catch (JavaModelException e) { 98 } 100 if (resource instanceof IFile) 101 return (IFile) resource; 102 else 103 return null; 104 } 105 106 109 public boolean isShownInEditor(Match match, IEditorPart editor) { 110 Object element= match.getElement(); 111 IJavaElement je= ((JavaElementLine) element).getJavaElement(); 112 IEditorInput editorInput= editor.getEditorInput(); 113 if (editorInput instanceof IFileEditorInput) { 114 try { 115 return ((IFileEditorInput)editorInput).getFile().equals(je.getCorrespondingResource()); 116 } catch (JavaModelException e) { 117 return false; 118 } 119 } else if (editorInput instanceof IClassFileEditorInput) { 120 return ((IClassFileEditorInput)editorInput).getClassFile().equals(je); 121 } 122 123 return false; 124 } 125 126 129 public String getLabel() { 130 return fQuery.getResultLabel(getMatchCount()); 131 } 132 133 136 public String getTooltip() { 137 return getLabel(); 138 } 139 140 143 public ImageDescriptor getImageDescriptor() { 144 return JavaPluginImages.DESC_OBJS_SEARCH_REF; 145 } 146 147 150 public ISearchQuery getQuery() { 151 return fQuery; 152 } 153 154 public IFileMatchAdapter getFileMatchAdapter() { 155 return this; 156 } 157 158 public IEditorMatchAdapter getEditorMatchAdapter() { 159 return this; 160 } 161 162 private Match[] collectMatches(Object [] elements) { 163 Match[] matches= new Match[getMatchCount()]; 164 int writeIndex= 0; 165 for (int i= 0; i < elements.length; i++) { 166 Match[] perElement= getMatches(elements[i]); 167 for (int j= 0; j < perElement.length; j++) { 168 matches[writeIndex++]= perElement[j]; 169 } 170 } 171 return matches; 172 } 173 } 174 | Popular Tags |