01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring;
11:
12: import java.util.ArrayList;
13: import java.util.Arrays;
14: import java.util.HashSet;
15: import java.util.List;
16: import java.util.Set;
17:
18: import org.eclipse.core.runtime.Assert;
19:
20: import org.eclipse.core.resources.IResource;
21:
22: import org.eclipse.jdt.core.ICompilationUnit;
23: import org.eclipse.jdt.core.IJavaElement;
24: import org.eclipse.jdt.core.search.SearchMatch;
25:
26: import org.eclipse.jdt.internal.corext.util.SearchUtils;
27:
28: public class SearchResultGroup {
29:
30: private final IResource fResouce;
31: private final List fSearchMatches;
32:
33: public SearchResultGroup(IResource res, SearchMatch[] matches) {
34: Assert.isNotNull(matches);
35: fResouce = res;
36: fSearchMatches = new ArrayList(Arrays.asList(matches));
37: }
38:
39: public void add(SearchMatch match) {
40: Assert.isNotNull(match);
41: fSearchMatches.add(match);
42: }
43:
44: public IResource getResource() {
45: return fResouce;
46: }
47:
48: public SearchMatch[] getSearchResults() {
49: return (SearchMatch[]) fSearchMatches
50: .toArray(new SearchMatch[fSearchMatches.size()]);
51: }
52:
53: public static IResource[] getResources(
54: SearchResultGroup[] searchResultGroups) {
55: Set resourceSet = new HashSet(searchResultGroups.length);
56: for (int i = 0; i < searchResultGroups.length; i++) {
57: resourceSet.add(searchResultGroups[i].getResource());
58: }
59: return (IResource[]) resourceSet
60: .toArray(new IResource[resourceSet.size()]);
61: }
62:
63: public ICompilationUnit getCompilationUnit() {
64: if (getSearchResults() == null
65: || getSearchResults().length == 0)
66: return null;
67: return SearchUtils.getCompilationUnit(getSearchResults()[0]);
68: }
69:
70: public String toString() {
71: StringBuffer buf = new StringBuffer(fResouce.getFullPath()
72: .toString());
73: buf.append('\n');
74: for (int i = 0; i < fSearchMatches.size(); i++) {
75: SearchMatch match = (SearchMatch) fSearchMatches.get(i);
76: buf
77: .append(" ").append(match.getOffset()).append(", ").append(match.getLength()); //$NON-NLS-1$//$NON-NLS-2$
78: buf
79: .append(match.getAccuracy() == SearchMatch.A_ACCURATE ? "; acc" : "; inacc"); //$NON-NLS-1$//$NON-NLS-2$
80: if (match.isInsideDocComment())
81: buf.append("; inDoc"); //$NON-NLS-1$
82: if (match.getElement() instanceof IJavaElement)
83: buf
84: .append("; in: ").append(((IJavaElement) match.getElement()).getElementName()); //$NON-NLS-1$
85: buf.append('\n');
86: }
87: return buf.toString();
88: }
89: }
|