001package conexp.fx.gui.util; 002 003import java.util.concurrent.CountDownLatch; 004 005/* 006 * #%L 007 * Concept Explorer FX 008 * %% 009 * Copyright (C) 2010 - 2023 Francesco Kriegel 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as 013 * published by the Free Software Foundation, either version 3 of the 014 * License, or (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public 022 * License along with this program. If not, see 023 * <http://www.gnu.org/licenses/gpl-3.0.html>. 024 * #L% 025 */ 026 027import javafx.application.Platform; 028 029public final class Platform2 { 030 031 public static final void runOnFXThread(final Runnable runnable) { 032 if (Platform.isFxApplicationThread()) 033 runnable.run(); 034 else 035 Platform.runLater(runnable); 036 } 037 038 public static final void runOnFXThreadAndWait(final Runnable runnable) throws InterruptedException { 039 if (Platform.isFxApplicationThread()) 040 runnable.run(); 041 else { 042 final CountDownLatch cdl = new CountDownLatch(1); 043 Platform.runLater(() -> { 044 runnable.run(); 045 cdl.countDown(); 046 }); 047 cdl.await(); 048 } 049 } 050 051 public static final void runOnFXThreadAndWaitTryCatch(final Runnable runnable) { 052 try { 053 runOnFXThreadAndWait(runnable); 054 } catch (InterruptedException __) {} 055 } 056 057}