Add progress listener that displays the progress of the upload.
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 1 Sep 2010 17:53:27 +0000 (19:53 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 1 Sep 2010 17:53:27 +0000 (19:53 +0200)
src/de/todesbaum/jsite/application/ProjectInserter.java
src/de/todesbaum/jsite/gui/ProjectInsertPage.java
src/de/todesbaum/jsite/main/CLI.java

index c7aa8ee..56b9903 100644 (file)
@@ -53,6 +53,7 @@ import de.todesbaum.util.freenet.fcp2.Verbosity;
 import de.todesbaum.util.io.Closer;
 import de.todesbaum.util.io.ReplacingOutputStream;
 import de.todesbaum.util.io.StreamCopier;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
 
 /**
  * Manages project inserts.
@@ -94,6 +95,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /** Whether the insert is cancelled. */
        private volatile boolean cancelled = false;
 
+       /** Progress listener for payload transfers. */
+       private ProgressListener progressListener;
+
        /**
         * Adds a listener to the list of registered listeners.
         *
@@ -222,8 +226,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /**
         * Starts the insert.
         */
-       public void start() {
+       public void start(ProgressListener progressListener) {
                cancelled = false;
+               this.progressListener = progressListener;
                fileScanner = new FileScanner(project);
                fileScanner.addFileScannerListener(this);
                new Thread(fileScanner).start();
@@ -529,7 +534,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* start request */
                try {
-                       client.execute(putDir);
+                       client.execute(putDir, progressListener);
+                       fireProjectUploadFinished();
                } catch (IOException ioe1) {
                        fireProjectInsertFinished(false, ioe1);
                        return;
@@ -537,17 +543,12 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* parse progress and success messages */
                String finalURI = null;
-               boolean firstMessage = true;
                boolean success = false;
                boolean finished = false;
                boolean disconnected = false;
                while (!finished && !cancelled) {
                        Message message = client.readMessage();
                        finished = (message == null) || (disconnected = client.isDisconnected());
-                       if (firstMessage) {
-                               fireProjectUploadFinished();
-                               firstMessage = false;
-                       }
                        logger.log(Level.FINE, "Received message: " + message);
                        if (!finished) {
                                @SuppressWarnings("null")
index 2e9286e..9310c6b 100644 (file)
@@ -55,6 +55,7 @@ import de.todesbaum.jsite.application.Project;
 import de.todesbaum.jsite.application.ProjectInserter;
 import de.todesbaum.jsite.i18n.I18n;
 import de.todesbaum.jsite.i18n.I18nContainer;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
 import de.todesbaum.util.swing.TWizard;
 import de.todesbaum.util.swing.TWizardPage;
 
@@ -220,7 +221,27 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
                progressBar.setValue(0);
                progressBar.setString(I18n.getMessage("jsite.insert.starting"));
                progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
-               projectInserter.start();
+               projectInserter.start(new ProgressListener() {
+
+                       public void onProgress(final long copied, final long length) {
+                               SwingUtilities.invokeLater(new Runnable() {
+
+                                       /**
+                                        * {@inheritDoc}
+                                        */
+                                       @SuppressWarnings("synthetic-access")
+                                       public void run() {
+                                               int divisor = 1;
+                                               while (((copied / divisor) > Integer.MAX_VALUE) || ((length / divisor) > Integer.MAX_VALUE)) {
+                                                       divisor *= 10;
+                                               }
+                                               progressBar.setMaximum((int) (length / divisor));
+                                               progressBar.setValue((int) (copied / divisor));
+                                               progressBar.setString("Uploaded: " + copied + " / " + length);
+                                       }
+                               });
+                       }
+               });
        }
 
        /**
@@ -315,6 +336,14 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
         */
        public void projectUploadFinished(Project project) {
                startTime = System.currentTimeMillis();
+               SwingUtilities.invokeLater(new Runnable() {
+
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               progressBar.setString(I18n.getMessage("jsite.insert.starting"));
+                               progressBar.setValue(0);
+                       }
+               });
        }
 
        /**
index 03ddb50..3643599 100644 (file)
@@ -26,6 +26,7 @@ import de.todesbaum.jsite.application.InsertListener;
 import de.todesbaum.jsite.application.Node;
 import de.todesbaum.jsite.application.Project;
 import de.todesbaum.jsite.application.ProjectInserter;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
 
 /**
  * Command-line interface for jSite.
@@ -201,7 +202,12 @@ public class CLI implements InsertListener {
                        return false;
                }
                projectInserter.setProject(currentProject);
-               projectInserter.start();
+               projectInserter.start(new ProgressListener() {
+
+                       public void onProgress(long copied, long length) {
+                               System.out.print("Uploaded: " + copied + " / " + length + " bytes...\r");
+                       }
+               });
                synchronized (lockObject) {
                        while (!finished) {
                                try {