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.layouts; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
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.configurable.mvc.LayoutController; |
25 | |
import org.kuali.student.common.ui.client.configurable.mvc.ToolView; |
26 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
27 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
28 | |
import org.kuali.student.common.ui.client.event.ActionEvent; |
29 | |
import org.kuali.student.common.ui.client.event.SaveActionEvent; |
30 | |
import org.kuali.student.common.ui.client.mvc.ActionCompleteCallback; |
31 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
32 | |
import org.kuali.student.common.ui.client.mvc.View; |
33 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
34 | |
import org.kuali.student.common.ui.client.widgets.containers.KSTitleContainerImpl; |
35 | |
import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData; |
36 | |
import org.kuali.student.common.ui.client.widgets.menus.impl.KSBlockMenuImpl; |
37 | |
import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanel; |
38 | |
import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanel.TabPosition; |
39 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
40 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo.ErrorLevel; |
41 | |
|
42 | |
import com.google.gwt.event.dom.client.ClickEvent; |
43 | |
import com.google.gwt.event.dom.client.ClickHandler; |
44 | |
import com.google.gwt.user.client.ui.Composite; |
45 | |
import com.google.gwt.user.client.ui.FlowPanel; |
46 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
47 | |
import com.google.gwt.user.client.ui.SimplePanel; |
48 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
49 | |
import com.google.gwt.user.client.ui.Widget; |
50 | |
|
51 | 0 | public class TabbedSectionLayout extends LayoutController implements ConfigurableLayout{ |
52 | |
|
53 | |
|
54 | 0 | private final Map<String, KSMenuItemData> menuHierarchyMap = new HashMap<String, KSMenuItemData>(); |
55 | |
|
56 | 0 | private Map<String, TabLayout> tabLayoutMap = new HashMap<String, TabLayout>(); |
57 | |
|
58 | 0 | private Map<String, String> sectionNameTabMap = new HashMap<String, String>(); |
59 | |
|
60 | 0 | private boolean loaded = false; |
61 | 0 | private final Map<String, Enum<?>> viewEnums = new HashMap<String, Enum<?>>(); |
62 | |
|
63 | 0 | Enum<?> defaultView = null; |
64 | |
|
65 | 0 | private KSTabPanel tabPanel = new KSTabPanel(); |
66 | 0 | private KSTitleContainerImpl container = new KSTitleContainerImpl(); |
67 | |
|
68 | 0 | private boolean updateableSection = true; |
69 | |
|
70 | 0 | private class TabLayout extends Composite{ |
71 | 0 | private FlowPanel layout = new FlowPanel(); |
72 | 0 | private SimplePanel content = new SimplePanel(); |
73 | 0 | private FlowPanel contentLayout = new FlowPanel(); |
74 | 0 | private KSBlockMenuImpl menu = new KSBlockMenuImpl(); |
75 | |
|
76 | 0 | private int currSectionIdx = 0; |
77 | 0 | protected final ArrayList<View> orderedSectionViews = new ArrayList<View>(); |
78 | 0 | private final HorizontalPanel sectionButtonPanel = new HorizontalPanel(); |
79 | 0 | private final ArrayList<KSMenuItemData> sectionMenuItems = new ArrayList<KSMenuItemData>(); |
80 | 0 | private final List<KSMenuItemData> topLevelMenuItems = new ArrayList<KSMenuItemData>(); |
81 | 0 | private boolean menuAdded = false; |
82 | 0 | private Enum<?> tabDefaultView = null; |
83 | |
|
84 | |
public Enum<?> getTabDefaultView() { |
85 | 0 | return tabDefaultView; |
86 | |
} |
87 | |
|
88 | |
public void setTabDefaultView(Enum<?> tabDefaultView) { |
89 | 0 | this.tabDefaultView = tabDefaultView; |
90 | 0 | } |
91 | |
|
92 | |
public HorizontalPanel getButtonPanel(){ |
93 | 0 | return this.sectionButtonPanel; |
94 | |
} |
95 | |
|
96 | |
public KSButton getNextButton() { |
97 | 0 | return nextButton; |
98 | |
} |
99 | |
|
100 | 0 | private KSButton nextButton = new KSButton("Save & Continue", new ClickHandler(){ |
101 | |
public void onClick(final ClickEvent event) { |
102 | |
|
103 | 0 | final SaveActionEvent saveActionEvent = new SaveActionEvent(); |
104 | 0 | saveActionEvent.setAcknowledgeRequired(false); |
105 | 0 | saveActionEvent.setActionCompleteCallback(new ActionCompleteCallback(){ |
106 | |
public void onActionComplete(ActionEvent action) { |
107 | 0 | int nextSectionIndex = currSectionIdx + 1; |
108 | |
|
109 | 0 | if (nextSectionIndex < sectionMenuItems.size()) { |
110 | 0 | sectionMenuItems.get(nextSectionIndex).getClickHandler().onClick(event); |
111 | |
} |
112 | 0 | } |
113 | |
}); |
114 | |
|
115 | 0 | fireApplicationEvent(saveActionEvent); |
116 | 0 | } |
117 | |
} |
118 | |
); |
119 | |
|
120 | 0 | public TabLayout(){ |
121 | 0 | if (updateableSection) { |
122 | 0 | sectionButtonPanel.add(nextButton); |
123 | |
} |
124 | 0 | menu.setTopLevelItems(topLevelMenuItems); |
125 | 0 | contentLayout.add(content); |
126 | 0 | contentLayout.add(sectionButtonPanel); |
127 | |
|
128 | 0 | layout.add(contentLayout); |
129 | 0 | this.initWidget(layout); |
130 | 0 | } |
131 | |
|
132 | |
public void init(){ |
133 | 0 | contentLayout.setStyleName("ks-page-content"); |
134 | |
|
135 | 0 | menu.setStyleName("ks-page-sub-navigation-container"); |
136 | 0 | menu.setTopLevelItems(topLevelMenuItems); |
137 | 0 | } |
138 | |
|
139 | |
public void setContent(Widget content){ |
140 | 0 | this.content.setWidget(content); |
141 | 0 | } |
142 | |
|
143 | |
public void addMenuItem(String[] hierarchy, final SectionView section) { |
144 | 0 | String path = ""; |
145 | 0 | StringBuilder pathBuffer= new StringBuilder(); |
146 | 0 | pathBuffer.append(path); |
147 | 0 | KSMenuItemData current = null; |
148 | 0 | for (int i=1; i<hierarchy.length; i++) { |
149 | |
|
150 | |
|
151 | 0 | if(hierarchy[i]==null){ |
152 | 0 | return; |
153 | |
} |
154 | 0 | pathBuffer.append("/"); |
155 | 0 | pathBuffer.append(hierarchy[i]); |
156 | 0 | KSMenuItemData item = menuHierarchyMap.get(pathBuffer.toString()); |
157 | 0 | if (item == null) { |
158 | 0 | item = new KSMenuItemData(hierarchy[i]); |
159 | 0 | if (current == null) { |
160 | 0 | topLevelMenuItems.add(item); |
161 | 0 | current = item; |
162 | |
} else { |
163 | 0 | current.addSubItem(item); |
164 | 0 | current = item; |
165 | |
} |
166 | 0 | menuHierarchyMap.put(pathBuffer.toString(), item); |
167 | |
} else { |
168 | 0 | current = item; |
169 | |
} |
170 | |
} |
171 | |
|
172 | 0 | final KSMenuItemData sectionItem = new KSMenuItemData(section.getName()); |
173 | 0 | current.addSubItem(sectionItem); |
174 | 0 | sectionMenuItems.add(sectionItem); |
175 | 0 | orderedSectionViews.add(section); |
176 | |
|
177 | 0 | sectionItem.setClickHandler(new ClickHandler() { |
178 | |
public void onClick(ClickEvent event) { |
179 | |
|
180 | 0 | int newMenuItemIdx = sectionMenuItems.indexOf(sectionItem); |
181 | 0 | if (currSectionIdx != newMenuItemIdx){ |
182 | 0 | currSectionIdx = newMenuItemIdx; |
183 | 0 | sectionButtonPanel.setVisible(true); |
184 | 0 | showView(section.getViewEnum(), NO_OP_CALLBACK); |
185 | |
} |
186 | 0 | } |
187 | |
}); |
188 | |
|
189 | 0 | if(!menuAdded){ |
190 | 0 | layout.insert(menu, 0); |
191 | 0 | menuAdded = true; |
192 | |
} |
193 | |
|
194 | 0 | if (tabDefaultView == null){ |
195 | 0 | tabDefaultView = section.getViewEnum(); |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
public void renderView(View view) { |
200 | 0 | content.setWidget((Widget)view); |
201 | 0 | if(menuAdded){ |
202 | 0 | if (currSectionIdx == sectionMenuItems.size() - 1){ |
203 | 0 | nextButton.setVisible(false); |
204 | |
} else { |
205 | 0 | nextButton.setVisible(true); |
206 | |
} |
207 | 0 | currSectionIdx = orderedSectionViews.indexOf(view); |
208 | 0 | if(currSectionIdx == -1){ |
209 | 0 | return; |
210 | |
} |
211 | 0 | sectionMenuItems.get(currSectionIdx).setSelected(true); |
212 | |
} |
213 | |
else{ |
214 | 0 | nextButton.setVisible(false); |
215 | |
} |
216 | 0 | } |
217 | |
|
218 | |
public void removeContent() { |
219 | 0 | content.clear(); |
220 | 0 | } |
221 | |
|
222 | |
public void addButton(KSButton button) { |
223 | 0 | sectionButtonPanel.add(button); |
224 | 0 | } |
225 | |
|
226 | |
public void clear() { |
227 | 0 | for (View view:orderedSectionViews){ |
228 | 0 | view.clear(); |
229 | |
} |
230 | |
|
231 | 0 | } |
232 | |
|
233 | |
public void updateModel() { |
234 | 0 | for(View sectionView : orderedSectionViews){ |
235 | 0 | sectionView.updateModel(); |
236 | |
} |
237 | |
|
238 | 0 | } |
239 | |
|
240 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
241 | 0 | showView(tabDefaultView, onReadyCallback); |
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
} |
246 | |
|
247 | |
private void init(){ |
248 | 0 | for(TabLayout layout: tabLayoutMap.values()){ |
249 | 0 | layout.init(); |
250 | |
} |
251 | 0 | } |
252 | |
|
253 | |
public TabbedSectionLayout(String controllerId){ |
254 | 0 | super(controllerId); |
255 | 0 | container.setContent(tabPanel); |
256 | 0 | container.setTitle("New Course Proposal"); |
257 | 0 | super.initWidget(container); |
258 | 0 | } |
259 | |
|
260 | |
public TabbedSectionLayout(String controllerId, KSTitleContainerImpl container){ |
261 | 0 | super(controllerId); |
262 | 0 | this.container.setContent(tabPanel); |
263 | 0 | this.container.setTitle(container.getTitle()); |
264 | 0 | this.container.setStatus(container.getStatus()); |
265 | 0 | this.container.setLinkText(container.getLinkText()); |
266 | 0 | super.initWidget(this.container); |
267 | 0 | } |
268 | |
|
269 | |
|
270 | |
public KSTitleContainerImpl getContainer(){ |
271 | 0 | return this.container; |
272 | |
} |
273 | |
|
274 | |
public void setContainer(KSTitleContainerImpl container){ |
275 | 0 | this.container=container; |
276 | 0 | } |
277 | |
|
278 | |
@Override |
279 | |
public <V extends Enum<?>> void getView(V viewType, Callback<View> callback) { |
280 | 0 | callback.exec(viewMap.get(viewType)); |
281 | 0 | } |
282 | |
|
283 | |
@Override |
284 | |
public Enum<?> getViewEnumValue(String enumValue) { |
285 | 0 | return viewEnums.get(enumValue); |
286 | |
} |
287 | |
|
288 | |
public boolean isUpdateableSection() { |
289 | 0 | return updateableSection; |
290 | |
} |
291 | |
|
292 | |
public void setUpdateableSection(boolean isUpdateable) { |
293 | 0 | this.updateableSection = isUpdateable; |
294 | 0 | } |
295 | |
|
296 | |
@Override |
297 | |
protected void hideView(View view) { |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | 0 | } |
303 | |
|
304 | |
@Override |
305 | |
protected void renderView(View view) { |
306 | 0 | String tabName = sectionNameTabMap.get(view.getName()); |
307 | 0 | if(!(tabPanel.getSelectedTabKey().equals(tabName))){ |
308 | 0 | tabPanel.selectTab(tabName); |
309 | |
} |
310 | 0 | TabLayout layout = tabLayoutMap.get(tabName); |
311 | |
|
312 | 0 | layout.renderView(view); |
313 | 0 | view.getName(); |
314 | 0 | } |
315 | |
|
316 | |
@Override |
317 | |
public void showDefaultView(final Callback<Boolean> onReadyCallback) { |
318 | 0 | if (!loaded){ |
319 | 0 | init(); |
320 | 0 | loaded = true; |
321 | |
} |
322 | |
|
323 | 0 | super.showDefaultView(onReadyCallback); |
324 | 0 | } |
325 | |
|
326 | |
@Override |
327 | |
public void addSection(String[] hierarchy, final SectionView section) { |
328 | 0 | viewEnums.put(section.getViewEnum().toString(), section.getViewEnum()); |
329 | 0 | String tabKey = hierarchy[0]; |
330 | |
|
331 | 0 | sectionNameTabMap.put(section.getName(), tabKey); |
332 | 0 | viewMap.put(section.getViewEnum(), section); |
333 | 0 | section.setController(this); |
334 | 0 | section.setLayoutController(this); |
335 | |
|
336 | |
final TabLayout layout; |
337 | 0 | if(!(tabPanel.hasTabKey(tabKey))){ |
338 | 0 | layout = new TabLayout(); |
339 | 0 | tabLayoutMap.put(tabKey, layout); |
340 | 0 | tabPanel.addTab(tabKey, tabKey, layout); |
341 | 0 | if(section != null){ |
342 | 0 | layout.setTabDefaultView(section.getViewEnum()); |
343 | |
} |
344 | |
|
345 | |
|
346 | 0 | tabPanel.addTabCustomCallback(tabKey, new Callback<String>(){ |
347 | |
|
348 | |
@Override |
349 | |
public void exec(String result) { |
350 | 0 | layout.beforeShow(NO_OP_CALLBACK); |
351 | 0 | } |
352 | |
|
353 | |
}); |
354 | |
} |
355 | |
else{ |
356 | 0 | layout = tabLayoutMap.get(tabKey); |
357 | |
} |
358 | |
|
359 | 0 | if(hierarchy.length > 1){ |
360 | 0 | layout.addMenuItem(hierarchy, section); |
361 | |
} |
362 | |
else{ |
363 | 0 | layout.renderView(section); |
364 | |
} |
365 | |
|
366 | 0 | if (defaultView == null){ |
367 | 0 | defaultView = section.getViewEnum(); |
368 | |
} |
369 | 0 | } |
370 | |
|
371 | |
@Override |
372 | |
public void addTool(final ToolView tool) { |
373 | 0 | viewEnums.put(tool.getViewEnum().toString(), tool.getViewEnum()); |
374 | 0 | String tabKey = tool.getName(); |
375 | |
|
376 | |
TabLayout layout; |
377 | 0 | if(!(tabPanel.hasTabKey(tabKey))){ |
378 | 0 | layout = new TabLayout(); |
379 | 0 | tabLayoutMap.put(tabKey, layout); |
380 | 0 | tabPanel.addTab(tabKey, tabKey, tool.getImage(), layout, TabPosition.RIGHT); |
381 | |
|
382 | 0 | tabPanel.addTabCustomCallback(tabKey, new Callback<String>(){ |
383 | |
|
384 | |
@Override |
385 | |
public void exec(String result) { |
386 | |
|
387 | 0 | showView(tool.getViewEnum(), NO_OP_CALLBACK); |
388 | 0 | } |
389 | |
|
390 | |
}); |
391 | |
} |
392 | |
else{ |
393 | 0 | layout = tabLayoutMap.get(tabKey); |
394 | |
} |
395 | |
|
396 | |
|
397 | 0 | sectionNameTabMap.put(tool.getName(), tabKey); |
398 | 0 | viewMap.put(tool.getViewEnum(), tool); |
399 | 0 | tool.setController(this); |
400 | |
|
401 | 0 | } |
402 | |
|
403 | |
public void addToolbar(Widget toolbar){ |
404 | 0 | this.container.setToolbar(toolbar); |
405 | 0 | } |
406 | |
|
407 | |
public void showStartSection(final Callback<Boolean> onReadyCallback){ |
408 | 0 | this.showStartPopup(onReadyCallback); |
409 | 0 | } |
410 | |
|
411 | |
public SectionView getStartSection(){ |
412 | 0 | if(startPopupView instanceof SectionView){ |
413 | 0 | return (SectionView)startPopupView; |
414 | |
} |
415 | |
else{ |
416 | 0 | return null; |
417 | |
} |
418 | |
|
419 | |
} |
420 | |
|
421 | |
@Override |
422 | |
public void addStartSection(final SectionView section){ |
423 | 0 | this.addStartViewPopup(section); |
424 | |
|
425 | 0 | HorizontalPanel buttonPanel = new HorizontalPanel(); |
426 | |
|
427 | 0 | VerticalPanel panel = new VerticalPanel(); |
428 | 0 | panel.add(section); |
429 | 0 | buttonPanel.add(new KSButton("Save",new ClickHandler(){ |
430 | |
public void onClick(ClickEvent event) { |
431 | 0 | section.updateModel(); |
432 | 0 | SaveActionEvent saveActionEvent = new SaveActionEvent(); |
433 | |
|
434 | 0 | saveActionEvent.setActionCompleteCallback(new ActionCompleteCallback(){ |
435 | |
public void onActionComplete(ActionEvent action) { |
436 | 0 | startViewWindow.hide(); |
437 | 0 | } |
438 | |
}); |
439 | |
|
440 | 0 | fireApplicationEvent(saveActionEvent); |
441 | 0 | } |
442 | |
})); |
443 | 0 | buttonPanel.add(new KSButton("Cancel", new ClickHandler(){ |
444 | |
public void onClick(ClickEvent event) { |
445 | 0 | startViewWindow.hide(); |
446 | 0 | } |
447 | |
})); |
448 | |
|
449 | 0 | panel.add(buttonPanel); |
450 | 0 | section.setController(this); |
451 | 0 | startViewWindow.setWidget(panel); |
452 | 0 | } |
453 | |
|
454 | |
public void addButton(String tabKey, KSButton button){ |
455 | 0 | TabLayout layout = tabLayoutMap.get(tabKey); |
456 | |
|
457 | 0 | if(layout != null){ |
458 | 0 | layout.addButton(button); |
459 | |
} |
460 | |
|
461 | 0 | } |
462 | |
|
463 | |
public HorizontalPanel getButtonPanel(String tabKey){ |
464 | 0 | TabLayout layout = tabLayoutMap.get(tabKey); |
465 | |
|
466 | 0 | if(layout != null){ |
467 | 0 | return layout.getButtonPanel(); |
468 | |
} |
469 | 0 | return null; |
470 | |
} |
471 | |
|
472 | |
public KSButton getNextButton(String tabKey) { |
473 | 0 | TabLayout layout = tabLayoutMap.get(tabKey); |
474 | |
|
475 | 0 | if (layout != null) { |
476 | 0 | return layout.getNextButton(); |
477 | |
} |
478 | 0 | return null; |
479 | |
} |
480 | |
|
481 | |
public void clear(){ |
482 | 0 | super.clear(); |
483 | 0 | for(TabLayout layout: tabLayoutMap.values()){ |
484 | 0 | layout.clear(); |
485 | |
} |
486 | |
|
487 | |
|
488 | 0 | } |
489 | |
|
490 | |
public void updateModel(){ |
491 | 0 | for(TabLayout layout: tabLayoutMap.values()){ |
492 | 0 | layout.updateModel(); |
493 | |
} |
494 | 0 | } |
495 | |
|
496 | |
@Override |
497 | |
public Class<? extends Enum<?>> getViewsEnum() { |
498 | |
|
499 | 0 | return null; |
500 | |
} |
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly){ |
511 | 0 | boolean isValid = true; |
512 | |
|
513 | 0 | if (checkCurrentSectionOnly){ |
514 | |
|
515 | 0 | if(this.isStartViewShowing()){ |
516 | 0 | isValid = isValid(validationResults, getStartSection()); |
517 | |
} else { |
518 | 0 | View v = getCurrentView(); |
519 | 0 | if(v instanceof Section){ |
520 | 0 | isValid = isValid(validationResults, (Section)v); |
521 | |
} |
522 | 0 | } |
523 | |
} else { |
524 | |
|
525 | 0 | container.clearMessages(); |
526 | 0 | String errorSections = ""; |
527 | 0 | StringBuilder errorSectionsbuffer = new StringBuilder(); |
528 | 0 | errorSectionsbuffer.append(errorSections); |
529 | 0 | for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { |
530 | 0 | View v = entry.getValue(); |
531 | 0 | if (v instanceof Section){ |
532 | 0 | if (!isValid(validationResults, (Section)v)){ |
533 | 0 | isValid = false; |
534 | 0 | errorSectionsbuffer.append(((SectionView)v).getName() + ", "); |
535 | |
|
536 | |
} |
537 | |
} |
538 | 0 | } |
539 | 0 | errorSections = errorSectionsbuffer.toString(); |
540 | 0 | if (!errorSections.isEmpty()){ |
541 | 0 | errorSections = errorSections.substring(0, errorSections.length()-2); |
542 | 0 | container.addMessage("Following section(s) has errors & must be corrected: " + errorSections); |
543 | |
} |
544 | |
} |
545 | |
|
546 | 0 | return isValid; |
547 | |
} |
548 | |
|
549 | |
private boolean isValid(List<ValidationResultInfo> validationResults, Section section){ |
550 | 0 | section.setFieldHasHadFocusFlags(true); |
551 | 0 | ErrorLevel status = section.processValidationResults(validationResults); |
552 | |
|
553 | 0 | return (status != ErrorLevel.ERROR); |
554 | |
} |
555 | |
} |