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