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.Data;
21  import org.kuali.student.common.assembly.data.QueryPath;
22  import org.kuali.student.common.ui.client.mvc.DataModel;
23  import org.kuali.student.common.ui.client.widgets.KSCheckBox;
24  
25  import com.google.gwt.core.client.GWT;
26  import com.google.gwt.user.client.ui.HasValue;
27  
28  /**
29   * Model widget binding for HasValue widgets.  HasValue widgets can return any object type, so the
30   * to from translated is done with known/expected types for KS.
31   * @author Kuali Student Team
32   *
33   */
34  public class HasValueBinding extends ModelWidgetBindingSupport<HasValue> {
35  
36      public static HasValueBinding INSTANCE = new HasValueBinding();
37  
38      private HasValueBinding() {}
39  
40      @Override
41      public void setModelValue(HasValue object, DataModel model, String path) {
42          QueryPath qPath = QueryPath.parse(path);
43          Object value = object.getValue();
44          if (!nullsafeEquals(model.get(qPath), value)) {
45              setDirtyFlag(model, qPath);
46          }
47          // TODO: Type checking to ensure that the value type of widget matches model defintion
48          if (value instanceof String) {
49              model.set(qPath, (String) value);
50          } else if (value instanceof Character) {
51              model.set(qPath, value.toString());
52          } else if (value instanceof Integer) {
53              model.set(qPath, (Integer) value);
54          } else if (value instanceof Long) {
55              model.set(qPath, (Long) value);
56          } else if (value instanceof Float) {
57              model.set(qPath, (Float) value);
58          } else if (value instanceof Double) {
59              model.set(qPath, (Double) value);
60          } else if (value instanceof Byte) {
61              model.set(qPath, ((Byte) value).intValue());
62          } else if (value instanceof Boolean) {
63              model.set(qPath, (Boolean) value);
64          } else if (value instanceof Date) {
65              model.set(qPath, (Date) value);
66          } else if (value instanceof Data.Value){
67          	model.set(qPath, (Data.Value) value);
68          } else if (value == null){
69          	String s = null;
70          	model.set(qPath, s);
71          }
72  
73      }
74  
75      @Override
76      public void setWidgetValue(HasValue object, DataModel model, String path) {
77          QueryPath qPath = QueryPath.parse(path);
78          Object value = model.get(qPath);
79  
80          if (value != null && object != null) {
81              object.setValue(value);
82          } else if (object != null) {
83              try {
84              	if (object instanceof KSCheckBox) {
85              		object.setValue(false);
86              	} else {
87              		object.setValue("");
88              	}
89              } catch (RuntimeException e) {
90                  GWT.log("Warning: Ignoring error attempting to setValue for " + object.getClass().getName(), e);
91              }
92          }
93      }
94  
95  }