KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > ReplaceTask


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
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 /**
32  * Task that checks validity of found files and then
33  * (if everything is valid) replaces the matching substrings
34  * with the replacement string/pattern.
35  *
36  * @author Tim Boudreau
37  * @author Marian Petras
38  */

39 final class ReplaceTask implements Runnable JavaDoc {
40     
41     /**
42      * maximum number of errors detected before replacing
43      * and displayed to the user
44      */

45     private static final int MAX_ERRORS_CHECKED = 20;
46     
47     private final MatchingObject[] matchingObjects;
48     private final ProgressHandle progressHandle;
49     private final List JavaDoc<String JavaDoc> problems;
50     
51     /** */
52     private ResultStatus resultStatus = null;
53     
54     enum ResultStatus {
55         SUCCESS,
56         PRE_CHECK_FAILED,
57         PROBLEMS_ENCOUNTERED
58     }
59     
60     /**
61      */

62     ReplaceTask(MatchingObject[] matchingObjects) {
63         this.matchingObjects = matchingObjects;
64         
65         problems = new ArrayList JavaDoc<String JavaDoc>(4);
66         progressHandle = ProgressHandleFactory.createHandle(
67                 NbBundle.getMessage(getClass(), "LBL_Replacing")); //NOI18N
68
}
69     
70     /**
71      */

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     /**
85      */

86     private void replace() {
87         assert !EventQueue.isDispatchThread();
88         
89         checkForErrors();
90         if (resultStatus == null) { //the check passed
91
doReplace();
92         }
93     }
94     
95     /**
96      */

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     /**
118      *
119      * @return list of strings describing problems that happened during
120      * the replace, or {@code null} if no problem happened
121      */

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 JavaDoc invDescription = obj.getInvalidityDescription();
135             if (invDescription != null) {
136                 problems.add(invDescription);
137                 continue;
138             }
139             
140             String JavaDoc 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 JavaDoc ex) {
149                 ex.printStackTrace(); //PENDING - ex.printStackTrace()?
150
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     /**
164      *
165      * @see #getProblems()
166      */

167     ResultStatus getResultStatus() {
168         return resultStatus;
169     }
170     
171     /**
172      * Returns a list of problems encountered during the pre-check or
173      * during replacing. The type of problems (pre-check or replacing)
174      * can be determined from the results status returned by method
175      * {@link #getResultStatus()}.
176      *
177      * @return array of problems, or {@code null} if no problems have been
178      * encountered
179      * @see #getResultStatus()
180      */

181     String JavaDoc[] getProblems() {
182         return problems.isEmpty()
183                ? null
184                : problems.toArray(new String JavaDoc[problems.size()]);
185     }
186     
187 }
Popular Tags