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