1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.configurable.mvc.views;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
22 import org.kuali.student.common.ui.client.configurable.mvc.LayoutController;
23 import org.kuali.student.common.ui.client.configurable.mvc.sections.BaseSection;
24 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
25 import org.kuali.student.common.ui.client.mvc.Callback;
26 import org.kuali.student.common.ui.client.mvc.Controller;
27 import org.kuali.student.common.ui.client.mvc.DataModel;
28 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
29 import org.kuali.student.common.ui.client.mvc.View;
30 import org.kuali.student.r1.common.assembly.data.Metadata;
31 import org.kuali.student.r1.common.assembly.data.ModelDefinition;
32
33 import com.google.gwt.user.client.Window;
34 import com.google.gwt.user.client.ui.Widget;
35
36
37
38
39
40
41
42
43 public abstract class SectionView extends BaseSection implements View {
44
45 protected String modelId;
46 protected DataModel model;
47
48 private Enum<?> viewEnum;
49 private String viewName;
50
51 private List<View> views = new ArrayList<View>();
52
53 public SectionView() {}
54
55
56
57
58
59 public SectionView(Enum<?> viewEnum, String viewName) {
60 this.viewEnum = viewEnum;
61 this.viewName = viewName;
62 }
63
64 public void init(Enum<?> viewEnum, String viewName) {
65 this.viewEnum = viewEnum;
66 this.viewName = viewName;
67 }
68
69
70
71
72
73
74 @Override
75 public Enum<?> getViewEnum() {
76 return viewEnum;
77 }
78
79 public void setViewEnum(Enum<?> viewEnum) {
80 this.viewEnum = viewEnum;
81 }
82
83
84
85
86
87
88
89
90
91 @Override
92 public void beforeShow(final Callback<Boolean> onReadyCallback) {
93
94 super.clearValidationErrors();
95
96 if (getController() != null) {
97 getController().requestModel(modelId, new ModelRequestCallback<DataModel>() {
98
99 @Override
100 public void onRequestFail(Throwable cause) {
101 Window.alert("Failed to get model: " + modelId + " for SectionView " + getName());
102 onReadyCallback.exec(false);
103 }
104
105 @Override
106 public void onModelReady(DataModel m) {
107 model = m;
108 updateWidgetData(m);
109 resetFieldInteractionFlags();
110 onReadyCallback.exec(true);
111 }
112
113 });
114 }
115
116 for (Section section : sections) {
117 if (section instanceof SectionView) {
118 ((SectionView) section).beforeShow(new Callback<Boolean>() {
119 @Override
120 public void exec(Boolean result) {
121 }
122 });
123 }
124 }
125 for (View view : views) {
126 view.beforeShow(Controller.NO_OP_CALLBACK);
127 }
128
129 }
130
131 public String getModelId() {
132 return modelId;
133 }
134
135 public void setModelId(String modelId) {
136 this.modelId = modelId;
137 }
138
139
140
141
142
143
144
145 @Override
146 public boolean beforeHide() {
147 return true;
148 }
149
150
151
152
153
154
155 @Override
156 public Controller getController() {
157 return super.getLayoutController();
158 }
159
160
161
162
163
164
165 @Override
166 public String getName() {
167 return viewName;
168 }
169
170 public void setName(String name) {
171 this.viewName = name;
172 }
173
174 public void setController(Controller controller) {
175 if (controller instanceof LayoutController) {
176 super.setLayoutController((LayoutController) controller);
177 } else {
178 throw new IllegalArgumentException("Configurable UI sections require a LayoutController, not a base MVC controller");
179 }
180 }
181
182
183
184
185 public void updateView() {
186 getController().requestModel(modelId, new ModelRequestCallback<DataModel>() {
187 @Override
188 public void onModelReady(DataModel m) {
189
190 SectionView.this.model = m;
191 updateWidgetData(m);
192 }
193
194
195 @Override
196 public void onRequestFail(Throwable cause) {
197 Window.alert("Failed to get model");
198 }
199 });
200
201 }
202
203
204
205
206
207 public void updateView(DataModel m) {
208 this.model = m;
209 updateWidgetData(m);
210 }
211
212
213
214
215 public Widget asWidget() {
216 return this.getLayout();
217 }
218
219
220
221
222 @Override
223 public String collectHistory(String historyStack) {
224 return null;
225 }
226
227
228
229
230 @Override
231 public void onHistoryEvent(String historyStack) {
232
233 }
234
235
236
237
238 @Override
239 public void collectBreadcrumbNames(List<String> names) {
240 names.add(this.getName());
241 }
242
243
244
245
246
247 public void addView(View view) {
248 views.add(view);
249 addWidget(view.asWidget());
250 }
251
252 public DataModel getModel() {
253 return model;
254 }
255
256 public void updateMetadata(ModelDefinition modelDefinition) {
257 updateMetadata(modelDefinition, this);
258 }
259
260 private void updateMetadata(ModelDefinition modelDefinition, Section topSection) {
261 for (Section section : topSection.getSections()) {
262 updateMetadata(modelDefinition, section);
263 }
264 for (FieldDescriptor field : topSection.getFields()) {
265 Metadata newMetadata = modelDefinition.getMetadata(field.getFieldKey());
266 if (newMetadata != null) {
267 field.setMetadata(newMetadata);
268 }
269 }
270 }
271
272 @Override
273 public String toString() {
274 return viewName;
275 }
276
277 public boolean isExportButtonActive() {
278 return false;
279 }
280
281 @Override
282 public void showExport(boolean show) {
283
284
285 }
286 }