Coverage Report - org.kuali.student.common.ui.client.widgets.KSLightBox
 
Classes in this File Line Coverage Branch Coverage Complexity
KSLightBox
0%
0/132
0%
0/38
1.655
KSLightBox$1
0%
0/3
N/A
1.655
KSLightBox$KSDialogResizeHandler
0%
0/3
N/A
1.655
KSLightBox$KSDialogResizeHandler$1
0%
0/6
N/A
1.655
KSLightBox$Size
0%
0/4
N/A
1.655
KSLightBox$Size$1
0%
0/2
N/A
1.655
KSLightBox$Size$2
0%
0/2
N/A
1.655
KSLightBox$Size$3
0%
0/2
N/A
1.655
 
 1  
 
 2  
 /**
 3  
  * Copyright 2010 The Kuali Foundation Licensed under the
 4  
  * Educational Community License, Version 2.0 (the "License"); you may
 5  
  * not use this file except in compliance with the License. You may
 6  
  * obtain a copy of the License at
 7  
  *
 8  
  * http://www.osedu.org/licenses/ECL-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing,
 11  
  * software distributed under the License is distributed on an "AS IS"
 12  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 13  
  * or implied. See the License for the specific language governing
 14  
  * permissions and limitations under the License.
 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 /*implements HasCloseHandlers<KSLightBox> */{
 52  
 //  private final HandlerManager handlers = new HandlerManager(this);
 53  
 
 54  0
     private static final List<String> FOCUSABLE_TAGS = Arrays.asList("INPUT", "SELECT", "BUTTON", "TEXTAREA");
 55  
 
 56  0
     public enum Size{
 57  0
             SMALL{
 58  
                     public String toString(){
 59  0
                             return "ks-lightbox-small";
 60  
                     }
 61  
             },
 62  0
             MEDIUM{
 63  
                     public String toString(){
 64  0
                             return "ks-lightbox-medium";
 65  
                     }
 66  
             },
 67  0
             LARGE{
 68  
                     public String toString(){
 69  0
                             return "ks-lightbox-large";
 70  
                     }
 71  
             }
 72  
     }
 73  0
     private int maxWidth = 800;
 74  0
     private int maxHeight = 0;
 75  0
     private int minWidth = 400;
 76  0
     private int minHeight = 200;
 77  0
     private int permWidth = -1;
 78  0
     private int permHeight = -1;
 79  0
     private FlowPanel mainPanel = new FlowPanel();
 80  0
     private FlowPanel titlePanel = new FlowPanel();
 81  0
     private ScrollPanel scrollPanel = new ScrollPanel();
 82  
 
 83  0
     private Anchor closeLink = new Anchor();
 84  0
     private KSDialogResizeHandler resizeHandler = new KSDialogResizeHandler();
 85  
     private HandlerRegistration resizeHandlerRegistrater;
 86  
 
 87  0
     private FlexTable verticalPanel = new FlexTable();
 88  0
     private HorizontalPanel buttonPanel = new HorizontalPanel();
 89  0
     public KSLightBox() {
 90  0
             ((Widget) this.getCaption()).setVisible(false);
 91  0
         init();
 92  0
     }
 93  
     private void init(){
 94  
             /*
 95  
              * If statements change the size of the layout table to accommodate
 96  
              * for different size ks-lightboxes.
 97  
              */
 98  0
             if(super.getStyleName().equals("ks-lightbox-small"))
 99  0
                     verticalPanel.setStyleName("ks-lightbox-small-layoutTable");
 100  
             
 101  0
             else if(super.getStyleName().equals("ks-lightbox-medium"))
 102  0
                     verticalPanel.setStyleName("ks-lightbox-medium-layoutTable");
 103  
             
 104  0
             else if(super.getStyleName().equals("ks-lightbox-large"))
 105  0
                     verticalPanel.setStyleName("ks-lightbox-large-layoutTable");
 106  
             
 107  
             else{
 108  0
                     verticalPanel.setStyleName("ks-lightbox-layoutTable");
 109  0
                     super.setStyleName("ks-lightbox");
 110  
             }
 111  
                     
 112  
             
 113  0
         mainPanel.setStyleName("ks-lightbox-mainPanel");
 114  0
         titlePanel.setStyleName("ks-lightbox-titlePanel");
 115  0
         closeLink.setStyleName("ks-lightbox-title-closeLink");
 116  0
         scrollPanel.setStyleName("ks-lightbox-title-scrollPanel");
 117  
 
 118  0
         setGlassEnabled(true);
 119  0
         super.setWidget(mainPanel);
 120  0
         mainPanel.add(titlePanel);
 121  0
         mainPanel.add(scrollPanel);
 122  0
         titlePanel.add(closeLink);
 123  
 
 124  0
         verticalPanel.setWidget(1, 0, buttonPanel);
 125  0
         verticalPanel.getRowFormatter().setStyleName(1, "ks-lightbox-buttonRow");
 126  0
         scrollPanel.add(verticalPanel);
 127  
 
 128  0
         installResizeHandler();
 129  
         //super.
 130  0
         closeLink.addClickHandler(new ClickHandler(){
 131  
 
 132  
             @Override
 133  
             public void onClick(ClickEvent event) {
 134  0
                hide();
 135  0
             }
 136  
         });
 137  
 
 138  
 
 139  0
     }
 140  0
     public KSLightBox(boolean addCloseLink) {
 141  0
             ((Widget) this.getCaption()).setVisible(false);
 142  0
         init();
 143  0
         closeLink.setVisible(addCloseLink);
 144  0
     }
 145  
     /*
 146  
      * Default constructor
 147  
      */
 148  0
     public KSLightBox(String title) {
 149  0
         init();
 150  0
         super.setText(title);
 151  0
     }
 152  
     
 153  
     /*Overloaded constructor makes it possible to select one of three sizes
 154  
      * (small,medium,large) and set the size of the KS lightbox accordingly
 155  
      * NOTE: only medium has been implemented in CSS*/
 156  
     public KSLightBox(String title,Size size)
 157  0
     {
 158  0
             super.setStyleName(size.toString());
 159  0
             init();
 160  0
             super.setText(title);
 161  0
     }
 162  
     public void uninstallResizeHandler(){
 163  0
         if(resizeHandlerRegistrater != null){
 164  0
             resizeHandlerRegistrater.removeHandler();
 165  0
             resizeHandlerRegistrater = null;
 166  
 
 167  
         }
 168  0
     }
 169  
     public void installResizeHandler(){
 170  0
         if(resizeHandlerRegistrater == null){
 171  0
             resizeHandlerRegistrater =  Window.addResizeHandler(resizeHandler);
 172  
         }
 173  0
     }
 174  
     public void setCloseLinkVisible(boolean visible){
 175  0
         closeLink.setVisible(visible);
 176  0
     }
 177  
     public void addButton(Widget button){
 178  0
             button.addStyleName("ks-button-spacing");
 179  0
         buttonPanel.add(button);
 180  0
     }
 181  
 
 182  
     @SuppressWarnings("unchecked")
 183  
         public void addButtonGroup(ButtonGroup group){
 184  0
             buttonPanel.add(group);
 185  0
     }
 186  
 
 187  
     public void setWidget(Widget content){
 188  0
         verticalPanel.setWidget(0, 0, content);
 189  0
         verticalPanel.getRowFormatter().setStyleName(0, "ks-lightbox-contentRow");
 190  0
     }
 191  
     public void setMaxWidth(int width){
 192  0
         this.maxWidth = width;
 193  0
     }
 194  
     public void setMaxHeight(int height){
 195  0
         this.maxHeight = height;
 196  0
     }
 197  
     public void setSize(int width, int height){
 198  0
         super.setSize(width+"px", height+"px");
 199  0
         this.permHeight = height;
 200  0
         this.permWidth = width;
 201  0
         scrollPanel.setSize((width+10)+"px", (height+10)+"px");
 202  0
     }
 203  
 
 204  
     public void showButtons(boolean show){
 205  0
             buttonPanel.setVisible(show);
 206  0
     }
 207  
 
 208  
     @Override
 209  
     public void hide(){
 210  0
         super.hide();
 211  0
         uninstallResizeHandler();
 212  0
     }
 213  
     @Override
 214  
     public void show(){
 215  0
         resizeDialog();
 216  0
         installResizeHandler();
 217  0
         super.show();
 218  0
         super.center();
 219  0
         grabFocus();
 220  0
     }
 221  
 
 222  
     private void grabFocus() {
 223  0
         Widget mainContent = verticalPanel.getWidget(0, 0);
 224  0
         NodeList<Element> nodeList = mainContent.getElement().getElementsByTagName("*");
 225  0
         for (int i = 0; i < nodeList.getLength(); i++) {
 226  0
             Element e = nodeList.getItem(i);
 227  0
             if (FOCUSABLE_TAGS.contains(e.getTagName().toUpperCase())) {
 228  0
                 e.focus();
 229  0
                 return;
 230  
             }
 231  
         }
 232  
 
 233  
 
 234  
 /*
 235  
         Widget mainContent = verticalPanel.getWidget(0, 0);
 236  
         if(mainContent instanceof HasWidgets){
 237  
             HasWidgets hasWidget = (HasWidgets) mainContent;
 238  
             Iterator<Widget> iter =  hasWidget.iterator();
 239  
             
 240  
             for(;iter.hasNext();){
 241  
                 Widget w = iter.next();
 242  
               //  if(w instanceof Composite ){
 243  
                 //    Composite c = (Composite)w;
 244  
                   //  c.
 245  
                // }
 246  
                 if(w instanceof FocusWidget){
 247  
                     ((FocusWidget)w).setFocus(true);
 248  
                     return;
 249  
                 }else if(w instanceof HasWidgets){
 250  
                     grabFocus((HasWidgets) w);
 251  
                 }
 252  
             }
 253  
         }
 254  
 */
 255  0
     }
 256  
 /*    private void grabFocus(HasWidgets hasWidgets){
 257  
         Iterator<Widget> iter =  hasWidgets.iterator();
 258  
         for(;iter.hasNext();){
 259  
             Widget w = iter.next();
 260  
             if(w instanceof FocusWidget){
 261  
                 ((FocusWidget)w).setFocus(true);
 262  
                 return;
 263  
             }else if(w instanceof HasWidgets){
 264  
                 grabFocus((HasWidgets) w);
 265  
             }
 266  
         }
 267  
     }
 268  
 */
 269  
     @Override
 270  
     protected void onPreviewNativeEvent(NativePreviewEvent preview) {
 271  0
         super.onPreviewNativeEvent(preview);
 272  0
         NativeEvent evt = preview.getNativeEvent();
 273  0
         if (evt.getType().equals("keydown")) {
 274  0
             switch (evt.getKeyCode()) {
 275  
             case KeyCodes.KEY_ESCAPE:
 276  0
                 hide();
 277  
                 break;
 278  
             }
 279  
         }
 280  0
     }
 281  
     public Widget getWidget() {
 282  0
             return verticalPanel.getWidget(0, 0);
 283  
     }
 284  
 
 285  
     public void removeCloseLink(){
 286  0
         closeLink.setVisible(false);
 287  0
     }
 288  
     
 289  
     public HandlerRegistration addCloseLinkClickHandler(ClickHandler clickHandler) {
 290  0
         return closeLink.addClickHandler(clickHandler);
 291  
     }
 292  
 //    public HandlerRegistration addCloseHandler(CloseHandler<KSLightBox> handler){
 293  
   //      return handlers.addHandler(CloseEvent.getType(), handler);
 294  
    // }
 295  
 
 296  
     private void resizeDialog(){
 297  
 
 298  0
             int width = maxWidth;
 299  0
         int height = maxHeight;
 300  
 
 301  
         //Width calculation
 302  0
         if(permWidth != -1){
 303  0
                     width = permWidth;
 304  
             }
 305  
         else{
 306  0
                 if (Window.getClientWidth() < 850){
 307  0
                 width = Window.getClientWidth() - 160;
 308  
             }
 309  0
                 if(width > maxWidth){
 310  0
                 width = maxWidth;
 311  
             }
 312  0
                 if(width < minWidth){
 313  0
                         width = minWidth;
 314  
                 }
 315  
         }
 316  
 
 317  
         //Height calculation
 318  0
         if(permHeight != -1){
 319  0
                     height = permHeight;
 320  
             }
 321  
         else{
 322  0
                 height = Window.getClientHeight() - 160;
 323  
 
 324  0
                 if(height > maxHeight && maxHeight != 0){
 325  0
                 height = maxHeight;
 326  
             }
 327  0
                 if(height < minHeight){
 328  0
                         height = minHeight;
 329  
                 }
 330  
         }
 331  
 
 332  0
         if(width > 0 && height > 0){
 333  0
             super.setSize(width + "px", height + "px");
 334  0
             scrollPanel.setSize((width+10)+"px", (height+10)+"px");
 335  
         }
 336  
 
 337  
 /*        DeferredCommand.addCommand(new Command(){
 338  
 
 339  
             @Override
 340  
             public void execute() {
 341  
                     //center();
 342  
                 int left = (Window.getClientWidth() - getOffsetWidth()) >> 1;
 343  
                 int top = (Window.getClientHeight() - getOffsetHeight()) >> 1;
 344  
                 setPopupPosition(Math.max(Window.getScrollLeft() + left, 0), Math.max(
 345  
                     Window.getScrollTop() + top, 0));
 346  
             }
 347  
         });  */
 348  0
     }
 349  
 
 350  0
     class KSDialogResizeHandler implements ResizeHandler{
 351  
         @Override
 352  
         public void onResize(ResizeEvent event) {
 353  0
             DeferredCommand.addCommand(new Command(){
 354  
 
 355  
                 @Override
 356  
                 public void execute() {
 357  0
                     resizeDialog();
 358  0
                     int left = (Window.getClientWidth() - getOffsetWidth()) >> 1;
 359  0
                     int top = (Window.getClientHeight() - getOffsetHeight()) >> 1;
 360  0
                     setPopupPosition(Math.max(Window.getScrollLeft() + left, 0), Math.max(
 361  
                         Window.getScrollTop() + top, 0));
 362  0
                 }
 363  
             });
 364  0
         }
 365  
     }
 366  
 
 367  
 }