1 /*******************************************************************************
2  *  Copyright (c) 2005, 2016 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Martin Karpisek <martin.karpisek@gmail.com> - Bug 507831
14  *******************************************************************************/
15 package org.eclipse.pde.internal.ui.search.dependencies;
16 
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Set;
20 import org.eclipse.jdt.core.*;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.pde.internal.ui.PDEPluginImages;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.search.SearchResult;
25 import org.eclipse.search.ui.ISearchQuery;
26 import org.eclipse.search.ui.text.*;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IEditorPart;
29 
30 public class DependencyExtentSearchResult extends SearchResult {
31 
32 	/**
33 	 * @param query
34 	 */
DependencyExtentSearchResult(ISearchQuery query)35 	public DependencyExtentSearchResult(ISearchQuery query) {
36 		super(query);
37 	}
38 
39 	@Override
getEditorMatchAdapter()40 	public IEditorMatchAdapter getEditorMatchAdapter() {
41 		return this;
42 	}
43 
44 	@Override
getFileMatchAdapter()45 	public IFileMatchAdapter getFileMatchAdapter() {
46 		return null;
47 	}
48 
49 	@Override
isShownInEditor(Match match, IEditorPart editor)50 	public boolean isShownInEditor(Match match, IEditorPart editor) {
51 		return true;
52 	}
53 
54 	@Override
computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor)55 	public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
56 		IEditorInput editorInput = editor.getEditorInput();
57 		IJavaElement element = editorInput.getAdapter(IJavaElement.class);
58 		if (element != null) {
59 			Set<Match> matches = new HashSet<>();
60 			collectMatches(matches, element);
61 			return matches.toArray(new Match[matches.size()]);
62 		}
63 		return super.computeContainedMatches(result, editor);
64 
65 	}
66 
collectMatches(Set<Match> matches, IJavaElement element)67 	private void collectMatches(Set<Match> matches, IJavaElement element) {
68 		Match[] m = getMatches(element);
69 		if (m.length != 0) {
70 			Collections.addAll(matches, m);
71 		}
72 		if (element instanceof IParent) {
73 			IParent parent = (IParent) element;
74 			try {
75 				IJavaElement[] children = parent.getChildren();
76 				for (IJavaElement child : children) {
77 					collectMatches(matches, child);
78 				}
79 			} catch (JavaModelException e) {
80 				// we will not be tracking these results
81 			}
82 		}
83 	}
84 
85 	@Override
getLabel()86 	public String getLabel() {
87 		int count = getMatchCount();
88 		return fQuery.getLabel() + " - " + count + " " + (count == 1 ? PDEUIMessages.DependencyExtentSearchResult_dependency : PDEUIMessages.DependencyExtentSearchResult_dependencies); //$NON-NLS-1$ //$NON-NLS-2$
89 	}
90 
91 	@Override
getTooltip()92 	public String getTooltip() {
93 		return null;
94 	}
95 
96 	@Override
getImageDescriptor()97 	public ImageDescriptor getImageDescriptor() {
98 		return PDEPluginImages.DESC_PSEARCH_OBJ;
99 	}
100 
101 	@Override
getQuery()102 	public ISearchQuery getQuery() {
103 		return fQuery;
104 	}
105 
106 }
107