KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > search > NLSSearchQuery


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
12 package org.eclipse.jdt.internal.ui.refactoring.nls.search;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.SubProgressMonitor;
19
20 import org.eclipse.core.resources.IFile;
21
22 import org.eclipse.search.ui.ISearchQuery;
23 import org.eclipse.search.ui.ISearchResult;
24 import org.eclipse.search.ui.text.AbstractTextSearchResult;
25 import org.eclipse.search.ui.text.Match;
26
27 import org.eclipse.jdt.core.Flags;
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.IField;
30 import org.eclipse.jdt.core.IJavaElement;
31 import org.eclipse.jdt.core.ISourceRange;
32 import org.eclipse.jdt.core.IType;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jdt.core.search.IJavaSearchConstants;
35 import org.eclipse.jdt.core.search.IJavaSearchScope;
36 import org.eclipse.jdt.core.search.SearchEngine;
37 import org.eclipse.jdt.core.search.SearchParticipant;
38 import org.eclipse.jdt.core.search.SearchPattern;
39
40 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSRefactoring;
41 import org.eclipse.jdt.internal.corext.util.Messages;
42 import org.eclipse.jdt.internal.corext.util.SearchUtils;
43
44 import org.eclipse.jdt.ui.JavaElementLabels;
45
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.JavaUIStatus;
48 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
49
50
51 public class NLSSearchQuery implements ISearchQuery {
52
53     private NLSSearchResult fResult;
54     private IJavaElement[] fWrapperClass;
55     private IFile[] fPropertiesFile;
56     private IJavaSearchScope fScope;
57     private String JavaDoc fScopeDescription;
58     
59     public NLSSearchQuery(IJavaElement[] wrapperClass, IFile[] propertiesFile, IJavaSearchScope scope, String JavaDoc scopeDescription) {
60         fWrapperClass= wrapperClass;
61         fPropertiesFile= propertiesFile;
62         fScope= scope;
63         fScopeDescription= scopeDescription;
64     }
65     
66     /*
67      * @see org.eclipse.search.ui.ISearchQuery#run(org.eclipse.core.runtime.IProgressMonitor)
68      */

69     public IStatus run(IProgressMonitor monitor) {
70         monitor.beginTask("", 5 * fWrapperClass.length); //$NON-NLS-1$
71

72         try {
73             final AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult();
74             textResult.removeAll();
75             AppearanceAwareLabelProvider labelProvider= new AppearanceAwareLabelProvider(JavaElementLabels.ALL_POST_QUALIFIED, 0);
76             
77             for (int i= 0; i < fWrapperClass.length; i++) {
78                 IJavaElement wrapperClass= fWrapperClass[i];
79                 IFile propertieFile= fPropertiesFile[i];
80                 if (! wrapperClass.exists())
81                     return JavaUIStatus.createError(0, Messages.format(NLSSearchMessages.NLSSearchQuery_wrapperNotExists, wrapperClass.getElementName()), null);
82                 if (! wrapperClass.exists())
83                     return JavaUIStatus.createError(0, Messages.format(NLSSearchMessages.NLSSearchQuery_propertiesNotExists, propertieFile.getName()), null);
84                 
85                 SearchPattern pattern= SearchPattern.createPattern(wrapperClass, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
86                 SearchParticipant[] participants= new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};
87                 
88                 NLSSearchResultRequestor requestor= new NLSSearchResultRequestor(propertieFile, fResult);
89                 try {
90                     SearchEngine engine= new SearchEngine();
91                     engine.search(pattern, participants, fScope, requestor, new SubProgressMonitor(monitor, 4));
92                     requestor.reportUnusedPropertyNames(new SubProgressMonitor(monitor, 1));
93                     
94                     ICompilationUnit compilationUnit= ((IType)wrapperClass).getCompilationUnit();
95                     CompilationUnitEntry groupElement= new CompilationUnitEntry(Messages.format(NLSSearchMessages.NLSSearchResultCollector_unusedKeys, labelProvider.getText(compilationUnit)), compilationUnit);
96                     
97                     boolean hasUnusedPropertie= false;
98                     IField[] fields= ((IType)wrapperClass).getFields();
99                     for (int j= 0; j < fields.length; j++) {
100                         IField field= fields[j];
101                         if (isNLSField(field)) {
102                             ISourceRange sourceRange= field.getSourceRange();
103                             if (sourceRange != null) {
104                                 String JavaDoc fieldName= field.getElementName();
105                                 if (!requestor.hasPropertyKey(fieldName)) {
106                                     fResult.addMatch(new Match(compilationUnit, sourceRange.getOffset(), sourceRange.getLength()));
107                                 }
108                                 if (!requestor.isUsedPropertyKey(fieldName)) {
109                                     hasUnusedPropertie= true;
110                                     fResult.addMatch(new Match(groupElement, sourceRange.getOffset(), sourceRange.getLength()));
111                                 }
112                             }
113                         }
114                     }
115                     if (hasUnusedPropertie)
116                         fResult.addCompilationUnitGroup(groupElement);
117                     
118                 } catch (CoreException e) {
119                     JavaPlugin.log(e);
120                 }
121             }
122         } finally {
123             monitor.done();
124         }
125         return Status.OK_STATUS;
126     }
127
128     private boolean isNLSField(IField field) throws JavaModelException {
129         int flags= field.getFlags();
130         if (!Flags.isPublic(flags))
131             return false;
132         
133         if (!Flags.isStatic(flags))
134             return false;
135         
136         String JavaDoc fieldName= field.getElementName();
137         if (NLSRefactoring.BUNDLE_NAME.equals(fieldName))
138             return false;
139         
140         if ("RESOURCE_BUNDLE".equals(fieldName)) //$NON-NLS-1$
141
return false;
142                 
143         return true;
144     }
145
146     /*
147      * @see org.eclipse.search.ui.ISearchQuery#getLabel()
148      */

149     public String JavaDoc getLabel() {
150         return NLSSearchMessages.NLSSearchQuery_label;
151     }
152
153     public String JavaDoc getResultLabel(int nMatches) {
154         if (fWrapperClass.length == 1) {
155             if (nMatches == 1) {
156                 String JavaDoc[] args= new String JavaDoc[] {fWrapperClass[0].getElementName(), fScopeDescription};
157                 return Messages.format(NLSSearchMessages.SearchOperation_singularLabelPostfix, args);
158             }
159             String JavaDoc[] args= new String JavaDoc[] {fWrapperClass[0].getElementName(), String.valueOf(nMatches), fScopeDescription};
160             return Messages.format(NLSSearchMessages.SearchOperation_pluralLabelPatternPostfix, args);
161         } else {
162             if (nMatches == 1) {
163                 return Messages.format(NLSSearchMessages.NLSSearchQuery_oneProblemInScope_description, fScopeDescription);
164             }
165             return Messages.format(NLSSearchMessages.NLSSearchQuery_xProblemsInScope_description, new Object JavaDoc[] {String.valueOf(nMatches), fScopeDescription});
166         }
167     }
168     
169     /*
170      * @see org.eclipse.search.ui.ISearchQuery#canRerun()
171      */

172     public boolean canRerun() {
173         return true;
174     }
175
176     /*
177      * @see org.eclipse.search.ui.ISearchQuery#canRunInBackground()
178      */

179     public boolean canRunInBackground() {
180         return true;
181     }
182
183     /*
184      * @see org.eclipse.search.ui.ISearchQuery#getSearchResult()
185      */

186     public ISearchResult getSearchResult() {
187         if (fResult == null)
188             fResult= new NLSSearchResult(this);
189         return fResult;
190     }
191 }
192
Popular Tags