1 11 12 package org.eclipse.jdt.ui; 13 14 import java.util.regex.Pattern ; 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 28 public class CodeStyleConfiguration { 29 30 private static final Pattern SEMICOLON_PATTERN= Pattern.compile(";"); 32 private CodeStyleConfiguration() { 33 } 35 36 37 49 public static ImportRewrite createImportRewrite(ICompilationUnit cu, boolean restoreExistingImports) throws JavaModelException { 50 return configureImportRewrite(ImportRewrite.create(cu, restoreExistingImports)); 51 } 52 53 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 order= PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IMPORTORDER, project); 70 rewrite.setImportOrder(SEMICOLON_PATTERN.split(order, 0)); 71 72 String 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 e) { 79 } 81 String 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 e) { 88 } 90 return rewrite; 91 } 92 93 94 95 } 96 | Popular Tags |