KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > SearchResultGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.corext.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.core.resources.IResource;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.search.SearchMatch;
26
27 import org.eclipse.jdt.internal.corext.util.SearchUtils;
28
29 public class SearchResultGroup {
30
31     private final IResource fResouce;
32     private final List JavaDoc fSearchMatches;
33     
34     public SearchResultGroup(IResource res, SearchMatch[] matches){
35         Assert.isNotNull(matches);
36         fResouce= res;
37         fSearchMatches= new ArrayList JavaDoc(Arrays.asList(matches));
38     }
39
40     public void add(SearchMatch match) {
41         Assert.isNotNull(match);
42         fSearchMatches.add(match);
43     }
44     
45     public IResource getResource() {
46         return fResouce;
47     }
48     
49     public SearchMatch[] getSearchResults() {
50         return (SearchMatch[]) fSearchMatches.toArray(new SearchMatch[fSearchMatches.size()]);
51     }
52     
53     public static IResource[] getResources(SearchResultGroup[] searchResultGroups){
54         Set JavaDoc resourceSet= new HashSet JavaDoc(searchResultGroups.length);
55         for (int i= 0; i < searchResultGroups.length; i++) {
56             resourceSet.add(searchResultGroups[i].getResource());
57         }
58         return (IResource[]) resourceSet.toArray(new IResource[resourceSet.size()]);
59     }
60     
61     public ICompilationUnit getCompilationUnit(){
62         if (getSearchResults() == null || getSearchResults().length == 0)
63             return null;
64         return SearchUtils.getCompilationUnit(getSearchResults()[0]);
65     }
66     
67     public String JavaDoc toString() {
68         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(fResouce.getFullPath().toString());
69         buf.append('\n');
70         for (int i= 0; i < fSearchMatches.size(); i++) {
71             SearchMatch match= (SearchMatch) fSearchMatches.get(i);
72             buf.append(" ").append(match.getOffset()).append(", ").append(match.getLength()); //$NON-NLS-1$//$NON-NLS-2$
73
buf.append(match.getAccuracy() == SearchMatch.A_ACCURATE ? "; acc" : "; inacc"); //$NON-NLS-1$//$NON-NLS-2$
74
if (match.isInsideDocComment())
75                 buf.append("; inDoc"); //$NON-NLS-1$
76
if (match.getElement() instanceof IJavaElement)
77                 buf.append("; in: ").append(((IJavaElement) match.getElement()).getElementName()); //$NON-NLS-1$
78
buf.append('\n');
79         }
80         return buf.toString();
81     }
82 }
83
Popular Tags