From cdd31ce2ba37cd79ac4b17f898f7ce881a9b891a Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Sun, 25 Nov 2007 02:23:40 +0100 Subject: [PATCH] Add a progress monitor inteface and some simple implementions of it The interface is very similar to the Eclipse's progress monitors, but I don't want to depend directly on Eclipse in jgit. Not-Signed-off-by: Robin Rosenberg LICENSE! --- .../spearce/jgit/lib/AbstractProgressMonitor.java | 84 ++++++++++++++++++++++ .../org/spearce/jgit/lib/NullProgressMonitor.java | 29 ++++++++ .../src/org/spearce/jgit/lib/ProgressMonitor.java | 82 +++++++++++++++++++++ .../org/spearce/jgit/lib/TextProgressMonitor.java | 34 +++++++++ 4 files changed, 229 insertions(+) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/AbstractProgressMonitor.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/NullProgressMonitor.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/ProgressMonitor.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/TextProgressMonitor.java diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/AbstractProgressMonitor.java b/org.spearce.jgit/src/org/spearce/jgit/lib/AbstractProgressMonitor.java new file mode 100644 index 00000000..5bb16474 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/AbstractProgressMonitor.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2007 Robin Rosenberg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + */ +package org.spearce.jgit.lib; + +/** + * Base class for progress monitors. This class tracks progress + * but does not report it anywhere; + */ +public abstract class AbstractProgressMonitor implements ProgressMonitor { + + private int total; + private int worked; + private String task; + private boolean done; + + public void done() { + done = true; + worked = total; + report(); + } + + public boolean isDone() { + return done; + } + + public boolean isCancelled() { + return false; + } + + public void setCancelled(final boolean cancelled) { + // empty + } + + public void setTask(final String task) { + this.task = task; + report(); + } + + public void worked(final int amount) { + worked += amount; + report(); + } + + public void beginTask(final String task, final int total) { + this.task = task; + this.total = total; + report(); + } + + /** + * Report progress + */ + abstract protected void report(); + + public String getTask() { + return task; + } + + public int getWorked() { + return worked; + } + + public void setTotalWork(final int work) { + total = work; + } + + public int getTotal() { + return total; + } +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/NullProgressMonitor.java b/org.spearce.jgit/src/org/spearce/jgit/lib/NullProgressMonitor.java new file mode 100644 index 00000000..a08ce4a2 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/NullProgressMonitor.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2007 Robin Rosenberg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + */ +package org.spearce.jgit.lib; + +/** + * A NullProgressMonitor does not report progress anywhere. + */ +public class NullProgressMonitor extends AbstractProgressMonitor { + + @Override + protected void report() { + // Nothing + } + +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ProgressMonitor.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ProgressMonitor.java new file mode 100644 index 00000000..47ae9eaf --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ProgressMonitor.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 IBM Corporation and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.spearce.jgit.lib; + +/** + * A progress monitor. A ripoff of IProgressMonitor. + */ +public interface ProgressMonitor { + /** + * Set task name + * + * @param message + */ + void setTask(String message); + + /** + * @return taskname + */ + String getTask(); + + /** + * Set the total expected amount of work + * + * @param work + */ + void setTotalWork(int work); + + /** + * @return amount worked so far + */ + int getWorked(); + + /** + * @param work + */ + void worked(int work); + + /** + * @return total expected amount of work + */ + int getTotal(); + + /** + * Indicate the task is completed. + */ + void done(); + + /** + * @return true if done. + */ + boolean isDone(); + + /** + * @return true if cancel has been requested. + */ + boolean isCancelled(); + + /** + * Request the task to be cancelled + * + * @param cancelled + */ + void setCancelled(boolean cancelled); + + /** + * Begin a task + * + * @param task + * @param total + */ + void beginTask(String task, int total); + +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/TextProgressMonitor.java b/org.spearce.jgit/src/org/spearce/jgit/lib/TextProgressMonitor.java new file mode 100644 index 00000000..8696989e --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/TextProgressMonitor.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2007 Robin Rosenberg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + */ +package org.spearce.jgit.lib; + +/** + * A simple progress reporter printing on stdout + */ +public class TextProgressMonitor extends AbstractProgressMonitor { + + int lastWorked; + + @Override + protected void report() { + int tot = getTotal() + 1; + if ((lastWorked+1)*100/tot != (getWorked()+1)*100/tot) + System.out.println(getTask() + " " + (getWorked()*100 / tot) + "%"); + lastWorked = getWorked(); + } + +} -- 2.11.4.GIT