1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 public static final String FIND_BROKEN_NLS_KEYS_ACTION_ID= "org.eclipse.jdt.ui.edit.text.java.find.broken.nls.keys"; 77 public static final String ACTION_HANDLER_ID= "org.eclipse.jdt.ui.actions.FindNLSProblems"; 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 93 public FindBrokenNLSKeysAction(JavaEditor editor) { 94 this(editor.getEditorSite()); 95 fEditor= editor; 96 setEnabled(getCompilationUnit(editor) != null); 97 } 98 99 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 113 public void run(IStructuredSelection selection) { 114 if (selection.size() == 1) { 115 Object 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 scope= "workspace"; if (selection.size() == 1) { 132 Object 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 scope) { 145 List wrappers= new ArrayList (); 146 List properties= new ArrayList (); 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 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 174 public void selectionChanged(IStructuredSelection selection) { 175 setEnabled(canEnable(selection)); 176 } 177 178 private SearchPatternData[] getNLSFiles(IStructuredSelection selection) { 179 Object [] selectedElements= selection.toArray(); 180 List result= new ArrayList (); 181 collectNLSFiles(selectedElements, result); 182 return (SearchPatternData[])result.toArray(new SearchPatternData[result.size()]); 183 } 184 185 private boolean canEnable(IStructuredSelection selection) { 186 Object [] 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())) 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 [] objects, List 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 [] {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 [] {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 [] {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 [] {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())) return null; 319 320 IPath propertyFullPath= file.getFullPath(); 321 String [] javaExtensions= JavaCore.getJavaLikeExtensions(); 323 for (int i= 0; i < javaExtensions.length; i++) { 324 String extension= javaExtensions[i]; 325 IPath cuPath= propertyFullPath.removeFileExtension().addFileExtension(extension); 326 IFile cuFile= (IFile)JavaPlugin.getWorkspace().getRoot().findMember(cuPath); 327 328 if (cuFile == null) { String 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 resourceBundleName= NLSHintHelper.getResourceBundleName(compilationUnit); 345 if (resourceBundleName != null) { 346 String resourceName= resourceBundleName + NLSRefactoring.PROPERTY_FILE_EXT; 347 String 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 |