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; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.LinkedHashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.Map.Entry; |
23 | |
|
24 | |
import org.kuali.student.common.ui.client.application.Application; |
25 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.MenuSectionController; |
26 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.TabMenuController; |
27 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.ViewLayoutController; |
28 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
29 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
30 | |
import org.kuali.student.common.ui.client.event.ActionEvent; |
31 | |
import org.kuali.student.common.ui.client.event.SaveActionEvent; |
32 | |
import org.kuali.student.common.ui.client.event.SectionUpdateEvent; |
33 | |
import org.kuali.student.common.ui.client.event.SectionUpdateHandler; |
34 | |
import org.kuali.student.common.ui.client.event.ValidateRequestEvent; |
35 | |
import org.kuali.student.common.ui.client.event.ValidateRequestHandler; |
36 | |
import org.kuali.student.common.ui.client.mvc.ActionCompleteCallback; |
37 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
38 | |
import org.kuali.student.common.ui.client.mvc.Controller; |
39 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
40 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
41 | |
import org.kuali.student.common.ui.client.mvc.View; |
42 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
43 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
44 | |
import org.kuali.student.common.ui.client.widgets.KSLightBox; |
45 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement; |
46 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
47 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
48 | |
|
49 | |
import com.google.gwt.core.client.GWT; |
50 | |
import com.google.gwt.event.dom.client.ClickEvent; |
51 | |
import com.google.gwt.event.dom.client.ClickHandler; |
52 | |
import com.google.gwt.user.client.ui.FlowPanel; |
53 | |
import com.google.gwt.user.client.ui.Widget; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | public abstract class LayoutController extends Controller implements ViewLayoutController, View { |
64 | |
|
65 | 0 | protected Map<Enum<?>, View> viewMap = new LinkedHashMap<Enum<?>, View>(); |
66 | 0 | protected Map<String, Enum<?>> viewEnumMap = new HashMap<String, Enum<?>>(); |
67 | |
protected Enum<?> defaultView; |
68 | |
|
69 | |
protected String name; |
70 | |
protected Enum<?> viewType; |
71 | |
|
72 | |
protected View startPopupView; |
73 | |
protected KSLightBox startViewWindow; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public LayoutController(){ |
80 | 0 | super(); |
81 | |
|
82 | 0 | addApplicationEventHandler(SectionUpdateEvent.TYPE, new SectionUpdateHandler(){ |
83 | |
|
84 | |
@Override |
85 | |
public void onSectionUpdate(final SectionUpdateEvent event) { |
86 | 0 | LayoutController.this.requestModel(new ModelRequestCallback<DataModel>(){ |
87 | |
|
88 | |
@Override |
89 | |
public void onRequestFail(Throwable cause) { |
90 | 0 | GWT.log("Unable to retrieve model for section update", cause); |
91 | 0 | } |
92 | |
|
93 | |
@Override |
94 | |
public void onModelReady(DataModel model) { |
95 | 0 | event.getSection().updateModel(model); |
96 | 0 | event.getSection().updateWidgetData(model); |
97 | |
|
98 | 0 | } |
99 | |
}); |
100 | |
|
101 | 0 | } |
102 | |
}); |
103 | |
|
104 | 0 | addApplicationEventHandler(ValidateRequestEvent.TYPE, new ValidateRequestHandler() { |
105 | |
|
106 | |
@Override |
107 | |
public void onValidateRequest(final ValidateRequestEvent event) { |
108 | 0 | FieldDescriptor originatingField = event.getFieldDescriptor(); |
109 | 0 | String modelId = null; |
110 | 0 | if (originatingField != null) { |
111 | 0 | modelId = originatingField.getModelId(); |
112 | |
} |
113 | 0 | if (modelId == null) { |
114 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
115 | |
@Override |
116 | |
public void onModelReady(DataModel model) { |
117 | 0 | validate(model, event); |
118 | 0 | } |
119 | |
|
120 | |
@Override |
121 | |
public void onRequestFail(Throwable cause) { |
122 | 0 | GWT.log("Unable to retrieve model for validation", cause); |
123 | 0 | } |
124 | |
|
125 | |
}); |
126 | |
} else { |
127 | 0 | requestModel(modelId, new ModelRequestCallback<DataModel>() { |
128 | |
@Override |
129 | |
public void onModelReady(DataModel model) { |
130 | 0 | validate(model, event); |
131 | 0 | } |
132 | |
|
133 | |
@Override |
134 | |
public void onRequestFail(Throwable cause) { |
135 | 0 | GWT.log("Unable to retrieve model for validation", cause); |
136 | 0 | } |
137 | |
|
138 | |
}); |
139 | |
} |
140 | 0 | } |
141 | |
|
142 | |
}); |
143 | 0 | } |
144 | |
|
145 | |
private void validate(DataModel model, final ValidateRequestEvent event) { |
146 | 0 | if(event.validateSingleField()){ |
147 | 0 | model.validateField(event.getFieldDescriptor(), new Callback<List<ValidationResultInfo>>() { |
148 | |
@Override |
149 | |
public void exec(List<ValidationResultInfo> result) { |
150 | 0 | if(event.getFieldDescriptor() != null){ |
151 | |
|
152 | |
|
153 | 0 | FieldElement element = event.getFieldDescriptor().getFieldElement(); |
154 | 0 | if(element != null){ |
155 | 0 | element.clearValidationErrors(); |
156 | 0 | for(int i = 0; i < result.size(); i++){ |
157 | 0 | ValidationResultInfo vr = result.get(i); |
158 | 0 | if(vr.getElement().equals(event.getFieldDescriptor().getFieldKey()) |
159 | |
&& event.getFieldDescriptor().hasHadFocus()){ |
160 | 0 | element.processValidationResult(vr); |
161 | |
} |
162 | |
} |
163 | |
} |
164 | |
} |
165 | |
|
166 | 0 | } |
167 | |
}); |
168 | |
} |
169 | |
else{ |
170 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
171 | |
@Override |
172 | |
public void exec(List<ValidationResultInfo> result) { |
173 | 0 | isValid(result, false, true); |
174 | 0 | } |
175 | |
}); |
176 | |
} |
177 | 0 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public ErrorLevel checkForErrors(List<ValidationResultInfo> list){ |
185 | 0 | ErrorLevel errorLevel = ErrorLevel.OK; |
186 | |
|
187 | 0 | for(ValidationResultInfo vr: list){ |
188 | 0 | if(vr.getErrorLevel().getLevel() > errorLevel.getLevel()){ |
189 | 0 | errorLevel = vr.getErrorLevel(); |
190 | |
} |
191 | 0 | if(errorLevel.equals(ErrorLevel.ERROR)){ |
192 | 0 | break; |
193 | |
} |
194 | |
} |
195 | |
|
196 | 0 | return errorLevel; |
197 | |
|
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public static LayoutController findParentLayout(Widget w){ |
207 | 0 | LayoutController result = null; |
208 | |
while (true) { |
209 | 0 | if (w == null) { |
210 | 0 | break; |
211 | 0 | } else if (w instanceof HasLayoutController) { |
212 | 0 | result = ((HasLayoutController)w).getLayoutController(); |
213 | 0 | if (result != null) { |
214 | 0 | break; |
215 | |
} |
216 | 0 | } else if (w instanceof LayoutController) { |
217 | 0 | result = (LayoutController) w; |
218 | 0 | break; |
219 | |
} |
220 | 0 | w = w.getParent(); |
221 | |
|
222 | |
} |
223 | 0 | return result; |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public void addStartViewPopup(final View view){ |
230 | 0 | startPopupView = view; |
231 | 0 | if(startViewWindow == null){ |
232 | 0 | startViewWindow = new KSLightBox(); |
233 | |
} |
234 | |
|
235 | 0 | FlowPanel panel = new FlowPanel(); |
236 | 0 | panel.add(view.asWidget()); |
237 | 0 | KSButton save = new KSButton("Save",new ClickHandler(){ |
238 | |
public void onClick(ClickEvent event) { |
239 | 0 | view.updateModel(); |
240 | 0 | SaveActionEvent saveActionEvent = new SaveActionEvent(true); |
241 | |
|
242 | 0 | saveActionEvent.setActionCompleteCallback(new ActionCompleteCallback(){ |
243 | |
public void onActionComplete(ActionEvent action) { |
244 | 0 | startViewWindow.hide(); |
245 | 0 | } |
246 | |
}); |
247 | |
|
248 | |
|
249 | 0 | fireApplicationEvent(saveActionEvent); |
250 | 0 | } |
251 | |
}); |
252 | 0 | startViewWindow.addButton(save); |
253 | |
|
254 | 0 | KSButton cancel = new KSButton("Cancel", new ClickHandler(){ |
255 | |
public void onClick(ClickEvent event) { |
256 | 0 | startViewWindow.hide(); |
257 | 0 | } |
258 | |
}); |
259 | 0 | startViewWindow.addButton(cancel); |
260 | |
|
261 | 0 | if(view instanceof SectionView){ |
262 | 0 | ((SectionView) view).setController(this); |
263 | |
} |
264 | 0 | startViewWindow.setWidget(panel); |
265 | 0 | } |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
public boolean isStartViewShowing(){ |
271 | 0 | if(startViewWindow == null){ |
272 | 0 | return false; |
273 | |
} |
274 | 0 | return startViewWindow.isShowing(); |
275 | |
} |
276 | |
|
277 | |
public View getStartPopupView(){ |
278 | 0 | return startPopupView; |
279 | |
} |
280 | |
|
281 | |
public void showStartPopup(final Callback<Boolean> onReadyCallback){ |
282 | 0 | startPopupView.beforeShow(new Callback<Boolean>() { |
283 | |
@Override |
284 | |
public void exec(Boolean result) { |
285 | 0 | if (result) { |
286 | 0 | startViewWindow.show(); |
287 | |
} |
288 | 0 | onReadyCallback.exec(result); |
289 | 0 | } |
290 | |
}); |
291 | 0 | } |
292 | |
|
293 | |
public KSLightBox getStartPopup(){ |
294 | 0 | return startViewWindow; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public void addView(View view){ |
304 | 0 | viewMap.put(view.getViewEnum(), view); |
305 | 0 | viewEnumMap.put(view.getViewEnum().toString(), view.getViewEnum()); |
306 | 0 | if(view instanceof SectionView){ |
307 | 0 | ((SectionView) view).setController(this); |
308 | |
} |
309 | 0 | else if(view instanceof ToolView){ |
310 | 0 | ((ToolView) view).setController(this); |
311 | |
} |
312 | 0 | } |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
public <V extends Enum<?>> void setDefaultView(V viewType){ |
318 | 0 | this.defaultView = viewType; |
319 | 0 | } |
320 | |
|
321 | |
public Enum<?> getDefaultView(){ |
322 | 0 | return this.defaultView; |
323 | |
} |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
public abstract void updateModel(); |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public void updateModelFromView(Enum<?> viewType){ |
335 | 0 | View v = viewMap.get(viewType); |
336 | 0 | if(v != null){ |
337 | 0 | v.updateModel(); |
338 | |
} |
339 | 0 | } |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public void updateModelFromCurrentView(){ |
345 | 0 | if(this.getCurrentView() != null){ |
346 | 0 | this.getCurrentView().updateModel(); |
347 | |
} |
348 | 0 | } |
349 | |
|
350 | |
@Override |
351 | |
public <V extends Enum<?>> void getView(V viewType, Callback<View> callback) { |
352 | 0 | callback.exec(viewMap.get(viewType)); |
353 | 0 | } |
354 | |
|
355 | |
@Override |
356 | |
public Enum<?> getViewEnumValue(String enumValue) { |
357 | 0 | return viewEnumMap.get(enumValue); |
358 | |
} |
359 | |
|
360 | |
@Override |
361 | |
public void showDefaultView(final Callback<Boolean> onReadyCallback) { |
362 | 0 | HistoryManager.setLogNavigationHistory(false); |
363 | |
|
364 | 0 | if(defaultView != null){ |
365 | 0 | showView(defaultView, onReadyCallback); |
366 | |
} |
367 | 0 | else if(!viewMap.isEmpty()){ |
368 | 0 | if(defaultView == null){ |
369 | 0 | showView(viewMap.entrySet().iterator().next().getKey(), onReadyCallback); |
370 | |
} |
371 | |
} |
372 | |
|
373 | 0 | } |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
public void showFirstView(Callback<Boolean> onReadyCallback){ |
382 | 0 | HistoryManager.setLogNavigationHistory(false); |
383 | 0 | if(!viewMap.isEmpty()){ |
384 | 0 | showView(viewMap.entrySet().iterator().next().getKey(), onReadyCallback); |
385 | |
} |
386 | |
else{ |
387 | 0 | showDefaultView(onReadyCallback); |
388 | |
} |
389 | 0 | } |
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly){ |
400 | 0 | return isValid(validationResults, checkCurrentSectionOnly, true); |
401 | |
} |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly, boolean allFields){ |
411 | |
|
412 | 0 | boolean isValid = true; |
413 | |
|
414 | 0 | if (checkCurrentSectionOnly){ |
415 | |
|
416 | 0 | View v = getCurrentView(); |
417 | 0 | if(v instanceof Section){ |
418 | 0 | isValid = isValid(validationResults, (Section)v, allFields); |
419 | |
} |
420 | 0 | if(this.isStartViewShowing()){ |
421 | 0 | if(startPopupView instanceof Section){ |
422 | 0 | isValid = isValid(validationResults, ((Section) startPopupView), allFields) && isValid; |
423 | |
} |
424 | |
} |
425 | 0 | } else { |
426 | |
|
427 | 0 | String errorSections = ""; |
428 | 0 | StringBuilder errorSectionsbuffer = new StringBuilder(); |
429 | 0 | errorSectionsbuffer.append(errorSections); |
430 | 0 | for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { |
431 | 0 | View v = entry.getValue(); |
432 | 0 | if (v instanceof Section){ |
433 | 0 | if (!isValid(validationResults, (Section)v, allFields)){ |
434 | 0 | isValid = false; |
435 | 0 | errorSectionsbuffer.append(((SectionView)v).getName() + ", "); |
436 | |
} |
437 | |
} |
438 | 0 | } |
439 | 0 | if(this.isStartViewShowing()){ |
440 | 0 | if(startPopupView instanceof Section){ |
441 | 0 | isValid = isValid(validationResults, ((Section) startPopupView), allFields) && isValid; |
442 | |
} |
443 | |
} |
444 | 0 | errorSections = errorSectionsbuffer.toString(); |
445 | 0 | if (!errorSections.isEmpty()){ |
446 | 0 | errorSections = errorSections.substring(0, errorSections.length()-2); |
447 | |
|
448 | |
} |
449 | |
} |
450 | |
|
451 | 0 | return isValid; |
452 | |
} |
453 | |
|
454 | |
private boolean isValid(List<ValidationResultInfo> validationResults, Section section, boolean allFields){ |
455 | |
ErrorLevel status; |
456 | 0 | if(allFields){ |
457 | 0 | section.setFieldHasHadFocusFlags(true); |
458 | 0 | status = section.processValidationResults(validationResults); |
459 | |
} |
460 | |
else{ |
461 | 0 | status = section.processValidationResults(validationResults, false); |
462 | |
} |
463 | |
|
464 | 0 | return (status != ErrorLevel.ERROR); |
465 | |
} |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
protected void clearAllWarnings(){ |
471 | 0 | for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { |
472 | 0 | View v = entry.getValue(); |
473 | 0 | if (v instanceof Section){ |
474 | 0 | ((Section)v).clearValidationWarnings(); |
475 | |
} |
476 | 0 | } |
477 | 0 | } |
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
@Override |
489 | |
public void beforeViewChange(Enum<?> viewChangingTo, Callback<Boolean> okToChange) { |
490 | |
|
491 | 0 | if(this.getCurrentView() instanceof Controller){ |
492 | 0 | ((Controller)this.getCurrentView()).beforeViewChange(viewChangingTo, okToChange); |
493 | |
} |
494 | |
else{ |
495 | 0 | okToChange.exec(true); |
496 | |
} |
497 | 0 | if(this instanceof MenuSectionController){ |
498 | 0 | ((MenuSectionController)this).showExport(isExportButtonActive()); |
499 | 0 | } else if(this instanceof TabMenuController){ |
500 | 0 | ((TabMenuController)this).showExport(isExportButtonActive()); |
501 | |
} |
502 | |
|
503 | 0 | } |
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
protected void showWarnings(){ |
509 | 0 | clearAllWarnings(); |
510 | 0 | if (!Application.getApplicationContext().getValidationWarnings().isEmpty()){ |
511 | 0 | isValid(Application.getApplicationContext().getValidationWarnings(), true); |
512 | |
} |
513 | 0 | } |
514 | |
|
515 | |
@Override |
516 | |
public Widget asWidget() { |
517 | 0 | return this; |
518 | |
} |
519 | |
|
520 | |
@Override |
521 | |
public boolean beforeHide() { |
522 | 0 | return true; |
523 | |
} |
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
@Override |
531 | |
public void beforeShow(Callback<Boolean> onReadyCallback) { |
532 | 0 | onReadyCallback.exec(true); |
533 | 0 | } |
534 | |
|
535 | |
@Override |
536 | |
public Controller getController() { |
537 | 0 | return parentController; |
538 | |
} |
539 | |
|
540 | |
@Override |
541 | |
public String getName() { |
542 | 0 | if(name == null && viewType != null){ |
543 | 0 | return viewType.toString(); |
544 | |
} |
545 | |
else{ |
546 | 0 | return name; |
547 | |
} |
548 | |
} |
549 | |
|
550 | |
@Override |
551 | |
public Enum<?> getViewEnum() { |
552 | 0 | return viewType; |
553 | |
} |
554 | |
|
555 | |
public void setViewEnum(Enum<?> viewType){ |
556 | 0 | this.viewType= viewType; |
557 | 0 | } |
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
public void setName(String name){ |
565 | 0 | this.name = name; |
566 | 0 | } |
567 | |
|
568 | |
public void setController(Controller controller){ |
569 | 0 | parentController = controller; |
570 | 0 | } |
571 | |
|
572 | |
@Override |
573 | |
public void collectBreadcrumbNames(List<String> names) { |
574 | 0 | names.add(this.getName()); |
575 | 0 | if(this.getCurrentView() != null){ |
576 | 0 | this.getCurrentView().collectBreadcrumbNames(names); |
577 | |
} |
578 | 0 | } |
579 | |
|
580 | |
@Override |
581 | |
public void clear() { |
582 | |
|
583 | 0 | } |
584 | |
|
585 | |
public boolean isExportButtonActive() { |
586 | 0 | return false; |
587 | |
} |
588 | |
} |