KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > SearchResult


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.search;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
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     /* (non-Javadoc)
42      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getEditorMatchAdapter()
43      */

44     public IEditorMatchAdapter getEditorMatchAdapter() {
45         return this;
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.search.ui.ISearchResult#getLabel()
50      */

51     public String JavaDoc getLabel() {
52         int numMatches = getMatchCount();
53         return fQuery.getLabel() + " - " + numMatches + " " + (numMatches == 1 ? PDEUIMessages.SearchResult_match : PDEUIMessages.SearchResult_matches); //$NON-NLS-1$ //$NON-NLS-2$
54
}
55
56     /* (non-Javadoc)
57      * @see org.eclipse.search.ui.ISearchResult#getTooltip()
58      */

59     public String JavaDoc getTooltip() {
60         return null;
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
65      */

66     public ImageDescriptor getImageDescriptor() {
67         return PDEPluginImages.DESC_PSEARCH_OBJ;
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.search.ui.ISearchResult#getQuery()
72      */

73     public ISearchQuery getQuery() {
74         return fQuery;
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.search.ui.text.IEditorMatchAdapter#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
79      */

80     public boolean isShownInEditor(Match match, IEditorPart editor) {
81         Object JavaDoc element = match.getElement();
82         if (element instanceof IPluginObject)
83             return isMatchContained(editor, (IPluginObject)element);
84         return false;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.search.ui.text.IEditorMatchAdapter#computeContainedMatches(org.eclipse.search.ui.text.AbstractTextSearchResult, org.eclipse.ui.IEditorPart)
89      */

90     public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
91         ArrayList JavaDoc list = new ArrayList JavaDoc();
92         Object JavaDoc[] 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     /* (non-Javadoc)
112      * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFileMatchAdapter()
113      */

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 JavaDoc file = (File JavaDoc)editor.getEditorInput().getAdapter(File JavaDoc.class);
126         if (file != null) {
127             IPath path = new Path(object.getModel().getInstallLocation());
128             IPath filePath = null;
129             if ("MANIFEST.MF".equals(file.getName())) //$NON-NLS-1$
130
filePath = new Path(file.getParentFile().getParent());
131             else if (file.getName().endsWith("jar")) { //$NON-NLS-1$
132
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