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.util;
17  
18  import com.google.gwt.dom.client.Element;
19  import com.google.gwt.dom.client.Style;
20  import com.google.gwt.user.client.Command;
21  import com.google.gwt.user.client.DeferredCommand;
22  import com.google.gwt.user.client.Timer;
23  import com.google.gwt.user.client.ui.UIObject;
24  
25  /**
26   * Utility class for DOM element manipulations, UIObject effects, etc.
27   * 
28   */
29  public class Elements {
30  
31  	public interface FadeCallback {
32  		public void onFadeComplete();
33  
34  		public void onFadeStart();
35  	}
36  
37  	public static class EmptyFadeCallback implements FadeCallback {
38          public void onFadeComplete() {}
39          public void onFadeStart() {}
40  	}
41  	/**
42  	 * Causes an element to "fade in" via opacity. Make sure that element to
43  	 * fade is not visible before calling this method. When the callback is
44  	 * invoked with onFadeStart, the element can be set to visible as it is
45  	 * entirely transparent. When onFadeComplete is called, the element will be
46  	 * entirely opaque.
47  	 * 
48  	 * @param target
49  	 *            the UIObject to be faded in
50  	 * @param milliseconds
51  	 *            how long the fade effect should take to process
52  	 * @param maxOpacity
53  	 * 			  final opacity
54  	 * @param callback
55  	 *            the callback to be invoked on fade progress
56  	 */
57  	public static void fadeIn(final UIObject target, final int milliseconds, final int maxOpacity,
58  			final FadeCallback callback) {
59  	    fadeIn(target.getElement(), milliseconds, maxOpacity, callback);
60  	}
61  	public static void fadeIn(final Element e, final int milliseconds, final int maxOpacity,
62              final FadeCallback callback) {
63  
64  	    final int interval = milliseconds / 50;
65  		setOpacity(e, 0);
66  		DeferredCommand.addCommand(new Command() {
67  
68  			@Override
69  			public void execute() {
70  				callback.onFadeStart();
71  
72  				final Timer t = new Timer() {
73  					int pct = 0;
74  
75  					@Override
76  					public void run() {
77  						pct += 2;
78  						pct = Math.min(pct, maxOpacity);
79  						setOpacity(e, pct);
80  						if (pct == maxOpacity) {
81  							this.cancel();
82  							callback.onFadeComplete();
83  						}
84  					}
85  				};
86  				t.scheduleRepeating(interval);
87  
88  			}
89  		});
90  	}
91  
92  	/**
93  	 * Causes an element to "fade out" via opacity. When onFadeComplete is
94  	 * called, the element will be entirely transparent.
95  	 * 
96  	 * @param target
97  	 *            the UIObject to be faded out
98  	 * @param milliseconds
99  	 *            how long the fade effect should take to process
100 	 * @param minOpacity
101 	 * 			  final opacity
102 	 * @param callback
103 	 *            the callback to be invoked on fade progress
104 	 */
105 	public static void fadeOut(final UIObject target, final int milliseconds, final int minOpacity, 
106 			final FadeCallback callback) {
107 	    fadeOut(target.getElement(), milliseconds, minOpacity, callback);
108 	}
109 	public static void fadeOut(final Element e, final int milliseconds, final int minOpacity, 
110 	       final FadeCallback callback) {
111 
112 		final int interval = milliseconds / 50;
113 		DeferredCommand.addCommand(new Command() {
114 
115 			@Override
116 			public void execute() {
117 				callback.onFadeStart();
118 
119 				final Timer t = new Timer() {
120 					int pct = 100;
121 
122 					@Override
123 					public void run() {
124 						pct -= 2;
125 						pct = Math.max(pct, minOpacity);
126 						setOpacity(e, pct);
127 						if (pct == minOpacity) {
128 							this.cancel();
129 							callback.onFadeComplete();
130 						}
131 					}
132 				};
133 				t.scheduleRepeating(interval);
134 
135 			}
136 		});
137 	}
138 
139 	/**
140      * Sets a UIObject's opacity
141      * 
142      * @param u
143      * @param percent
144      */
145     public static void setOpacity(final UIObject u, final int percent) {
146         setOpacity(u.getElement(), percent);
147     }
148     
149 	/**
150 	 * Sets a DOM element's opacity
151 	 * 
152 	 * @param e
153 	 * @param percent
154 	 */
155 	public static void setOpacity(final Element e, final int percent) {
156 		final Style s = e.getStyle();
157 		final double d = ((double) percent / (double) 100);
158 		s.setProperty("opacity", String.valueOf(d));
159 		s.setProperty("MozOpacity", String.valueOf(d));
160 		s.setProperty("KhtmlOpacity", String.valueOf(d));
161 		s.setProperty("filter", "alpha(opacity=" + percent + ")");
162 	}
163 
164 	/**
165 	 * Enables/disables text selection for the specified element.
166 	 * 
167 	 * @param e
168 	 * @param selectable
169 	 */
170 	public static native void setTextSelectable(Element e, boolean selectable)/*-{
171 																				if (selectable) {
172 																				e.ondrag = null;
173 																				e.onselectstart = null;
174 																				e.style.MozUserSelect="text";
175 																				} else {
176 																				e.ondrag = function () { return false; };
177 																				e.onselectstart = function () { return false; };
178 																				e.style.MozUserSelect="none";
179 																				}
180 																				}-*/;
181 }