KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > dependencies > DependencyExtentSearchResult


1 /*******************************************************************************
2  * Copyright (c) 2005 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.dependencies;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IParent;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.pde.internal.ui.PDEPluginImages;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.pde.internal.ui.search.SearchResult;
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 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorPart;
30
31
32 public class DependencyExtentSearchResult extends SearchResult implements IEditorMatchAdapter {
33
34     /**
35      * @param query
36      */

37     public DependencyExtentSearchResult(ISearchQuery query) {
38         super(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.text.AbstractTextSearchResult#getFileMatchAdapter()
50      */

51     public IFileMatchAdapter getFileMatchAdapter() {
52         return null;
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.search.ui.text.IEditorMatchAdapter#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
57      */

58     public boolean isShownInEditor(Match match, IEditorPart editor) {
59         return true;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.search.ui.text.IEditorMatchAdapter#computeContainedMatches(org.eclipse.search.ui.text.AbstractTextSearchResult, org.eclipse.ui.IEditorPart)
64      */

65     public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
66         IEditorInput editorInput = editor.getEditorInput();
67         IJavaElement element = (IJavaElement) editorInput.getAdapter(IJavaElement.class);
68         if (element != null) {
69             Set JavaDoc matches= new HashSet JavaDoc();
70             collectMatches(matches, element);
71             return (Match[]) matches.toArray(new Match[matches.size()]);
72         }
73         return super.computeContainedMatches(result, editor);
74
75     }
76     
77     private void collectMatches(Set JavaDoc matches, IJavaElement element) {
78         Match[] m= getMatches(element);
79         if (m.length != 0) {
80             for (int i= 0; i < m.length; i++) {
81                 matches.add(m[i]);
82             }
83         }
84         if (element instanceof IParent) {
85             IParent parent= (IParent) element;
86             try {
87                 IJavaElement[] children= parent.getChildren();
88                 for (int i= 0; i < children.length; i++) {
89                     collectMatches(matches, children[i]);
90                 }
91             } catch (JavaModelException e) {
92                 // we will not be tracking these results
93
}
94         }
95     }
96
97
98     /* (non-Javadoc)
99      * @see org.eclipse.search.ui.ISearchResult#getLabel()
100      */

101     public String JavaDoc getLabel() {
102         int count = getMatchCount();
103         return fQuery.getLabel() + " - " + count + " " + (count == 1 ? PDEUIMessages.DependencyExtentSearchResult_dependency : PDEUIMessages.DependencyExtentSearchResult_dependencies); //$NON-NLS-1$ //$NON-NLS-2$
104
}
105
106     /* (non-Javadoc)
107      * @see org.eclipse.search.ui.ISearchResult#getTooltip()
108      */

109     public String JavaDoc getTooltip() {
110         return null;
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
115      */

116     public ImageDescriptor getImageDescriptor() {
117         return PDEPluginImages.DESC_PSEARCH_OBJ;
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.search.ui.ISearchResult#getQuery()
122      */

123     public ISearchQuery getQuery() {
124         return fQuery;
125     }
126     
127 }
128
Popular Tags