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;
17  
18  import com.google.gwt.core.client.GWT;
19  import com.google.gwt.event.dom.client.BlurEvent;
20  import com.google.gwt.event.dom.client.BlurHandler;
21  import com.google.gwt.event.dom.client.HasBlurHandlers;
22  import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
23  import com.google.gwt.event.logical.shared.ValueChangeEvent;
24  import com.google.gwt.event.logical.shared.ValueChangeHandler;
25  import com.google.gwt.user.client.ui.Widget;
26  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityGroup;
27  import org.kuali.student.common.ui.client.mvc.Callback;
28  import org.kuali.student.common.ui.client.mvc.HasFocusLostCallbacks;
29  import org.kuali.student.common.ui.client.widgets.KSLabel;
30  import org.kuali.student.common.ui.client.widgets.list.HasSelectionChangeHandlers;
31  import org.kuali.student.common.ui.client.widgets.list.KSSelectedList;
32  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
33  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
34  import org.kuali.student.common.ui.client.widgets.search.KSPicker;
35  
36  /**
37   * Adds the appropriate handler to the widget contained within the FieldDescriptor for when
38   * to set that the field has had focus/user interaction based on what type of widget
39   *
40   * @author Kuali Student Team
41   */
42  public class ValidationEventBindingImpl implements ValidationEventBinding {
43  
44      public void bind(final FieldDescriptor fd) {
45          Widget w = fd.getFieldWidget();
46  
47          if (w instanceof HasSelectionChangeHandlers) {
48              ((HasSelectionChangeHandlers) w).addSelectionChangeHandler(new SelectionChangeHandler() {
49                  @Override
50                  public void onSelectionChange(SelectionChangeEvent event) {
51                      if (event.isUserInitiated()) {
52                          processValidationEvent(fd);
53                      }
54                  }
55              });
56          } else if(w instanceof KSPicker && ((KSPicker)w).getInputWidget() instanceof HasSelectionChangeHandlers){
57              ((KSPicker)w).addSelectionChangeHandler(new SelectionChangeHandler() {
58                  @Override
59                  public void onSelectionChange(SelectionChangeEvent event) {
60                  	if(event.isUserInitiated()){
61                  		processValidationEvent(fd);
62                  	}
63                  }
64              });
65          } else if (w instanceof HasBlurHandlers) {
66              ((HasBlurHandlers) w).addBlurHandler(new BlurHandler() {
67                  public void onBlur(BlurEvent event) {
68                      processValidationEvent(fd);
69                  }
70              });
71          } else if (w instanceof HasFocusLostCallbacks) {
72              ((HasFocusLostCallbacks) w).addFocusLostCallback(new Callback<Boolean>() {
73                  @Override
74                  public void exec(Boolean result) {
75                      processValidationEvent(fd);
76                  }
77              });
78          } else if (w instanceof HasValueChangeHandlers) {
79              ((HasValueChangeHandlers<Object>) w).addValueChangeHandler(new ValueChangeHandler<Object>() {
80  
81                  @Override
82                  public void onValueChange(ValueChangeEvent<Object> event) {
83                      processValidationEvent(fd);
84                  }
85              });
86          } else if (w instanceof KSLabel
87                  || w instanceof org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityComposite
88                  || w instanceof MultiplicityGroup) {
89              GWT.log("Do nothing these are valid but do not fire validation events (maybe?)");
90              //Do nothing these are valid but do not fire validation events (maybe?)
91          } else {
92              GWT.log("The field with key: " + fd.getFieldKey() +
93                      " does not use a widget which implements an interface that can perform on the fly validation", null);
94          }
95          //Dont add focus lost to the oracle if it is repeating
96          if (w instanceof KSSelectedList && !((KSSelectedList)w).getConfig().isRepeating) {
97              ((HasFocusLostCallbacks) w).addFocusLostCallback(new Callback<Boolean>() {
98                  @Override
99                  public void exec(Boolean result) {
100                     processValidationEvent(fd);
101                 }
102             });
103         }
104 
105     }
106 
107     private void processValidationEvent(FieldDescriptor fieldDescriptor) {
108         fieldDescriptor.setHasHadFocus(true);
109         fieldDescriptor.getValidationRequestCallback().exec(true);
110     }
111 }