| 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 | 0 | public class HasTextBinding extends ModelWidgetBindingSupport<HasText> { |
| 29 | 0 | public static HasTextBinding INSTANCE = new HasTextBinding(); |
| 30 | |
|
| 31 | 0 | private ClientDateParser dateParser = new ClientDateParser(); |
| 32 | |
|
| 33 | 0 | private HasTextBinding() {} |
| 34 | |
|
| 35 | |
@Override |
| 36 | |
public void setModelValue(HasText object, DataModel model, String path) { |
| 37 | |
try { |
| 38 | 0 | QueryPath qPath = QueryPath.parse(path); |
| 39 | 0 | DataType type = model.getType(qPath); |
| 40 | 0 | String newValue = object.getText().trim(); |
| 41 | |
|
| 42 | |
try { |
| 43 | 0 | switch (type) { |
| 44 | |
case STRING: |
| 45 | 0 | if (!nullsafeEquals(model.get(qPath), newValue)) { |
| 46 | 0 | model.set(qPath, newValue); |
| 47 | 0 | setDirtyFlag(model, qPath); |
| 48 | |
} |
| 49 | |
break; |
| 50 | |
case INTEGER: |
| 51 | 0 | if(newValue != null && newValue.isEmpty()){ |
| 52 | 0 | Integer value = null; |
| 53 | 0 | model.set(qPath, value); |
| 54 | 0 | setDirtyFlag(model, qPath); |
| 55 | 0 | } |
| 56 | |
else{ |
| 57 | 0 | int intValue = Integer.parseInt(newValue); |
| 58 | 0 | if (!nullsafeEquals(model.get(qPath), intValue)) { |
| 59 | 0 | model.set(qPath, intValue); |
| 60 | 0 | setDirtyFlag(model, qPath); |
| 61 | |
} |
| 62 | |
} |
| 63 | 0 | break; |
| 64 | |
case LONG: |
| 65 | 0 | long longValue = Long.parseLong(newValue); |
| 66 | 0 | if (!nullsafeEquals(model.get(qPath), longValue)) { |
| 67 | 0 | model.set(qPath, longValue); |
| 68 | 0 | setDirtyFlag(model, qPath); |
| 69 | |
} |
| 70 | |
break; |
| 71 | |
case FLOAT: |
| 72 | 0 | float floatValue = Float.parseFloat(newValue); |
| 73 | 0 | if (!nullsafeEquals(model.get(qPath), floatValue)) { |
| 74 | 0 | model.set(qPath, floatValue); |
| 75 | 0 | setDirtyFlag(model, qPath); |
| 76 | |
} |
| 77 | |
break; |
| 78 | |
case DOUBLE: |
| 79 | 0 | double doubleValue = Double.parseDouble(newValue); |
| 80 | 0 | if (!nullsafeEquals(model.get(qPath), doubleValue)) { |
| 81 | 0 | model.set(qPath, doubleValue); |
| 82 | 0 | setDirtyFlag(model, qPath); |
| 83 | |
} |
| 84 | |
break; |
| 85 | |
case BOOLEAN: |
| 86 | 0 | if (newValue.equalsIgnoreCase("true") || newValue.equalsIgnoreCase("false")) { |
| 87 | 0 | boolean booleanValue = Boolean.parseBoolean(newValue); |
| 88 | 0 | if (!nullsafeEquals(model.get(qPath), booleanValue)) { |
| 89 | 0 | model.set(qPath, booleanValue); |
| 90 | 0 | setDirtyFlag(model, qPath); |
| 91 | |
} |
| 92 | 0 | } else { |
| 93 | 0 | throw new UnsupportedOperationException("BooleanTypes can only be set with true or false"); |
| 94 | |
} |
| 95 | |
break; |
| 96 | |
case DATE: |
| 97 | 0 | Date dateValue = dateParser.parseDate(newValue); |
| 98 | 0 | if (!nullsafeEquals(model.get(qPath), dateValue)) { |
| 99 | 0 | model.set(qPath, dateValue); |
| 100 | 0 | setDirtyFlag(model, qPath); |
| 101 | |
} |
| 102 | |
break; |
| 103 | |
} |
| 104 | 0 | } catch (Exception e) { |
| 105 | 0 | GWT.log("Unable to coerce type for " + path + ", falling back to String", e); |
| 106 | 0 | model.set(qPath, newValue); |
| 107 | 0 | } |
| 108 | 0 | } catch (Exception e) { |
| 109 | 0 | GWT.log("Error setting model value for: " + path, e); |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
@Override |
| 114 | |
public void setWidgetValue(HasText object, DataModel model, String path) { |
| 115 | |
try { |
| 116 | 0 | QueryPath qPath = QueryPath.parse(path); |
| 117 | |
|
| 118 | 0 | Object value = null; |
| 119 | 0 | if(model!=null){ |
| 120 | 0 | value = model.get(qPath); |
| 121 | |
} |
| 122 | |
|
| 123 | 0 | if (value != null && object != null) { |
| 124 | 0 | if (value instanceof Date) { |
| 125 | 0 | object.setText(dateParser.toString((Date) value)); |
| 126 | |
} else { |
| 127 | 0 | object.setText(value.toString()); |
| 128 | |
} |
| 129 | 0 | } else if (value == null && object != null) { |
| 130 | 0 | object.setText(""); |
| 131 | |
} |
| 132 | 0 | } catch (Exception e) { |
| 133 | 0 | GWT.log("Error setting widget value for: " + path, e); |
| 134 | 0 | } |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
} |