Coverage Report - org.kuali.student.lum.common.client.widgets.CourseWidget
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseWidget
0%
0/61
0%
0/16
1.867
CourseWidget$1
0%
0/8
0%
0/4
1.867
CourseWidget$2
0%
0/11
0%
0/2
1.867
 
 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.ui.client.configurable.mvc.DefaultWidgetFactory;
 20  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.VerticalSection;
 21  
 import org.kuali.student.common.ui.client.mvc.Callback;
 22  
 import org.kuali.student.common.ui.client.mvc.HasDataValue;
 23  
 import org.kuali.student.common.ui.client.widgets.KSDropDown;
 24  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 25  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo;
 26  
 import org.kuali.student.common.ui.client.widgets.impl.KSDropDownImpl;
 27  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
 28  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
 29  
 import org.kuali.student.common.ui.client.widgets.list.impl.SimpleListItems;
 30  
 import org.kuali.student.common.ui.client.widgets.progress.BlockingTask;
 31  
 import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator;
 32  
 import org.kuali.student.common.ui.client.widgets.rules.AccessWidgetValue;
 33  
 import org.kuali.student.common.ui.client.widgets.search.KSPicker;
 34  
 import org.kuali.student.core.assembly.data.Data;
 35  
 import org.kuali.student.core.assembly.data.Metadata;
 36  
 
 37  
 import com.google.gwt.user.client.ui.Composite;
 38  
 
 39  0
 public class CourseWidget extends Composite implements AccessWidgetValue, HasDataValue {
 40  
 
 41  0
     private CluSetRetriever courseMetadataRetriever  = new CluSetRetrieverImpl();
 42  
     private Callback getCluNameCallback;
 43  
 
 44  
     //widgets
 45  0
     private VerticalSection layout = new VerticalSection();
 46  
     private KSDropDown courseTypeWidget;
 47  0
     private KSPicker courseWidget = null;
 48  
     private KSLabel previousCourseCode;
 49  
     private String previousCourseId;
 50  
 
 51  
     //data
 52  0
     private Metadata searchCourseMetadata = null;
 53  0
     private BlockingTask initializeTask = new BlockingTask("Initializing");
 54  
 
 55  0
     public CourseWidget() {
 56  0
         this.initWidget(layout);
 57  0
     }
 58  
 
 59  
     @Override
 60  
     public void initWidget(List<Metadata> fieldsMetadata) {
 61  
 
 62  0
         previousCourseCode = null;
 63  0
         previousCourseId = null;
 64  
         
 65  0
         layout.clear();
 66  
 
 67  0
         final VerticalSection choosingSection = new VerticalSection();
 68  0
         choosingSection.addWidget(new KSLabel("<b>Add a course</b>"));
 69  
 
 70  
         //first metadata is a drop down to select type of course
 71  0
         createAndAddCourseTypesDropdown();
 72  
 
 73  0
         retrieveMetadata();
 74  0
     }
 75  
 
 76  
     private void createAndAddCourseTypesDropdown() {
 77  0
         courseTypeWidget = new KSDropDown();
 78  0
         SimpleListItems courseTypes = new SimpleListItems();
 79  0
         courseTypes.addItem(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD, "Approved Courses");
 80  0
         courseTypes.addItem(CommonWidgetConstants.CLU_SET_PROPOSED_CLUS_FIELD, "Proposed Courses");
 81  0
         courseTypeWidget.setListItems(courseTypes);
 82  0
         courseTypeWidget.addSelectionChangeHandler(new SelectionChangeHandler() {
 83  
 
 84  
             @Override
 85  
             public void onSelectionChange(SelectionChangeEvent event) {
 86  0
                 String courseTypeSelected = ((KSDropDownImpl)event.getWidget()).getSelectedItem();
 87  0
                 if (courseTypeSelected == null) {
 88  0
                     if (courseWidget != null) {
 89  0
                         layout.remove(courseWidget);
 90  
                     }                    
 91  0
                     return;
 92  
                 }
 93  0
                 addCourseListWidget(true, courseTypeSelected);
 94  0
             }
 95  
         });        
 96  0
         layout.add(courseTypeWidget);
 97  0
     }
 98  
 
 99  
     private void retrieveMetadata() {
 100  0
         if (searchCourseMetadata == null) {
 101  0
             KSBlockingProgressIndicator.addTask(initializeTask);
 102  0
             courseMetadataRetriever.getMetadata("courseSet", new Callback<Metadata>(){
 103  
                 @Override
 104  
                 public void exec(Metadata result) {
 105  0
                     searchCourseMetadata = result;
 106  0
                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD).getConstraints().get(0).setMaxOccurs(1);
 107  0
                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_PROPOSED_CLUS_FIELD).getConstraints().get(0).setMaxOccurs(1);
 108  0
                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD).getConstraints().get(0).setId("single");
 109  0
                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_PROPOSED_CLUS_FIELD).getConstraints().get(0).setId("single");
 110  0
                     KSBlockingProgressIndicator.removeTask(initializeTask);
 111  
 
 112  0
                     if (previousCourseCode == null) {
 113  0
                         courseTypeWidget.selectItem(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD);
 114  0
                         addCourseListWidget(true, courseTypeWidget.getSelectedItem());
 115  
                     }
 116  0
                 }
 117  
             });
 118  
         } else {
 119  0
             if (previousCourseCode == null) {
 120  0
                 courseTypeWidget.selectItem(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD);
 121  0
                 addCourseListWidget(true, courseTypeWidget.getSelectedItem());
 122  
             }
 123  
         }
 124  0
     }
 125  
 
 126  
     private void addCourseListWidget(boolean enabled, String courseType) {
 127  0
         if (courseWidget != null) {
 128  0
             layout.remove(courseWidget);
 129  
         }
 130  0
         if (previousCourseCode != null) {
 131  0
             layout.remove(previousCourseCode);
 132  0
             previousCourseCode = null;
 133  0
             previousCourseId = null;
 134  
         }
 135  0
         courseWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(searchCourseMetadata.getProperties().get(courseType));
 136  0
         courseWidget.getSearchPanel().setMutipleSelect(false);
 137  
     //    ((KSSuggestBox) courseWidget.getInputWidget()).setEnabled(enabled);
 138  0
         layout.add(courseWidget);
 139  0
     }
 140  
 
 141  
     protected MessageKeyInfo generateMessageInfo(String labelKey) {
 142  0
         return new MessageKeyInfo("clusetmanagement", "clusetmanagement", "draft", labelKey);
 143  
     }
 144  
 
 145  
         @Override
 146  
         public void addValueChangeCallback(Callback<Data.Value> callback) {
 147  0
         }
 148  
 
 149  
         @Override
 150  
         public void setValue(Data.Value value) {    
 151  0
         }
 152  
 
 153  
     @Override
 154  
     public void getValue(Callback<String> doneSaveCallback) { 
 155  0
     }
 156  
 
 157  
     @Override
 158  
     public void setValue(final String id) {
 159  0
         if (id != null) {
 160  0
             getCluNameCallback.exec(id);
 161  
         }        
 162  0
     }
 163  
 
 164  
     public void setLabelContent(String id, final String code) {
 165  0
         layout.clear();
 166  0
         previousCourseId = id;
 167  0
         previousCourseCode = new KSLabel(code);
 168  0
         layout.add(previousCourseCode);
 169  0
         createAndAddCourseTypesDropdown();
 170  0
     }
 171  
 
 172  
     public void addGetCluNameCallback(Callback callback) {
 173  0
         this.getCluNameCallback = callback;    
 174  0
     }
 175  
 
 176  
     @Override
 177  
     public Data.Value getValue() {
 178  0
         Data.Value pickerValue = courseWidget.getValue();
 179  0
         if ((pickerValue.toString().isEmpty()) && (previousCourseCode != null) && (!previousCourseCode.getText().isEmpty())) {
 180  0
             return new Data.StringValue(previousCourseId);
 181  
         }
 182  
 
 183  0
         return pickerValue;
 184  
     }
 185  
 }