comparison src/org/tmatesoft/hg/core/StatusCommand.java @ 109:dd4d2d0e42cd

Handler for StatusCommand to get notifications in the form of HgStatus object
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 29 Jan 2011 04:17:13 +0100
parents a3a2e5deb320
children 4f509f5bc8cb
comparison
equal deleted inserted replaced
108:0c9804857000 109:dd4d2d0e42cd
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.core; 17 package org.tmatesoft.hg.core;
18 18
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; 19 import static org.tmatesoft.hg.core.StatusCommand.HgStatus.Kind.*;
20 import static org.tmatesoft.hg.repo.HgRepository.TIP; 20 import static org.tmatesoft.hg.repo.HgRepository.*;
21 import static org.tmatesoft.hg.repo.HgRepository.WORKING_COPY;
22 21
23 import java.util.ConcurrentModificationException; 22 import java.util.ConcurrentModificationException;
24 23
25 import org.tmatesoft.hg.core.LogCommand.FileRevision;
26 import org.tmatesoft.hg.core.Path.Matcher; 24 import org.tmatesoft.hg.core.Path.Matcher;
25 import org.tmatesoft.hg.core.StatusCommand.HgStatus.Kind;
27 import org.tmatesoft.hg.repo.HgRepository; 26 import org.tmatesoft.hg.repo.HgRepository;
27 import org.tmatesoft.hg.repo.HgStatusCollector;
28 import org.tmatesoft.hg.repo.HgStatusInspector; 28 import org.tmatesoft.hg.repo.HgStatusInspector;
29 import org.tmatesoft.hg.repo.HgStatusCollector;
30 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; 29 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
31 30
32 /** 31 /**
33 * 32 *
34 * @author Artem Tikhomirov 33 * @author Artem Tikhomirov
39 38
40 private int startRevision = TIP; 39 private int startRevision = TIP;
41 private int endRevision = WORKING_COPY; 40 private int endRevision = WORKING_COPY;
42 private boolean visitSubRepo = true; 41 private boolean visitSubRepo = true;
43 42
44 private HgStatusInspector visitor; 43 private Handler handler;
45 private final Mediator mediator = new Mediator(); 44 private final Mediator mediator = new Mediator();
46 45
47 public StatusCommand(HgRepository hgRepo) { 46 public StatusCommand(HgRepository hgRepo) {
48 repo = hgRepo; 47 repo = hgRepo;
49 defaults(); 48 defaults();
141 * 140 *
142 * @param handler callback to get status information 141 * @param handler callback to get status information
143 * @throws IllegalArgumentException if handler is <code>null</code> 142 * @throws IllegalArgumentException if handler is <code>null</code>
144 * @throws ConcurrentModificationException if this command already runs (i.e. being used from another thread) 143 * @throws ConcurrentModificationException if this command already runs (i.e. being used from another thread)
145 */ 144 */
146 public void execute(final HgStatusInspector handler) { 145 public void execute(Handler statusHandler) {
147 if (handler == null) { 146 if (statusHandler == null) {
148 throw new IllegalArgumentException(); 147 throw new IllegalArgumentException();
149 } 148 }
150 if (visitor != null) { 149 if (handler != null) {
151 throw new ConcurrentModificationException(); 150 throw new ConcurrentModificationException();
152 } 151 }
153 visitor = handler; 152 handler = statusHandler;
154 HgStatusCollector sc = new HgStatusCollector(repo); // TODO from CommandContext 153 HgStatusCollector sc = new HgStatusCollector(repo); // TODO from CommandContext
155 // PathPool pathHelper = new PathPool(repo.getPathHelper()); // TODO from CommandContext 154 // PathPool pathHelper = new PathPool(repo.getPathHelper()); // TODO from CommandContext
156 try { 155 try {
157 // XXX if I need a rough estimation (for ProgressMonitor) of number of work units, 156 // XXX if I need a rough estimation (for ProgressMonitor) of number of work units,
158 // I may use number of files in either rev1 or rev2 manifest edition 157 // I may use number of files in either rev1 or rev2 manifest edition
168 sc.walk(startRevision, endRevision, mediator); 167 sc.walk(startRevision, endRevision, mediator);
169 } 168 }
170 } 169 }
171 } finally { 170 } finally {
172 mediator.done(); 171 mediator.done();
173 visitor = null; 172 handler = null;
174 } 173 }
175 } 174 }
176 175
176 public interface Handler {
177 void handleStatus(HgStatus s);
178 }
179
180 public static class HgStatus {
181 public enum Kind {
182 Modified, Added, Removed, Unknown, Missing, Clean, Ignored
183 };
184 private final Kind kind;
185 private final Path path;
186 private final Path origin;
187
188 HgStatus(Kind kind, Path path) {
189 this(kind, path, null);
190 }
191
192 HgStatus(Kind kind, Path path, Path copyOrigin) {
193 this.kind = kind;
194 this.path = path;
195 origin = copyOrigin;
196 }
197
198 public Kind getKind() {
199 return kind;
200 }
201
202 public Path getPath() {
203 return path;
204 }
205
206 public Path getOriginalPath() {
207 return origin;
208 }
209
210 public boolean isCopy() {
211 return origin != null;
212 }
213 }
214
215
177 private class Mediator implements HgStatusInspector { 216 private class Mediator implements HgStatusInspector {
178 boolean needModified; 217 boolean needModified;
179 boolean needAdded; 218 boolean needAdded;
180 boolean needRemoved; 219 boolean needRemoved;
181 boolean needUnknown; 220 boolean needUnknown;
195 } 234 }
196 235
197 public void modified(Path fname) { 236 public void modified(Path fname) {
198 if (needModified) { 237 if (needModified) {
199 if (matcher == null || matcher.accept(fname)) { 238 if (matcher == null || matcher.accept(fname)) {
200 visitor.modified(fname); 239 handler.handleStatus(new HgStatus(Modified, fname));
201 } 240 }
202 } 241 }
203 } 242 }
204 public void added(Path fname) { 243 public void added(Path fname) {
205 if (needAdded) { 244 if (needAdded) {
206 if (matcher == null || matcher.accept(fname)) { 245 if (matcher == null || matcher.accept(fname)) {
207 visitor.added(fname); 246 handler.handleStatus(new HgStatus(Added, fname));
208 } 247 }
209 } 248 }
210 } 249 }
211 public void removed(Path fname) { 250 public void removed(Path fname) {
212 if (needRemoved) { 251 if (needRemoved) {
213 if (matcher == null || matcher.accept(fname)) { 252 if (matcher == null || matcher.accept(fname)) {
214 visitor.removed(fname); 253 handler.handleStatus(new HgStatus(Removed, fname));
215 } 254 }
216 } 255 }
217 } 256 }
218 public void copied(Path fnameOrigin, Path fnameAdded) { 257 public void copied(Path fnameOrigin, Path fnameAdded) {
219 if (needCopies) { 258 if (needCopies) {
220 if (matcher == null || matcher.accept(fnameAdded)) { 259 if (matcher == null || matcher.accept(fnameAdded)) {
221 visitor.copied(fnameOrigin, fnameAdded); 260 handler.handleStatus(new HgStatus(Kind.Added, fnameAdded, fnameOrigin));
222 } 261 }
223 } 262 }
224 } 263 }
225 public void missing(Path fname) { 264 public void missing(Path fname) {
226 if (needMissing) { 265 if (needMissing) {
227 if (matcher == null || matcher.accept(fname)) { 266 if (matcher == null || matcher.accept(fname)) {
228 visitor.missing(fname); 267 handler.handleStatus(new HgStatus(Missing, fname));
229 } 268 }
230 } 269 }
231 } 270 }
232 public void unknown(Path fname) { 271 public void unknown(Path fname) {
233 if (needUnknown) { 272 if (needUnknown) {
234 if (matcher == null || matcher.accept(fname)) { 273 if (matcher == null || matcher.accept(fname)) {
235 visitor.unknown(fname); 274 handler.handleStatus(new HgStatus(Unknown, fname));
236 } 275 }
237 } 276 }
238 } 277 }
239 public void clean(Path fname) { 278 public void clean(Path fname) {
240 if (needClean) { 279 if (needClean) {
241 if (matcher == null || matcher.accept(fname)) { 280 if (matcher == null || matcher.accept(fname)) {
242 visitor.clean(fname); 281 handler.handleStatus(new HgStatus(Clean, fname));
243 } 282 }
244 } 283 }
245 } 284 }
246 public void ignored(Path fname) { 285 public void ignored(Path fname) {
247 if (needIgnored) { 286 if (needIgnored) {
248 if (matcher == null || matcher.accept(fname)) { 287 if (matcher == null || matcher.accept(fname)) {
249 visitor.ignored(fname); 288 handler.handleStatus(new HgStatus(Ignored, fname));
250 } 289 }
251 } 290 }
252 } 291 }
253 } 292 }
254 } 293 }