1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * SteelBlue: DCI User Interfaces
5 * http://tidalwave.it/projects/steelblue
6 *
7 * Copyright (C) 2015 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * 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
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/steelblue-src
22 * git clone https://github.com/tidalwave-it/steelblue-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.role.ui.javafx.impl.common;
27
28 import javax.annotation.Nonnull;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.Executor;
31 import javafx.scene.effect.BoxBlur;
32 import javafx.stage.Window;
33 import javafx.application.Platform;
34 import lombok.RequiredArgsConstructor;
35 import lombok.Setter;
36
37 /***************************************************************************************************************************************************************
38 *
39 * @author Fabrizio Giudici
40 *
41 **************************************************************************************************************************************************************/
42 @RequiredArgsConstructor
43 public abstract class DelegateSupport
44 {
45 @Nonnull
46 protected final Executor executor;
47
48 @Setter
49 protected Window mainWindow;
50
51 /***********************************************************************************************************************************************************
52 * Runs the given (@link Callable} while disabling the given {@link Window}.
53 *
54 * @param window the {@code Window} to disable
55 * @param callable the (@code Callable} to run
56 * @return the (@code Callable} result
57 **********************************************************************************************************************************************************/
58 protected <T> T runWhileDisabling (@Nonnull final Window window, @Nonnull final Callable<T> callable)
59 {
60 final var root = window.getScene().getRoot();
61 final var effect = root.getEffect();
62 final var disabled = root.isDisable();
63
64 try
65 {
66 root.setDisable(true);
67 root.setEffect(createDisablingEffect());
68 return callable.call();
69 }
70 catch (Exception e)
71 {
72 throw new RuntimeException(e);
73 }
74 finally
75 {
76 root.setEffect(effect);
77 root.setDisable(disabled);
78 }
79 }
80
81 /***********************************************************************************************************************************************************
82 * TODO: delegate to a provider
83 **********************************************************************************************************************************************************/
84 @Nonnull
85 private BoxBlur createDisablingEffect()
86 {
87 final var bb = new BoxBlur();
88 bb.setWidth(5);
89 bb.setHeight(5);
90 bb.setIterations(3);
91 return bb;
92 }
93
94 /***********************************************************************************************************************************************************
95 * TODO: delegate to a provider
96 **********************************************************************************************************************************************************/
97 public static boolean isOSX()
98 {
99 return System.getProperty("os.name").contains("OS X");
100 }
101
102 /***********************************************************************************************************************************************************
103 *
104 **********************************************************************************************************************************************************/
105 protected void assertIsFxApplicationThread()
106 {
107 if (!Platform.isFxApplicationThread())
108 {
109 throw new AssertionError("Must run in the JavaFX Application Thread");
110 }
111 }
112 }