001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui;
011:
012: import java.util.regex.Pattern;
013:
014: import org.eclipse.jdt.core.ICompilationUnit;
015: import org.eclipse.jdt.core.IJavaProject;
016: import org.eclipse.jdt.core.JavaModelException;
017: import org.eclipse.jdt.core.dom.CompilationUnit;
018: import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
019:
020: /**
021: * Gives access to the import rewrite configured with the settings as specified in the user interface.
022: * These settings are kept in JDT UI for compatibility reasons.
023: *
024: * @since 3.2
025: */
026: public class CodeStyleConfiguration {
027:
028: private static final Pattern SEMICOLON_PATTERN = Pattern
029: .compile(";"); //$NON-NLS-1$
030:
031: private CodeStyleConfiguration() {
032: // do not instantiate and subclass
033: }
034:
035: /**
036: * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(ICompilationUnit, boolean)} and
037: * configures the rewriter with the settings as specified in the JDT UI preferences.
038: * <p>
039: *
040: * @param cu the compilation unit to create the rewriter on
041: * @param restoreExistingImports specifies if the existing imports should be kept or removed.
042: * @return the new rewriter configured with the settings as specified in the JDT UI preferences.
043: * @throws JavaModelException thrown when the compilation unit could not be accessed.
044: *
045: * @see ImportRewrite#create(ICompilationUnit, boolean)
046: */
047: public static ImportRewrite createImportRewrite(
048: ICompilationUnit cu, boolean restoreExistingImports)
049: throws JavaModelException {
050: return configureImportRewrite(ImportRewrite.create(cu,
051: restoreExistingImports));
052: }
053:
054: /**
055: * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(CompilationUnit, boolean)} and
056: * configures the rewriter with the settings as specified in the JDT UI preferences.
057: *
058: * @param astRoot the AST root to create the rewriter on
059: * @param restoreExistingImports specifies if the existing imports should be kept or removed.
060: * @return the new rewriter configured with the settings as specified in the JDT UI preferences.
061: *
062: * @see ImportRewrite#create(CompilationUnit, boolean)
063: */
064: public static ImportRewrite createImportRewrite(
065: CompilationUnit astRoot, boolean restoreExistingImports) {
066: return configureImportRewrite(ImportRewrite.create(astRoot,
067: restoreExistingImports));
068: }
069:
070: private static ImportRewrite configureImportRewrite(
071: ImportRewrite rewrite) {
072: IJavaProject project = rewrite.getCompilationUnit()
073: .getJavaProject();
074: String order = PreferenceConstants.getPreference(
075: PreferenceConstants.ORGIMPORTS_IMPORTORDER, project);
076: rewrite.setImportOrder(SEMICOLON_PATTERN.split(order, 0));
077:
078: String thres = PreferenceConstants.getPreference(
079: PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD,
080: project);
081: try {
082: int num = Integer.parseInt(thres);
083: if (num == 0)
084: num = 1;
085: rewrite.setOnDemandImportThreshold(num);
086: } catch (NumberFormatException e) {
087: // ignore
088: }
089: String thresStatic = PreferenceConstants
090: .getPreference(
091: PreferenceConstants.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD,
092: project);
093: try {
094: int num = Integer.parseInt(thresStatic);
095: if (num == 0)
096: num = 1;
097: rewrite.setStaticOnDemandImportThreshold(num);
098: } catch (NumberFormatException e) {
099: // ignore
100: }
101: return rewrite;
102: }
103:
104: }
|