KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > FindBrokenNLSKeysAction


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.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IStorage;
22
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27
28 import org.eclipse.jface.text.ITextSelection;
29
30 import org.eclipse.ui.IWorkbenchSite;
31
32 import org.eclipse.jdt.core.ICompilationUnit;
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.IPackageFragment;
36 import org.eclipse.jdt.core.IPackageFragmentRoot;
37 import org.eclipse.jdt.core.IType;
38 import org.eclipse.jdt.core.JavaCore;
39 import org.eclipse.jdt.core.JavaModelException;
40
41 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSHintHelper;
42 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSRefactoring;
43
44 import org.eclipse.jdt.ui.IWorkingCopyManager;
45 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
46
47 import org.eclipse.jdt.internal.ui.JavaPlugin;
48 import org.eclipse.jdt.internal.ui.browsing.LogicalPackage;
49 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
50 import org.eclipse.jdt.internal.ui.refactoring.nls.search.SearchBrokenNLSKeysUtil;
51
52 public class FindBrokenNLSKeysAction extends SelectionDispatchAction {
53     
54     private static class SearchPatternData {
55
56         private final IType fAccessorType;
57         private final IFile fPropertyFile;
58         
59         public SearchPatternData(IType accessorType, IFile propertyFile) {
60             fAccessorType= accessorType;
61             fPropertyFile= propertyFile;
62         }
63
64         public IFile getPropertyFile() {
65             return fPropertyFile;
66         }
67
68         public IType getWrapperClass() {
69             return fAccessorType;
70         }
71
72     }
73
74     //TODO: Add to API: IJavaEditorActionDefinitionIds
75
public static final String JavaDoc FIND_BROKEN_NLS_KEYS_ACTION_ID= "org.eclipse.jdt.ui.edit.text.java.find.broken.nls.keys"; //$NON-NLS-1$
76

77     //TODO: Add to API: JdtActionConstants
78
public static final String JavaDoc ACTION_HANDLER_ID= "org.eclipse.jdt.ui.actions.FindNLSProblems"; //$NON-NLS-1$
79

80     private JavaEditor fEditor;
81     
82     public FindBrokenNLSKeysAction(IWorkbenchSite site) {
83         super(site);
84         setText(ActionMessages.FindNLSProblemsAction_Name);
85         setToolTipText(ActionMessages.FindNLSProblemsAction_ToolTip);
86         setDescription(ActionMessages.FindNLSProblemsAction_Description);
87     }
88     
89     /**
90      * Note: This constructor is for internal use only. Clients should not call this constructor.
91      * @param editor the Java editor
92      */

93     public FindBrokenNLSKeysAction(JavaEditor editor) {
94         this(editor.getEditorSite());
95         fEditor= editor;
96         setEnabled(getCompilationUnit(editor) != null);
97     }
98
99     /* (non-Javadoc)
100      * Method declared on SelectionDispatchAction.
101      */

102     public void run(ITextSelection selection) {
103         ISelectionProvider selectionProvider= fEditor.getSelectionProvider();
104         if (selectionProvider == null)
105             return;
106         
107         run(new StructuredSelection(selectionProvider.getSelection()));
108     }
109
110     /* (non-Javadoc)
111      * Method declared on SelectionDispatchAction.
112      */

113     public void run(IStructuredSelection selection) {
114         if (selection.size() == 1) {
115             Object JavaDoc firstElement= selection.getFirstElement();
116             if (firstElement instanceof IJavaElement) {
117                 IJavaElement javaElement= (IJavaElement) firstElement;
118                 if (!ActionUtil.isProcessable(getShell(), javaElement)) {
119                     return;
120                 }
121             }
122         }
123         
124         SearchPatternData[] data= getNLSFiles(selection);
125         if (data == null || data.length == 0) {
126             MessageDialog.openInformation(getShell(), ActionMessages.FindNLSProblemsAction_ErrorDialogTitle, ActionMessages.FindNLSProblemsAction_NoPropertieFilesFoundErrorDescription);
127             return;
128         }
129             
130         String JavaDoc scope= "workspace"; //$NON-NLS-1$
131
if (selection.size() == 1) {
132             Object JavaDoc firstElement= selection.getFirstElement();
133             if (firstElement instanceof IJavaElement) {
134                 scope= ((IJavaElement)firstElement).getElementName();
135             } else if (firstElement instanceof IFile) {
136                 scope= ((IFile)firstElement).getName();
137             } else if (firstElement instanceof IFolder) {
138                 scope= ((IFolder)firstElement).getName();
139             }
140         }
141         run(data, scope);
142     }
143     
144     private void run(SearchPatternData[] data, String JavaDoc scope) {
145         List JavaDoc wrappers= new ArrayList JavaDoc();
146         List JavaDoc properties= new ArrayList JavaDoc();
147         for (int i= 0; i < data.length; i++) {
148             SearchPatternData current= data[i];
149             if (current.getWrapperClass() != null || current.getPropertyFile() != null) {
150                 wrappers.add(current.getWrapperClass());
151                 properties.add(current.getPropertyFile());
152             }
153         }
154         IType[] accessorClasses= (IType[])wrappers.toArray(new IType[wrappers.size()]);
155         IFile[] propertieFiles= (IFile[])properties.toArray(new IFile[properties.size()]);
156         SearchBrokenNLSKeysUtil.search(scope, accessorClasses, propertieFiles);
157     }
158
159     /* (non-Javadoc)
160      * Method declared on SelectionDispatchAction.
161      */

162     public void selectionChanged(ITextSelection selection) {
163         ISelectionProvider selectionProvider= fEditor.getSelectionProvider();
164         if (selectionProvider == null) {
165             setEnabled(false);
166         } else {
167             selectionChanged(new StructuredSelection(selectionProvider.getSelection()));
168         }
169     }
170     
171     /* (non-Javadoc)
172      * Method declared on SelectionDispatchAction.
173      */

174     public void selectionChanged(IStructuredSelection selection) {
175         setEnabled(canEnable(selection));
176     }
177     
178     private SearchPatternData[] getNLSFiles(IStructuredSelection selection) {
179         Object JavaDoc[] selectedElements= selection.toArray();
180         List JavaDoc result= new ArrayList JavaDoc();
181         collectNLSFiles(selectedElements, result);
182         return (SearchPatternData[])result.toArray(new SearchPatternData[result.size()]);
183     }
184     
185     private boolean canEnable(IStructuredSelection selection) {
186         Object JavaDoc[] selected= selection.toArray();
187         for (int i= 0; i < selected.length; i++) {
188             try {
189                 if (selected[i] instanceof IJavaElement) {
190                     IJavaElement elem= (IJavaElement) selected[i];
191                     if (elem.exists()) {
192                         switch (elem.getElementType()) {
193                             case IJavaElement.TYPE:
194                                 if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
195                                     return true;
196                                 }
197                                 return false;
198                             case IJavaElement.COMPILATION_UNIT:
199                                 return true;
200                             case IJavaElement.IMPORT_CONTAINER:
201                                 return false;
202                             case IJavaElement.PACKAGE_FRAGMENT:
203                             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
204                                 IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
205                                 return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
206                             case IJavaElement.JAVA_PROJECT:
207                                 return true;
208                         }
209                     }
210                 } else if (selected[i] instanceof LogicalPackage) {
211                     return true;
212                 } else if (selected[i] instanceof IFile) {
213                     IFile file= (IFile)selected[i];
214                     if ("properties".equalsIgnoreCase(file.getFileExtension())) //$NON-NLS-1$
215
return true;
216                 }
217             } catch (JavaModelException e) {
218                 if (!e.isDoesNotExist()) {
219                     JavaPlugin.log(e);
220                 }
221             }
222         }
223         return false;
224     }
225
226     private void collectNLSFiles(Object JavaDoc[] objects, List JavaDoc result) {
227         try {
228             for (int i= 0; i < objects.length; i++) {
229                 if (objects[i] instanceof IJavaElement) {
230                     IJavaElement elem= (IJavaElement) objects[i];
231                     if (elem.exists()) {
232                         switch (elem.getElementType()) {
233                             case IJavaElement.TYPE:
234                                 if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
235                                     SearchPatternData data= tryIfPropertyCuSelected((ICompilationUnit)elem.getParent());
236                                     if (data != null) {
237                                         result.add(data);
238                                     }
239                                 }
240                                 break;
241                             case IJavaElement.COMPILATION_UNIT:
242                                 SearchPatternData data= tryIfPropertyCuSelected((ICompilationUnit)elem);
243                                 if (data != null) {
244                                     result.add(data);
245                                 }
246                                 break;
247                             case IJavaElement.IMPORT_CONTAINER:
248                                 break;
249                             case IJavaElement.PACKAGE_FRAGMENT:
250                                 IPackageFragment fragment= (IPackageFragment)elem;
251                                 if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE)
252                                     collectNLSFiles(new Object JavaDoc[] {fragment.getCorrespondingResource()}, result);
253                                 break;
254                             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
255                             {
256                                 IPackageFragmentRoot root= (IPackageFragmentRoot) elem;
257                                 if (root.getKind() == IPackageFragmentRoot.K_SOURCE)
258                                     collectNLSFiles(new Object JavaDoc[] {root.getCorrespondingResource()}, result);
259                                 break;
260                             }
261                             case IJavaElement.JAVA_PROJECT:
262                             {
263                                 IJavaProject javaProject= (IJavaProject)elem;
264                                 IPackageFragmentRoot[] allPackageFragmentRoots= javaProject.getAllPackageFragmentRoots();
265                                 for (int j= 0; j < allPackageFragmentRoots.length; j++) {
266                                     IPackageFragmentRoot root= allPackageFragmentRoots[j];
267                                     if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
268                                         if (javaProject.equals(root.getJavaProject())) {
269                                             collectNLSFiles(new Object JavaDoc[] {root.getCorrespondingResource()}, result);
270                                         }
271                                     }
272                                 }
273                                 break;
274                             }
275                         }
276                     }
277                 } else if (objects[i] instanceof LogicalPackage) {
278                     LogicalPackage logicalPackage= (LogicalPackage)objects[i];
279                     collectNLSFiles(new Object JavaDoc[] {logicalPackage.getJavaProject()}, result);
280                 } else if (objects[i] instanceof IFolder) {
281                     collectNLSFiles(((IFolder)objects[i]).members(), result);
282                 } else if (objects[i] instanceof IFile) {
283                     SearchPatternData data= tryIfPropertyFileSelected((IFile)objects[i]);
284                     if (data != null) {
285                         result.add(data);
286                     }
287                 }
288             }
289         } catch (JavaModelException e) {
290             if (!e.isDoesNotExist()) {
291                 JavaPlugin.log(e);
292             }
293         } catch (CoreException e) {
294             JavaPlugin.log(e);
295         }
296     }
297     
298     private SearchPatternData tryIfPropertyCuSelected(ICompilationUnit compilationUnit) throws JavaModelException {
299         if (compilationUnit == null)
300             return null;
301         
302         if (!ActionUtil.isOnBuildPath(compilationUnit))
303             return null;
304         
305         IType[] types= compilationUnit.getTypes();
306         if (types.length > 1)
307             return null;
308         
309         IStorage bundle= NLSHintHelper.getResourceBundle(compilationUnit);
310         if (!(bundle instanceof IFile))
311             return null;
312
313         return new SearchPatternData(types[0], (IFile)bundle);
314     }
315     
316     private SearchPatternData tryIfPropertyFileSelected(IFile file) throws JavaModelException {
317         if (!"properties".equalsIgnoreCase(file.getFileExtension())) //$NON-NLS-1$
318
return null;
319         
320         IPath propertyFullPath= file.getFullPath();
321         // Try to find a corresponding CU
322
String JavaDoc[] javaExtensions= JavaCore.getJavaLikeExtensions();
323         for (int i= 0; i < javaExtensions.length; i++) {
324             String JavaDoc extension= javaExtensions[i];
325             IPath cuPath= propertyFullPath.removeFileExtension().addFileExtension(extension);
326             IFile cuFile= (IFile)JavaPlugin.getWorkspace().getRoot().findMember(cuPath);
327             
328             if (cuFile == null) { //try with uppercase first char
329
String JavaDoc filename= cuPath.removeFileExtension().lastSegment();
330                 if (filename != null && filename.length() > 0) {
331                     filename= Character.toUpperCase(filename.charAt(0)) + filename.substring(1);
332                     IPath dirPath= propertyFullPath.removeLastSegments(1).addTrailingSeparator();
333                     cuPath= dirPath.append(filename).addFileExtension(extension);
334                     cuFile= (IFile)JavaPlugin.getWorkspace().getRoot().findMember(cuPath);
335                 }
336             }
337
338             if (cuFile != null && cuFile.exists()) {
339                 IJavaElement element= JavaCore.create(cuFile);
340                 if (element != null && element.exists() && element.getElementType() == IJavaElement.COMPILATION_UNIT && ActionUtil.isOnBuildPath(element)) {
341                     ICompilationUnit compilationUnit= (ICompilationUnit)element;
342                     IType type= (compilationUnit).findPrimaryType();
343                     if (type != null) {
344                         String JavaDoc resourceBundleName= NLSHintHelper.getResourceBundleName(compilationUnit);
345                         if (resourceBundleName != null) {
346                             String JavaDoc resourceName= resourceBundleName + NLSRefactoring.PROPERTY_FILE_EXT;
347                             String JavaDoc name= file.getName();
348                             if (resourceName.endsWith(name)) {
349                                 return new SearchPatternData(type, file);
350                             }
351                         }
352                     }
353                 }
354             }
355         }
356
357         return null;
358     }
359     
360     private static ICompilationUnit getCompilationUnit(JavaEditor editor) {
361         IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
362         ICompilationUnit cu= manager.getWorkingCopy(editor.getEditorInput());
363         return cu;
364     }
365
366 }
367
Popular Tags