From b7eb7e52c74f7106325d95170c63a5a4addeaa29 Mon Sep 17 00:00:00 2001 From: pesckal Date: Fri, 24 Jun 2011 05:08:49 +0000 Subject: [PATCH] Refactored ProgressMonitor --- app/src/processing/app/LibraryManager.java | 30 +++---- app/src/processing/app/ProgressMonitor.java | 89 +++++++++++++++++++++ 2 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 app/src/processing/app/ProgressMonitor.java diff --git a/app/src/processing/app/LibraryManager.java b/app/src/processing/app/LibraryManager.java index 1ea39026c..38d75368f 100644 --- a/app/src/processing/app/LibraryManager.java +++ b/app/src/processing/app/LibraryManager.java @@ -34,20 +34,22 @@ import java.util.zip.*; import javax.swing.*; import javax.swing.event.*; -class ProgressMonitor { +class ProgressMonitor2 extends AbstractProgressMonitor { JProgressBar progressBar; - boolean isCancelled = false; - public ProgressMonitor(JProgressBar progressBar) { + public ProgressMonitor2(JProgressBar progressBar) { this.progressBar = progressBar; } - public boolean isCanceled() { - return isCancelled; + public void startTask(String name, int maxValue) { + progressBar.setString(name); + progressBar.setIndeterminate(maxValue == UNKNOWN); + progressBar.setMaximum(maxValue); } - - public void cancel() { - isCancelled = true; + + public void setProgress(int value) { + super.setProgress(value); + progressBar.setValue(value); } } @@ -77,14 +79,12 @@ public class LibraryManager { public void run() { libraryUrl.setEnabled(false); installButton.setEnabled(false); - progressMonitor.progressBar.setIndeterminate(false); - progressMonitor.progressBar.setString("Downloading"); libFile = downloadLibrary(url, progressMonitor); if (libFile != null) { - progressMonitor.progressBar.setString("Installing"); - progressMonitor.progressBar.setIndeterminate(true); + progressMonitor.startTask("Installing", ProgressMonitor.UNKNOWN); + installLibrary(libFile); } @@ -237,7 +237,7 @@ public class LibraryManager { URL url = new URL(libraryUrl.getText()); - final ProgressMonitor pm = new ProgressMonitor(installProgressBar); + final ProgressMonitor2 pm = new ProgressMonitor2(installProgressBar); dialog.addWindowListener(new WindowAdapter() { @@ -522,7 +522,7 @@ public class LibraryManager { // } int fileSize = urlConn.getContentLength(); - progressMonitor.progressBar.setMaximum(fileSize); + progressMonitor.startTask("Downloading", fileSize); InputStream in = urlConn.getInputStream(); FileOutputStream out = new FileOutputStream(dest); @@ -533,7 +533,7 @@ public class LibraryManager { out.write(b, 0, len); bytesDownloaded += len; - progressMonitor.progressBar.setValue(bytesDownloaded); + progressMonitor.setProgress(bytesDownloaded); } out.close(); diff --git a/app/src/processing/app/ProgressMonitor.java b/app/src/processing/app/ProgressMonitor.java new file mode 100644 index 000000000..c62c6e367 --- /dev/null +++ b/app/src/processing/app/ProgressMonitor.java @@ -0,0 +1,89 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-11 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + This program 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 program 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 program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package processing.app; + +/** + * The ProgressMonitor interface should be implemented by objects that observe + * progress of a task or activity. + */ +public interface ProgressMonitor { + + int UNKNOWN = -1; + + /** + * Starts a new task with the given name. + * + * @param maxValue + * the amount of progress that must be made before a task is + * finished. This may be set to UNKNOWN. + */ + public void startTask(String name, int maxValue); + + /** + * Updates the amount of progress for the current task. + */ + public void setProgress(int value); + + /** + * Returns the progress made toward the current task, as previously set by a + * call to setProgress(). + */ + public int getProgress(); + + /** + * @return true if a cancellation has been requested, false + * otherwise + */ + public boolean isCanceled(); + + /** + * Requests for the task to be cancelled by setting isCanceled() to true. + */ + public void cancel(); + +} + +abstract class AbstractProgressMonitor implements ProgressMonitor { + boolean isCanceled = false; + + boolean isFinished = false; + + int progress = 0; + + public void setProgress(int value) { + progress = value; + } + + public int getProgress() { + return progress; + } + + public boolean isCanceled() { + return isCanceled; + } + + public void cancel() { + isCanceled = true; + } + +}