Skip to content

Method: onCancel()

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 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.ui.core;
27:
28: import jakarta.annotation.Nonnull;
29: import javax.annotation.concurrent.Immutable;
30: import it.tidalwave.util.Callback;
31: import lombok.AccessLevel;
32: import lombok.AllArgsConstructor;
33: import lombok.Getter;
34: import lombok.SneakyThrows;
35: import lombok.ToString;
36: import lombok.With;
37: import static it.tidalwave.util.BundleUtilities.getMessage;
38:
39: /***************************************************************************************************************************************************************
40: *
41: * This class models a notification that will be presented to a user, and a feedback is expected (confirmation or cancellation).
42: *
43: * @since 2.0-ALPHA-2
44: * @author Fabrizio Giudici
45: *
46: **************************************************************************************************************************************************************/
47: @Getter @Immutable
48: @ToString(callSuper = true)
49: public class UserNotificationWithFeedback extends UserNotification
50: {
51: /***********************************************************************************************************************************************************
52: * This class provides a few callback methods to notify a choice from the user.
53: **********************************************************************************************************************************************************/
54: @AllArgsConstructor(access = AccessLevel.PRIVATE)
55: public static class Feedback
56: {
57: @With
58: private final Callback onConfirm;
59:
60: @With
61: private final Callback onCancel;
62:
63: /*******************************************************************************************************************************************************
64: * {@return {@code true} if the current instance has a callback for confirmation}.
65: ******************************************************************************************************************************************************/
66: public boolean canConfirm()
67: {
68: return onConfirm != Callback.EMPTY;
69: }
70:
71: /*******************************************************************************************************************************************************
72: * {@return {@code true} if the current instance has a callback for cancellation}.
73: ******************************************************************************************************************************************************/
74: public boolean canCancel()
75: {
76: return onCancel != Callback.EMPTY;
77: }
78:
79: /*******************************************************************************************************************************************************
80: * Callback method invoked when the user confirms an operation.
81: * @throws Exception in cases of error
82: ******************************************************************************************************************************************************/
83: @SuppressWarnings("RedundantThrows")
84: @SneakyThrows(Throwable.class)
85: private void onConfirm()
86: throws Exception
87: {
88: onConfirm.call();
89: }
90:
91: /*******************************************************************************************************************************************************
92: * Callback method invoked when the user cancels an operation.
93: * @throws Exception in cases of error
94: ******************************************************************************************************************************************************/
95: @SuppressWarnings("RedundantThrows")
96: @SneakyThrows(Throwable.class)
97: private void onCancel()
98: throws Exception
99: {
100: onCancel.call();
101: }
102: }
103:
104: @Nonnull
105: protected final Feedback feedback;
106:
107: /***********************************************************************************************************************************************************
108: * @param text the notification text
109: * @param caption the notification caption
110: * @param feedback the feedback
111: **********************************************************************************************************************************************************/
112: protected UserNotificationWithFeedback (@Nonnull final String text, @Nonnull final String caption, @Nonnull final Feedback feedback)
113: {
114: super(text, caption);
115: this.feedback = feedback;
116: }
117:
118: /***********************************************************************************************************************************************************
119: * {@return a notification with empty caption and text}.
120: **********************************************************************************************************************************************************/
121: @Nonnull
122: public static UserNotificationWithFeedback notificationWithFeedback()
123: {
124: return new UserNotificationWithFeedback("", "", feedback());
125: }
126:
127: /***********************************************************************************************************************************************************
128: * {@return a notification with a caption}.
129: * @param caption the caption
130: **********************************************************************************************************************************************************/
131: @Override @Nonnull
132: public UserNotificationWithFeedback withCaption (@Nonnull final String caption)
133: {
134: return new UserNotificationWithFeedback(text, caption, feedback);
135: }
136:
137: /***********************************************************************************************************************************************************
138: * {@return a notification with a caption from a bundle}.
139: * @param bundleClass the class where to search the resource bundle from
140: * @param resourceName the resource name of the caption in the bundle
141: * @param params some (optional) parameters to the resource
142: **********************************************************************************************************************************************************/
143: @Override @Nonnull
144: public UserNotificationWithFeedback withCaption (@Nonnull final Class<?> bundleClass, @Nonnull final String resourceName, @Nonnull final Object ... params)
145: {
146: return new UserNotificationWithFeedback(text, getMessage(bundleClass, resourceName, params), feedback);
147: }
148:
149: /***********************************************************************************************************************************************************
150: * {@return a notification with a text}.
151: * @param text the text
152: **********************************************************************************************************************************************************/
153: @Override @Nonnull
154: public UserNotificationWithFeedback withText (@Nonnull final String text)
155: {
156: return new UserNotificationWithFeedback(text, caption, feedback);
157: }
158:
159: /***********************************************************************************************************************************************************
160: * {@return a notification with a text from a bundle}.
161: * @param bundleClass the class where to search the resource bundle from
162: * @param resourceName the resource name of the text in the bundle
163: * @param params some (optional) parameters to the resource
164: **********************************************************************************************************************************************************/
165: @Override @Nonnull
166: public UserNotificationWithFeedback withText (@Nonnull final Class<?> bundleClass, @Nonnull final String resourceName, @Nonnull final Object ... params)
167: {
168: return new UserNotificationWithFeedback(getMessage(bundleClass, resourceName, params), caption, feedback);
169: }
170:
171: /***********************************************************************************************************************************************************
172: * {@return a notification {@link Feedback}}.
173: * @param feedback the {@code Feedback} to associate
174: **********************************************************************************************************************************************************/
175: @Nonnull
176: public UserNotificationWithFeedback withFeedback (@Nonnull final Feedback feedback)
177: {
178: return new UserNotificationWithFeedback(text, caption, feedback);
179: }
180:
181: /***********************************************************************************************************************************************************
182: * Notifies a confirmation to the user notification.
183: * @throws Exception in cases of error
184: **********************************************************************************************************************************************************/
185: public void confirm()
186: throws Exception
187: {
188: feedback.onConfirm();
189: }
190:
191: /***********************************************************************************************************************************************************
192: * Notifies a cancellation to the user notification.
193: * @throws Exception in cases of error
194: **********************************************************************************************************************************************************/
195: public void cancel()
196: throws Exception
197: {
198: feedback.onCancel();
199: }
200:
201: /***********************************************************************************************************************************************************
202: * {@return a new {@code Feedback} that does nothing}. This method should be chained with {@code withOnConfirm()} and/or {@code withOnCancel(Callback)} to
203: * specify the relative callbacks.
204: * <pre>{@code}
205: * feedback().withOnConfirm(this::doSomething).withOnCancel(this::doSomethingElse);
206: * }</pre>
207: **********************************************************************************************************************************************************/
208: @Nonnull
209: public static Feedback feedback()
210: {
211: return new Feedback(Callback.EMPTY, Callback.EMPTY);
212: }
213: }