Skip to content
Snippets Groups Projects

Enhance data upload pane

Closed Petr Bainar requested to merge observerObservable into master
2 files
+ 71
42
Compare changes
  • Side-by-side
  • Inline
Files
2
package cz.it4i.fiji.haas_spim_benchmark.core;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import net.imagej.updater.util.Progress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cz.it4i.fiji.haas.ui.UpdatableObservableValue;
import cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager.BenchmarkJob;
import net.imagej.updater.util.Progress;
public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJob> {
public class ObservableBenchmarkJob extends
UpdatableObservableValue<BenchmarkJob>
{
public static final Logger log = LoggerFactory
.getLogger(cz.it4i.fiji.haas_spim_benchmark.core.ObservableBenchmarkJob.class);
public static final Logger log = LoggerFactory.getLogger(
cz.it4i.fiji.haas_spim_benchmark.core.ObservableBenchmarkJob.class);
private final P_TransferProgress downloadProgress = new P_TransferProgress(val -> getValue().setDownloaded(val),
() -> getValue().isDownloaded(), () -> getValue().needsDownload());
private final P_TransferProgress uploadProgress = new P_TransferProgress(val -> getValue().setUploaded(val),
() -> getValue().isUploaded(), () -> getValue().needsUpload());
private final P_TransferProgress downloadProgress = new P_TransferProgress(
val -> getValue().setDownloaded(val), () -> getValue().isDownloaded(),
() -> getValue().needsDownload());
private final P_TransferProgress uploadProgress = new P_TransferProgress(
val -> getValue().setUploaded(val), () -> getValue().isUploaded(),
() -> getValue().needsUpload());
private final Executor executor;
private final P_Observable fileTransferObservers = new P_Observable();
public interface TransferProgress {
public Long getRemainingMiliseconds();
@@ -34,8 +44,10 @@ public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJo
public Float getRemainingPercents();
}
public ObservableBenchmarkJob(BenchmarkJob wrapped, Function<BenchmarkJob, UpdateStatus> updateFunction,
Function<BenchmarkJob, Object> stateProvider, Executor executorUI) {
public ObservableBenchmarkJob(BenchmarkJob wrapped,
Function<BenchmarkJob, UpdateStatus> updateFunction,
Function<BenchmarkJob, Object> stateProvider, Executor executorUI)
{
super(wrapped, updateFunction, stateProvider);
this.executor = executorUI;
wrapped.setDownloadNotifier(downloadProgress);
@@ -56,6 +68,14 @@ public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJo
getValue().setUploadNotifier(null);
}
public void startObservingFileTransfer(final Observer observer) {
fileTransferObservers.addObserver(observer);
}
public void stopObservingFileTransfer(final Observer observer) {
fileTransferObservers.deleteObserver(observer);
}
@Override
protected void fireValueChangedEvent() {
executor.execute(() -> {
@@ -63,6 +83,19 @@ public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJo
});
}
private void notifyFileTransferObservers() {
fileTransferObservers.setChanged();
fileTransferObservers.notifyObservers();
}
private class P_Observable extends Observable {
@Override
public synchronized void setChanged() {
super.setChanged();
}
}
private class P_TransferProgress implements Progress, TransferProgress {
private final Supplier<Boolean> doneStatusSupplier;
@@ -71,9 +104,10 @@ public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJo
private Long start;
private Long remainingMiliseconds;
private Float remainingPercents;
public P_TransferProgress(Consumer<Boolean> doneStatusConsumer, Supplier<Boolean> doneStatusSupplier,
Supplier<Boolean> workingSupplier) {
public P_TransferProgress(Consumer<Boolean> doneStatusConsumer,
Supplier<Boolean> doneStatusSupplier, Supplier<Boolean> workingSupplier)
{
this.doneStatusConsumer = doneStatusConsumer;
this.doneStatusSupplier = doneStatusSupplier;
this.workingSupplier = workingSupplier;
@@ -83,9 +117,11 @@ public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJo
public synchronized void setCount(int count, int total) {
if (total < -1) {
clearProgress();
} else if (start != null) {
}
else if (start != null) {
long delta = System.currentTimeMillis() - start;
remainingMiliseconds = (long) ((double) delta / count * (total - count));
remainingMiliseconds = (long) ((double) delta / count * (total -
count));
remainingPercents = (((float) total - count) / total * 100);
}
fireValueChangedEvent();
@@ -125,17 +161,15 @@ public class ObservableBenchmarkJob extends UpdatableObservableValue<BenchmarkJo
}
@Override
public void setItemCount(int count, int total) {
}
public void setItemCount(int count, int total) {}
@Override
public void itemDone(Object item) {
fireValueChangedEvent();
public void itemDone(final Object item) {
notifyFileTransferObservers();
}
@Override
public void setTitle(String title) {
}
public void setTitle(String title) {}
@Override
public boolean isDone() {
Loading