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