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  package org.kuali.student.lum.common.client.widgets;
16  
17  import java.util.List;
18  
19  import org.kuali.student.common.assembly.data.Data;
20  import org.kuali.student.common.assembly.data.Metadata;
21  import org.kuali.student.common.search.dto.SearchResultCell;
22  import org.kuali.student.common.search.dto.SearchResultRow;
23  import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
24  import org.kuali.student.common.ui.client.mvc.Callback;
25  import org.kuali.student.common.ui.client.mvc.HasDataValue;
26  import org.kuali.student.common.ui.client.widgets.KSDropDown;
27  import org.kuali.student.common.ui.client.widgets.impl.KSDropDownImpl;
28  import org.kuali.student.common.ui.client.widgets.list.KSRadioButtonList;
29  import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
30  import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems;
31  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
32  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
33  import org.kuali.student.common.ui.client.widgets.list.impl.KSRadioButtonListImpl;
34  import org.kuali.student.common.ui.client.widgets.search.KSPicker;
35  import org.kuali.student.core.statement.ui.client.widgets.rules.AccessWidgetValue;
36  
37  import com.google.gwt.user.client.ui.Composite;
38  import com.google.gwt.user.client.ui.FlowPanel;
39  import com.google.gwt.user.client.ui.Widget;
40  
41  public class GradeWidget extends Composite implements AccessWidgetValue, HasDataValue {
42  
43      protected static final String GRADE_TYPE_ID_CELL_KEY = "lrc.resultColumn.resultComponent.id";
44      protected static final String GRADE_TYPE_NAME_CELL_KEY = "lrc.resultColumn.resultComponent.name";
45      private FlowPanel layout = new FlowPanel();
46      KSPicker gradeTypeWidget;
47      KSPicker gradeWidget = null;
48      Metadata gradeMetadata;
49      private String initialGradeValue = null;
50  
51      public GradeWidget() {
52          this.initWidget(layout);
53      }
54  
55      @Override
56      public void initWidget(List<Metadata> fieldsMetadata) {
57          layout.clear();
58          gradeTypeWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(fieldsMetadata.get(0));
59          layout.add(gradeTypeWidget);
60          gradeMetadata = fieldsMetadata.get(1);
61          setupHandlers();
62      }
63  
64      private void setupHandlers() {
65  
66          ((KSRadioButtonList)gradeTypeWidget.getInputWidget()).addSelectionChangeHandler(new SelectionChangeHandler() {
67  
68              @Override
69              public void onSelectionChange(SelectionChangeEvent event) {
70                  String gradeTypeSelected = ((KSRadioButtonListImpl)event.getWidget()).getSelectedItem();
71                  gradeMetadata.getInitialLookup().getParams().get(0).setDefaultValueList(null);
72                  gradeMetadata.getInitialLookup().getParams().get(0).setDefaultValueString(gradeTypeSelected);
73                  
74                  addGradeListWidget(true, !event.isUserInitiated());
75              }
76          });
77      }
78  
79      private void addGradeListWidget(boolean enabled, boolean addInitialValueCallback) {
80          if (gradeWidget != null) {
81              layout.remove(gradeWidget);
82          }
83          
84          // this callback ensures the initial grade value is selected if it should be set
85          // since the values are populated async, a widget ready callback must be used
86          if(addInitialValueCallback) {
87              ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).addWidgetReadyCallback(new Callback<Widget>() {
88              
89                  @Override
90                  public void exec(Widget result) {
91                      gradeWidget.setValue(initialGradeValue);
92                      KSDropDownImpl theWidget = (KSDropDownImpl) result;
93                      theWidget.selectItem(initialGradeValue);
94                  }
95              });
96          }
97          
98          gradeWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(gradeMetadata);
99          ((KSDropDown)gradeWidget.getInputWidget()).setEnabled(enabled);
100         layout.add(gradeWidget);
101     }
102 
103 	@Override
104 	public void addValueChangeCallback(Callback<Data.Value> callback) {
105 	}
106 
107 	@Override
108 	public void setValue(Data.Value value) {
109 	    
110 	    if(value != null && value.toString() != null) {
111 	        initialGradeValue = value.toString();
112 	    }
113 	    
114         addGradeListWidget((initialGradeValue != null), false);
115         gradeWidget.setValue(value);
116 	    
117 		// if setting an initial value that is not the blank default (i.e. populating the grade widget from a previously saved value)
118 		if(initialGradeValue != null) {
119 		    
120 		    // add a callback to select the grade type after the grade dropdown has been initialized
121 	        final String selectedGradeValue = value.toString();
122 		
123     		Callback<Widget> afterInitCallback = new Callback<Widget>(){
124                 @Override
125                 public void exec(Widget widget) {
126     
127                     List<SearchResultRow> gradeItems = ((SearchResultListItems) ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).getListItems()).getReadOnlyResults();
128                     
129                     // find the selected row for the grade
130                     SearchResultRow selectedGradeRow = null;
131                     String selectedGradeTypeId = null;
132                     for(SearchResultRow row : gradeItems) {
133                         for(SearchResultCell cell : row.getCells()) {
134                             if(cell.getValue().equals(selectedGradeValue)) {
135                                 selectedGradeRow = row;
136                             }
137                             
138                             // get the grade type id from any row  
139                             // If the right row is found, this value will contain the grade type we are looking for
140                             if(cell.getKey().equals(GRADE_TYPE_ID_CELL_KEY)) {
141                                 selectedGradeTypeId = cell.getValue();
142                             }
143                             
144                         }
145                         
146                         // once the grade row is found, break
147                         if(selectedGradeRow != null) {
148                             break;
149                         }
150                     }
151                     
152                     // make sure to check for null, though it would indicate bad data if true
153                     if(selectedGradeRow == null) {
154                         return;
155                     }
156                     
157                     // now we can select the grade type in the grade type widget
158                     ((KSSelectItemWidgetAbstract)gradeTypeWidget.getInputWidget()).selectItem(selectedGradeTypeId);
159                     
160                 }
161             };
162     		
163     		((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).addWidgetReadyCallback(afterInitCallback);
164 		}
165 	}
166 
167     @Override
168     public void getValue(Callback<String> doneSaveCallback) { 
169     }
170 
171     @Override
172     public void setValue(String id) {
173     }
174 
175     @Override
176     public Data.Value getValue() {
177         return (gradeWidget != null ? gradeWidget.getValue() : null);
178     }
179 }