1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.student.common.ui.client.widgets; |
18 | |
|
19 | |
import java.util.Arrays; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup; |
23 | |
|
24 | |
import com.google.gwt.dom.client.Element; |
25 | |
import com.google.gwt.dom.client.NativeEvent; |
26 | |
import com.google.gwt.dom.client.NodeList; |
27 | |
import com.google.gwt.event.dom.client.ClickEvent; |
28 | |
import com.google.gwt.event.dom.client.ClickHandler; |
29 | |
import com.google.gwt.event.dom.client.KeyCodes; |
30 | |
import com.google.gwt.event.logical.shared.ResizeEvent; |
31 | |
import com.google.gwt.event.logical.shared.ResizeHandler; |
32 | |
import com.google.gwt.event.shared.HandlerRegistration; |
33 | |
import com.google.gwt.user.client.Command; |
34 | |
import com.google.gwt.user.client.DeferredCommand; |
35 | |
import com.google.gwt.user.client.Window; |
36 | |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
37 | |
import com.google.gwt.user.client.ui.Anchor; |
38 | |
import com.google.gwt.user.client.ui.Composite; |
39 | |
import com.google.gwt.user.client.ui.DialogBox; |
40 | |
import com.google.gwt.user.client.ui.FlexTable; |
41 | |
import com.google.gwt.user.client.ui.FlowPanel; |
42 | |
import com.google.gwt.user.client.ui.FocusWidget; |
43 | |
import com.google.gwt.user.client.ui.HasWidgets; |
44 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
45 | |
import com.google.gwt.user.client.ui.ScrollPanel; |
46 | |
import com.google.gwt.user.client.ui.Widget; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class KSLightBox extends DialogBox { |
52 | |
|
53 | |
|
54 | 0 | private static final List<String> FOCUSABLE_TAGS = Arrays.asList("INPUT", "SELECT", "BUTTON", "TEXTAREA"); |
55 | |
|
56 | 0 | private int maxWidth = 800; |
57 | 0 | private int maxHeight = 0; |
58 | 0 | private int minWidth = 400; |
59 | 0 | private int minHeight = 200; |
60 | 0 | private int permWidth = -1; |
61 | 0 | private int permHeight = -1; |
62 | 0 | private FlowPanel mainPanel = new FlowPanel(); |
63 | 0 | private FlowPanel titlePanel = new FlowPanel(); |
64 | 0 | private ScrollPanel scrollPanel = new ScrollPanel(); |
65 | |
|
66 | 0 | private Anchor closeLink = new Anchor(); |
67 | 0 | private KSDialogResizeHandler resizeHandler = new KSDialogResizeHandler(); |
68 | |
private HandlerRegistration resizeHandlerRegistrater; |
69 | |
|
70 | 0 | private FlexTable verticalPanel = new FlexTable(); |
71 | 0 | private HorizontalPanel buttonPanel = new HorizontalPanel(); |
72 | 0 | public KSLightBox() { |
73 | 0 | ((Widget) this.getCaption()).setVisible(false); |
74 | 0 | init(); |
75 | 0 | } |
76 | |
private void init(){ |
77 | 0 | super.setStyleName("ks-lightbox"); |
78 | |
|
79 | 0 | mainPanel.setStyleName("ks-lightbox-mainPanel"); |
80 | 0 | titlePanel.setStyleName("ks-lightbox-titlePanel"); |
81 | 0 | closeLink.setStyleName("ks-lightbox-title-closeLink"); |
82 | 0 | scrollPanel.setStyleName("ks-lightbox-title-scrollPanel"); |
83 | |
|
84 | 0 | setGlassEnabled(true); |
85 | 0 | super.setWidget(mainPanel); |
86 | 0 | mainPanel.add(titlePanel); |
87 | 0 | mainPanel.add(scrollPanel); |
88 | 0 | titlePanel.add(closeLink); |
89 | |
|
90 | 0 | verticalPanel.setStyleName("ks-lightbox-layoutTable"); |
91 | 0 | verticalPanel.setWidget(1, 0, buttonPanel); |
92 | 0 | verticalPanel.getRowFormatter().setStyleName(1, "ks-lightbox-buttonRow"); |
93 | 0 | scrollPanel.add(verticalPanel); |
94 | |
|
95 | 0 | installResizeHandler(); |
96 | |
|
97 | 0 | closeLink.addClickHandler(new ClickHandler(){ |
98 | |
|
99 | |
@Override |
100 | |
public void onClick(ClickEvent event) { |
101 | 0 | hide(); |
102 | 0 | } |
103 | |
}); |
104 | |
|
105 | |
|
106 | 0 | } |
107 | 0 | public KSLightBox(boolean addCloseLink) { |
108 | 0 | ((Widget) this.getCaption()).setVisible(false); |
109 | 0 | init(); |
110 | 0 | closeLink.setVisible(addCloseLink); |
111 | 0 | } |
112 | 0 | public KSLightBox(String title) { |
113 | 0 | init(); |
114 | 0 | super.setText(title); |
115 | 0 | } |
116 | |
public void uninstallResizeHandler(){ |
117 | 0 | if(resizeHandlerRegistrater != null){ |
118 | 0 | resizeHandlerRegistrater.removeHandler(); |
119 | 0 | resizeHandlerRegistrater = null; |
120 | |
|
121 | |
} |
122 | 0 | } |
123 | |
public void installResizeHandler(){ |
124 | 0 | if(resizeHandlerRegistrater == null){ |
125 | 0 | resizeHandlerRegistrater = Window.addResizeHandler(resizeHandler); |
126 | |
} |
127 | 0 | } |
128 | |
public void setCloseLinkVisible(boolean visible){ |
129 | 0 | closeLink.setVisible(visible); |
130 | 0 | } |
131 | |
public void addButton(Widget button){ |
132 | 0 | button.addStyleName("ks-button-spacing"); |
133 | 0 | buttonPanel.add(button); |
134 | 0 | } |
135 | |
|
136 | |
@SuppressWarnings("unchecked") |
137 | |
public void addButtonGroup(ButtonGroup group){ |
138 | 0 | buttonPanel.add(group); |
139 | 0 | } |
140 | |
|
141 | |
public void setWidget(Widget content){ |
142 | 0 | verticalPanel.setWidget(0, 0, content); |
143 | 0 | verticalPanel.getRowFormatter().setStyleName(0, "ks-lightbox-contentRow"); |
144 | 0 | } |
145 | |
public void setMaxWidth(int width){ |
146 | 0 | this.maxWidth = width; |
147 | 0 | } |
148 | |
public void setMaxHeight(int height){ |
149 | 0 | this.maxHeight = height; |
150 | 0 | } |
151 | |
public void setSize(int width, int height){ |
152 | 0 | super.setSize(width+"px", height+"px"); |
153 | 0 | this.permHeight = height; |
154 | 0 | this.permWidth = width; |
155 | 0 | scrollPanel.setSize((width+10)+"px", (height+10)+"px"); |
156 | 0 | } |
157 | |
|
158 | |
public void showButtons(boolean show){ |
159 | 0 | buttonPanel.setVisible(show); |
160 | 0 | } |
161 | |
|
162 | |
@Override |
163 | |
public void hide(){ |
164 | 0 | super.hide(); |
165 | 0 | uninstallResizeHandler(); |
166 | 0 | } |
167 | |
@Override |
168 | |
public void show(){ |
169 | 0 | resizeDialog(); |
170 | 0 | installResizeHandler(); |
171 | 0 | super.show(); |
172 | 0 | super.center(); |
173 | 0 | grabFocus(); |
174 | 0 | } |
175 | |
|
176 | |
private void grabFocus() { |
177 | 0 | Widget mainContent = verticalPanel.getWidget(0, 0); |
178 | 0 | NodeList<Element> nodeList = mainContent.getElement().getElementsByTagName("*"); |
179 | 0 | for (int i = 0; i < nodeList.getLength(); i++) { |
180 | 0 | Element e = nodeList.getItem(i); |
181 | 0 | if (FOCUSABLE_TAGS.contains(e.getTagName().toUpperCase())) { |
182 | 0 | e.focus(); |
183 | 0 | return; |
184 | |
} |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | 0 | } |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
@Override |
224 | |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
225 | 0 | super.onPreviewNativeEvent(preview); |
226 | 0 | NativeEvent evt = preview.getNativeEvent(); |
227 | 0 | if (evt.getType().equals("keydown")) { |
228 | 0 | switch (evt.getKeyCode()) { |
229 | |
case KeyCodes.KEY_ESCAPE: |
230 | 0 | hide(); |
231 | |
break; |
232 | |
} |
233 | |
} |
234 | 0 | } |
235 | |
public Widget getWidget() { |
236 | 0 | return verticalPanel.getWidget(0, 0); |
237 | |
} |
238 | |
|
239 | |
public void removeCloseLink(){ |
240 | 0 | closeLink.setVisible(false); |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
private void resizeDialog(){ |
247 | |
|
248 | 0 | int width = maxWidth; |
249 | 0 | int height = maxHeight; |
250 | |
|
251 | |
|
252 | 0 | if(permWidth != -1){ |
253 | 0 | width = permWidth; |
254 | |
} |
255 | |
else{ |
256 | 0 | if (Window.getClientWidth() < 850){ |
257 | 0 | width = Window.getClientWidth() - 160; |
258 | |
} |
259 | 0 | if(width > maxWidth){ |
260 | 0 | width = maxWidth; |
261 | |
} |
262 | 0 | if(width < minWidth){ |
263 | 0 | width = minWidth; |
264 | |
} |
265 | |
} |
266 | |
|
267 | |
|
268 | 0 | if(permHeight != -1){ |
269 | 0 | height = permHeight; |
270 | |
} |
271 | |
else{ |
272 | 0 | height = Window.getClientHeight() - 160; |
273 | |
|
274 | 0 | if(height > maxHeight && maxHeight != 0){ |
275 | 0 | height = maxHeight; |
276 | |
} |
277 | 0 | if(height < minHeight){ |
278 | 0 | height = minHeight; |
279 | |
} |
280 | |
} |
281 | |
|
282 | 0 | if(width > 0 && height > 0){ |
283 | 0 | super.setSize(width + "px", height + "px"); |
284 | 0 | scrollPanel.setSize((width+10)+"px", (height+10)+"px"); |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | 0 | } |
299 | |
|
300 | 0 | class KSDialogResizeHandler implements ResizeHandler{ |
301 | |
@Override |
302 | |
public void onResize(ResizeEvent event) { |
303 | 0 | DeferredCommand.addCommand(new Command(){ |
304 | |
|
305 | |
@Override |
306 | |
public void execute() { |
307 | 0 | resizeDialog(); |
308 | 0 | int left = (Window.getClientWidth() - getOffsetWidth()) >> 1; |
309 | 0 | int top = (Window.getClientHeight() - getOffsetHeight()) >> 1; |
310 | 0 | setPopupPosition(Math.max(Window.getScrollLeft() + left, 0), Math.max( |
311 | |
Window.getScrollTop() + top, 0)); |
312 | 0 | } |
313 | |
}); |
314 | 0 | } |
315 | |
} |
316 | |
|
317 | |
} |