001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.search;
011:
012: import java.util.HashMap;
013: import java.util.HashSet;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Status;
020:
021: import org.eclipse.core.resources.IFile;
022:
023: import org.eclipse.jface.resource.ImageDescriptor;
024:
025: import org.eclipse.ui.IEditorPart;
026:
027: import org.eclipse.search.ui.ISearchQuery;
028: import org.eclipse.search.ui.text.AbstractTextSearchResult;
029: import org.eclipse.search.ui.text.IEditorMatchAdapter;
030: import org.eclipse.search.ui.text.IFileMatchAdapter;
031: import org.eclipse.search.ui.text.Match;
032: import org.eclipse.search.ui.text.MatchFilter;
033:
034: import org.eclipse.jdt.core.IClassFile;
035: import org.eclipse.jdt.core.ICompilationUnit;
036: import org.eclipse.jdt.core.IJavaElement;
037: import org.eclipse.jdt.core.IParent;
038: import org.eclipse.jdt.core.JavaModelException;
039:
040: import org.eclipse.jdt.ui.search.IMatchPresentation;
041:
042: import org.eclipse.jdt.internal.ui.JavaPlugin;
043:
044: public class JavaSearchResult extends AbstractTextSearchResult
045: implements IEditorMatchAdapter, IFileMatchAdapter {
046:
047: private static final Match[] NO_MATCHES = new Match[0];
048:
049: private final JavaSearchQuery fQuery;
050: private final Map fElementsToParticipants;
051:
052: public JavaSearchResult(JavaSearchQuery query) {
053: fQuery = query;
054: fElementsToParticipants = new HashMap();
055: setActiveMatchFilters(JavaMatchFilter.getLastUsedFilters());
056: }
057:
058: public ImageDescriptor getImageDescriptor() {
059: return fQuery.getImageDescriptor();
060: }
061:
062: public String getLabel() {
063: return fQuery.getResultLabel(getMatchCount());
064: }
065:
066: public String getTooltip() {
067: return getLabel();
068: }
069:
070: public Match[] computeContainedMatches(
071: AbstractTextSearchResult result, IEditorPart editor) {
072: return computeContainedMatches(editor.getEditorInput());
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.search.ui.text.AbstractTextSearchResult#setMatchFilters(org.eclipse.search.ui.text.MatchFilter[])
077: */
078: public void setActiveMatchFilters(MatchFilter[] filters) {
079: super .setActiveMatchFilters(filters);
080: JavaMatchFilter.setLastUsedFilters(filters);
081: }
082:
083: /* (non-Javadoc)
084: * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getAllMatchFilters()
085: */
086: public MatchFilter[] getAllMatchFilters() {
087: return JavaMatchFilter.allFilters(fQuery);
088: }
089:
090: public Match[] computeContainedMatches(
091: AbstractTextSearchResult result, IFile file) {
092: return computeContainedMatches(file);
093: }
094:
095: private Match[] computeContainedMatches(IAdaptable adaptable) {
096: IJavaElement javaElement = (IJavaElement) adaptable
097: .getAdapter(IJavaElement.class);
098: Set matches = new HashSet();
099: if (javaElement != null) {
100: collectMatches(matches, javaElement);
101: }
102: IFile file = (IFile) adaptable.getAdapter(IFile.class);
103: if (file != null) {
104: collectMatches(matches, file);
105: }
106: if (!matches.isEmpty()) {
107: return (Match[]) matches.toArray(new Match[matches.size()]);
108: }
109: return NO_MATCHES;
110: }
111:
112: private void collectMatches(Set matches, IFile element) {
113: Match[] m = getMatches(element);
114: if (m.length != 0) {
115: for (int i = 0; i < m.length; i++) {
116: matches.add(m[i]);
117: }
118: }
119: }
120:
121: private void collectMatches(Set matches, IJavaElement element) {
122: Match[] m = getMatches(element);
123: if (m.length != 0) {
124: for (int i = 0; i < m.length; i++) {
125: matches.add(m[i]);
126: }
127: }
128: if (element instanceof IParent) {
129: IParent parent = (IParent) element;
130: try {
131: IJavaElement[] children = parent.getChildren();
132: for (int i = 0; i < children.length; i++) {
133: collectMatches(matches, children[i]);
134: }
135: } catch (JavaModelException e) {
136: // we will not be tracking these results
137: }
138: }
139: }
140:
141: /* (non-Javadoc)
142: * @see org.eclipse.search.ui.ISearchResultCategory#getFile(java.lang.Object)
143: */
144: public IFile getFile(Object element) {
145: if (element instanceof IJavaElement) {
146: IJavaElement javaElement = (IJavaElement) element;
147: ICompilationUnit cu = (ICompilationUnit) javaElement
148: .getAncestor(IJavaElement.COMPILATION_UNIT);
149: if (cu != null) {
150: return (IFile) cu.getResource();
151: } else {
152: IClassFile cf = (IClassFile) javaElement
153: .getAncestor(IJavaElement.CLASS_FILE);
154: if (cf != null)
155: return (IFile) cf.getResource();
156: }
157: return null;
158: }
159: if (element instanceof IFile)
160: return (IFile) element;
161: return null;
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see org.eclipse.search2.ui.text.IStructureProvider#isShownInEditor(org.eclipse.search2.ui.text.Match,
168: * org.eclipse.ui.IEditorPart)
169: */
170: public boolean isShownInEditor(Match match, IEditorPart editor) {
171: Object element = match.getElement();
172: if (element instanceof IJavaElement) {
173: element = ((IJavaElement) element).getOpenable(); // class file or compilation unit
174: return element != null
175: && element.equals(editor.getEditorInput()
176: .getAdapter(IJavaElement.class));
177: } else if (element instanceof IFile) {
178: return element.equals(editor.getEditorInput().getAdapter(
179: IFile.class));
180: }
181: return false;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.eclipse.search.ui.ISearchResult#getQuery()
186: */
187: public ISearchQuery getQuery() {
188: return fQuery;
189: }
190:
191: synchronized IMatchPresentation getSearchParticpant(Object element) {
192: return (IMatchPresentation) fElementsToParticipants
193: .get(element);
194: }
195:
196: boolean addMatch(Match match, IMatchPresentation participant) {
197: Object element = match.getElement();
198: if (fElementsToParticipants.get(element) != null) {
199: // TODO must access the participant id / label to properly report the error.
200: JavaPlugin
201: .log(new Status(
202: IStatus.WARNING,
203: JavaPlugin.getPluginId(),
204: 0,
205: "A second search participant was found for an element", null)); //$NON-NLS-1$
206: return false;
207: }
208: fElementsToParticipants.put(element, participant);
209: addMatch(match);
210: return true;
211: }
212:
213: public void removeAll() {
214: synchronized (this ) {
215: fElementsToParticipants.clear();
216: }
217: super .removeAll();
218: }
219:
220: public void removeMatch(Match match) {
221: synchronized (this ) {
222: if (getMatchCount(match.getElement()) == 1)
223: fElementsToParticipants.remove(match.getElement());
224: }
225: super .removeMatch(match);
226: }
227:
228: public IFileMatchAdapter getFileMatchAdapter() {
229: return this ;
230: }
231:
232: public IEditorMatchAdapter getEditorMatchAdapter() {
233: return this;
234: }
235:
236: }
|