KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > SortMembersCleanUp


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.ui.fix;
13
14 import com.ibm.icu.text.MessageFormat;
15
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IMarker;
26 import org.eclipse.core.resources.IResource;
27
28 import org.eclipse.debug.core.model.IBreakpoint;
29
30 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
31
32 import org.eclipse.jdt.core.ICompilationUnit;
33 import org.eclipse.jdt.core.dom.CompilationUnit;
34
35 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
36 import org.eclipse.jdt.internal.corext.fix.IFix;
37 import org.eclipse.jdt.internal.corext.fix.SortMembersFix;
38
39 import org.eclipse.jdt.ui.text.java.IProblemLocation;
40
41 public class SortMembersCleanUp extends AbstractCleanUp {
42     
43     private HashSet JavaDoc fTouchedFiles;
44
45     public SortMembersCleanUp() {
46         super();
47     }
48     
49     public SortMembersCleanUp(Map JavaDoc options) {
50         super(options);
51     }
52
53     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
54         if (compilationUnit == null)
55             return null;
56         
57         boolean sortMembers= isEnabled(CleanUpConstants.SORT_MEMBERS);
58         IFix fix= SortMembersFix.createCleanUp(compilationUnit, sortMembers, sortMembers && isEnabled(CleanUpConstants.SORT_MEMBERS_ALL));
59         if (fix != null) {
60             if (fTouchedFiles == null) {
61                 fTouchedFiles= new HashSet JavaDoc();
62             }
63             fTouchedFiles.add(((ICompilationUnit)compilationUnit.getJavaElement()).getResource());
64         }
65         return fix;
66     }
67     
68     public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException {
69         if (fTouchedFiles == null) {
70             return super.checkPostConditions(monitor);
71         } else {
72             if (monitor == null)
73                 monitor= new NullProgressMonitor();
74             
75             monitor.beginTask("", fTouchedFiles.size()); //$NON-NLS-1$
76

77             try {
78                 RefactoringStatus result= new RefactoringStatus();
79                 for (Iterator JavaDoc iterator= fTouchedFiles.iterator(); iterator.hasNext();) {
80                     IFile file= (IFile)iterator.next();
81                     if (containsRelevantMarkers(file)) {
82                         String JavaDoc fileLocation= file.getProjectRelativePath().toOSString();
83                         String JavaDoc projectName= file.getProject().getName();
84                         result.addWarning(MessageFormat.format(MultiFixMessages.SortMembersCleanUp_RemoveMarkersWarning0, new Object JavaDoc[] {fileLocation, projectName}));
85                     }
86                     
87                     monitor.worked(1);
88                 }
89                 
90                 return result;
91             } finally {
92                 monitor.done();
93                 fTouchedFiles= null;
94             }
95             
96         }
97     }
98
99     /**
100      * {@inheritDoc}
101      */

102     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
103         return null;
104     }
105
106     public Map JavaDoc getRequiredOptions() {
107         return null;
108     }
109     
110     /**
111      * {@inheritDoc}
112      */

113     public String JavaDoc[] getDescriptions() {
114         if (isEnabled(CleanUpConstants.SORT_MEMBERS)) {
115             if (isEnabled(CleanUpConstants.SORT_MEMBERS_ALL)) {
116                 return new String JavaDoc[] {MultiFixMessages.SortMembersCleanUp_AllMembers_description};
117             } else {
118                 return new String JavaDoc[] {MultiFixMessages.SortMembersCleanUp_Excluding_description};
119             }
120         }
121         return null;
122     }
123     
124     public String JavaDoc getPreview() {
125         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
126         
127         buf.append("class SortExample {\n"); //$NON-NLS-1$
128

129         if ((isEnabled(CleanUpConstants.SORT_MEMBERS) && isEnabled(CleanUpConstants.SORT_MEMBERS_ALL))) {
130             buf.append(" private String bar;\n"); //$NON-NLS-1$
131
buf.append(" private String foo;\n"); //$NON-NLS-1$
132
} else {
133             buf.append(" private String foo;\n"); //$NON-NLS-1$
134
buf.append(" private String bar;\n"); //$NON-NLS-1$
135
}
136         
137         if (isEnabled(CleanUpConstants.SORT_MEMBERS)) {
138             buf.append(" private void bar() {}\n"); //$NON-NLS-1$
139
buf.append(" private void foo() {}\n"); //$NON-NLS-1$
140
} else {
141             buf.append(" private void foo() {}\n"); //$NON-NLS-1$
142
buf.append(" private void bar() {}\n"); //$NON-NLS-1$
143
}
144         
145         buf.append("}\n"); //$NON-NLS-1$
146

147         return buf.toString();
148     }
149
150     /**
151      * {@inheritDoc}
152      */

153     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
154         return -1;
155     }
156
157     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
158         return false;
159     }
160     
161     public boolean requireAST(ICompilationUnit unit) throws CoreException {
162         return isEnabled(CleanUpConstants.SORT_MEMBERS);
163     }
164     
165     private static boolean containsRelevantMarkers(IFile file) throws CoreException {
166         IMarker[] bookmarks= file.findMarkers(IMarker.BOOKMARK, true, IResource.DEPTH_INFINITE);
167         if (bookmarks.length != 0)
168             return true;
169         
170         IMarker[] tasks= file.findMarkers(IMarker.TASK, true, IResource.DEPTH_INFINITE);
171         if (tasks.length != 0)
172             return true;
173         
174         IMarker[] breakpoints= file.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
175         if (breakpoints.length != 0)
176             return true;
177         
178         return false;
179     }
180
181 }
182
Popular Tags