KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > AccessorClass


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc fAccessorClassName;
46     private final IPath fAccessorPath;
47     private final IPath fResourceBundlePath;
48     private final IPackageFragment fAccessorPackage;
49
50     private static String JavaDoc lineDelim= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
51

52     private AccessorClass(ICompilationUnit cu, String JavaDoc 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 JavaDoc 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"); //$NON-NLS-1$
64
}
65
66     private String JavaDoc 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 JavaDoc getUnformattedSource(IProgressMonitor pm) throws CoreException {
71         ICompilationUnit newCu= null;
72         try {
73             newCu= WorkingCopyUtil.getNewWorkingCopy(fAccessorPackage, fAccessorPath.lastSegment());
74
75             String JavaDoc comment= CodeGeneration.getTypeComment(newCu, fAccessorClassName, lineDelim);
76             String JavaDoc classContent= createClass();
77             String JavaDoc cuContent= CodeGeneration.getCompilationUnitContent(newCu, comment, classContent, lineDelim);
78             if (cuContent == null) {
79                 StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
80                 if (!fAccessorPackage.isDefaultPackage()) {
81                     buf.append("package ").append(fAccessorPackage.getElementName()).append(';'); //$NON-NLS-1$
82
}
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 JavaDoc[] 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"); //$NON-NLS-1$
107
is.addImport("java.util.ResourceBundle"); //$NON-NLS-1$
108
is.create(false, pm);
109     }
110
111     private String JavaDoc createClass() throws CoreException {
112         String JavaDoc ld= lineDelim; //want shorter name
113
return "public class " + fAccessorClassName + " {" //$NON-NLS-2$ //$NON-NLS-1$
114
+ "private static final String " + NLSRefactoring.BUNDLE_NAME + " = \"" + getResourceBundleName() + "\";" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
115
+ NLSElement.createTagText(1) + ld + ld + "private static final ResourceBundle " + getResourceBundleConstantName() + "= ResourceBundle.getBundle(" //$NON-NLS-1$ //$NON-NLS-2$
116
+ NLSRefactoring.BUNDLE_NAME + ");" + ld + ld //$NON-NLS-1$
117
+ createConstructor() + ld + createGetStringMethod() + ld + "}" + ld; //$NON-NLS-1$
118
}
119
120     private String JavaDoc getResourceBundleConstantName() {
121         return "RESOURCE_BUNDLE";//$NON-NLS-1$
122
}
123
124     private String JavaDoc createGetStringMethod() throws CoreException {
125         String JavaDoc bodyStatement= new StringBuffer JavaDoc().append("try {").append(lineDelim) //$NON-NLS-1$
126
.append("return ") //$NON-NLS-1$
127
.append(getResourceBundleConstantName()).append(".getString(key);").append(lineDelim) //$NON-NLS-1$
128
.append("} catch (MissingResourceException e) {").append(lineDelim) //$NON-NLS-1$
129
.append("return '!' + key + '!';").append(lineDelim) //$NON-NLS-1$
130
.append("}").toString(); //$NON-NLS-1$
131

132         String JavaDoc methodBody= CodeGeneration.getMethodBodyContent(fCu, fAccessorClassName, "getString", false, bodyStatement, //$NON-NLS-1$
133
lineDelim);
134         if (methodBody == null) {
135             methodBody= ""; //$NON-NLS-1$
136
}
137         return "public static String getString(String key) {" //$NON-NLS-1$
138
+ lineDelim + methodBody + lineDelim + '}';
139     }
140
141     private String JavaDoc createConstructor() {
142         return "private " + fAccessorClassName + "(){" + //$NON-NLS-2$//$NON-NLS-1$
143
lineDelim + '}';
144     }
145
146     /* Currently not used.
147      private String createGetStringMethodComment() throws CoreException {
148      if (fCodeGenerationSettings.createComments) {
149      String comment= CodeGeneration.getMethodComment(fCu, fAccessorClassName, "getString", //$NON-NLS-1$
150      new String[]{"key"}, //$NON-NLS-1$
151      new String[0], "QString;", //$NON-NLS-1$
152      null, lineDelim);
153      if (comment == null) {
154      return "";//$NON-NLS-1$
155      }
156
157      return comment + lineDelim;
158      } else {
159      return "";//$NON-NLS-1$
160      }
161      }
162      */

163
164     private String JavaDoc getPropertyFileName() {
165         return fResourceBundlePath.lastSegment();
166     }
167
168     private String JavaDoc getPropertyFileNameWithoutExtension() {
169         String JavaDoc fileName= getPropertyFileName();
170         return fileName.substring(0, fileName.indexOf(NLSRefactoring.PROPERTY_FILE_EXT));
171     }
172
173     private String JavaDoc 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")); //$NON-NLS-1$
186
}
187 }
188
Popular Tags