| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 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.ui.client.mvc.DataModel; | 
  | 21 |  |  import org.kuali.student.common.ui.client.validator.ClientDateParser; | 
  | 22 |  |  import org.kuali.student.core.assembly.data.QueryPath; | 
  | 23 |  |  import org.kuali.student.core.assembly.data.Data.DataType; | 
  | 24 |  |   | 
  | 25 |  |  import com.google.gwt.core.client.GWT; | 
  | 26 |  |  import com.google.gwt.user.client.ui.HasText; | 
  | 27 |  |   | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 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 |  |              try { | 
  | 55 | 0 |                  switch (type) { | 
  | 56 |  |                      case STRING: | 
  | 57 | 0 |                          if (!nullsafeEquals(model.get(qPath), newValue)) { | 
  | 58 | 0 |                              model.set(qPath, newValue); | 
  | 59 | 0 |                              setDirtyFlag(model, qPath); | 
  | 60 |  |                          } | 
  | 61 |  |                          break; | 
  | 62 |  |                      case INTEGER: | 
  | 63 | 0 |                                  if(newValue != null && newValue.isEmpty()){ | 
  | 64 | 0 |                                          Integer value = null; | 
  | 65 | 0 |                                          model.set(qPath, value); | 
  | 66 | 0 |                                          setDirtyFlag(model, qPath); | 
  | 67 | 0 |                                  } | 
  | 68 |  |                                  else{ | 
  | 69 | 0 |                              int intValue = Integer.parseInt(newValue); | 
  | 70 | 0 |                              if (!nullsafeEquals(model.get(qPath), intValue)) { | 
  | 71 | 0 |                                  model.set(qPath, intValue); | 
  | 72 | 0 |                                  setDirtyFlag(model, qPath); | 
  | 73 |  |                              } | 
  | 74 |  |                                  } | 
  | 75 | 0 |                          break; | 
  | 76 |  |                      case LONG: | 
  | 77 | 0 |                          long longValue = Long.parseLong(newValue); | 
  | 78 | 0 |                          if (!nullsafeEquals(model.get(qPath), longValue)) { | 
  | 79 | 0 |                              model.set(qPath, longValue); | 
  | 80 | 0 |                              setDirtyFlag(model, qPath); | 
  | 81 |  |                          } | 
  | 82 |  |                          break; | 
  | 83 |  |                      case FLOAT: | 
  | 84 | 0 |                          float floatValue = Float.parseFloat(newValue); | 
  | 85 | 0 |                          if (!nullsafeEquals(model.get(qPath), floatValue)) { | 
  | 86 | 0 |                              model.set(qPath, floatValue); | 
  | 87 | 0 |                              setDirtyFlag(model, qPath); | 
  | 88 |  |                          } | 
  | 89 |  |                          break; | 
  | 90 |  |                      case DOUBLE: | 
  | 91 | 0 |                          double doubleValue = Double.parseDouble(newValue); | 
  | 92 | 0 |                          if (!nullsafeEquals(model.get(qPath), doubleValue)) { | 
  | 93 | 0 |                              model.set(qPath, doubleValue); | 
  | 94 | 0 |                              setDirtyFlag(model, qPath); | 
  | 95 |  |                          } | 
  | 96 |  |                          break; | 
  | 97 |  |                      case BOOLEAN: | 
  | 98 | 0 |                          if (newValue.equalsIgnoreCase("true") || newValue.equalsIgnoreCase("false")) { | 
  | 99 | 0 |                              boolean booleanValue = Boolean.parseBoolean(newValue); | 
  | 100 | 0 |                              if (!nullsafeEquals(model.get(qPath), booleanValue)) { | 
  | 101 | 0 |                                  model.set(qPath, booleanValue); | 
  | 102 | 0 |                                  setDirtyFlag(model, qPath); | 
  | 103 |  |                              } | 
  | 104 | 0 |                          } else { | 
  | 105 | 0 |                              throw new UnsupportedOperationException("BooleanTypes can only be set with true or false"); | 
  | 106 |  |                          } | 
  | 107 |  |                          break; | 
  | 108 |  |                      case DATE: | 
  | 109 | 0 |                          Date dateValue = dateParser.parseDate(newValue); | 
  | 110 | 0 |                          if (!nullsafeEquals(model.get(qPath), dateValue)) { | 
  | 111 | 0 |                              model.set(qPath, dateValue); | 
  | 112 | 0 |                              setDirtyFlag(model, qPath); | 
  | 113 |  |                          } | 
  | 114 |  |                          break; | 
  | 115 |  |                  } | 
  | 116 | 0 |              } catch (Exception e) { | 
  | 117 | 0 |                  GWT.log("Unable to coerce type for " + path + ", falling back to String", e); | 
  | 118 | 0 |                  model.set(qPath, newValue); | 
  | 119 | 0 |              } | 
  | 120 | 0 |          } catch (Exception e) { | 
  | 121 | 0 |              GWT.log("Error setting model value for: " + path, e); | 
  | 122 | 0 |          } | 
  | 123 | 0 |      } | 
  | 124 |  |   | 
  | 125 |  |      @Override | 
  | 126 |  |      public void setWidgetValue(HasText object, DataModel model, String path) { | 
  | 127 |  |          try { | 
  | 128 | 0 |              QueryPath qPath = QueryPath.parse(path); | 
  | 129 |  |               | 
  | 130 | 0 |              Object value = null; | 
  | 131 | 0 |              if(model!=null){ | 
  | 132 | 0 |                      value = model.get(qPath); | 
  | 133 |  |              } | 
  | 134 |  |   | 
  | 135 | 0 |              if (value != null && object != null) { | 
  | 136 | 0 |                  if (value instanceof Date) { | 
  | 137 | 0 |                      object.setText(dateParser.toString((Date) value)); | 
  | 138 |  |                  } else { | 
  | 139 | 0 |                      object.setText(value.toString()); | 
  | 140 |  |                  } | 
  | 141 | 0 |              } else if (value == null && object != null) { | 
  | 142 | 0 |                  object.setText(""); | 
  | 143 |  |              } | 
  | 144 | 0 |          } catch (Exception e) { | 
  | 145 | 0 |              GWT.log("Error setting widget value for: " + path, e); | 
  | 146 | 0 |          } | 
  | 147 | 0 |      } | 
  | 148 |  |   | 
  | 149 |  |  } |