Skip to contentMethod: notificationWithFeedback()
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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
12: * the License. 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
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util.ui;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.Immutable;
31: import it.tidalwave.util.Callback;
32: import lombok.AccessLevel;
33: import lombok.AllArgsConstructor;
34: import lombok.Getter;
35: import lombok.SneakyThrows;
36: import lombok.ToString;
37: import lombok.With;
38: import static it.tidalwave.util.BundleUtilities.getMessage;
39:
40: /***********************************************************************************************************************
41: *
42: * This class models a user notification where a feedback is expected (confirmation or cancellation).
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @Immutable
48: @ToString(callSuper = true)
49: public class UserNotificationWithFeedback extends UserNotification
50: {
51: /*******************************************************************************************************************
52: *
53: * This class provides a few callback methods to notify a choice from the user.
54: *
55: ******************************************************************************************************************/
56: @AllArgsConstructor(access = AccessLevel.PRIVATE)
57: public static class Feedback
58: {
59: @With
60: private final Callback onConfirm;
61:
62: @With
63: private final Callback onCancel;
64:
65: /***************************************************************************************************************
66: *
67: * Checks whether the current instance has a callback for confirmation.
68: *
69: * @return {@code true} if the instance has the callback
70: * @since 3.2-ALPHA-1
71: *
72: **************************************************************************************************************/
73: public boolean canConfirm()
74: {
75: return onConfirm != Callback.EMPTY;
76: }
77:
78: /***************************************************************************************************************
79: *
80: * Checks whether the current instance has a callback for cancellation.
81: *
82: * @return {@code true} if the instance has the callback
83: * @since 3.2-ALPHA-1
84: *
85: **************************************************************************************************************/
86: public boolean canCancel()
87: {
88: return onCancel != Callback.EMPTY;
89: }
90:
91: /***************************************************************************************************************
92: *
93: * Callback method invoked when the user confirms an operation.
94: *
95: * @throws Exception in cases of error
96: *
97: **************************************************************************************************************/
98: @SneakyThrows(Throwable.class)
99: private void onConfirm()
100: throws Exception
101: {
102: onConfirm.call();
103: }
104:
105: /***************************************************************************************************************
106: *
107: * Callback method invoked when the user cancels an operation.
108: *
109: * @throws Exception in cases of error
110: *
111: **************************************************************************************************************/
112: @SneakyThrows(Throwable.class)
113: private void onCancel()
114: throws Exception
115: {
116: onCancel.call();
117: }
118: }
119:
120: @Getter
121: protected final Feedback feedback;
122:
123: /*******************************************************************************************************************
124: *
125: * @param text the notification text
126: * @param caption the notification caption
127: * @param feedback the feedback
128: *
129: ******************************************************************************************************************/
130: protected UserNotificationWithFeedback (@Nonnull final String text,
131: @Nonnull final String caption,
132: @Nonnull final Feedback feedback)
133: {
134: super(text, caption);
135: this.feedback = feedback;
136: }
137:
138: /*******************************************************************************************************************
139: *
140: * Creates a notification with empty caption and text.
141: *
142: * @return the notification
143: *
144: ******************************************************************************************************************/
145: @Nonnull
146: public static UserNotificationWithFeedback notificationWithFeedback()
147: {
148: return new UserNotificationWithFeedback("", "", feedback());
149: }
150:
151: /*******************************************************************************************************************
152: *
153: * Associates a caption to the notification.
154: *
155: * @param caption the caption
156: * @return the notification
157: *
158: ******************************************************************************************************************/
159: @Override @Nonnull
160: public UserNotificationWithFeedback withCaption (@Nonnull final String caption)
161: {
162: return new UserNotificationWithFeedback(text, caption, feedback);
163: }
164:
165: /*******************************************************************************************************************
166: *
167: * Associates a caption to the notification, retrieved from a resource bundle.
168: *
169: * @param bundleClass the class where to search the resource bundle from
170: * @param resourceName the resource name of the caption in the bundle
171: * @param params some (optional) parameters to the resource
172: * @return the notification
173: *
174: ******************************************************************************************************************/
175: @Override @Nonnull
176: public UserNotificationWithFeedback withCaption (@Nonnull final Class<?> bundleClass,
177: @Nonnull final String resourceName,
178: @Nonnull final Object ... params)
179: {
180: return new UserNotificationWithFeedback(text, getMessage(bundleClass, resourceName, params), feedback);
181: }
182:
183: /*******************************************************************************************************************
184: *
185: * Associates a text to the notification.
186: *
187: * @param text the text
188: * @return the notification
189: *
190: ******************************************************************************************************************/
191: @Override @Nonnull
192: public UserNotificationWithFeedback withText (@Nonnull final String text)
193: {
194: return new UserNotificationWithFeedback(text, caption, feedback);
195: }
196:
197: /*******************************************************************************************************************
198: *
199: * Associates a text to the notification, retrieved from a resource bundle.
200: *
201: * @param bundleClass the class where to search the resource bundle from
202: * @param resourceName the resource name of the text in the bundle
203: * @param params some (optional) parameters to the resource
204: * @return the notification
205: *
206: ******************************************************************************************************************/
207: @Override @Nonnull
208: public UserNotificationWithFeedback withText (@Nonnull final Class<?> bundleClass,
209: @Nonnull final String resourceName,
210: @Nonnull final Object ... params)
211: {
212: return new UserNotificationWithFeedback(getMessage(bundleClass, resourceName, params), caption, feedback);
213: }
214:
215: /*******************************************************************************************************************
216: *
217: * Associates a {@link Feedback} to the notification.
218: *
219: * @param feedback the {@code Feedback} to associate
220: * @return the notification
221: *
222: ******************************************************************************************************************/
223: @Nonnull
224: public UserNotificationWithFeedback withFeedback (@Nonnull final Feedback feedback)
225: {
226: return new UserNotificationWithFeedback(text, caption, feedback);
227: }
228:
229: /*******************************************************************************************************************
230: *
231: * Notifies a confirmation to the user notification.
232: *
233: * @throws Exception in cases of error
234: *
235: ******************************************************************************************************************/
236: public void confirm()
237: throws Exception
238: {
239: feedback.onConfirm();
240: }
241:
242: /*******************************************************************************************************************
243: *
244: * Notifies a cancellation to the user notification.
245: *
246: * @throws Exception in cases of error
247: *
248: ******************************************************************************************************************/
249: public void cancel()
250: throws Exception
251: {
252: feedback.onCancel();
253: }
254:
255: /*******************************************************************************************************************
256: *
257: * Creates a new {@code Feedback} that does nothing. This method should be chained with {@code withOnConfirm()}
258: * and/or {@code withOnCancel(Callback)} to specify the relative callbacks.
259: *
260: * <pre>
261: * feedback().withOnConfirm(this::doSomething).withOnCancel(this::doSomethingElse);
262: * </pre>
263: *
264: * @return a feedback that does nothing in any case
265: * @since 3.2-ALPHA-1 (was previously on {@code Feedback8}
266: *
267: ******************************************************************************************************************/
268: @Nonnull
269: public static Feedback feedback()
270: {
271: return new Feedback(Callback.EMPTY, Callback.EMPTY);
272: }
273: }