2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.search;
44
import java.awt.EventQueue;
45
import java.io.IOException;
46
import java.util.ArrayList;
47
import java.util.List;
48
import org.netbeans.api.progress.ProgressHandle;
49
import org.netbeans.api.progress.ProgressHandleFactory;
50
import org.netbeans.modules.search.MatchingObject.InvalidityStatus;
51
import org.openide.filesystems.FileAlreadyLockedException;
52
import org.openide.filesystems.FileLock;
53
import org.openide.util.NbBundle;
54
import org.openide.util.UserQuestionException;
57
* Task that checks validity of found files and then
58
* (if everything is valid) replaces the matching substrings
59
* with the replacement string/pattern.
61
* @author Tim Boudreau
62
* @author Marian Petras
64
final class ReplaceTask implements Runnable {
67
* maximum number of errors detected before replacing
68
* and displayed to the user
70
private static final int MAX_ERRORS_CHECKED = 20;
72
private final MatchingObject[] matchingObjects;
73
private final ProgressHandle progressHandle;
74
private final List<String> problems;
77
private ResultStatus resultStatus = null;
87
ReplaceTask(MatchingObject[] matchingObjects) {
88
this.matchingObjects = matchingObjects;
90
problems = new ArrayList<String>(4);
91
progressHandle = ProgressHandleFactory.createHandle(
92
NbBundle.getMessage(getClass(), "LBL_Replacing")); //NOI18N
98
assert !EventQueue.isDispatchThread();
100
progressHandle.start(matchingObjects.length * 2);
103
assert resultStatus != null;
105
progressHandle.finish();
111
private void replace() {
112
assert !EventQueue.isDispatchThread();
115
if (resultStatus == null) { //the check passed
122
private void checkForErrors() {
123
assert !EventQueue.isDispatchThread();
127
for (int i = 0; i < matchingObjects.length; i++) {
128
InvalidityStatus status = matchingObjects[i].checkValidity();
129
if (status != null) {
130
problems.add(status.getDescription(
131
matchingObjects[i].getFile().getPath()));
132
if (++errorsCount > MAX_ERRORS_CHECKED) {
137
if (!problems.isEmpty()) {
138
resultStatus = ResultStatus.PRE_CHECK_FAILED;
144
* @return list of strings describing problems that happened during
145
* the replace, or {@code null} if no problem happened
147
private void doReplace() {
148
assert !EventQueue.isDispatchThread();
150
for (int i = 0; i < matchingObjects.length; i++) {
151
final MatchingObject obj = matchingObjects[i];
153
progressHandle.progress(obj.getName(),
154
i + matchingObjects.length);
155
if (!obj.isSelected() || !obj.isValid()) {
159
String invDescription = obj.getInvalidityDescription();
160
if (invDescription != null) {
161
problems.add(invDescription);
165
String errMessage = null;
166
FileLock fileLock = null;
168
fileLock = obj.lock();
169
MatchingObject.InvalidityStatus status = obj.replace();
170
if (status == null) {
173
errMessage = status.getDescription(obj.getFile().getPath());
175
} catch (FileAlreadyLockedException ex) {
176
errMessage = createMsgFileLocked(obj);
177
} catch (UserQuestionException ex) {
178
errMessage = createMsgFileLocked(obj);
179
} catch (IOException ex) {
180
ex.printStackTrace(); //PENDING - ex.printStackTrace()?
181
errMessage = ex.getLocalizedMessage();
182
if (errMessage == null) {
183
errMessage = ex.getMessage();
186
if (fileLock != null) {
187
fileLock.releaseLock();
190
if (errMessage != null) {
191
problems.add(errMessage);
194
resultStatus = problems.isEmpty() ? ResultStatus.SUCCESS
195
: ResultStatus.PROBLEMS_ENCOUNTERED;
198
private static String createMsgFileLocked(MatchingObject matchingObj) {
199
return NbBundle.getMessage(
201
"MSG_cannot_access_file_already_locked", //NOI18N
202
matchingObj.getName());
207
* @see #getProblems()
209
ResultStatus getResultStatus() {
214
* Returns a list of problems encountered during the pre-check or
215
* during replacing. The type of problems (pre-check or replacing)
216
* can be determined from the results status returned by method
217
* {@link #getResultStatus()}.
219
* @return array of problems, or {@code null} if no problems have been
221
* @see #getResultStatus()
223
String[] getProblems() {
224
return problems.isEmpty()
226
: problems.toArray(new String[problems.size()]);
b'\\ No newline at end of file'