1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
43
44
45
46
47
48
49
50
51
52
53
54
55
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
94
95
96
97
98
99
100
101
102
103
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
141
142
143
144
145 public static void setOpacity(final UIObject u, final int percent) {
146 setOpacity(u.getElement(), percent);
147 }
148
149
150
151
152
153
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
166
167
168
169
170 public static native void setTextSelectable(Element e, boolean selectable)
171
172
173
174
175
176
177
178
179
180 ;
181 }