KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > SortMembersFix


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Alex Blewitt - https://bugs.eclipse.org/bugs/show_bug.cgi?id=168954
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.corext.fix;
13
14 import org.eclipse.text.edits.TextEdit;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
20 import org.eclipse.ltk.core.refactoring.GroupCategory;
21 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
22 import org.eclipse.ltk.core.refactoring.TextChange;
23
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.dom.CompilationUnit;
26 import org.eclipse.jdt.core.util.CompilationUnitSorter;
27
28 import org.eclipse.jdt.internal.corext.codemanipulation.SortMembersOperation.DefaultJavaElementComparator;
29 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
30
31 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
32
33 public class SortMembersFix implements IFix {
34     
35     public static IFix createCleanUp(CompilationUnit compilationUnit, boolean sortMembers, boolean sortFields) throws CoreException {
36         if (!sortMembers && !sortFields)
37             return null;
38         
39         ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement();
40         
41         String JavaDoc label= FixMessages.SortMembersFix_Change_description;
42         CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label)));
43         
44         TextEdit edit= CompilationUnitSorter.sort(compilationUnit, new DefaultJavaElementComparator(!sortFields), 0, group, null);
45         if (edit == null)
46             return null;
47         
48         TextChange change= new CompilationUnitChange(label, cu);
49         change.setEdit(edit);
50         change.addTextEditGroup(group);
51         
52         return new SortMembersFix(change, cu);
53     }
54     
55     private final ICompilationUnit fCompilationUnit;
56     private final TextChange fChange;
57     
58     public SortMembersFix(TextChange change, ICompilationUnit compilationUnit) {
59         fChange= change;
60         fCompilationUnit= compilationUnit;
61     }
62     
63     /**
64      * {@inheritDoc}
65      */

66     public TextChange createChange() throws CoreException {
67         return fChange;
68     }
69     
70     /**
71      * {@inheritDoc}
72      */

73     public ICompilationUnit getCompilationUnit() {
74         return fCompilationUnit;
75     }
76     
77     /**
78      * {@inheritDoc}
79      */

80     public String JavaDoc getDescription() {
81         return FixMessages.SortMembersFix_Fix_description;
82     }
83     
84     /**
85      * {@inheritDoc}
86      */

87     public IStatus getStatus() {
88         return StatusInfo.OK_STATUS;
89     }
90 }
91
Popular Tags