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 com.google.gwt.dom.client.Element;
19  import com.google.gwt.event.dom.client.ClickEvent;
20  import com.google.gwt.event.dom.client.ClickHandler;
21  import com.google.gwt.event.dom.client.MouseOutEvent;
22  import com.google.gwt.event.dom.client.MouseOutHandler;
23  import com.google.gwt.event.dom.client.MouseOverEvent;
24  import com.google.gwt.event.dom.client.MouseOverHandler;
25  import com.google.gwt.event.logical.shared.CloseEvent;
26  import com.google.gwt.event.logical.shared.CloseHandler;
27  import com.google.gwt.event.logical.shared.HasCloseHandlers;
28  import com.google.gwt.event.shared.HandlerRegistration;
29  import com.google.gwt.user.client.DOM;
30  import com.google.gwt.user.client.Event;
31  import com.google.gwt.user.client.ui.Composite;
32  import com.google.gwt.user.client.ui.HTML;
33  import com.google.gwt.user.client.ui.HTMLPanel;
34  import com.google.gwt.user.client.ui.Widget;
35  
36  public class KSNotification extends Composite implements HasCloseHandlers<KSNotification>{
37  	private final int duration;
38  	public static final int DEFAULT_DURATION = 4000;
39  	private final String id = HTMLPanel.createUniqueId();
40  	private final String contentId = HTMLPanel.createUniqueId();
41  	private final String closeLinkId = HTMLPanel.createUniqueId();
42  	private final HTMLPanel panel = new HTMLPanel("<p id='" + contentId + "'></p><a href='javascript:return false;' title='Close' class='ks-notification-close' style='visibility: hidden' id='" + closeLinkId + "'></a>");
43  	
44  	public KSNotification(final String message, boolean isHtml, final int duration) {
45  		this.duration = duration;
46  		panel.setStyleName("ks-notification-message");
47  		panel.getElement().setId(id);
48  		
49  		if (isHtml) {
50  			panel.add(new HTML(message), contentId);
51  		} else {
52  			panel.getElementById(contentId).setInnerText(message);
53  		}
54  		
55  		initHandlers();
56  		super.initWidget(panel);
57  	}
58  	
59  	public KSNotification(final String message, boolean isHtml, boolean isError, final int duration) {
60  		this(message, isHtml, duration);
61  		if (isError){
62  			panel.setStyleName("ks-notification-error");			
63  		}
64  	}
65  
66  	public KSNotification(final String message, boolean isHtml) {
67  		this.duration = DEFAULT_DURATION;
68  		panel.setStyleName("ks-notification-message");
69  		panel.getElement().setId(id);
70  		
71  		if (isHtml) {
72  			panel.add(new HTML(message), contentId);
73  		} else {
74  			panel.getElementById(contentId).setInnerText(message);
75  		}
76  		
77  		initHandlers();
78  		super.initWidget(panel);
79  	}
80  
81  	public KSNotification(final Widget widget, final int duration) {
82  		this.duration = duration;
83  		panel.setStyleName("ks-notification-message");
84  		panel.getElement().setId(id);
85  		panel.add(widget, contentId);
86  		
87  		initHandlers();
88  		super.initWidget(panel);
89  	}
90  
91  	private void initHandlers() {
92  		addDomHandler(new MouseOverHandler() {
93  			@Override
94  			public void onMouseOver(MouseOverEvent event) {
95  				DOM.getElementById(closeLinkId).getStyle().setProperty("visibility", "visible");
96  			}
97  		}, MouseOverEvent.getType());
98  
99  		addDomHandler(new MouseOutHandler() {
100 			@Override
101 			public void onMouseOut(MouseOutEvent event) {
102 				DOM.getElementById(closeLinkId).getStyle().setProperty("visibility", "hidden");
103 			}
104 		}, MouseOutEvent.getType());
105 		
106 		DOM.sinkEvents(panel.getElementById(closeLinkId), Event.ONCLICK);
107 		addDomHandler(new ClickHandler() {
108 			@Override
109 			public void onClick(ClickEvent event) {
110 				Element e = Element.as(event.getNativeEvent().getEventTarget());
111 				if (e.equals(panel.getElementById(closeLinkId))) {
112 					CloseEvent.fire(KSNotification.this, KSNotification.this);
113 				}
114 			}
115 		}, ClickEvent.getType());
116 	}
117 	
118 	/**
119 	 * @return the duration
120 	 */
121 	public int getDuration() {
122 		return duration;
123 	}
124 
125 	public String getId() {
126 		return id;
127 	}
128 
129 	public String getContentId() {
130 		return contentId;
131 	}
132 
133 	public String getCloseLinkId() {
134 		return closeLinkId;
135 	}
136 
137 	
138 	@Override
139 	public HandlerRegistration addCloseHandler(
140 			CloseHandler<KSNotification> handler) {
141 		return addHandler(handler, CloseEvent.getType());
142 	}
143 }