Skip to contentMethod: lambda$showInModalDialog$1(Optional, UserNotificationWithFeedback)
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.dialog;
27:
28: import javax.annotation.Nonnull;
29: import java.util.Optional;
30: import java.util.concurrent.Executor;
31: import java.io.IOException;
32: import java.net.URI;
33: import java.net.URISyntaxException;
34: import java.awt.Desktop;
35: import javafx.beans.value.ChangeListener;
36: import javafx.beans.value.ObservableValue;
37: import javafx.scene.Node;
38: import javafx.scene.control.Alert;
39: import javafx.scene.control.ButtonType;
40: import javafx.scene.control.Dialog;
41: import javafx.scene.control.Label;
42: import javafx.scene.paint.Color;
43: import javafx.scene.web.WebView;
44: import javafx.application.Platform;
45: import it.tidalwave.util.BundleUtilities;
46: import it.tidalwave.util.Callback;
47: import it.tidalwave.util.ui.UserNotificationWithFeedback;
48: import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport;
49: import lombok.RequiredArgsConstructor;
50: import lombok.SneakyThrows;
51: import lombok.extern.slf4j.Slf4j;
52:
53: /***************************************************************************************************************************************************************
54: *
55: * @author Fabrizio Giudici
56: *
57: **************************************************************************************************************************************************************/
58: @Slf4j
59: public class DialogBindings extends DelegateSupport
60: {
61: @RequiredArgsConstructor
62: static class UrlOpener implements ChangeListener<String>
63: {
64: @Nonnull
65: private final WebView webView;
66:
67: @Nonnull
68: private final String text;
69:
70: @Override
71: public void changed (final ObservableValue<? extends String> observableValue, final String oldValue, final String newValue)
72: {
73: if (newValue != null && !newValue.isEmpty())
74: {
75: Platform.runLater(() ->
76: {
77: try
78: {
79: log.debug("Opening {} ...", newValue);
80: Desktop.getDesktop().browse(new URI(newValue));
81: }
82: catch (IOException | URISyntaxException e)
83: {
84: log.warn("", e);
85: }
86: });
87:
88: webView.getEngine().loadContent(text);
89: }
90: }
91: }
92:
93: /***********************************************************************************************************************************************************
94: *
95: **********************************************************************************************************************************************************/
96: public DialogBindings (@Nonnull final Executor executor)
97: {
98: super(executor);
99: }
100:
101: /***********************************************************************************************************************************************************
102: * {@inheritDoc}
103: **********************************************************************************************************************************************************/
104: public void showInModalDialog (@Nonnull final UserNotificationWithFeedback notification,
105: @Nonnull final Optional<Node> node)
106: {
107: // FIXME: should not be needed
108: Platform.runLater(() ->
109: {
110: log.debug("modalDialog({}, {})", node, notification);
111:
112: // final Dialog<ButtonType> dialog = new Dialog<>();
113: final Dialog<ButtonType> dialog = new Alert(Alert.AlertType.NONE);
114: dialog.initOwner(mainWindow);
115: dialog.setTitle(notification.getCaption());
116: dialog.setResizable(false);
117: final var dialogPane = dialog.getDialogPane();
118: final var text = notification.getText();
119:
120:• if (!text.startsWith("<html>"))
121: {
122: dialog.setContentText(text);
123: node.ifPresent(n -> dialogPane.setContent(n));
124: }
125: else
126: {
127: final var webView = new WebView();
128: webView.setPrefSize(600, 300); // FIXME: proportional to screen
129: webView.setContextMenuEnabled(false);
130: final var label = new Label();
131: dialogPane.setContent(label);
132: final var fontFamily = label.getFont().getFamily();
133: // System.err.println("FILL " + label.getTextFill());
134: final var textColor = "white"; // FIXME
135: var backgroundColor = "#252525"; // FIXME
136:
137: try
138: {
139: // Available in JDK 18+
140: final var setPageFill = webView.getClass().getDeclaredMethod("setPageFill", Color.class);
141: setPageFill.invoke(webView, Color.TRANSPARENT);
142: backgroundColor = "transparent";
143: }
144: catch (Exception e)
145: {
146: log.trace("WebView.setPageFill() not available", e);
147: }
148:
149: // FIXME
150: final var text2 = BundleUtilities.getMessage(getClass(),"htmlTemplate", text, textColor, backgroundColor, fontFamily);
151: // System.err.println(text2);
152: webView.getEngine().loadContent(text2);
153: webView.getEngine().locationProperty().addListener(new UrlOpener(webView, text2));
154: dialogPane.setContent(webView);
155: }
156:
157: final var feedback = notification.getFeedback();
158: final var hasOnCancel = feedback.canCancel();
159:
160: final var buttonTypes = dialogPane.getButtonTypes();
161: buttonTypes.clear();
162: buttonTypes.add(ButtonType.OK);
163:
164:• if (hasOnCancel)
165: {
166: buttonTypes.add(ButtonType.CANCEL);
167: }
168:
169: // okButton.disableProperty().bind(new PropertyAdapter<>(valid)); // FIXME: doesn't work
170:
171: final var result = dialog.showAndWait();
172:
173:• if (result.isEmpty())
174: {
175:• if (hasOnCancel)
176: {
177: wrap(notification::cancel);
178: }
179: else
180: {
181: wrap(notification::confirm);
182: }
183: }
184: else
185: {
186:• if (result.get() == ButtonType.OK)
187: {
188: wrap(notification::confirm);
189: }
190:• else if (result.get() == ButtonType.CANCEL)
191: {
192: wrap(notification::cancel);
193: }
194: else
195: {
196: throw new IllegalStateException("Unexpected button pressed: " + result.get());
197: }
198: }
199: });
200: }
201:
202: /***********************************************************************************************************************************************************
203: *
204: **********************************************************************************************************************************************************/
205: @SneakyThrows(Throwable.class)
206: private static void wrap (@Nonnull final Callback callback)
207: {
208: callback.call();
209: }
210: }