1 | |
package org.kuali.student.lum.lu.ui.course.client.requirements; |
2 | |
|
3 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.BasicLayout; |
4 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
5 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
6 | |
import org.kuali.student.common.ui.client.mvc.Controller; |
7 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
8 | |
import org.kuali.student.common.ui.client.mvc.ModelProvider; |
9 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
10 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations; |
11 | |
import org.kuali.student.common.ui.client.widgets.dialog.ButtonMessageDialog; |
12 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup; |
13 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.YesNoCancelGroup; |
14 | |
|
15 | |
import com.google.gwt.core.client.GWT; |
16 | |
|
17 | 0 | public class CourseRequirementsViewController extends BasicLayout { |
18 | |
|
19 | 0 | public enum CourseRequirementsViews { |
20 | 0 | PREVIEW, |
21 | 0 | MANAGE |
22 | |
} |
23 | |
|
24 | |
public static final String COURSE_RULES_MODEL_ID = "courseRulesModelId"; |
25 | |
public static final String COURSE_PROPOSAL_MODEL = "courseProposalModel"; |
26 | |
|
27 | |
private CourseRequirementsSummaryView preview; |
28 | |
|
29 | |
public CourseRequirementsViewController(Controller controller, String name, Enum<?> viewType, boolean isReadOnly, boolean showSaveButtons) { |
30 | 0 | super(CourseRequirementsViewController.class.getName()); |
31 | 0 | super.setController(controller); |
32 | 0 | super.setName(name); |
33 | 0 | super.setViewEnum(viewType); |
34 | 0 | super.setDefaultModelId(COURSE_RULES_MODEL_ID); |
35 | 0 | super.setParentController(controller); |
36 | |
|
37 | 0 | this.setDefaultView(CourseRequirementsViews.PREVIEW); |
38 | |
|
39 | |
|
40 | 0 | super.registerModel(COURSE_RULES_MODEL_ID, new ModelProvider<DataModel>() { |
41 | |
@Override |
42 | |
public void requestModel(final ModelRequestCallback<DataModel> callback) { |
43 | 0 | callback.onModelReady(new DataModel()); |
44 | 0 | } |
45 | |
}); |
46 | |
|
47 | |
|
48 | 0 | preview = GWT.create(CourseRequirementsSummaryView.class); |
49 | 0 | preview.init( |
50 | |
this, |
51 | |
CourseRequirementsViews.PREVIEW, |
52 | |
(isReadOnly ? "Course Requirements" : ""), |
53 | |
COURSE_PROPOSAL_MODEL, |
54 | |
(controller instanceof HasRequirements ? ((HasRequirements) controller).getReqDataModel() |
55 | |
: new CourseRequirementsDataModel(this)), |
56 | |
isReadOnly, showSaveButtons); |
57 | 0 | super.addView(preview); |
58 | |
|
59 | 0 | if (!isReadOnly) { |
60 | 0 | CourseRequirementsManageView manageView = GWT.create(CourseRequirementsManageView.class); |
61 | 0 | manageView.init(this, CourseRequirementsViews.MANAGE, "Add and Combine Rules", COURSE_RULES_MODEL_ID); |
62 | 0 | super.addView(manageView); |
63 | |
} |
64 | 0 | } |
65 | |
|
66 | |
@Override |
67 | |
public void updateModel() { |
68 | 0 | preview.updateModel(); |
69 | 0 | } |
70 | |
|
71 | |
@Override |
72 | |
public void beforeViewChange(final Enum<?> viewChangingTo, final Callback<Boolean> okToChange) { |
73 | |
|
74 | 0 | if (getCurrentView() == null) { |
75 | 0 | okToChange.exec(true); |
76 | 0 | return; |
77 | |
} |
78 | |
|
79 | 0 | super.beforeViewChange(viewChangingTo, new Callback<Boolean>() { |
80 | |
|
81 | |
@Override |
82 | |
public void exec(Boolean result) { |
83 | |
|
84 | |
|
85 | 0 | if (getCurrentView() instanceof CourseRequirementsManageView) { |
86 | 0 | okToChange.exec(true); |
87 | 0 | return; |
88 | |
} |
89 | |
|
90 | |
|
91 | 0 | if (viewChangingTo.name().equals(CourseRequirementsViewController.CourseRequirementsViews.MANAGE.name())) |
92 | |
{ |
93 | |
|
94 | 0 | okToChange.exec(true); |
95 | 0 | return; |
96 | |
} |
97 | |
|
98 | |
|
99 | 0 | if (!((SectionView) getCurrentView()).isDirty()) { |
100 | 0 | okToChange.exec(true); |
101 | 0 | return; |
102 | |
} |
103 | |
|
104 | |
|
105 | 0 | ButtonGroup<ButtonEnumerations.YesNoCancelEnum> buttonGroup = new YesNoCancelGroup(); |
106 | 0 | final ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum> dialog = |
107 | |
new ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum>("Warning", "You may have unsaved changes. Save changes?", buttonGroup); |
108 | 0 | buttonGroup.addCallback(new Callback<ButtonEnumerations.YesNoCancelEnum>() { |
109 | |
|
110 | |
@Override |
111 | |
public void exec(ButtonEnumerations.YesNoCancelEnum result) { |
112 | 0 | switch (result) { |
113 | |
case YES: |
114 | 0 | dialog.hide(); |
115 | 0 | preview.storeRules(true, new Callback<Boolean>() { |
116 | |
@Override |
117 | |
public void exec(Boolean result) { |
118 | 0 | okToChange.exec(result); |
119 | 0 | } |
120 | |
}); |
121 | 0 | break; |
122 | |
case NO: |
123 | 0 | dialog.hide(); |
124 | 0 | preview.revertRuleChanges(); |
125 | 0 | okToChange.exec(true); |
126 | 0 | break; |
127 | |
case CANCEL: |
128 | 0 | okToChange.exec(false); |
129 | 0 | dialog.hide(); |
130 | |
break; |
131 | |
} |
132 | 0 | } |
133 | |
}); |
134 | 0 | dialog.show(); |
135 | 0 | } |
136 | |
}); |
137 | 0 | } |
138 | |
|
139 | |
@Override |
140 | |
public void beforeShow(final Callback<Boolean> onReadyCallback){ |
141 | 0 | showDefaultView(onReadyCallback); |
142 | 0 | } |
143 | |
|
144 | |
public void storeRules(Callback<Boolean> callback){ |
145 | 0 | preview.storeRules(true, callback); |
146 | 0 | } |
147 | |
} |