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 org.kuali.student.common.assembly.data.QueryPath;
19  import org.kuali.student.common.ui.client.mvc.DataModel;
20  import org.kuali.student.common.ui.client.widgets.RichTextEditor;
21  
22  /**
23   * Sets to and from model data that is marked as rich text (it has plain and formatted text in the model)
24   * 
25   * @author Kuali Student Team
26   *
27   */
28  public class RichTextBinding extends ModelWidgetBindingSupport<RichTextEditor> {
29      public static RichTextBinding INSTANCE = new RichTextBinding();
30  
31      private RichTextBinding() {}
32  
33      @Override
34      public void setModelValue(RichTextEditor object, DataModel model, String path) {
35          String richTextRoot = path + QueryPath.getPathSeparator();
36  
37          QueryPath qPath = QueryPath.parse(richTextRoot + "plain");
38  
39          String oldValue = model.get(qPath);
40          String newValue = object.getText();
41  
42          if (!nullsafeEquals(oldValue, newValue)) {
43              model.set(qPath, newValue);
44              setDirtyFlag(model, qPath);
45          }
46  
47          qPath = QueryPath.parse(richTextRoot + "formatted");
48  
49          oldValue = model.get(qPath);
50          newValue = object.getHTML();
51  
52          if (!nullsafeEquals(oldValue, newValue)) {
53              model.set(qPath, newValue);
54              setDirtyFlag(model, qPath);
55          }
56  
57          // TODO: Should these defaults be set in server assembly defaults?
58          // Commenting type and state as it is not required for rich text
59          // qPath = QueryPath.parse(richTextRoot + "type");
60          // model.set(qPath, "kuali.not.applicable");
61          //        
62          // qPath = QueryPath.parse(richTextRoot + "state");
63          // model.set(qPath, "(n/a)");
64      }
65  
66      @Override
67      public void setWidgetValue(RichTextEditor object, DataModel model, String path) {
68          String richTextRoot = path + QueryPath.getPathSeparator();
69  
70          QueryPath qPath = QueryPath.parse(richTextRoot + "plain");
71          String formatted = model.get(qPath);
72  
73          qPath = QueryPath.parse(richTextRoot + "formatted");
74          String plain = model.get(qPath);
75  
76          if (formatted != null) {
77              object.setHTML(formatted);
78          } else if (plain != null) {
79              object.setText(plain);
80          } else {
81              object.setHTML("");
82          }
83      }
84  
85  }