1 12 package org.eclipse.jdt.internal.ui.fix; 13 14 import com.ibm.icu.text.MessageFormat; 15 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 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 fTouchedFiles; 44 45 public SortMembersCleanUp() { 46 super(); 47 } 48 49 public SortMembersCleanUp(Map 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 (); 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()); 77 try { 78 RefactoringStatus result= new RefactoringStatus(); 79 for (Iterator iterator= fTouchedFiles.iterator(); iterator.hasNext();) { 80 IFile file= (IFile)iterator.next(); 81 if (containsRelevantMarkers(file)) { 82 String fileLocation= file.getProjectRelativePath().toOSString(); 83 String projectName= file.getProject().getName(); 84 result.addWarning(MessageFormat.format(MultiFixMessages.SortMembersCleanUp_RemoveMarkersWarning0, new Object [] {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 102 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 103 return null; 104 } 105 106 public Map getRequiredOptions() { 107 return null; 108 } 109 110 113 public String [] getDescriptions() { 114 if (isEnabled(CleanUpConstants.SORT_MEMBERS)) { 115 if (isEnabled(CleanUpConstants.SORT_MEMBERS_ALL)) { 116 return new String [] {MultiFixMessages.SortMembersCleanUp_AllMembers_description}; 117 } else { 118 return new String [] {MultiFixMessages.SortMembersCleanUp_Excluding_description}; 119 } 120 } 121 return null; 122 } 123 124 public String getPreview() { 125 StringBuffer buf= new StringBuffer (); 126 127 buf.append("class SortExample {\n"); 129 if ((isEnabled(CleanUpConstants.SORT_MEMBERS) && isEnabled(CleanUpConstants.SORT_MEMBERS_ALL))) { 130 buf.append(" private String bar;\n"); buf.append(" private String foo;\n"); } else { 133 buf.append(" private String foo;\n"); buf.append(" private String bar;\n"); } 136 137 if (isEnabled(CleanUpConstants.SORT_MEMBERS)) { 138 buf.append(" private void bar() {}\n"); buf.append(" private void foo() {}\n"); } else { 141 buf.append(" private void foo() {}\n"); buf.append(" private void bar() {}\n"); } 144 145 buf.append("}\n"); 147 return buf.toString(); 148 } 149 150 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 |