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