1 11 package org.eclipse.pde.internal.ui.search; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.Path; 20 import org.eclipse.jface.resource.ImageDescriptor; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.pde.core.plugin.IPluginObject; 23 import org.eclipse.pde.internal.ui.PDEPluginImages; 24 import org.eclipse.pde.internal.ui.PDEUIMessages; 25 import org.eclipse.search.ui.ISearchQuery; 26 import org.eclipse.search.ui.text.AbstractTextSearchResult; 27 import org.eclipse.search.ui.text.IEditorMatchAdapter; 28 import org.eclipse.search.ui.text.IFileMatchAdapter; 29 import org.eclipse.search.ui.text.ISearchEditorAccess; 30 import org.eclipse.search.ui.text.Match; 31 import org.eclipse.ui.IEditorPart; 32 import org.eclipse.ui.texteditor.ITextEditor; 33 34 public class SearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter { 35 protected ISearchQuery fQuery; 36 37 public SearchResult(ISearchQuery query) { 38 fQuery = query; 39 } 40 41 44 public IEditorMatchAdapter getEditorMatchAdapter() { 45 return this; 46 } 47 48 51 public String getLabel() { 52 int numMatches = getMatchCount(); 53 return fQuery.getLabel() + " - " + numMatches + " " + (numMatches == 1 ? PDEUIMessages.SearchResult_match : PDEUIMessages.SearchResult_matches); } 55 56 59 public String getTooltip() { 60 return null; 61 } 62 63 66 public ImageDescriptor getImageDescriptor() { 67 return PDEPluginImages.DESC_PSEARCH_OBJ; 68 } 69 70 73 public ISearchQuery getQuery() { 74 return fQuery; 75 } 76 77 80 public boolean isShownInEditor(Match match, IEditorPart editor) { 81 Object element = match.getElement(); 82 if (element instanceof IPluginObject) 83 return isMatchContained(editor, (IPluginObject)element); 84 return false; 85 } 86 87 90 public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) { 91 ArrayList list = new ArrayList (); 92 Object [] objects = result.getElements(); 93 for (int i = 0; i < objects.length; i++) { 94 if (objects[i] instanceof IPluginObject) { 95 IPluginObject object = (IPluginObject)objects[i]; 96 if (isMatchContained(editor, object)){ 97 Match[] matches = getMatches(object); 98 for (int j = 0; j < matches.length; j++) { 99 IDocument document = getDocument(editor, matches[j]); 100 if (document != null) 101 list.add(ManifestEditorOpener.findExactMatch(document, matches[j])); 102 } 103 } 104 } 105 } 106 return (Match[]) list.toArray(new Match[list.size()]); 107 } 108 109 110 111 114 public IFileMatchAdapter getFileMatchAdapter() { 115 return null; 116 } 117 118 protected boolean isMatchContained(IEditorPart editor, IPluginObject object) { 119 IFile resource = (IFile)editor.getEditorInput().getAdapter(IFile.class); 120 if (resource != null) { 121 IResource objectResource = object.getModel().getUnderlyingResource(); 122 if (objectResource != null) 123 return resource.getProject().equals(objectResource.getProject()); 124 } 125 File file = (File )editor.getEditorInput().getAdapter(File .class); 126 if (file != null) { 127 IPath path = new Path(object.getModel().getInstallLocation()); 128 IPath filePath = null; 129 if ("MANIFEST.MF".equals(file.getName())) filePath = new Path(file.getParentFile().getParent()); 131 else if (file.getName().endsWith("jar")) { filePath = new Path(file.getPath()); 133 } else { 134 filePath = new Path(file.getParent()); 135 } 136 return path.equals(filePath); 137 } 138 return false; 139 } 140 141 protected IDocument getDocument(IEditorPart editor, Match match) { 142 IDocument document = null; 143 if (editor instanceof ISearchEditorAccess) { 144 document = ((ISearchEditorAccess)editor).getDocument(match); 145 } else if (editor instanceof ITextEditor) { 146 document = ((ITextEditor)editor).getDocumentProvider().getDocument(editor.getEditorInput()); 147 } 148 return document; 149 } 150 151 } 152 | Popular Tags |