001: /*******************************************************************************
002: * Copyright (c) 2005 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.pde.internal.ui.search.dependencies;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014:
015: import org.eclipse.jdt.core.IJavaElement;
016: import org.eclipse.jdt.core.IParent;
017: import org.eclipse.jdt.core.JavaModelException;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.pde.internal.ui.PDEPluginImages;
020: import org.eclipse.pde.internal.ui.PDEUIMessages;
021: import org.eclipse.pde.internal.ui.search.SearchResult;
022: import org.eclipse.search.ui.ISearchQuery;
023: import org.eclipse.search.ui.text.AbstractTextSearchResult;
024: import org.eclipse.search.ui.text.IEditorMatchAdapter;
025: import org.eclipse.search.ui.text.IFileMatchAdapter;
026: import org.eclipse.search.ui.text.Match;
027: import org.eclipse.ui.IEditorInput;
028: import org.eclipse.ui.IEditorPart;
029:
030: public class DependencyExtentSearchResult extends SearchResult
031: implements IEditorMatchAdapter {
032:
033: /**
034: * @param query
035: */
036: public DependencyExtentSearchResult(ISearchQuery query) {
037: super (query);
038: }
039:
040: /* (non-Javadoc)
041: * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getEditorMatchAdapter()
042: */
043: public IEditorMatchAdapter getEditorMatchAdapter() {
044: return this ;
045: }
046:
047: /* (non-Javadoc)
048: * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFileMatchAdapter()
049: */
050: public IFileMatchAdapter getFileMatchAdapter() {
051: return null;
052: }
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.search.ui.text.IEditorMatchAdapter#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
056: */
057: public boolean isShownInEditor(Match match, IEditorPart editor) {
058: return true;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.search.ui.text.IEditorMatchAdapter#computeContainedMatches(org.eclipse.search.ui.text.AbstractTextSearchResult, org.eclipse.ui.IEditorPart)
063: */
064: public Match[] computeContainedMatches(
065: AbstractTextSearchResult result, IEditorPart editor) {
066: IEditorInput editorInput = editor.getEditorInput();
067: IJavaElement element = (IJavaElement) editorInput
068: .getAdapter(IJavaElement.class);
069: if (element != null) {
070: Set matches = new HashSet();
071: collectMatches(matches, element);
072: return (Match[]) matches.toArray(new Match[matches.size()]);
073: }
074: return super .computeContainedMatches(result, editor);
075:
076: }
077:
078: private void collectMatches(Set matches, IJavaElement element) {
079: Match[] m = getMatches(element);
080: if (m.length != 0) {
081: for (int i = 0; i < m.length; i++) {
082: matches.add(m[i]);
083: }
084: }
085: if (element instanceof IParent) {
086: IParent parent = (IParent) element;
087: try {
088: IJavaElement[] children = parent.getChildren();
089: for (int i = 0; i < children.length; i++) {
090: collectMatches(matches, children[i]);
091: }
092: } catch (JavaModelException e) {
093: // we will not be tracking these results
094: }
095: }
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.search.ui.ISearchResult#getLabel()
100: */
101: public String getLabel() {
102: int count = getMatchCount();
103: return fQuery.getLabel()
104: + " - " + count + " " + (count == 1 ? PDEUIMessages.DependencyExtentSearchResult_dependency : PDEUIMessages.DependencyExtentSearchResult_dependencies); //$NON-NLS-1$ //$NON-NLS-2$
105: }
106:
107: /* (non-Javadoc)
108: * @see org.eclipse.search.ui.ISearchResult#getTooltip()
109: */
110: public String getTooltip() {
111: return null;
112: }
113:
114: /* (non-Javadoc)
115: * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
116: */
117: public ImageDescriptor getImageDescriptor() {
118: return PDEPluginImages.DESC_PSEARCH_OBJ;
119: }
120:
121: /* (non-Javadoc)
122: * @see org.eclipse.search.ui.ISearchResult#getQuery()
123: */
124: public ISearchQuery getQuery() {
125: return fQuery;
126: }
127:
128: }
|