Coverage Report - org.kuali.student.common.ui.client.widgets.list.impl.KSLabelListImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KSLabelListImpl
0%
0/72
0%
0/24
1.722
KSLabelListImpl$1
0%
0/2
N/A
1.722
KSLabelListImpl$2
0%
0/3
N/A
1.722
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.widgets.list.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.dto.Idable;
 22  
 import org.kuali.student.common.ui.client.mvc.Callback;
 23  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 24  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 25  
 import org.kuali.student.common.ui.client.widgets.list.ListItems;
 26  
 import org.kuali.student.common.ui.client.widgets.list.ModelListItems;
 27  
 
 28  
 import com.google.gwt.event.dom.client.BlurHandler;
 29  
 import com.google.gwt.event.dom.client.FocusHandler;
 30  
 import com.google.gwt.event.shared.HandlerRegistration;
 31  
 import com.google.gwt.user.client.ui.FlexTable;
 32  
 import com.google.gwt.user.client.ui.Widget;
 33  
 
 34  
 
 35  
 /**
 36  
  * This class is a temporary hack to display a list of strings.
 37  
  *
 38  
  * @author Kuali Student Team
 39  
  *
 40  
  */
 41  
 public class KSLabelListImpl extends KSSelectItemWidgetAbstract {
 42  
 
 43  0
     private static final HandlerRegistration NO_OP_REGISTRATION = new HandlerRegistration() {
 44  
                 @Override
 45  
                 public void removeHandler() {
 46  
                         // do nothing
 47  0
                 }
 48  
     };
 49  
 
 50  
 
 51  0
         private FlexTable labels = new FlexTable();
 52  0
     private List<String> selectedItems = new ArrayList<String>();
 53  
 
 54  0
     private int maxCols = 1; //default max columns
 55  0
     private boolean enabled = true;
 56  
 
 57  0
     public KSLabelListImpl() {
 58  0
         initWidget(labels);
 59  0
     }
 60  
 
 61  
     /**
 62  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#deSelectItem(java.lang.String)
 63  
      */
 64  
     public void deSelectItem(String id) {
 65  0
        this.selectedItems.remove(id);
 66  0
     }
 67  
 
 68  
     /**
 69  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#getSelectedItems()
 70  
      */
 71  
     public List<String> getSelectedItems() {
 72  0
         return selectedItems;
 73  
     }
 74  
     
 75  
     /**
 76  
      * This is done this way, as Enumerated types are sometimes in the droplist and normal getSelectedItems return "kuali.atp.fall" and not just "Fall"
 77  
      * @return
 78  
      */
 79  
     public List<String> getSelectedItemsForExport() {
 80  0
             List<String> selectedList = new ArrayList<String>();
 81  0
             int rowCnt = this.labels.getRowCount();
 82  0
             for (int i = 0; i < rowCnt; i++) {
 83  0
                         Widget theWidget = this.labels.getWidget(i, 0);
 84  0
                         if (theWidget instanceof KSLabel) {
 85  0
                                 KSLabel ksLbl = (KSLabel) theWidget;
 86  0
                                 String label = ksLbl.getText();
 87  0
                                 selectedList.add(label);
 88  
                         }
 89  
                 }
 90  0
         return selectedList;
 91  
     }
 92  
 
 93  
 
 94  
 
 95  
     /**
 96  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#selectItem(java.lang.String)
 97  
      */
 98  
     public void selectItem(String id) {
 99  0
         if (!selectedItems.contains(id)){
 100  0
              this.selectedItems.add(id);
 101  
         }
 102  
 
 103  
         //FIXME: Is there a better way to display selected item here without redrawing
 104  0
         redraw();
 105  0
     }
 106  
 
 107  
     public void redraw(){
 108  0
         labels.clear();
 109  0
         int currCount = 0;
 110  0
         int row = 0;
 111  0
         int col = 0;
 112  
 
 113  0
         int itemCount = 0;
 114  0
         if (super.getListItems() != null){
 115  0
                 itemCount = super.getListItems().getItemCount();
 116  
         } else {
 117  0
                 itemCount = selectedItems.size();
 118  
         }
 119  
 
 120  0
         if (maxCols <= 2){
 121  
             //Row flow - increment row faster than column
 122  0
             int maxRows = (itemCount / maxCols) + (itemCount % 2);
 123  0
             for (String id:selectedItems){
 124  0
                 currCount++;
 125  0
                 row = (currCount % maxRows);
 126  0
                 row = ((row == 0) ? maxRows:row) - 1;
 127  
 
 128  0
                 labels.setWidget(row, col, createLabel(id));
 129  
 
 130  0
                 col += ((row + 1)/ maxRows) * 1;
 131  
             }
 132  0
         } else {
 133  
             //Column flow - increment column faster than row
 134  0
             for (String id:selectedItems){
 135  0
                 currCount++;
 136  0
                 col = currCount % maxCols;
 137  0
                 col = ((col == 0) ? maxCols:col) - 1;
 138  
 
 139  0
                 labels.setWidget(row, col, createLabel(id));
 140  
 
 141  0
                 row += ((col + 1 )/ maxCols) * 1;
 142  
             }
 143  
         }
 144  
 
 145  0
         super.setInitialized(true);
 146  0
     }
 147  
 
 148  
     @Override
 149  
     public <T extends Idable> void setListItems(ListItems listItems) {
 150  0
         if(listItems instanceof ModelListItems){
 151  0
             Callback<T> redrawCallback = new Callback<T>(){
 152  
 
 153  
                 @Override
 154  
                 public void exec(T result){
 155  0
                     KSLabelListImpl.this.redraw();
 156  0
                 }
 157  
             };
 158  0
             ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback);
 159  0
             ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback);
 160  0
             ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback);
 161  0
             ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback);
 162  
         }
 163  
 
 164  0
         super.setListItems(listItems);
 165  
 
 166  0
         redraw();
 167  0
     }
 168  
 
 169  
     private KSLabel createLabel(String id){
 170  0
             if (super.getListItems() == null || super.getListItems().getItemCount() == 0){
 171  0
                     return new KSLabel(id);
 172  
             } else {
 173  0
                     return new KSLabel(super.getListItems().getItemText(id));
 174  
             }
 175  
     }
 176  
 
 177  0
     public void onLoad() {}
 178  
 
 179  
     @Override
 180  
     public void setColumnSize(int col) {
 181  0
         maxCols = col;
 182  0
     }
 183  
 
 184  
     @Override
 185  
     public void setEnabled(boolean b) {
 186  
 //        enabled = b;
 187  
 //        for (int i=0; i < labels.getRowCount(); i++){
 188  
 //            for (int j=0; j < labels.getCellCount(i); j++){
 189  
 //                ((KSLabel)labels.getWidget(i, j)).setEnabled(b);
 190  
 //            }
 191  
 //        }
 192  0
     }
 193  
 
 194  
     @Override
 195  
     public boolean isEnabled() {
 196  0
         return enabled;
 197  
     }
 198  
 
 199  
     @Override
 200  
     public boolean isMultipleSelect(){
 201  0
         return true;
 202  
     }
 203  
     @Override
 204  
     public void clear(){
 205  0
         selectedItems.clear();
 206  0
         redraw();
 207  0
     }
 208  
 
 209  
         @Override
 210  
         public HandlerRegistration addFocusHandler(FocusHandler handler) {
 211  0
                 return NO_OP_REGISTRATION;
 212  
         }
 213  
 
 214  
         @Override
 215  
         public HandlerRegistration addBlurHandler(BlurHandler handler) {
 216  0
                 return NO_OP_REGISTRATION;
 217  
         }
 218  
 
 219  
 }