1 11 package org.eclipse.jdt.internal.corext.refactoring.nls; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.resources.ResourcesPlugin; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 20 import org.eclipse.jface.preference.IPreferenceStore; 21 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaProject; 25 import org.eclipse.jdt.core.IPackageFragment; 26 import org.eclipse.jdt.core.IPackageFragmentRoot; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.formatter.CodeFormatter; 29 30 import org.eclipse.jdt.ui.CodeGeneration; 31 import org.eclipse.jdt.ui.PreferenceConstants; 32 33 import org.eclipse.jdt.internal.corext.codemanipulation.ImportsStructure; 34 import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange; 35 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 36 import org.eclipse.jdt.internal.corext.util.WorkingCopyUtil; 37 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 38 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 39 40 import org.eclipse.ltk.core.refactoring.Change; 41 42 class AccessorClass { 43 44 private final ICompilationUnit fCu; 45 private final String fAccessorClassName; 46 private final IPath fAccessorPath; 47 private final IPath fResourceBundlePath; 48 private final IPackageFragment fAccessorPackage; 49 50 private static String lineDelim= System.getProperty("line.separator", "\n"); 52 private AccessorClass(ICompilationUnit cu, String accessorClassname, IPath accessorPath, IPackageFragment accessorPackage, IPath resourceBundlePath) { 53 fCu= cu; 54 fAccessorClassName= accessorClassname; 55 fAccessorPath= accessorPath; 56 fAccessorPackage= accessorPackage; 57 fResourceBundlePath= resourceBundlePath; 58 } 59 60 public static Change create(ICompilationUnit cu, String accessorClassname, IPath accessorPath, IPackageFragment accessorPackage, IPath resourceBundlePath, IProgressMonitor pm) throws CoreException { 61 AccessorClass accessorClass= new AccessorClass(cu, accessorClassname, accessorPath, accessorPackage, resourceBundlePath); 62 63 return new CreateTextFileChange(accessorPath, accessorClass.createAccessorCUSource(pm), null, "java"); } 65 66 private String createAccessorCUSource(IProgressMonitor pm) throws CoreException { 67 return CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, getUnformattedSource(pm), 0, null, null, fCu.getJavaProject()); 68 } 69 70 private String getUnformattedSource(IProgressMonitor pm) throws CoreException { 71 ICompilationUnit newCu= null; 72 try { 73 newCu= WorkingCopyUtil.getNewWorkingCopy(fAccessorPackage, fAccessorPath.lastSegment()); 74 75 String comment= CodeGeneration.getTypeComment(newCu, fAccessorClassName, lineDelim); 76 String classContent= createClass(); 77 String cuContent= CodeGeneration.getCompilationUnitContent(newCu, comment, classContent, lineDelim); 78 if (cuContent == null) { 79 StringBuffer buf= new StringBuffer (); 80 if (!fAccessorPackage.isDefaultPackage()) { 81 buf.append("package ").append(fAccessorPackage.getElementName()).append(';'); } 83 buf.append(lineDelim).append(lineDelim); 84 if (comment != null) { 85 buf.append(comment).append(lineDelim); 86 } 87 buf.append(classContent); 88 cuContent= buf.toString(); 89 } 90 91 newCu.getBuffer().setContents(cuContent); 92 addImportsToAccessorCu(newCu, pm); 93 return newCu.getSource(); 94 } finally { 95 if (newCu != null) { 96 newCu.discardWorkingCopy(); 97 } 98 } 99 } 100 101 private void addImportsToAccessorCu(ICompilationUnit newCu, IProgressMonitor pm) throws CoreException { 102 IPreferenceStore store= PreferenceConstants.getPreferenceStore(); 103 String [] order= JavaPreferencesSettings.getImportOrderPreference(store); 104 int importThreshold= JavaPreferencesSettings.getImportNumberThreshold(store); 105 ImportsStructure is= new ImportsStructure(newCu, order, importThreshold, true); 106 is.addImport("java.util.MissingResourceException"); is.addImport("java.util.ResourceBundle"); is.create(false, pm); 109 } 110 111 private String createClass() throws CoreException { 112 String ld= lineDelim; return "public class " + fAccessorClassName + " {" + "private static final String " + NLSRefactoring.BUNDLE_NAME + " = \"" + getResourceBundleName() + "\";" + NLSElement.createTagText(1) + ld + ld + "private static final ResourceBundle " + getResourceBundleConstantName() + "= ResourceBundle.getBundle(" + NLSRefactoring.BUNDLE_NAME + ");" + ld + ld + createConstructor() + ld + createGetStringMethod() + ld + "}" + ld; } 119 120 private String getResourceBundleConstantName() { 121 return "RESOURCE_BUNDLE"; } 123 124 private String createGetStringMethod() throws CoreException { 125 String bodyStatement= new StringBuffer ().append("try {").append(lineDelim) .append("return ") .append(getResourceBundleConstantName()).append(".getString(key);").append(lineDelim) .append("} catch (MissingResourceException e) {").append(lineDelim) .append("return '!' + key + '!';").append(lineDelim) .append("}").toString(); 132 String methodBody= CodeGeneration.getMethodBodyContent(fCu, fAccessorClassName, "getString", false, bodyStatement, lineDelim); 134 if (methodBody == null) { 135 methodBody= ""; } 137 return "public static String getString(String key) {" + lineDelim + methodBody + lineDelim + '}'; 139 } 140 141 private String createConstructor() { 142 return "private " + fAccessorClassName + "(){" + lineDelim + '}'; 144 } 145 146 163 164 private String getPropertyFileName() { 165 return fResourceBundlePath.lastSegment(); 166 } 167 168 private String getPropertyFileNameWithoutExtension() { 169 String fileName= getPropertyFileName(); 170 return fileName.substring(0, fileName.indexOf(NLSRefactoring.PROPERTY_FILE_EXT)); 171 } 172 173 private String getResourceBundleName() throws CoreException { 174 IResource res= ResourcesPlugin.getWorkspace().getRoot().findMember(fResourceBundlePath.removeLastSegments(1)); 175 if (res != null && res.exists()) { 176 IJavaElement el= JavaCore.create(res); 177 if (el instanceof IPackageFragment) { 178 IPackageFragment p= (IPackageFragment) el; 179 return p.getElementName() + '.' + getPropertyFileNameWithoutExtension(); 180 } else 181 if ((el instanceof IPackageFragmentRoot) || (el instanceof IJavaProject)) { 182 return getPropertyFileNameWithoutExtension(); 183 } 184 } 185 throw new CoreException(new StatusInfo(IStatus.ERROR, "Resourcebundle not specified")); } 187 } 188 | Popular Tags |