KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > CodeStyleConfiguration


1 /*******************************************************************************
2  * Copyright (c) 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.ui;
13
14 import java.util.regex.Pattern JavaDoc;
15
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.core.dom.CompilationUnit;
20 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
21
22 /**
23  * Gives access to the import rewrite configured with the settings as specified in the user interface.
24  * These settings are kept in JDT UI for compatibility reasons.
25  *
26  * @since 3.2
27  */

28 public class CodeStyleConfiguration {
29     
30     private static final Pattern JavaDoc SEMICOLON_PATTERN= Pattern.compile(";"); //$NON-NLS-1$
31

32     private CodeStyleConfiguration() {
33         // do not instantiate and subclass
34
}
35     
36     
37     /**
38      * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(ICompilationUnit, boolean)} and
39      * configures the rewriter with the settings as specified in the JDT UI preferences.
40      * <p>
41      *
42      * @param cu the compilation unit to create the rewriter on
43      * @param restoreExistingImports specifies if the existing imports should be kept or removed.
44      * @return the new rewriter configured with the settings as specified in the JDT UI preferences.
45      * @throws JavaModelException thrown when the compilation unit could not be accessed.
46      *
47      * @see ImportRewrite#create(ICompilationUnit, boolean)
48      */

49     public static ImportRewrite createImportRewrite(ICompilationUnit cu, boolean restoreExistingImports) throws JavaModelException {
50         return configureImportRewrite(ImportRewrite.create(cu, restoreExistingImports));
51     }
52     
53     /**
54      * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(CompilationUnit, boolean)} and
55      * configures the rewriter with the settings as specified in the JDT UI preferences.
56      *
57      * @param astRoot the AST root to create the rewriter on
58      * @param restoreExistingImports specifies if the existing imports should be kept or removed.
59      * @return the new rewriter configured with the settings as specified in the JDT UI preferences.
60      *
61      * @see ImportRewrite#create(CompilationUnit, boolean)
62      */

63     public static ImportRewrite createImportRewrite(CompilationUnit astRoot, boolean restoreExistingImports) {
64         return configureImportRewrite(ImportRewrite.create(astRoot, restoreExistingImports));
65     }
66     
67     private static ImportRewrite configureImportRewrite(ImportRewrite rewrite) {
68         IJavaProject project= rewrite.getCompilationUnit().getJavaProject();
69         String JavaDoc order= PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IMPORTORDER, project);
70         rewrite.setImportOrder(SEMICOLON_PATTERN.split(order, 0));
71
72         String JavaDoc thres= PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD, project);
73         try {
74             int num= Integer.parseInt(thres);
75             if (num == 0)
76                 num= 1;
77             rewrite.setOnDemandImportThreshold(num);
78         } catch (NumberFormatException JavaDoc e) {
79             // ignore
80
}
81         String JavaDoc thresStatic= PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD, project);
82         try {
83             int num= Integer.parseInt(thresStatic);
84             if (num == 0)
85                 num= 1;
86             rewrite.setStaticOnDemandImportThreshold(num);
87         } catch (NumberFormatException JavaDoc e) {
88             // ignore
89
}
90         return rewrite;
91     }
92
93
94
95 }
96
Popular Tags