/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.gui.executors;

import com.haulmont.cuba.gui.AppConfig;

/**
 * Simple wrapper to a {@link BackgroundTask} to support restarting execution of the same task.
 *
 * @author artamonov
 * @version $Id$
 */
@SuppressWarnings("unused")
public class BackgroundTaskWrapper<T, V> {

    private BackgroundTask<T, V> task;

    private BackgroundTaskHandler<V> taskHandler;
    private BackgroundWorker backgroundWorker;

    public BackgroundTaskWrapper(BackgroundTask<T, V> task) {
        this.task = task;
        this.backgroundWorker = AppConfig.getBackgroundWorker();
    }

    public void restart() {
        cancel();

        taskHandler = backgroundWorker.handle(task);
        taskHandler.execute();
    }

    public V getResult() {
        if (taskHandler != null)
            return taskHandler.getResult();
        else
            return null;
    }

    public void cancel() {
        if (taskHandler != null)
            taskHandler.cancel();
    }
}
@@ -245,44 +245,133 @@
 %0D%0A%0D%0A
-/**%0D%0A * Simple wrapper to a %7B@link B
+import javax.annotation.Nullable;%0D%0Aimport java.util.Objects;%0D%0A%0D%0A/**%0D%0A * Provides simple API for cases when the same type of b
 ackg
@@ -379,107 +379,171 @@
 ound
-Task%7D to support restarting execution of the same task.%0D%0A *%0D%0A * @author artamonov%0D%0A * @version $Id$
+ task%0D%0A * gets started, restarted and cancelled repetitively.%0D%0A *%0D%0A * @author artamonov%0D%0A * @version $Id$%0D%0A * @see com.haulmont.cuba.gui.executors.BackgroundWorker
 %0D%0A *
@@ -796,16 +796,26 @@
 Wrapper(
+@Nullable 
 Backgrou
@@ -953,140 +953,1019 @@
 lic 
-void restart() %7B%0D%0A        cancel();%0D%0A%0D%0A        taskHandler = backgroundWorker.handle(task);%0D%0A        taskHandler.execute();%0D%0A    %7D%0D%0A
+BackgroundTaskWrapper() %7B%0D%0A        this(null);%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Cancel running task if there is at the moment.%0D%0A     * Launch it again.%0D%0A     */%0D%0A    public void restart() %7B%0D%0A        cancel();%0D%0A%0D%0A        Objects.requireNonNull(task, %22Task must be specified either in constructor or by passing it to restart() method%22);%0D%0A        taskHandler = backgroundWorker.handle(task);%0D%0A        taskHandler.execute();%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Cancel running task if there is at the moment.%0D%0A     * Launch new task specified as parameter.%0D%0A     *%0D%0A     * @param task task to start, also will be saved for future restarts%0D%0A     */%0D%0A    public void restart(BackgroundTask%3CT, V%3E task) %7B%0D%0A        cancel();%0D%0A%0D%0A        this.task = task;%0D%0A        taskHandler = backgroundWorker.handle(task);%0D%0A        taskHandler.execute();%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * If there is running task, block until its completion and return result.%0D%0A     *%0D%0A     * @return last task's result or null if no tasks were run yet%0D%0A     */%0D%0A    @Nullable
 %0D%0A  
@@ -2110,32 +2110,95 @@
 null;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Cancel running task if there is any.%0D%0A     */%0D%0A
     public void 

Support parameterized tasks in BackgroundTaskWrapper #PL-3424