KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > history > SearchExecutor


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 package org.netbeans.modules.subversion.ui.history;
20
21 import org.openide.util.NbBundle;
22 import org.openide.util.RequestProcessor;
23 import org.openide.ErrorManager;
24 import org.netbeans.modules.subversion.util.SvnUtils;
25 import org.netbeans.modules.subversion.Subversion;
26 import org.netbeans.modules.subversion.client.SvnProgressSupport;
27 import org.netbeans.modules.subversion.client.SvnClient;
28 import org.tigris.subversion.svnclientadapter.*;
29
30 import javax.swing.*;
31 import java.text.SimpleDateFormat JavaDoc;
32 import java.text.DateFormat JavaDoc;
33 import java.util.*;
34 import java.io.File JavaDoc;
35
36 /**
37  * Executes searches in Search History panel.
38  *
39  * @author Maros Sandor
40  */

41 class SearchExecutor implements Runnable JavaDoc {
42
43     public static final SimpleDateFormat JavaDoc simpleDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm"); // NOI18N
44

45     static final SimpleDateFormat JavaDoc fullDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss Z"); // NOI18N
46
static final DateFormat JavaDoc [] dateFormats = new DateFormat JavaDoc[] {
47         fullDateFormat,
48         new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss"), // NOI18N
49
simpleDateFormat,
50         new SimpleDateFormat JavaDoc("yyyy-MM-dd"), // NOI18N
51
};
52     
53     private final SearchHistoryPanel master;
54     private Map<SVNUrl, Set<File JavaDoc>> workFiles;
55     private Map<String JavaDoc,File JavaDoc> pathToRoot;
56     private final SearchCriteriaPanel criteria;
57     private boolean filterUsername;
58     private boolean filterMessage;
59     
60     private int completedSearches;
61     private boolean searchCanceled;
62     private List<RepositoryRevision> results = new ArrayList<RepositoryRevision>();
63
64     public SearchExecutor(SearchHistoryPanel master) {
65         this.master = master;
66         criteria = master.getCriteria();
67         filterUsername = criteria.getUsername() != null;
68         filterMessage = criteria.getCommitMessage() != null;
69         
70         pathToRoot = new HashMap<String JavaDoc, File JavaDoc>();
71         if (searchingUrl()) {
72             String JavaDoc rootPath = SvnUtils.getRepositoryPath(master.getRoots()[0]);
73             pathToRoot.put(rootPath, master.getRoots()[0]);
74         } else {
75             workFiles = new HashMap<SVNUrl, Set<File JavaDoc>>();
76             for (File JavaDoc file : master.getRoots()) {
77                 String JavaDoc rootPath = SvnUtils.getRepositoryPath(file);
78                 String JavaDoc fileAbsPath = file.getAbsolutePath().replace(File.separatorChar, '/');
79                 int commonPathLength = getCommonPostfixLength(rootPath, fileAbsPath);
80                 pathToRoot.put(rootPath.substring(0, rootPath.length() - commonPathLength),
81                                new File JavaDoc(fileAbsPath.substring(0, fileAbsPath.length() - commonPathLength)));
82                 SVNUrl rootUrl = SvnUtils.getRepositoryRootUrl(file);
83                 Set<File JavaDoc> set = workFiles.get(rootUrl);
84                 if (set == null) {
85                     set = new HashSet<File JavaDoc>(2);
86                     workFiles.put(rootUrl, set);
87                 }
88                 set.add(file);
89             }
90         }
91     }
92
93     private int getCommonPostfixLength(String JavaDoc a, String JavaDoc b) {
94         int ai = a.length() - 1;
95         int bi = b.length() - 1;
96         int slash = -1;
97         for (;;) {
98             if (ai < 0 || bi < 0) break;
99             char ca = a.charAt(ai);
100             char cb = b.charAt(bi);
101             if(ca == '/') slash = ai;
102             if ( ca != cb ) {
103                 if(slash > -1) {
104                     return a.length() - slash;
105                 }
106                 break;
107             }
108             ai--; bi--;
109         }
110         return a.length() - ai - 1;
111     }
112
113
114     
115     public void run() {
116
117         final SVNRevision fromRevision = criteria.getFrom();
118         final SVNRevision toRevision = criteria.getTo();
119
120         completedSearches = 0;
121         if (searchingUrl()) {
122             RequestProcessor rp = Subversion.getInstance().getRequestProcessor(master.getRepositoryUrl());
123             SvnProgressSupport support = new SvnProgressSupport() {
124                 public void perform() {
125                     search(master.getRepositoryUrl(), null, fromRevision, toRevision, this);
126                 }
127             };
128             support.start(rp, master.getRepositoryUrl(), NbBundle.getMessage(SearchExecutor.class, "MSG_Search_Progress")); // NOI18N
129
} else {
130             for (Iterator i = workFiles.keySet().iterator(); i.hasNext();) {
131                 final SVNUrl rootUrl = (SVNUrl) i.next();
132                 final Set<File JavaDoc> files = workFiles.get(rootUrl);
133                 RequestProcessor rp = Subversion.getInstance().getRequestProcessor(rootUrl);
134                 SvnProgressSupport support = new SvnProgressSupport() {
135                     public void perform() {
136                         search(rootUrl, files, fromRevision, toRevision, this);
137                     }
138                 };
139                 support.start(rp, rootUrl, NbBundle.getMessage(SearchExecutor.class, "MSG_Search_Progress")); // NOI18N
140
}
141         }
142     }
143
144     private void search(SVNUrl rootUrl, Set<File JavaDoc> files, SVNRevision fromRevision, SVNRevision toRevision, SvnProgressSupport progressSupport) {
145         SvnClient client;
146         try {
147             client = Subversion.getInstance().getClient(rootUrl, progressSupport);
148         } catch (SVNClientException ex) {
149             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
150             return;
151         }
152         if (progressSupport.isCanceled()) {
153             searchCanceled = true;
154             return;
155         }
156         
157         if (searchingUrl()) {
158             try {
159                 ISVNLogMessage [] messages = client.getLogMessages(rootUrl, null, fromRevision, toRevision, false, true, 0);
160                 appendResults(rootUrl, messages);
161             } catch (SVNClientException e) {
162                 progressSupport.annotate(e);
163             }
164         } else {
165             String JavaDoc [] paths = new String JavaDoc[files.size()];
166             int idx = 0;
167             for (File JavaDoc file : files) {
168                 paths[idx++] = SvnUtils.getRelativePath(rootUrl, file);
169             }
170             try {
171                 ISVNLogMessage [] messages = client.getLogMessages(rootUrl, paths, fromRevision, toRevision, false, true);
172                 appendResults(rootUrl, messages);
173             } catch (SVNClientException e) {
174                 progressSupport.annotate(e);
175             }
176         }
177     }
178     
179     /**
180      * Processes search results from a single repository.
181      *
182      * @param rootUrl repository root URL
183      * @param logMessages events in chronological order
184      */

185     private synchronized void appendResults(SVNUrl rootUrl, ISVNLogMessage[] logMessages) {
186         // /tags/tag-JavaAppX => /branches/brenc2-JavaAppX
187
Map<String JavaDoc, String JavaDoc> historyPaths = new HashMap<String JavaDoc, String JavaDoc>();
188
189         // traverse in reverse chronological order
190
for (int i = logMessages.length - 1; i >= 0; i--) {
191             ISVNLogMessage logMessage = logMessages[i];
192             if (filterUsername && !criteria.getUsername().equals(logMessage.getAuthor())) continue;
193             if (filterMessage && logMessage.getMessage().indexOf(criteria.getCommitMessage()) == -1) continue;
194             RepositoryRevision rev = new RepositoryRevision(logMessage, rootUrl);
195             for (RepositoryRevision.Event event : rev.getEvents()) {
196                 if (event.getChangedPath().getAction() == 'A' && event.getChangedPath().getCopySrcPath() != null) {
197                     // this indicates that in this revision, the file/folder was copied to a new location
198
String JavaDoc existingMapping = historyPaths.get(event.getChangedPath().getPath());
199                     if (existingMapping == null) {
200                         existingMapping = event.getChangedPath().getPath();
201                     }
202                     historyPaths.put(event.getChangedPath().getCopySrcPath(), existingMapping);
203                 }
204                 String JavaDoc originalFilePath = event.getChangedPath().getPath();
205                 for (String JavaDoc srcPath : historyPaths.keySet()) {
206                     if (originalFilePath.startsWith(srcPath)) {
207                         originalFilePath = historyPaths.get(srcPath) + originalFilePath.substring(srcPath.length());
208                         break;
209                     }
210                 }
211                 File JavaDoc file = computeFile(originalFilePath);
212                 event.setFile(file);
213             }
214             results.add(rev);
215         }
216         checkFinished();
217     }
218
219     private boolean searchingUrl() {
220         return master.getRepositoryUrl() != null;
221     }
222     
223     private File JavaDoc computeFile(String JavaDoc path) {
224         for (String JavaDoc s : pathToRoot.keySet()) {
225             if (path.startsWith(s)) {
226                 return new File JavaDoc(pathToRoot.get(s), path.substring(s.length()));
227             }
228         }
229         return null;
230     }
231
232     private void checkFinished() {
233         completedSearches++;
234         if (searchingUrl() && completedSearches >= 1 || workFiles.size() == completedSearches) {
235             SwingUtilities.invokeLater(new Runnable JavaDoc() {
236                 public void run() {
237                     master.setResults(results);
238                 }
239             });
240         }
241     }
242
243   
244 }
245
Popular Tags