Coverage Report - org.kuali.student.lum.common.client.widgets.GradeWidget
 
Classes in this File Line Coverage Branch Coverage Complexity
GradeWidget
0%
0/36
0%
0/14
2.083
GradeWidget$1
0%
0/6
0%
0/2
2.083
GradeWidget$2
0%
0/5
N/A
2.083
GradeWidget$3
0%
0/16
0%
0/12
2.083
 
 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  
 package org.kuali.student.lum.common.client.widgets;
 16  
 
 17  
 import java.util.List;
 18  
 
 19  
 import org.kuali.student.common.assembly.data.Data;
 20  
 import org.kuali.student.common.assembly.data.Metadata;
 21  
 import org.kuali.student.common.search.dto.SearchResultCell;
 22  
 import org.kuali.student.common.search.dto.SearchResultRow;
 23  
 import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
 24  
 import org.kuali.student.common.ui.client.mvc.Callback;
 25  
 import org.kuali.student.common.ui.client.mvc.HasDataValue;
 26  
 import org.kuali.student.common.ui.client.widgets.KSDropDown;
 27  
 import org.kuali.student.common.ui.client.widgets.impl.KSDropDownImpl;
 28  
 import org.kuali.student.common.ui.client.widgets.list.KSRadioButtonList;
 29  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 30  
 import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems;
 31  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
 32  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
 33  
 import org.kuali.student.common.ui.client.widgets.list.impl.KSRadioButtonListImpl;
 34  
 import org.kuali.student.common.ui.client.widgets.search.KSPicker;
 35  
 import org.kuali.student.core.statement.ui.client.widgets.rules.AccessWidgetValue;
 36  
 
 37  
 import com.google.gwt.user.client.ui.Composite;
 38  
 import com.google.gwt.user.client.ui.FlowPanel;
 39  
 import com.google.gwt.user.client.ui.Widget;
 40  
 
 41  0
 public class GradeWidget extends Composite implements AccessWidgetValue, HasDataValue {
 42  
 
 43  
     protected static final String GRADE_TYPE_ID_CELL_KEY = "lrc.resultColumn.resultComponent.id";
 44  
     protected static final String GRADE_TYPE_NAME_CELL_KEY = "lrc.resultColumn.resultComponent.name";
 45  0
     private FlowPanel layout = new FlowPanel();
 46  
     KSPicker gradeTypeWidget;
 47  0
     KSPicker gradeWidget = null;
 48  
     Metadata gradeMetadata;
 49  0
     private String initialGradeValue = null;
 50  
 
 51  0
     public GradeWidget() {
 52  0
         this.initWidget(layout);
 53  0
     }
 54  
 
 55  
     @Override
 56  
     public void initWidget(List<Metadata> fieldsMetadata) {
 57  0
         layout.clear();
 58  0
         gradeTypeWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(fieldsMetadata.get(0));
 59  0
         layout.add(gradeTypeWidget);
 60  0
         gradeMetadata = fieldsMetadata.get(1);
 61  0
         setupHandlers();
 62  0
     }
 63  
 
 64  
     private void setupHandlers() {
 65  
 
 66  0
         ((KSRadioButtonList)gradeTypeWidget.getInputWidget()).addSelectionChangeHandler(new SelectionChangeHandler() {
 67  
 
 68  
             @Override
 69  
             public void onSelectionChange(SelectionChangeEvent event) {
 70  0
                 String gradeTypeSelected = ((KSRadioButtonListImpl)event.getWidget()).getSelectedItem();
 71  0
                 gradeMetadata.getInitialLookup().getParams().get(0).setDefaultValueList(null);
 72  0
                 gradeMetadata.getInitialLookup().getParams().get(0).setDefaultValueString(gradeTypeSelected);
 73  
                 
 74  0
                 addGradeListWidget(true, !event.isUserInitiated());
 75  0
             }
 76  
         });
 77  0
     }
 78  
 
 79  
     private void addGradeListWidget(boolean enabled, boolean addInitialValueCallback) {
 80  0
         if (gradeWidget != null) {
 81  0
             layout.remove(gradeWidget);
 82  
         }
 83  
         
 84  
         // this callback ensures the initial grade value is selected if it should be set
 85  
         // since the values are populated async, a widget ready callback must be used
 86  0
         if(addInitialValueCallback) {
 87  0
             ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).addWidgetReadyCallback(new Callback<Widget>() {
 88  
             
 89  
                 @Override
 90  
                 public void exec(Widget result) {
 91  0
                     gradeWidget.setValue(initialGradeValue);
 92  0
                     KSDropDownImpl theWidget = (KSDropDownImpl) result;
 93  0
                     theWidget.selectItem(initialGradeValue);
 94  0
                 }
 95  
             });
 96  
         }
 97  
         
 98  0
         gradeWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(gradeMetadata);
 99  0
         ((KSDropDown)gradeWidget.getInputWidget()).setEnabled(enabled);
 100  0
         layout.add(gradeWidget);
 101  0
     }
 102  
 
 103  
         @Override
 104  
         public void addValueChangeCallback(Callback<Data.Value> callback) {
 105  0
         }
 106  
 
 107  
         @Override
 108  
         public void setValue(Data.Value value) {
 109  
             
 110  0
             if(value != null && value.toString() != null) {
 111  0
                 initialGradeValue = value.toString();
 112  
             }
 113  
             
 114  0
         addGradeListWidget((initialGradeValue != null), false);
 115  0
         gradeWidget.setValue(value);
 116  
             
 117  
                 // if setting an initial value that is not the blank default (i.e. populating the grade widget from a previously saved value)
 118  0
                 if(initialGradeValue != null) {
 119  
                     
 120  
                     // add a callback to select the grade type after the grade dropdown has been initialized
 121  0
                 final String selectedGradeValue = value.toString();
 122  
                 
 123  0
                     Callback<Widget> afterInitCallback = new Callback<Widget>(){
 124  
                 @Override
 125  
                 public void exec(Widget widget) {
 126  
     
 127  0
                     List<SearchResultRow> gradeItems = ((SearchResultListItems) ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).getListItems()).getReadOnlyResults();
 128  
                     
 129  
                     // find the selected row for the grade
 130  0
                     SearchResultRow selectedGradeRow = null;
 131  0
                     String selectedGradeTypeId = null;
 132  0
                     for(SearchResultRow row : gradeItems) {
 133  0
                         for(SearchResultCell cell : row.getCells()) {
 134  0
                             if(cell.getValue().equals(selectedGradeValue)) {
 135  0
                                 selectedGradeRow = row;
 136  
                             }
 137  
                             
 138  
                             // get the grade type id from any row  
 139  
                             // If the right row is found, this value will contain the grade type we are looking for
 140  0
                             if(cell.getKey().equals(GRADE_TYPE_ID_CELL_KEY)) {
 141  0
                                 selectedGradeTypeId = cell.getValue();
 142  
                             }
 143  
                             
 144  
                         }
 145  
                         
 146  
                         // once the grade row is found, break
 147  0
                         if(selectedGradeRow != null) {
 148  0
                             break;
 149  
                         }
 150  
                     }
 151  
                     
 152  
                     // make sure to check for null, though it would indicate bad data if true
 153  0
                     if(selectedGradeRow == null) {
 154  0
                         return;
 155  
                     }
 156  
                     
 157  
                     // now we can select the grade type in the grade type widget
 158  0
                     ((KSSelectItemWidgetAbstract)gradeTypeWidget.getInputWidget()).selectItem(selectedGradeTypeId);
 159  
                     
 160  0
                 }
 161  
             };
 162  
                     
 163  0
                     ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).addWidgetReadyCallback(afterInitCallback);
 164  
                 }
 165  0
         }
 166  
 
 167  
     @Override
 168  
     public void getValue(Callback<String> doneSaveCallback) { 
 169  0
     }
 170  
 
 171  
     @Override
 172  
     public void setValue(String id) {
 173  0
     }
 174  
 
 175  
     @Override
 176  
     public Data.Value getValue() {
 177  0
         return (gradeWidget != null ? gradeWidget.getValue() : null);
 178  
     }
 179  
 }