Fix build and test failures
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / EclipseGitProgressTransformer.java
blob051b946ed9923ebea71180f6303c1c1e1423ddd9
1 /*******************************************************************************
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.egit.core;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.core.runtime.SubProgressMonitor;
13 import org.eclipse.jgit.lib.ProgressMonitor;
15 /** Create a new Git to Eclipse progress monitor. */
16 public class EclipseGitProgressTransformer implements ProgressMonitor {
17 private final IProgressMonitor root;
19 private IProgressMonitor task;
21 private String msg;
23 private int lastWorked;
25 private int totalWork;
27 /**
28 * Create a new progress monitor.
30 * @param eclipseMonitor
31 * the Eclipse monitor we update.
33 public EclipseGitProgressTransformer(final IProgressMonitor eclipseMonitor) {
34 root = eclipseMonitor;
37 public void start(final int totalTasks) {
38 root.beginTask("", totalTasks * 1000);
41 public void beginTask(final String name, final int total) {
42 endTask();
43 msg = name;
44 lastWorked = 0;
45 totalWork = total;
46 task = new SubProgressMonitor(root, 1000);
47 if (totalWork == UNKNOWN)
48 task.beginTask("", IProgressMonitor.UNKNOWN);
49 else
50 task.beginTask("", totalWork);
51 task.subTask(msg);
54 public void update(final int work) {
55 if (task == null)
56 return;
58 final int cmp = lastWorked + work;
59 if (lastWorked == UNKNOWN && cmp > 0) {
60 task.subTask(msg + ", " + cmp);
61 } else if (totalWork <= 0) {
62 // Do nothing to update the task.
63 } else if (cmp * 100 / totalWork != lastWorked * 100 / totalWork) {
64 final StringBuilder m = new StringBuilder();
65 m.append(msg);
66 m.append(": ");
67 while (m.length() < 25)
68 m.append(' ');
70 if (totalWork == UNKNOWN) {
71 m.append(cmp);
72 } else {
73 final String twstr = String.valueOf(totalWork);
74 String cmpstr = String.valueOf(cmp);
75 while (cmpstr.length() < twstr.length())
76 cmpstr = " " + cmpstr;
77 final int pcnt = (cmp * 100 / totalWork);
78 if (pcnt < 100)
79 m.append(' ');
80 if (pcnt < 10)
81 m.append(' ');
82 m.append(pcnt);
83 m.append("% (");
84 m.append(cmpstr);
85 m.append("/");
86 m.append(twstr);
87 m.append(")");
89 task.subTask(m.toString());
91 lastWorked = cmp;
92 task.worked(work);
95 public void endTask() {
96 if (task != null) {
97 try {
98 task.done();
99 } finally {
100 task = null;
105 public boolean isCancelled() {
106 if (task != null)
107 return task.isCanceled();
108 return root.isCanceled();