1 12 13 package org.eclipse.jdt.core.util; 14 15 import java.util.Comparator ; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.jdt.core.ICompilationUnit; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.core.dom.AST; 22 import org.eclipse.jdt.core.dom.CompilationUnit; 23 import org.eclipse.jdt.internal.core.SortElementsOperation; 24 import org.eclipse.text.edits.TextEdit; 25 import org.eclipse.text.edits.TextEditGroup; 26 27 36 public final class CompilationUnitSorter { 37 38 41 private CompilationUnitSorter() { 42 } 44 45 48 private static void checkASTLevel(int level) { 49 switch (level) { 50 case AST.JLS2 : 51 case AST.JLS3 : 52 break; 53 default : 54 throw new IllegalArgumentException (); 55 } 56 } 57 58 84 public static final String RELATIVE_ORDER = "relativeOrder"; 86 189 public static void sort(ICompilationUnit compilationUnit, 190 int[] positions, 191 Comparator comparator, 192 int options, 193 IProgressMonitor monitor) throws JavaModelException { 194 sort(AST.JLS2, compilationUnit, positions, comparator, options, monitor); 195 } 196 197 323 public static void sort(int level, ICompilationUnit compilationUnit, 324 int[] positions, 325 Comparator comparator, 326 int options, 327 IProgressMonitor monitor) throws JavaModelException { 328 if (compilationUnit == null || comparator == null) { 329 throw new IllegalArgumentException (); 330 } 331 checkASTLevel(level); 332 ICompilationUnit[] compilationUnits = new ICompilationUnit[] { compilationUnit }; 333 SortElementsOperation operation = new SortElementsOperation(level, compilationUnits, positions, comparator); 334 operation.runOperation(monitor); 335 } 336 337 445 public static TextEdit sort(CompilationUnit unit, 446 Comparator comparator, 447 int options, 448 TextEditGroup group, 449 IProgressMonitor monitor) throws JavaModelException { 450 if (unit == null || comparator == null) { 451 throw new IllegalArgumentException (); 452 } 453 SortElementsOperation operation = new SortElementsOperation(AST.JLS3, new IJavaElement[] { unit.getJavaElement() }, null, comparator); 454 return operation.calculateEdit(unit, group); 455 } 456 } 457 | Popular Tags |