1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.notification;
17
18 import org.kuali.student.common.ui.client.mvc.Holder;
19 import org.kuali.student.common.ui.client.util.Elements;
20
21 import com.google.gwt.event.logical.shared.CloseEvent;
22 import com.google.gwt.event.logical.shared.CloseHandler;
23 import com.google.gwt.event.shared.HandlerRegistration;
24 import com.google.gwt.user.client.Timer;
25 import com.google.gwt.user.client.ui.FlowPanel;
26 import com.google.gwt.user.client.ui.RootPanel;
27
28 public class KSNotifier {
29 private static final FlowPanel notifier = new FlowPanel();
30
31 public static final int FADE_DURATION = 1000;
32
33 private static final int DEFAULT_FADE_DURATION = 4000;
34
35 static {
36 notifier.setStyleName("ks-notification-container");
37 }
38
39 private KSNotifier() {
40
41 }
42
43 public static void show(String message) {
44 add(new KSNotification(message, false, DEFAULT_FADE_DURATION));
45 }
46
47 public static void add(final KSNotification notification) {
48 if (notifier.getWidgetCount() == 0) {
49 RootPanel.get().add(notifier);
50 }
51
52 final Holder<HandlerRegistration> reg = new Holder<HandlerRegistration>();
53 reg.set(notification.addCloseHandler(new CloseHandler<KSNotification>() {
54 @Override
55 public void onClose(CloseEvent<KSNotification> event) {
56 reg.get().removeHandler();
57 remove(notification, false);
58 }
59 }));
60
61 Elements.setOpacity(notification.getElement(), 0);
62 notifier.add(notification);
63 Elements.fadeIn(notification, FADE_DURATION, 85, new Elements.FadeCallback() {
64
65 @Override
66 public void onFadeStart() {
67
68 }
69
70 @Override
71 public void onFadeComplete() {
72 new Timer() {
73 @Override
74 public void run() {
75 reg.get().removeHandler();
76 remove(notification, true);
77 }
78 }.schedule(notification.getDuration());
79 }
80 });
81
82 }
83
84 public static void remove(final KSNotification notification, final boolean fireEvents) {
85 if (notifier.getWidgetIndex(notification) != -1) {
86 Elements.fadeOut(notification, FADE_DURATION, 0, new Elements.FadeCallback() {
87 @Override
88 public void onFadeStart() {
89
90 }
91
92 @Override
93 public void onFadeComplete() {
94 notifier.remove(notification);
95 if (fireEvents) {
96 CloseEvent.fire(notification, notification);
97 }
98 if (notifier.getWidgetCount() == 0) {
99 RootPanel.get().remove(notifier);
100 }
101 }
102 });
103 }
104 }
105 }