From: Marc Englund Date: Tue, 13 May 2008 08:40:30 +0000 (+0000) Subject: set/getProgressListener deprecated, replaced with remove/addListener(ProgressListener). X-Git-Tag: 6.7.0.beta1~4787 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=226f902fbe8c562d2c2d9e6b3d67c72b6997a5a3;p=vaadin-framework.git set/getProgressListener deprecated, replaced with remove/addListener(ProgressListener). Fixes #1446. svn changeset:4442/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/tests/TestForUpload.java b/src/com/itmill/toolkit/tests/TestForUpload.java index 5138ff890b..de60399dad 100644 --- a/src/com/itmill/toolkit/tests/TestForUpload.java +++ b/src/com/itmill/toolkit/tests/TestForUpload.java @@ -53,6 +53,7 @@ public class TestForUpload extends CustomComponent implements private final Label l; private final ProgressIndicator pi = new ProgressIndicator(); + private final ProgressIndicator pi2 = new ProgressIndicator(); private final Label memoryStatus; @@ -91,6 +92,15 @@ public class TestForUpload extends CustomComponent implements up.addListener((StartedListener) this); up.setProgressListener(this); + up.addListener(new Upload.ProgressListener() { + + public void updateProgress(long readBytes, long contentLenght) { + pi2.setValue(new Float(readBytes / (float) contentLenght)); + + refreshMemUsage(); + } + + }); final Button b = new Button("b", this, "readState"); @@ -120,6 +130,10 @@ public class TestForUpload extends CustomComponent implements pi.setPollingInterval(1000); main.addComponent(pi); + pi2.setVisible(false); + pi2.setPollingInterval(1000); + main.addComponent(pi2); + memoryStatus = new Label(); main.addComponent(memoryStatus); @@ -326,6 +340,7 @@ public class TestForUpload extends CustomComponent implements public void uploadSucceeded(SucceededEvent event) { pi.setVisible(false); + pi2.setVisible(false); l.setValue("Finished upload, idle"); System.out.println(event); setBuffer(); @@ -351,6 +366,7 @@ public class TestForUpload extends CustomComponent implements public void uploadStarted(StartedEvent event) { pi.setVisible(true); + pi2.setVisible(true); l.setValue("Started uploading file " + event.getFilename()); textFieldValue.setValue(" TestFields value at the upload start is:" + textField.getValue()); diff --git a/src/com/itmill/toolkit/ui/Upload.java b/src/com/itmill/toolkit/ui/Upload.java index fb64489fb0..d1fcd45add 100644 --- a/src/com/itmill/toolkit/ui/Upload.java +++ b/src/com/itmill/toolkit/ui/Upload.java @@ -7,6 +7,8 @@ package com.itmill.toolkit.ui; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Method; +import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Map; import com.itmill.toolkit.Application; @@ -77,6 +79,7 @@ public class Upload extends AbstractComponent implements Component.Focusable { * upload */ private ProgressListener progressListener; + private LinkedHashSet progressListeners; /* TODO: Add a default constructor, receive to temp file. */ @@ -160,8 +163,7 @@ public class Upload extends AbstractComponent implements Component.Focusable { // update progress if listener set and contentLength // received synchronized (application) { - progressListener.updateProgress(totalBytes, - contentLength); + fireUpdateProgress(totalBytes, contentLength); } } } @@ -710,6 +712,31 @@ public class Upload extends AbstractComponent implements Component.Focusable { removeListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD); } + /** + * Adds the upload success event listener. + * + * @param listener + * the Listener to be added. + */ + public void addListener(ProgressListener listener) { + if (progressListeners == null) { + progressListeners = new LinkedHashSet(); + } + progressListeners.add(listener); + } + + /** + * Removes the upload success event listener. + * + * @param listener + * the Listener to be removed. + */ + public void removeListener(ProgressListener listener) { + if (progressListeners != null) { + progressListeners.remove(listener); + } + } + /** * Emit upload received event. * @@ -775,6 +802,30 @@ public class Upload extends AbstractComponent implements Component.Focusable { fireEvent(new Upload.SucceededEvent(this, filename, MIMEType, length)); } + /** + * Emits the progress event. + * + * @param totalBytes + * bytes received so far + * @param contentLength + * actual size of the file being uploaded, if known + * + */ + protected void fireUpdateProgress(long totalBytes, long contentLength) { + // this is implemented differently than other listeners to maintain + // backwards compatibility + if (progressListeners != null) { + for (Iterator it = progressListeners.iterator(); it.hasNext();) { + ProgressListener l = (ProgressListener) it.next(); + l.updateProgress(totalBytes, contentLength); + } + } + // deprecated: + if (progressListener != null) { + progressListener.updateProgress(totalBytes, contentLength); + } + } + /** * Returns the current receiver. * @@ -886,8 +937,9 @@ public class Upload extends AbstractComponent implements Component.Focusable { } /** - * Sets listener to track progress of upload. + * This method is deprecated, use addListener(ProgressListener) instead. * + * @deprecated Use addListener(ProgressListener) instead. * @param progressListener */ public void setProgressListener(ProgressListener progressListener) { @@ -895,8 +947,9 @@ public class Upload extends AbstractComponent implements Component.Focusable { } /** - * Gets listener that tracks progress of upload. + * This method is deprecated. * + * @deprecated Replaced with addListener/removeListener * @return listener * */