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 javafx.scene.Node;
32 import javafx.scene.control.Alert;
33 import javafx.scene.control.ButtonType;
34 import javafx.scene.control.Dialog;
35 import javafx.application.Platform;
36 import it.tidalwave.util.Callback;
37 import it.tidalwave.util.ui.UserNotificationWithFeedback;
38 import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport;
39 import lombok.SneakyThrows;
40 import lombok.extern.slf4j.Slf4j;
41
42 /***************************************************************************************************************************************************************
43 *
44 * @author Fabrizio Giudici
45 *
46 **************************************************************************************************************************************************************/
47 @Slf4j
48 public class DialogBindings extends DelegateSupport
49 {
50 /***********************************************************************************************************************************************************
51 *
52 **********************************************************************************************************************************************************/
53 public DialogBindings (@Nonnull final Executor executor)
54 {
55 super(executor);
56 }
57
58 /***********************************************************************************************************************************************************
59 * {@inheritDoc}
60 **********************************************************************************************************************************************************/
61 public void showInModalDialog (@Nonnull final UserNotificationWithFeedback notification,
62 @Nonnull final Optional<Node> node)
63 {
64 // FIXME: should not be needed
65 Platform.runLater(() ->
66 {
67 log.debug("modalDialog({}, {})", node, notification);
68
69 // final Dialog<ButtonType> dialog = new Dialog<>();
70 final Dialog<ButtonType> dialog = new Alert(Alert.AlertType.NONE);
71 dialog.initOwner(mainWindow);
72 dialog.setTitle(notification.getCaption());
73 dialog.setResizable(false);
74 dialog.setContentText(notification.getText());
75 node.ifPresent(n -> dialog.getDialogPane().setContent(n));
76
77 final var feedback = notification.getFeedback();
78 final var hasOnCancel = feedback.canCancel();
79
80 final var buttonTypes = dialog.getDialogPane().getButtonTypes();
81 buttonTypes.clear();
82 buttonTypes.add(ButtonType.OK);
83
84 if (hasOnCancel)
85 {
86 buttonTypes.add(ButtonType.CANCEL);
87 }
88
89 // okButton.disableProperty().bind(new PropertyAdapter<>(valid)); // FIXME: doesn't work
90
91 final var result = dialog.showAndWait();
92
93 if (result.isEmpty())
94 {
95 if (hasOnCancel)
96 {
97 wrap(notification::cancel);
98 }
99 else
100 {
101 wrap(notification::confirm);
102 }
103 }
104 else
105 {
106 if (result.get() == ButtonType.OK)
107 {
108 wrap(notification::confirm);
109 }
110 else if (result.get() == ButtonType.CANCEL)
111 {
112 wrap(notification::cancel);
113 }
114 else
115 {
116 throw new IllegalStateException("Unexpected button pressed: " + result.get());
117 }
118 }
119 });
120 }
121
122 /***********************************************************************************************************************************************************
123 *
124 **********************************************************************************************************************************************************/
125 @SneakyThrows(Throwable.class)
126 private static void wrap (@Nonnull final Callback callback)
127 {
128 callback.call();
129 }
130 }