All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.raumzeitfalle.fx.filechooser.Invoke Maven / Gradle / Ivy

The newest version!
/*-
 * #%L
 * FXFileChooser
 * %%
 * Copyright (C) 2017 - 2019 Oliver Loeffler, Raumzeitfalle.net
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package net.raumzeitfalle.fx.filechooser;

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.application.Platform;

class Invoke {

    private Invoke() {

    }

    static void later(Supplier action) {
        Platform.runLater(action::get);
    }

    static  void later(T value, Consumer consumingAction) {
        Platform.runLater(() -> consumingAction.accept(value));
    }

    static void laterWithDelay(Duration duration, Runnable r) {
        Platform.runLater(() -> {
            try {
                Thread.sleep(duration.toMillis());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            r.run();
        });
    }

    static void andWaitWithoutException(Runnable r) {
        try {
            andWait(r);
        } catch (InterruptedException | ExecutionException e) {
            Thread.currentThread().interrupt();
        }
    }
    
    static void andWaitWithoutException(Runnable r, long millis) {
        try {
            andWait(r, millis);
        } catch (InterruptedException | ExecutionException e) {
            Thread.currentThread().interrupt();
        } catch (TimeoutException t) {
            Logger.getLogger(Invoke.class.getName())
                  .log(Level.SEVERE, "Timeout!", t);
        }
    }

    static void andWait(Runnable r) throws InterruptedException, ExecutionException {
        FutureTask task = new FutureTask<>(r, null);
        Platform.runLater(task);
        task.get();
    }
    
    static void andWait(Runnable r, long timeout) throws InterruptedException, ExecutionException, TimeoutException {
        FutureTask task = new FutureTask<>(r, null);
        Platform.runLater(task);
        task.get(timeout, TimeUnit.MILLISECONDS);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy