Refactored ProgressMonitor

This commit is contained in:
pesckal
2011-06-24 05:08:49 +00:00
parent 99a3225657
commit b7eb7e52c7
2 changed files with 104 additions and 15 deletions
+15 -15
View File
@@ -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();
@@ -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 <code>true</code> 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;
}
}