| 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 org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup; |
| 20 | |
|
| 21 | |
import com.google.gwt.dom.client.NativeEvent; |
| 22 | |
import com.google.gwt.event.dom.client.ClickEvent; |
| 23 | |
import com.google.gwt.event.dom.client.ClickHandler; |
| 24 | |
import com.google.gwt.event.dom.client.KeyCodes; |
| 25 | |
import com.google.gwt.event.logical.shared.ResizeEvent; |
| 26 | |
import com.google.gwt.event.logical.shared.ResizeHandler; |
| 27 | |
import com.google.gwt.event.shared.HandlerRegistration; |
| 28 | |
import com.google.gwt.user.client.Command; |
| 29 | |
import com.google.gwt.user.client.DeferredCommand; |
| 30 | |
import com.google.gwt.user.client.Window; |
| 31 | |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
| 32 | |
import com.google.gwt.user.client.ui.Anchor; |
| 33 | |
import com.google.gwt.user.client.ui.DialogBox; |
| 34 | |
import com.google.gwt.user.client.ui.FlexTable; |
| 35 | |
import com.google.gwt.user.client.ui.FlowPanel; |
| 36 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
| 37 | |
import com.google.gwt.user.client.ui.ScrollPanel; |
| 38 | |
import com.google.gwt.user.client.ui.Widget; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 0 | public class KSLightBox extends DialogBox { |
| 44 | |
|
| 45 | 0 | private int maxWidth = 800; |
| 46 | 0 | private int maxHeight = 0; |
| 47 | 0 | private int minWidth = 400; |
| 48 | 0 | private int minHeight = 200; |
| 49 | 0 | private int permWidth = -1; |
| 50 | 0 | private int permHeight = -1; |
| 51 | 0 | private FlowPanel mainPanel = new FlowPanel(); |
| 52 | 0 | private FlowPanel titlePanel = new FlowPanel(); |
| 53 | 0 | private ScrollPanel scrollPanel = new ScrollPanel(); |
| 54 | |
|
| 55 | 0 | private Anchor closeLink = new Anchor(); |
| 56 | 0 | private KSDialogResizeHandler resizeHandler = new KSDialogResizeHandler(); |
| 57 | |
private HandlerRegistration resizeHandlerRegistrater; |
| 58 | |
|
| 59 | 0 | private FlexTable verticalPanel = new FlexTable(); |
| 60 | 0 | private HorizontalPanel buttonPanel = new HorizontalPanel(); |
| 61 | 0 | public KSLightBox() { |
| 62 | 0 | ((Widget) this.getCaption()).setVisible(false); |
| 63 | 0 | init(); |
| 64 | 0 | } |
| 65 | |
private void init(){ |
| 66 | 0 | super.setStyleName("ks-lightbox"); |
| 67 | |
|
| 68 | 0 | mainPanel.setStyleName("ks-lightbox-mainPanel"); |
| 69 | 0 | titlePanel.setStyleName("ks-lightbox-titlePanel"); |
| 70 | 0 | closeLink.setStyleName("ks-lightbox-title-closeLink"); |
| 71 | 0 | scrollPanel.setStyleName("ks-lightbox-title-scrollPanel"); |
| 72 | |
|
| 73 | 0 | setGlassEnabled(true); |
| 74 | 0 | super.setWidget(mainPanel); |
| 75 | 0 | mainPanel.add(titlePanel); |
| 76 | 0 | mainPanel.add(scrollPanel); |
| 77 | 0 | titlePanel.add(closeLink); |
| 78 | |
|
| 79 | 0 | verticalPanel.setStyleName("ks-lightbox-layoutTable"); |
| 80 | 0 | verticalPanel.setWidget(1, 0, buttonPanel); |
| 81 | 0 | verticalPanel.getRowFormatter().setStyleName(1, "ks-lightbox-buttonRow"); |
| 82 | 0 | scrollPanel.add(verticalPanel); |
| 83 | |
|
| 84 | 0 | installResizeHandler(); |
| 85 | |
|
| 86 | 0 | closeLink.addClickHandler(new ClickHandler(){ |
| 87 | |
|
| 88 | |
@Override |
| 89 | |
public void onClick(ClickEvent event) { |
| 90 | 0 | hide(); |
| 91 | 0 | } |
| 92 | |
}); |
| 93 | 0 | } |
| 94 | 0 | public KSLightBox(boolean addCloseLink) { |
| 95 | 0 | ((Widget) this.getCaption()).setVisible(false); |
| 96 | 0 | init(); |
| 97 | 0 | closeLink.setVisible(addCloseLink); |
| 98 | 0 | } |
| 99 | 0 | public KSLightBox(String title) { |
| 100 | 0 | init(); |
| 101 | 0 | super.setText(title); |
| 102 | 0 | } |
| 103 | |
public void uninstallResizeHandler(){ |
| 104 | 0 | if(resizeHandlerRegistrater != null){ |
| 105 | 0 | resizeHandlerRegistrater.removeHandler(); |
| 106 | 0 | resizeHandlerRegistrater = null; |
| 107 | |
|
| 108 | |
} |
| 109 | 0 | } |
| 110 | |
public void installResizeHandler(){ |
| 111 | 0 | if(resizeHandlerRegistrater == null){ |
| 112 | 0 | resizeHandlerRegistrater = Window.addResizeHandler(resizeHandler); |
| 113 | |
} |
| 114 | 0 | } |
| 115 | |
public void setCloseLinkVisible(boolean visible){ |
| 116 | 0 | closeLink.setVisible(visible); |
| 117 | 0 | } |
| 118 | |
public void addButton(Widget button){ |
| 119 | 0 | button.addStyleName("ks-button-spacing"); |
| 120 | 0 | buttonPanel.add(button); |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
@SuppressWarnings("unchecked") |
| 124 | |
public void addButtonGroup(ButtonGroup group){ |
| 125 | 0 | buttonPanel.add(group); |
| 126 | 0 | } |
| 127 | |
|
| 128 | |
public void setWidget(Widget content){ |
| 129 | 0 | verticalPanel.setWidget(0, 0, content); |
| 130 | 0 | verticalPanel.getRowFormatter().setStyleName(0, "ks-lightbox-contentRow"); |
| 131 | 0 | } |
| 132 | |
public void setMaxWidth(int width){ |
| 133 | 0 | this.maxWidth = width; |
| 134 | 0 | } |
| 135 | |
public void setMaxHeight(int height){ |
| 136 | 0 | this.maxHeight = height; |
| 137 | 0 | } |
| 138 | |
public void setSize(int width, int height){ |
| 139 | 0 | super.setSize(width+"px", height+"px"); |
| 140 | 0 | this.permHeight = height; |
| 141 | 0 | this.permWidth = width; |
| 142 | 0 | scrollPanel.setSize((width+10)+"px", (height+10)+"px"); |
| 143 | 0 | } |
| 144 | |
|
| 145 | |
public void showButtons(boolean show){ |
| 146 | 0 | buttonPanel.setVisible(show); |
| 147 | 0 | } |
| 148 | |
|
| 149 | |
@Override |
| 150 | |
public void hide(){ |
| 151 | 0 | super.hide(); |
| 152 | 0 | uninstallResizeHandler(); |
| 153 | 0 | } |
| 154 | |
@Override |
| 155 | |
public void show(){ |
| 156 | 0 | resizeDialog(); |
| 157 | 0 | installResizeHandler(); |
| 158 | 0 | super.show(); |
| 159 | 0 | super.center(); |
| 160 | 0 | } |
| 161 | |
@Override |
| 162 | |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
| 163 | 0 | super.onPreviewNativeEvent(preview); |
| 164 | 0 | NativeEvent evt = preview.getNativeEvent(); |
| 165 | 0 | if (evt.getType().equals("keydown")) { |
| 166 | 0 | switch (evt.getKeyCode()) { |
| 167 | |
case KeyCodes.KEY_ESCAPE: |
| 168 | 0 | hide(); |
| 169 | |
break; |
| 170 | |
} |
| 171 | |
} |
| 172 | 0 | } |
| 173 | |
public Widget getWidget() { |
| 174 | 0 | return verticalPanel.getWidget(0, 0); |
| 175 | |
} |
| 176 | |
|
| 177 | |
public void removeCloseLink(){ |
| 178 | 0 | closeLink.setVisible(false); |
| 179 | 0 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
private void resizeDialog(){ |
| 185 | |
|
| 186 | 0 | int width = maxWidth; |
| 187 | 0 | int height = maxHeight; |
| 188 | |
|
| 189 | |
|
| 190 | 0 | if(permWidth != -1){ |
| 191 | 0 | width = permWidth; |
| 192 | |
} |
| 193 | |
else{ |
| 194 | 0 | if (Window.getClientWidth() < 850){ |
| 195 | 0 | width = Window.getClientWidth() - 160; |
| 196 | |
} |
| 197 | 0 | if(width > maxWidth){ |
| 198 | 0 | width = maxWidth; |
| 199 | |
} |
| 200 | 0 | if(width < minWidth){ |
| 201 | 0 | width = minWidth; |
| 202 | |
} |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | 0 | if(permHeight != -1){ |
| 207 | 0 | height = permHeight; |
| 208 | |
} |
| 209 | |
else{ |
| 210 | 0 | height = Window.getClientHeight() - 160; |
| 211 | |
|
| 212 | 0 | if(height > maxHeight && maxHeight != 0){ |
| 213 | 0 | height = maxHeight; |
| 214 | |
} |
| 215 | 0 | if(height < minHeight){ |
| 216 | 0 | height = minHeight; |
| 217 | |
} |
| 218 | |
} |
| 219 | |
|
| 220 | 0 | if(width > 0 && height > 0){ |
| 221 | 0 | super.setSize(width + "px", height + "px"); |
| 222 | 0 | scrollPanel.setSize((width+10)+"px", (height+10)+"px"); |
| 223 | |
} |
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | 0 | } |
| 237 | |
|
| 238 | 0 | class KSDialogResizeHandler implements ResizeHandler{ |
| 239 | |
@Override |
| 240 | |
public void onResize(ResizeEvent event) { |
| 241 | 0 | DeferredCommand.addCommand(new Command(){ |
| 242 | |
|
| 243 | |
@Override |
| 244 | |
public void execute() { |
| 245 | 0 | resizeDialog(); |
| 246 | 0 | int left = (Window.getClientWidth() - getOffsetWidth()) >> 1; |
| 247 | 0 | int top = (Window.getClientHeight() - getOffsetHeight()) >> 1; |
| 248 | 0 | setPopupPosition(Math.max(Window.getScrollLeft() + left, 0), Math.max( |
| 249 | |
Window.getScrollTop() + top, 0)); |
| 250 | 0 | } |
| 251 | |
}); |
| 252 | 0 | } |
| 253 | |
} |
| 254 | |
|
| 255 | |
} |