@@ -56,9 +56,7 @@ public abstract class TransferProgressAwareHandler<T, R extends TransferProgress
5656 protected abstract TransferContext getTransferContext (T transferEvent );
5757
5858 /**
59- * UI that the transfer was started with. Set from onStart(context).
60- * <p>
61- * protected for tests to set the ui.
59+ * UI that the transfer was started with. Set from context if not given.
6260 */
6361 private UI ui ;
6462
@@ -112,6 +110,9 @@ public R whenStart(SerializableRunnable startHandler) {
112110 addTransferProgressListenerInternal (new TransferProgressListener () {
113111 @ Override
114112 public void onStart (TransferContext context ) {
113+ if (ui == null ) {
114+ setTransferUI (context .getUI ());
115+ }
115116 ui .access (startHandler ::run );
116117 }
117118 });
@@ -137,7 +138,7 @@ public R whenStart(SerializableConsumer<TransferContext> startHandler) {
137138 addTransferProgressListenerInternal (new TransferProgressListener () {
138139 @ Override
139140 public void onStart (TransferContext context ) {
140- ui . access ( () -> startHandler .accept (context ));
141+ uiAccess ( context , () -> startHandler .accept (context ));
141142 }
142143 });
143144 return (R ) this ;
@@ -169,7 +170,7 @@ public R onProgress(
169170 @ Override
170171 public void onProgress (TransferContext context ,
171172 long transferredBytes , long totalBytes ) {
172- ui . access ( () -> progressHandler .accept (context ,
173+ uiAccess ( context , () -> progressHandler .accept (context ,
173174 transferredBytes , totalBytes ));
174175 }
175176
@@ -226,7 +227,7 @@ public R onProgress(SerializableBiConsumer<Long, Long> progressHandler,
226227 @ Override
227228 public void onProgress (TransferContext context ,
228229 long transferredBytes , long totalBytes ) {
229- ui . access ( () -> progressHandler .accept (transferredBytes ,
230+ uiAccess ( context , () -> progressHandler .accept (transferredBytes ,
230231 totalBytes ));
231232 }
232233
@@ -287,13 +288,15 @@ public R whenComplete(
287288 addTransferProgressListenerInternal (new TransferProgressListener () {
288289 @ Override
289290 public void onError (TransferContext context , IOException reason ) {
290- ui .access (() -> completeOrTerminateHandler .accept (false ));
291+ uiAccess (context ,
292+ () -> completeOrTerminateHandler .accept (false ));
291293 }
292294
293295 @ Override
294296 public void onComplete (TransferContext context ,
295297 long transferredBytes ) {
296- ui .access (() -> completeOrTerminateHandler .accept (true ));
298+ uiAccess (context ,
299+ () -> completeOrTerminateHandler .accept (true ));
297300 }
298301 });
299302 return (R ) this ;
@@ -325,14 +328,14 @@ public R whenComplete(
325328 addTransferProgressListenerInternal (new TransferProgressListener () {
326329 @ Override
327330 public void onError (TransferContext context , IOException reason ) {
328- ui . access ( () -> completeOrTerminateHandler . accept ( context ,
329- false ));
331+ uiAccess ( context , () -> completeOrTerminateHandler
332+ . accept ( context , false ));
330333 }
331334
332335 @ Override
333336 public void onComplete (TransferContext context ,
334337 long transferredBytes ) {
335- ui . access (
338+ uiAccess ( context ,
336339 () -> completeOrTerminateHandler .accept (context , true ));
337340 }
338341 });
@@ -404,6 +407,13 @@ protected void setTransferUI(UI ui) {
404407 }
405408 }
406409
410+ protected void uiAccess (TransferContext context , final Command command ) {
411+ if (ui == null ) {
412+ setTransferUI (context .getUI ());
413+ }
414+ ui .access (command );
415+ }
416+
407417 /**
408418 * A wrapper for {@link TransferProgressListener} that ensures that UI
409419 * updates in transfer progress listeners are pushed to client
@@ -421,24 +431,25 @@ public TransferProgressListenerWrapper(
421431
422432 @ Override
423433 public void onStart (TransferContext context ) {
424- ui . access ( () -> delegate .onStart (context ));
434+ uiAccess ( context , () -> delegate .onStart (context ));
425435 }
426436
427437 @ Override
428438 public void onProgress (TransferContext context , long transferredBytes ,
429439 long totalBytes ) {
430- ui . access ( () -> delegate .onProgress (context , transferredBytes ,
431- totalBytes ));
440+ uiAccess ( context , () -> delegate .onProgress (context ,
441+ transferredBytes , totalBytes ));
432442 }
433443
434444 @ Override
435445 public void onError (TransferContext context , IOException reason ) {
436- ui . access ( () -> delegate .onError (context , reason ));
446+ uiAccess ( context , () -> delegate .onError (context , reason ));
437447 }
438448
439449 @ Override
440450 public void onComplete (TransferContext context , long transferredBytes ) {
441- ui .access (() -> delegate .onComplete (context , transferredBytes ));
451+ uiAccess (context ,
452+ () -> delegate .onComplete (context , transferredBytes ));
442453 }
443454
444455 @ Override
@@ -449,5 +460,13 @@ public long progressReportInterval() {
449460 protected void setTransferUI (UI ui ) {
450461 this .ui = ui ;
451462 }
463+
464+ protected void uiAccess (TransferContext context ,
465+ final Command command ) {
466+ if (ui == null ) {
467+ setTransferUI (context .getUI ());
468+ }
469+ ui .access (command );
470+ }
452471 }
453472}
0 commit comments