View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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                  // do nothing
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                      // do nothing
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 }