Coverage Report - org.kuali.student.common.ui.client.widgets.list.impl.KSRadioButtonListImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KSRadioButtonListImpl
0%
0/124
0%
0/48
2.143
KSRadioButtonListImpl$1
0%
0/3
N/A
2.143
 
 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.KSRadioButton;
 25  
 import org.kuali.student.common.ui.client.widgets.focus.FocusGroup;
 26  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 27  
 import org.kuali.student.common.ui.client.widgets.list.ListItems;
 28  
 import org.kuali.student.common.ui.client.widgets.list.ModelListItems;
 29  
 import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems;
 30  
 
 31  
 import com.google.gwt.event.dom.client.BlurHandler;
 32  
 import com.google.gwt.event.dom.client.FocusHandler;
 33  
 import com.google.gwt.event.dom.client.HasBlurHandlers;
 34  
 import com.google.gwt.event.dom.client.HasFocusHandlers;
 35  
 import com.google.gwt.event.dom.client.KeyUpEvent;
 36  
 import com.google.gwt.event.dom.client.KeyUpHandler;
 37  
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 38  
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 39  
 import com.google.gwt.event.shared.HandlerRegistration;
 40  
 import com.google.gwt.user.client.ui.FlexTable;
 41  
 
 42  
 
 43  
 /**
 44  
  *  This class will generate a checkbox list from a ListItems instance.
 45  
  *  For a ListItems instance with just one attribute it will be a list of checkboxes which
 46  
  *  may be horizontal or vertical as controlled by the maxCols attribute 
 47  
  *  If maxCols <=2 checkboxes will be vertical, otherwise horizontal.
 48  
  * 
 49  
  *  For ListItems with > 1 attribute a table with a header row will be generated 
 50  
  *  with each column being a ListItems attribute. This can be turned off using setIgnoreMultipleAttributes
 51  
  * 
 52  
  * @author Kuali Student Team 
 53  
  *
 54  
  */
 55  
 public class KSRadioButtonListImpl extends KSSelectItemWidgetAbstract implements KeyUpHandler, ValueChangeHandler<Boolean>, HasBlurHandlers, HasFocusHandlers {
 56  0
     private final FocusGroup focus = new FocusGroup(this);
 57  0
     private FlexTable layout = new FlexTable();
 58  0
     private List<String> selectedItems = new ArrayList<String>();
 59  0
     private static int groupNumber = 0;
 60  
     private String groupName;
 61  
 
 62  0
     private int maxCols = 1; //default max columns
 63  0
     private boolean enabled = true;
 64  0
     private boolean ignoreMultipleAttributes = false;
 65  
 
 66  0
     public KSRadioButtonListImpl() {
 67  0
             groupName = "radio" + groupNumber;
 68  0
             groupNumber++;
 69  0
         initWidget(layout);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#deSelectItem(java.lang.String)
 74  
      */
 75  
     public void deSelectItem(String id) {
 76  0
         for (int i=0; i < layout.getRowCount(); i++){
 77  0
             for (int j=0; j < layout.getCellCount(i); j++){
 78  0
                 KSRadioButton radiobutton = (KSRadioButton)layout.getWidget(i, j);
 79  0
                 if (radiobutton.getFormValue().equals(id)){
 80  0
                     this.selectedItems.remove(id);
 81  0
                     radiobutton.setValue(false);
 82  0
                     fireChangeEvent(false);
 83  0
                     break;
 84  
                 }
 85  
             }
 86  
         }                
 87  0
     }
 88  
 
 89  
     /**
 90  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#getSelectedItems()
 91  
      */
 92  
     public List<String> getSelectedItems() {
 93  0
         return selectedItems;
 94  
     }
 95  
 
 96  
 
 97  
 
 98  
     /**
 99  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#selectItem(java.lang.String)
 100  
      */
 101  
     public void selectItem(String id) {
 102  0
         if (!selectedItems.contains(id)){
 103  0
             for (int i=0; i < layout.getRowCount(); i++){
 104  0
                 for (int j=0; j < layout.getCellCount(i); j++){
 105  0
                         if(layout.getWidget(i, j) instanceof KSRadioButton){
 106  0
                                 KSRadioButton radiobutton = (KSRadioButton)layout.getWidget(i, j);
 107  0
                             if (radiobutton.getFormValue().equals(id)){
 108  0
                                     selectedItems.clear();
 109  0
                                 this.selectedItems.add(id);
 110  0
                                 radiobutton.setValue(true);
 111  0
                                 fireChangeEvent(false);
 112  0
                                 break;
 113  
                             }
 114  
                         }
 115  
                 }
 116  
             }
 117  
         }
 118  0
     }
 119  
 
 120  
     public void redraw(){
 121  0
         layout.clear();
 122  0
         int itemCount = super.getListItems().getItemCount();
 123  0
         int currCount = 0;
 124  0
         int row = 0;
 125  0
         int col = 0;
 126  
 
 127  
         // If ListItems has more than one attribute create a table with each attribute in its own column
 128  0
         if (!ignoreMultipleAttributes && super.getListItems().getAttrKeys() != null && super.getListItems().getAttrKeys().size() > 1) {
 129  0
             layout.addStyleName("KS-Checkbox-Table");
 130  
 //            layout.setWidget(row, col++, new KSLabel("Select"));
 131  
 //            for (String attr:super.getListItems().getAttrKeys()){
 132  
 //                layout.setWidget(row, col++, new KSLabel(attr));
 133  
 //            }
 134  
 //            row++;
 135  
 //            col=0;
 136  
 
 137  0
             for (String id:super.getListItems().getItemIds()){
 138  
 
 139  0
                layout.setWidget(row, col, createCheckbox(id));
 140  0
                SearchResultListItems searchList = (SearchResultListItems)super.getListItems();
 141  0
                String value = searchList.getItemAttribute(id, searchList.getAttrKeys().get(searchList.getItemTextAttrNdx()));
 142  0
                layout.setWidget(row,++col, new KSLabel(value));
 143  
 //                for (String attr:super.getListItems().getAttrKeys()){
 144  
 ////                    String value = super.getListItems().getItemAttribute(id, attr);
 145  
 ////                    layout.setWidget(row, ++col, new KSLabel(value));
 146  
 //                }                    
 147  
 
 148  0
                 row++;
 149  0
                 col = 0;
 150  0
             }
 151  
 
 152  
         }
 153  0
         else if (maxCols <= 2){
 154  
             //Row flow - increment row faster than column
 155  0
             int maxRows = (itemCount / maxCols) + (itemCount % 2);
 156  0
             for (String id:super.getListItems().getItemIds()){
 157  0
                 currCount++;
 158  0
                 row = (currCount % maxRows);
 159  0
                 row = ((row == 0) ? maxRows:row) - 1;
 160  
 
 161  0
                 layout.setWidget(row, col, createCheckboxWithLabel(id));                    
 162  
 
 163  0
                 col += ((row + 1)/ maxRows) * 1;
 164  
 
 165  
             }
 166  0
         } else {
 167  
             //Column flow - increment column faster than row
 168  0
             for (String id:super.getListItems().getItemIds()){
 169  0
                 currCount++;
 170  0
                 col = currCount % maxCols;
 171  0
                 col = ((col == 0) ? maxCols:col) - 1;
 172  
 
 173  0
                 layout.setWidget(row, col, createCheckboxWithLabel(id));
 174  
 
 175  0
                 row += ((col + 1 )/ maxCols) * 1;
 176  
             }
 177  
         }
 178  
         
 179  0
         setInitialized(true);
 180  0
     }
 181  
 
 182  
     @Override
 183  
     public <T extends Idable> void setListItems(ListItems listItems) {
 184  0
         if(listItems instanceof ModelListItems){
 185  0
             Callback<T> redrawCallback = new Callback<T>(){
 186  
 
 187  
                 @Override 
 188  
                 public void exec(T result){
 189  0
                     KSRadioButtonListImpl.this.redraw();
 190  0
                 }
 191  
             };
 192  0
             ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback);
 193  0
             ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback);
 194  0
             ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback);
 195  0
             ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback);
 196  
         }
 197  
 
 198  0
         super.setListItems(listItems);
 199  
 
 200  0
         redraw();
 201  0
     }
 202  
 
 203  
     private KSRadioButton createCheckbox(String id){
 204  0
         KSRadioButton radiobutton = new KSRadioButton(groupName);
 205  0
         radiobutton.setFormValue(id);
 206  0
         radiobutton.addValueChangeHandler(this);
 207  0
         radiobutton.addKeyUpHandler(this);
 208  0
         focus.addWidget(radiobutton);
 209  0
         return radiobutton;
 210  
     }
 211  
 
 212  
     private KSRadioButton createCheckboxWithLabel(String id){
 213  0
         KSRadioButton radiobutton = new KSRadioButton(groupName, getListItems().getItemText(id));
 214  0
         radiobutton.setFormValue(id);
 215  0
         radiobutton.addValueChangeHandler(this);
 216  0
         radiobutton.addKeyUpHandler(this);
 217  0
         focus.addWidget(radiobutton);
 218  0
         return radiobutton;
 219  
     }
 220  
 
 221  0
     public void onLoad() {}
 222  
 
 223  
     /**
 224  
      * @see org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract#setMaxColumns(int)
 225  
      */
 226  
     @Override
 227  
     public void setColumnSize(int col) {
 228  0
         maxCols = col;
 229  0
     }
 230  
 
 231  
     @Override
 232  
     public void setEnabled(boolean b) {
 233  0
         enabled = b;
 234  0
         for (int i=0; i < layout.getRowCount(); i++){
 235  0
             for (int j=0; j < layout.getCellCount(i); j++){
 236  0
                 ((KSRadioButton)layout.getWidget(i, j)).setEnabled(b);
 237  
             }
 238  
         }
 239  0
     }
 240  
 
 241  
     @Override
 242  
     public boolean isEnabled() {
 243  0
         return enabled;
 244  
     }
 245  
 
 246  
     @Override
 247  
     public boolean isMultipleSelect(){
 248  0
         return false;
 249  
     }
 250  
     
 251  
     public void setIgnoreMultipleAttributes(boolean ignoreMultiple){
 252  0
             this.ignoreMultipleAttributes = ignoreMultiple;
 253  0
     }
 254  
 
 255  
 
 256  
     @Override
 257  
     public void clear(){
 258  0
         selectedItems.clear();
 259  0
         fireChangeEvent(false);
 260  0
         redraw();
 261  0
     }
 262  
 
 263  
     @Override
 264  
     public HandlerRegistration addBlurHandler(BlurHandler handler) {
 265  0
         return focus.addBlurHandler(handler);
 266  
     }
 267  
 
 268  
     @Override
 269  
     public HandlerRegistration addFocusHandler(FocusHandler handler) {
 270  0
         return focus.addFocusHandler(handler);
 271  
     }
 272  
 
 273  
         @Override
 274  
         public void onValueChange(ValueChangeEvent<Boolean> event) {
 275  0
         KSRadioButton radiobutton = (KSRadioButton)(event.getSource());   
 276  0
         String value = radiobutton.getFormValue();
 277  0
         if (event.getValue()){
 278  0
             if (!selectedItems.contains(value)){
 279  0
                     selectedItems.clear();
 280  0
                 selectedItems.add(value);
 281  0
                 fireChangeEvent(true);
 282  
             }
 283  
         }
 284  0
         }
 285  
         
 286  
         @Override
 287  
         public void setName(String name) {
 288  0
                 super.setName(name);
 289  0
                 groupName = name;
 290  0
         }
 291  
 
 292  
         @Override
 293  
         public void onKeyUp(KeyUpEvent event) {
 294  0
                 KSRadioButton radiobutton = (KSRadioButton)(event.getSource());   
 295  0
         String value = radiobutton.getFormValue();
 296  0
         if (radiobutton.getValue()){
 297  0
             if (!selectedItems.contains(value)){
 298  0
                     selectedItems.clear();
 299  0
                 selectedItems.add(value);
 300  0
                 fireChangeEvent(true);
 301  
             }
 302  
         }
 303  0
         }
 304  
 }