View Javadoc

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