~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to utilities/src/org/netbeans/modules/search/ReplaceTask.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.search;
 
43
 
 
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;
 
55
 
 
56
/**
 
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.
 
60
 * 
 
61
 * @author  Tim Boudreau
 
62
 * @author  Marian Petras
 
63
 */
 
64
final class ReplaceTask implements Runnable {
 
65
    
 
66
    /**
 
67
     * maximum number of errors detected before replacing
 
68
     * and displayed to the user
 
69
     */
 
70
    private static final int MAX_ERRORS_CHECKED = 20;
 
71
    
 
72
    private final MatchingObject[] matchingObjects;
 
73
    private final ProgressHandle progressHandle;
 
74
    private final List<String> problems;
 
75
    
 
76
    /** */
 
77
    private ResultStatus resultStatus = null;
 
78
    
 
79
    enum ResultStatus {
 
80
        SUCCESS,
 
81
        PRE_CHECK_FAILED,
 
82
        PROBLEMS_ENCOUNTERED
 
83
    }
 
84
    
 
85
    /**
 
86
     */
 
87
    ReplaceTask(MatchingObject[] matchingObjects) {
 
88
        this.matchingObjects = matchingObjects;
 
89
        
 
90
        problems = new ArrayList<String>(4);
 
91
        progressHandle = ProgressHandleFactory.createHandle(
 
92
                NbBundle.getMessage(getClass(), "LBL_Replacing"));      //NOI18N
 
93
    }
 
94
    
 
95
    /**
 
96
     */
 
97
    public void run() {
 
98
        assert !EventQueue.isDispatchThread();
 
99
        
 
100
        progressHandle.start(matchingObjects.length * 2);
 
101
        try {
 
102
            replace();
 
103
            assert resultStatus != null;
 
104
        } finally {
 
105
            progressHandle.finish();
 
106
        }
 
107
    }
 
108
    
 
109
    /**
 
110
     */
 
111
    private void replace() {
 
112
        assert !EventQueue.isDispatchThread();
 
113
        
 
114
        checkForErrors();
 
115
        if (resultStatus == null) {       //the check passed
 
116
            doReplace();
 
117
        }
 
118
    }
 
119
    
 
120
    /**
 
121
     */
 
122
    private void checkForErrors() {
 
123
        assert !EventQueue.isDispatchThread();
 
124
        
 
125
        int errorsCount = 0;
 
126
        
 
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) {
 
133
                    break;
 
134
                }
 
135
            }
 
136
        }
 
137
        if (!problems.isEmpty()) {
 
138
            resultStatus = ResultStatus.PRE_CHECK_FAILED;
 
139
        }
 
140
    }
 
141
 
 
142
    /**
 
143
     * 
 
144
     * @return  list of strings describing problems that happened during
 
145
     *          the replace, or {@code null} if no problem happened
 
146
     */
 
147
    private void doReplace() {
 
148
        assert !EventQueue.isDispatchThread();
 
149
        
 
150
        for (int i = 0; i < matchingObjects.length; i++) {
 
151
            final MatchingObject obj = matchingObjects[i];
 
152
            
 
153
            progressHandle.progress(obj.getName(),
 
154
                                    i + matchingObjects.length);
 
155
            if (!obj.isSelected() || !obj.isValid()) {
 
156
                continue;
 
157
            }
 
158
            
 
159
            String invDescription = obj.getInvalidityDescription();
 
160
            if (invDescription != null) {
 
161
                problems.add(invDescription);
 
162
                continue;
 
163
            }
 
164
            
 
165
            String errMessage = null;
 
166
            FileLock fileLock = null;
 
167
            try {
 
168
                fileLock = obj.lock();
 
169
                MatchingObject.InvalidityStatus status = obj.replace();
 
170
                if (status == null) {
 
171
                    obj.write(fileLock);
 
172
                } else {
 
173
                    errMessage = status.getDescription(obj.getFile().getPath());
 
174
                }
 
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();
 
184
                }
 
185
            } finally {
 
186
                if (fileLock != null) {
 
187
                    fileLock.releaseLock();
 
188
                }
 
189
            }
 
190
            if (errMessage != null) {
 
191
                problems.add(errMessage);
 
192
            }
 
193
        }
 
194
        resultStatus = problems.isEmpty() ? ResultStatus.SUCCESS
 
195
                                          : ResultStatus.PROBLEMS_ENCOUNTERED;
 
196
    }
 
197
 
 
198
    private static String createMsgFileLocked(MatchingObject matchingObj) {
 
199
        return NbBundle.getMessage(
 
200
                ReplaceTask.class,
 
201
                "MSG_cannot_access_file_already_locked",                //NOI18N
 
202
                matchingObj.getName());
 
203
    }
 
204
    
 
205
    /**
 
206
     * 
 
207
     * @see  #getProblems()
 
208
     */
 
209
    ResultStatus getResultStatus() {
 
210
        return resultStatus;
 
211
    }
 
212
    
 
213
    /**
 
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()}.
 
218
     * 
 
219
     * @return  array of problems, or {@code null} if no problems have been
 
220
     *          encountered
 
221
     * @see  #getResultStatus()
 
222
     */
 
223
    String[] getProblems() {
 
224
        return problems.isEmpty()
 
225
               ? null
 
226
               : problems.toArray(new String[problems.size()]);
 
227
    }
 
228
    
 
229
}
 
 
b'\\ No newline at end of file'