Skip to content

Package: UserNotificationWithFeedback

UserNotificationWithFeedback

nameinstructionbranchcomplexitylinemethod
UserNotificationWithFeedback(String, String, UserNotificationWithFeedback.Feedback)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
cancel()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
confirm()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
feedback()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
notificationWithFeedback()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withCaption(Class, String, Object[])
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withCaption(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withFeedback(UserNotificationWithFeedback.Feedback)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withText(Class, String, Object[])
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withText(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 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
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: @SuppressWarnings("RedundantThrows")
99: @SneakyThrows(Throwable.class)
100: private void onConfirm()
101: throws Exception
102: {
103: onConfirm.call();
104: }
105:
106: /***************************************************************************************************************
107: *
108: * Callback method invoked when the user cancels an operation.
109: *
110: * @throws Exception in cases of error
111: *
112: **************************************************************************************************************/
113: @SuppressWarnings("RedundantThrows")
114: @SneakyThrows(Throwable.class)
115: private void onCancel()
116: throws Exception
117: {
118: onCancel.call();
119: }
120: }
121:
122: @Getter
123: protected final Feedback feedback;
124:
125: /*******************************************************************************************************************
126: *
127: * @param text the notification text
128: * @param caption the notification caption
129: * @param feedback the feedback
130: *
131: ******************************************************************************************************************/
132: protected UserNotificationWithFeedback (@Nonnull final String text,
133: @Nonnull final String caption,
134: @Nonnull final Feedback feedback)
135: {
136: super(text, caption);
137: this.feedback = feedback;
138: }
139:
140: /*******************************************************************************************************************
141: *
142: * Creates a notification with empty caption and text.
143: *
144: * @return the notification
145: *
146: ******************************************************************************************************************/
147: @Nonnull
148: public static UserNotificationWithFeedback notificationWithFeedback()
149: {
150: return new UserNotificationWithFeedback("", "", feedback());
151: }
152:
153: /*******************************************************************************************************************
154: *
155: * Associates a caption to the notification.
156: *
157: * @param caption the caption
158: * @return the notification
159: *
160: ******************************************************************************************************************/
161: @Override @Nonnull
162: public UserNotificationWithFeedback withCaption (@Nonnull final String caption)
163: {
164: return new UserNotificationWithFeedback(text, caption, feedback);
165: }
166:
167: /*******************************************************************************************************************
168: *
169: * Associates a caption to the notification, retrieved from a resource bundle.
170: *
171: * @param bundleClass the class where to search the resource bundle from
172: * @param resourceName the resource name of the caption in the bundle
173: * @param params some (optional) parameters to the resource
174: * @return the notification
175: *
176: ******************************************************************************************************************/
177: @Override @Nonnull
178: public UserNotificationWithFeedback withCaption (@Nonnull final Class<?> bundleClass,
179: @Nonnull final String resourceName,
180: @Nonnull final Object ... params)
181: {
182: return new UserNotificationWithFeedback(text, getMessage(bundleClass, resourceName, params), feedback);
183: }
184:
185: /*******************************************************************************************************************
186: *
187: * Associates a text to the notification.
188: *
189: * @param text the text
190: * @return the notification
191: *
192: ******************************************************************************************************************/
193: @Override @Nonnull
194: public UserNotificationWithFeedback withText (@Nonnull final String text)
195: {
196: return new UserNotificationWithFeedback(text, caption, feedback);
197: }
198:
199: /*******************************************************************************************************************
200: *
201: * Associates a text to the notification, retrieved from a resource bundle.
202: *
203: * @param bundleClass the class where to search the resource bundle from
204: * @param resourceName the resource name of the text in the bundle
205: * @param params some (optional) parameters to the resource
206: * @return the notification
207: *
208: ******************************************************************************************************************/
209: @Override @Nonnull
210: public UserNotificationWithFeedback withText (@Nonnull final Class<?> bundleClass,
211: @Nonnull final String resourceName,
212: @Nonnull final Object ... params)
213: {
214: return new UserNotificationWithFeedback(getMessage(bundleClass, resourceName, params), caption, feedback);
215: }
216:
217: /*******************************************************************************************************************
218: *
219: * Associates a {@link Feedback} to the notification.
220: *
221: * @param feedback the {@code Feedback} to associate
222: * @return the notification
223: *
224: ******************************************************************************************************************/
225: @Nonnull
226: public UserNotificationWithFeedback withFeedback (@Nonnull final Feedback feedback)
227: {
228: return new UserNotificationWithFeedback(text, caption, feedback);
229: }
230:
231: /*******************************************************************************************************************
232: *
233: * Notifies a confirmation to the user notification.
234: *
235: * @throws Exception in cases of error
236: *
237: ******************************************************************************************************************/
238: public void confirm()
239: throws Exception
240: {
241: feedback.onConfirm();
242: }
243:
244: /*******************************************************************************************************************
245: *
246: * Notifies a cancellation to the user notification.
247: *
248: * @throws Exception in cases of error
249: *
250: ******************************************************************************************************************/
251: public void cancel()
252: throws Exception
253: {
254: feedback.onCancel();
255: }
256:
257: /*******************************************************************************************************************
258: *
259: * Creates a new {@code Feedback} that does nothing. This method should be chained with {@code withOnConfirm()}
260: * and/or {@code withOnCancel(Callback)} to specify the relative callbacks.
261: *
262: * <pre>
263: * feedback().withOnConfirm(this::doSomething).withOnCancel(this::doSomethingElse);
264: * </pre>
265: *
266: * @return a feedback that does nothing in any case
267: * @since 3.2-ALPHA-1 (was previously on {@code Feedback8}
268: *
269: ******************************************************************************************************************/
270: @Nonnull
271: public static Feedback feedback()
272: {
273: return new Feedback(Callback.EMPTY, Callback.EMPTY);
274: }
275: }