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 | 0 | 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 | 0 | private FlowPanel layout = new FlowPanel(); |
46 | |
KSPicker gradeTypeWidget; |
47 | 0 | KSPicker gradeWidget = null; |
48 | |
Metadata gradeMetadata; |
49 | 0 | private String initialGradeValue = null; |
50 | |
|
51 | 0 | public GradeWidget() { |
52 | 0 | this.initWidget(layout); |
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
public void initWidget(List<Metadata> fieldsMetadata) { |
57 | 0 | layout.clear(); |
58 | 0 | gradeTypeWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(fieldsMetadata.get(0)); |
59 | 0 | layout.add(gradeTypeWidget); |
60 | 0 | gradeMetadata = fieldsMetadata.get(1); |
61 | 0 | setupHandlers(); |
62 | 0 | } |
63 | |
|
64 | |
private void setupHandlers() { |
65 | |
|
66 | 0 | ((KSRadioButtonList)gradeTypeWidget.getInputWidget()).addSelectionChangeHandler(new SelectionChangeHandler() { |
67 | |
|
68 | |
@Override |
69 | |
public void onSelectionChange(SelectionChangeEvent event) { |
70 | 0 | String gradeTypeSelected = ((KSRadioButtonListImpl)event.getWidget()).getSelectedItem(); |
71 | 0 | gradeMetadata.getInitialLookup().getParams().get(0).setDefaultValueList(null); |
72 | 0 | gradeMetadata.getInitialLookup().getParams().get(0).setDefaultValueString(gradeTypeSelected); |
73 | |
|
74 | 0 | addGradeListWidget(true, !event.isUserInitiated()); |
75 | 0 | } |
76 | |
}); |
77 | 0 | } |
78 | |
|
79 | |
private void addGradeListWidget(boolean enabled, boolean addInitialValueCallback) { |
80 | 0 | if (gradeWidget != null) { |
81 | 0 | layout.remove(gradeWidget); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | 0 | if(addInitialValueCallback) { |
87 | 0 | ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).addWidgetReadyCallback(new Callback<Widget>() { |
88 | |
|
89 | |
@Override |
90 | |
public void exec(Widget result) { |
91 | 0 | gradeWidget.setValue(initialGradeValue); |
92 | 0 | KSDropDownImpl theWidget = (KSDropDownImpl) result; |
93 | 0 | theWidget.selectItem(initialGradeValue); |
94 | 0 | } |
95 | |
}); |
96 | |
} |
97 | |
|
98 | 0 | gradeWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(gradeMetadata); |
99 | 0 | ((KSDropDown)gradeWidget.getInputWidget()).setEnabled(enabled); |
100 | 0 | layout.add(gradeWidget); |
101 | 0 | } |
102 | |
|
103 | |
@Override |
104 | |
public void addValueChangeCallback(Callback<Data.Value> callback) { |
105 | 0 | } |
106 | |
|
107 | |
@Override |
108 | |
public void setValue(Data.Value value) { |
109 | |
|
110 | 0 | if(value != null && value.toString() != null) { |
111 | 0 | initialGradeValue = value.toString(); |
112 | |
} |
113 | |
|
114 | 0 | addGradeListWidget((initialGradeValue != null), false); |
115 | 0 | gradeWidget.setValue(value); |
116 | |
|
117 | |
|
118 | 0 | if(initialGradeValue != null) { |
119 | |
|
120 | |
|
121 | 0 | final String selectedGradeValue = value.toString(); |
122 | |
|
123 | 0 | Callback<Widget> afterInitCallback = new Callback<Widget>(){ |
124 | |
@Override |
125 | |
public void exec(Widget widget) { |
126 | |
|
127 | 0 | List<SearchResultRow> gradeItems = ((SearchResultListItems) ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).getListItems()).getReadOnlyResults(); |
128 | |
|
129 | |
|
130 | 0 | SearchResultRow selectedGradeRow = null; |
131 | 0 | String selectedGradeTypeId = null; |
132 | 0 | for(SearchResultRow row : gradeItems) { |
133 | 0 | for(SearchResultCell cell : row.getCells()) { |
134 | 0 | if(cell.getValue().equals(selectedGradeValue)) { |
135 | 0 | selectedGradeRow = row; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | 0 | if(cell.getKey().equals(GRADE_TYPE_ID_CELL_KEY)) { |
141 | 0 | selectedGradeTypeId = cell.getValue(); |
142 | |
} |
143 | |
|
144 | |
} |
145 | |
|
146 | |
|
147 | 0 | if(selectedGradeRow != null) { |
148 | 0 | break; |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
|
153 | 0 | if(selectedGradeRow == null) { |
154 | 0 | return; |
155 | |
} |
156 | |
|
157 | |
|
158 | 0 | ((KSSelectItemWidgetAbstract)gradeTypeWidget.getInputWidget()).selectItem(selectedGradeTypeId); |
159 | |
|
160 | 0 | } |
161 | |
}; |
162 | |
|
163 | 0 | ((KSSelectItemWidgetAbstract)gradeWidget.getInputWidget()).addWidgetReadyCallback(afterInitCallback); |
164 | |
} |
165 | 0 | } |
166 | |
|
167 | |
@Override |
168 | |
public void getValue(Callback<String> doneSaveCallback) { |
169 | 0 | } |
170 | |
|
171 | |
@Override |
172 | |
public void setValue(String id) { |
173 | 0 | } |
174 | |
|
175 | |
@Override |
176 | |
public Data.Value getValue() { |
177 | 0 | return (gradeWidget != null ? gradeWidget.getValue() : null); |
178 | |
} |
179 | |
} |