JavaFXSafeRunner.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2015 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl.util;

  30. import javax.annotation.Nonnull;
  31. import javafx.application.Platform;
  32. import lombok.AccessLevel;
  33. import lombok.NoArgsConstructor;
  34. import lombok.extern.slf4j.Slf4j;

  35. /***********************************************************************************************************************
  36.  *
  37.  * @author  Fabrizio Giudici
  38.  *
  39.  **********************************************************************************************************************/
  40. @Slf4j @NoArgsConstructor(access=AccessLevel.PRIVATE)
  41. public final class JavaFXSafeRunner
  42.   {
  43.     /*******************************************************************************************************************
  44.      *
  45.      *
  46.      ******************************************************************************************************************/
  47.     public static void runSafely (@Nonnull final Runnable runnable)
  48.       {
  49.         final Runnable guardedRunnable = () ->
  50.           {
  51.             try
  52.               {
  53.                 runnable.run();
  54.               }
  55.             catch (Throwable t)
  56.               {
  57.                 log.warn("", t);
  58.               }
  59.           };

  60.         if (Platform.isFxApplicationThread())
  61.           {
  62.             guardedRunnable.run();
  63.           }
  64.         else
  65.           {
  66.             Platform.runLater(guardedRunnable);
  67.           }
  68.       }
  69.   }