1 19 20 package org.netbeans.modules.search; 21 22 import java.awt.EventQueue ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import org.netbeans.api.progress.ProgressHandle; 27 import org.netbeans.api.progress.ProgressHandleFactory; 28 import org.netbeans.modules.search.MatchingObject.InvalidityStatus; 29 import org.openide.util.NbBundle; 30 31 39 final class ReplaceTask implements Runnable { 40 41 45 private static final int MAX_ERRORS_CHECKED = 20; 46 47 private final MatchingObject[] matchingObjects; 48 private final ProgressHandle progressHandle; 49 private final List <String > problems; 50 51 52 private ResultStatus resultStatus = null; 53 54 enum ResultStatus { 55 SUCCESS, 56 PRE_CHECK_FAILED, 57 PROBLEMS_ENCOUNTERED 58 } 59 60 62 ReplaceTask(MatchingObject[] matchingObjects) { 63 this.matchingObjects = matchingObjects; 64 65 problems = new ArrayList <String >(4); 66 progressHandle = ProgressHandleFactory.createHandle( 67 NbBundle.getMessage(getClass(), "LBL_Replacing")); } 69 70 72 public void run() { 73 assert !EventQueue.isDispatchThread(); 74 75 progressHandle.start(matchingObjects.length * 2); 76 try { 77 replace(); 78 assert resultStatus != null; 79 } finally { 80 progressHandle.finish(); 81 } 82 } 83 84 86 private void replace() { 87 assert !EventQueue.isDispatchThread(); 88 89 checkForErrors(); 90 if (resultStatus == null) { doReplace(); 92 } 93 } 94 95 97 private void checkForErrors() { 98 assert !EventQueue.isDispatchThread(); 99 100 int errorsCount = 0; 101 102 for (int i = 0; i < matchingObjects.length; i++) { 103 InvalidityStatus status = matchingObjects[i].checkValidity(); 104 if (status != null) { 105 problems.add(status.getDescription( 106 matchingObjects[i].getFile().getPath())); 107 if (++errorsCount > MAX_ERRORS_CHECKED) { 108 break; 109 } 110 } 111 } 112 if (!problems.isEmpty()) { 113 resultStatus = ResultStatus.PRE_CHECK_FAILED; 114 } 115 } 116 117 122 private void doReplace() { 123 assert !EventQueue.isDispatchThread(); 124 125 for (int i = 0; i < matchingObjects.length; i++) { 126 final MatchingObject obj = matchingObjects[i]; 127 128 progressHandle.progress(obj.getName(), 129 i + matchingObjects.length); 130 if (!obj.isSelected() || !obj.isValid()) { 131 continue; 132 } 133 134 String invDescription = obj.getInvalidityDescription(); 135 if (invDescription != null) { 136 problems.add(invDescription); 137 continue; 138 } 139 140 String errMessage = null; 141 try { 142 MatchingObject.InvalidityStatus status = obj.replace(); 143 if (status == null) { 144 obj.write(); 145 } else { 146 errMessage = status.getDescription(obj.getFile().getPath()); 147 } 148 } catch (IOException ex) { 149 ex.printStackTrace(); errMessage = ex.getLocalizedMessage(); 151 if (errMessage == null) { 152 errMessage = ex.getMessage(); 153 } 154 } 155 if (errMessage != null) { 156 problems.add(errMessage); 157 } 158 } 159 resultStatus = problems.isEmpty() ? ResultStatus.SUCCESS 160 : ResultStatus.PROBLEMS_ENCOUNTERED; 161 } 162 163 167 ResultStatus getResultStatus() { 168 return resultStatus; 169 } 170 171 181 String [] getProblems() { 182 return problems.isEmpty() 183 ? null 184 : problems.toArray(new String [problems.size()]); 185 } 186 187 } | Popular Tags |