1 package org.kuali.student.common.ui.client.widgets;
2
3 import com.google.gwt.dom.client.NativeEvent;
4 import com.google.gwt.event.dom.client.ClickEvent;
5 import com.google.gwt.event.dom.client.ClickHandler;
6 import com.google.gwt.event.dom.client.KeyCodes;
7 import com.google.gwt.event.logical.shared.ResizeEvent;
8 import com.google.gwt.event.logical.shared.ResizeHandler;
9 import com.google.gwt.event.shared.GwtEvent;
10 import com.google.gwt.event.shared.HandlerManager;
11 import com.google.gwt.event.shared.HandlerRegistration;
12 import com.google.gwt.user.client.Command;
13 import com.google.gwt.user.client.DeferredCommand;
14 import com.google.gwt.user.client.Window;
15 import com.google.gwt.user.client.Event.NativePreviewEvent;
16 import com.google.gwt.user.client.ui.Anchor;
17 import com.google.gwt.user.client.ui.DialogBox;
18 import com.google.gwt.user.client.ui.FlexTable;
19 import com.google.gwt.user.client.ui.FlowPanel;
20 import com.google.gwt.user.client.ui.HorizontalPanel;
21 import com.google.gwt.user.client.ui.ScrollPanel;
22 import com.google.gwt.user.client.ui.Widget;
23
24
25 public class KSDialog extends DialogBox {
26
27 private final HandlerManager handlers = new HandlerManager(this);
28 private int maxWidth = 800;
29 private int maxHeight = 0;
30 private FlowPanel mainPanel = new FlowPanel();
31 private FlowPanel titlePanel = new FlowPanel();
32 private ScrollPanel scrollPanel = new ScrollPanel();
33
34 private Anchor closeLink = new Anchor();
35 private KSDialogResizeHandler resizeHandler = new KSDialogResizeHandler();
36 private HandlerRegistration resizeHandlerRegistrater;
37
38 private FlexTable verticalPanel = new FlexTable();
39 private HorizontalPanel buttonPanel = new HorizontalPanel();
40 public KSDialog() {
41 super.setStyleName("ks-lightbox");
42
43 mainPanel.setStyleName("ks-lightbox-mainPanel");
44 titlePanel.setStyleName("ks-lightbox-titlePanel");
45 closeLink.setStyleName("ks-lightbox-title-closeLink");
46 scrollPanel.setStyleName("ks-lightbox-title-scrollPanel");
47
48 setGlassEnabled(true);
49 super.setWidget(mainPanel);
50 mainPanel.add(titlePanel);
51 mainPanel.add(scrollPanel);
52 titlePanel.add(closeLink);
53
54 verticalPanel.setStyleName("ks-lightbox-layoutTable");
55 verticalPanel.setWidget(1, 0, buttonPanel);
56 verticalPanel.getRowFormatter().setStyleName(1, "ks-lightbox-buttonRow");
57 scrollPanel.add(verticalPanel);
58
59 installResizeHandler();
60
61 closeLink.addClickHandler(new ClickHandler(){
62
63 @Override
64 public void onClick(ClickEvent event) {
65 hide();
66 }
67 });
68
69 }
70 public void uninstallResizeHandler(){
71 if(resizeHandlerRegistrater != null){
72 resizeHandlerRegistrater.removeHandler();
73 }
74 }
75 public void installResizeHandler(){
76 if(resizeHandlerRegistrater == null){
77 resizeHandlerRegistrater = Window.addResizeHandler(resizeHandler);
78 }
79 }
80 public void setCloseLinkVisible(boolean visible){
81 closeLink.setVisible(visible);
82 }
83 public void addButton(Widget button){
84 buttonPanel.add(button);
85 }
86 public void setWidget(Widget content){
87 verticalPanel.setWidget(0, 0, content);
88 verticalPanel.getRowFormatter().setStyleName(0, "ks-lightbox-contentRow");
89 }
90 public void setMaxWidth(int width){
91 this.maxWidth = width;
92 }
93 public void setMaxHeight(int height){
94 this.maxHeight = height;
95 }
96 public void setSize(int width, int height){
97 super.setSize(width+"px", height+"px");
98 scrollPanel.setSize((width+10)+"px", (height+10)+"px");
99 }
100 @Override
101 public void hide(){
102 super.hide();
103 uninstallResizeHandler();
104 }
105 @Override
106 public void show(){
107 resizeDialog();
108 super.show();
109 }
110 @Override
111 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
112 super.onPreviewNativeEvent(preview);
113 NativeEvent evt = preview.getNativeEvent();
114 if (evt.getType().equals("keydown")) {
115 switch (evt.getKeyCode()) {
116 case KeyCodes.KEY_ESCAPE:
117 hide();
118 break;
119 }
120 }
121 }
122 private void resizeDialog(){
123 int width = maxWidth;
124 int height = maxHeight;
125 if (Window.getClientWidth() < 850){
126 width = Window.getClientWidth() - 160;
127 }
128
129 height = Window.getClientHeight() - 160;
130
131
132 if(width > maxWidth){
133 width = maxWidth;
134 }
135 if(height > maxHeight && maxHeight != 0){
136 height = maxHeight;
137 }
138
139 if(width > 0 && height > 0){
140 setSize(width, height);
141 }
142 DeferredCommand.addCommand(new Command(){
143
144 @Override
145 public void execute() {
146 int left = (Window.getClientWidth() - getOffsetWidth()) >> 1;
147 int top = (Window.getClientHeight() - getOffsetHeight()) >> 1;
148 setPopupPosition(Math.max(Window.getScrollLeft() + left, 0), Math.max(
149 Window.getScrollTop() + top, 0));
150 }
151 });
152 }
153
154
155 @Override
156 public void fireEvent(GwtEvent<?> event) {
157 handlers.fireEvent(event);
158 }
159
160 class KSDialogResizeHandler implements ResizeHandler{
161 @Override
162 public void onResize(ResizeEvent event) {
163 DeferredCommand.addCommand(new Command(){
164
165 @Override
166 public void execute() {
167 resizeDialog();
168
169 }
170 });
171 }
172 }
173 }