Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.binding.HasTextBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
HasTextBinding
0%
0/70
0%
0/52
11.667
HasTextBinding$1
0%
0/1
N/A
11.667
 
 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.configurable.mvc.binding;
 17  
 
 18  
 import java.util.Date;
 19  
 
 20  
 import org.kuali.student.common.assembly.data.QueryPath;
 21  
 import org.kuali.student.common.assembly.data.Data.DataType;
 22  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 23  
 import org.kuali.student.common.ui.client.validator.ClientDateParser;
 24  
 
 25  
 import com.google.gwt.core.client.GWT;
 26  
 import com.google.gwt.user.client.ui.HasText;
 27  
 
 28  
 /**
 29  
  * Model widget binding for HasText widgets which translates from string values to the necessary data
 30  
  * type for the model and vice versa.  
 31  
  * <br>Some standard GWT widgets which implement HasText are TextBox and TextArea.
 32  
  * 
 33  
  * @author Kuali Student Team
 34  
  *
 35  
  */
 36  0
 public class HasTextBinding extends ModelWidgetBindingSupport<HasText> {
 37  0
     public static HasTextBinding INSTANCE = new HasTextBinding();
 38  
 
 39  0
     private ClientDateParser dateParser = new ClientDateParser();
 40  
 
 41  0
     private HasTextBinding() {}
 42  
 
 43  
     @Override
 44  
     public void setModelValue(HasText object, DataModel model, String path) {
 45  
         try {
 46  0
             QueryPath qPath = QueryPath.parse(path);
 47  0
             DataType type = model.getType(qPath);
 48  
             
 49  0
             String newValue = null;
 50  0
             if(object.getText() != null) {
 51  0
                 newValue = object.getText().trim();
 52  
             }
 53  
 
 54  
             //If both model value is null and widget value is null or is empty no need to update model, so skip update
 55  0
             boolean skipUpdatingModel = model.get(qPath) == null && (newValue == null || newValue.isEmpty());
 56  
 
 57  0
             if (!skipUpdatingModel){
 58  
                     try {
 59  0
                         switch (type) {
 60  
                             case STRING:
 61  0
                                 if (!nullsafeEquals(model.get(qPath), newValue)) {
 62  0
                                     model.set(qPath, newValue);
 63  0
                                     setDirtyFlag(model, qPath);
 64  
                                 }
 65  
                                 break;
 66  
                             case INTEGER:
 67  0
                                         if(newValue != null && newValue.isEmpty()){
 68  0
                                                 Integer value = null;
 69  0
                                                 model.set(qPath, value);
 70  0
                                                 setDirtyFlag(model, qPath);
 71  0
                                         }
 72  
                                         else{
 73  0
                                     int intValue = Integer.parseInt(newValue);
 74  0
                                     if (!nullsafeEquals(model.get(qPath), intValue)) {
 75  0
                                         model.set(qPath, intValue);
 76  0
                                         setDirtyFlag(model, qPath);
 77  
                                     }
 78  
                                         }
 79  0
                                 break;
 80  
                             case LONG:
 81  0
                                 long longValue = Long.parseLong(newValue);
 82  0
                                 if (!nullsafeEquals(model.get(qPath), longValue)) {
 83  0
                                     model.set(qPath, longValue);
 84  0
                                     setDirtyFlag(model, qPath);
 85  
                                 }
 86  
                                 break;
 87  
                             case FLOAT:
 88  0
                                 float floatValue = Float.parseFloat(newValue);
 89  0
                                 if (!nullsafeEquals(model.get(qPath), floatValue)) {
 90  0
                                     model.set(qPath, floatValue);
 91  0
                                     setDirtyFlag(model, qPath);
 92  
                                 }
 93  
                                 break;
 94  
                             case DOUBLE:
 95  0
                                 double doubleValue = Double.parseDouble(newValue);
 96  0
                                 if (!nullsafeEquals(model.get(qPath), doubleValue)) {
 97  0
                                     model.set(qPath, doubleValue);
 98  0
                                     setDirtyFlag(model, qPath);
 99  
                                 }
 100  
                                 break;
 101  
                             case BOOLEAN:
 102  0
                                 if (newValue.equalsIgnoreCase("true") || newValue.equalsIgnoreCase("false")) {
 103  0
                                     boolean booleanValue = Boolean.parseBoolean(newValue);
 104  0
                                     if (!nullsafeEquals(model.get(qPath), booleanValue)) {
 105  0
                                         model.set(qPath, booleanValue);
 106  0
                                         setDirtyFlag(model, qPath);
 107  
                                     }
 108  0
                                 } else {
 109  0
                                     throw new UnsupportedOperationException("BooleanTypes can only be set with true or false");
 110  
                                 }
 111  
                                 break;
 112  
                             case DATE:
 113  0
                                 Date dateValue = dateParser.parseDate(newValue);
 114  0
                                 if (!nullsafeEquals(model.get(qPath), dateValue)) {
 115  0
                                     model.set(qPath, dateValue);
 116  0
                                     setDirtyFlag(model, qPath);
 117  
                                 }
 118  
                                 break;
 119  
                         }
 120  0
                     } catch (Exception e) {
 121  0
                         GWT.log("Unable to coerce type for " + path + ", falling back to String", e);
 122  0
                         model.set(qPath, newValue);
 123  0
                     }
 124  
             }
 125  0
         } catch (Exception e) {
 126  0
             GWT.log("Error setting model value for: " + path, e);
 127  0
         }
 128  0
     }
 129  
 
 130  
     @Override
 131  
     public void setWidgetValue(HasText object, DataModel model, String path) {
 132  
         try {
 133  0
             QueryPath qPath = QueryPath.parse(path);
 134  
             
 135  0
             Object value = null;
 136  0
             if(model!=null){
 137  0
                     value = model.get(qPath);
 138  
             }
 139  
 
 140  0
             if (value != null && object != null) {
 141  0
                 if (value instanceof Date) {
 142  0
                     object.setText(dateParser.toString((Date) value));
 143  
                 } else {
 144  0
                     object.setText(value.toString());
 145  
                 }
 146  0
             } else if (value == null && object != null) {
 147  0
                 object.setText("");
 148  
             }
 149  0
         } catch (Exception e) {
 150  0
             GWT.log("Error setting widget value for: " + path, e);
 151  0
         }
 152  0
     }
 153  
 
 154  
 }