1
2
3
4
5
6
7
8
9
10
11
12
13
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
85
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
118 if(initialGradeValue != null) {
119
120
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
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
139
140 if(cell.getKey().equals(GRADE_TYPE_ID_CELL_KEY)) {
141 selectedGradeTypeId = cell.getValue();
142 }
143
144 }
145
146
147 if(selectedGradeRow != null) {
148 break;
149 }
150 }
151
152
153 if(selectedGradeRow == null) {
154 return;
155 }
156
157
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 }