1 package org.kuali.student.lum.program.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.*;
6 import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations;
7 import org.kuali.student.common.ui.client.widgets.dialog.ButtonMessageDialog;
8 import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup;
9 import org.kuali.student.common.ui.client.widgets.field.layout.button.YesNoCancelGroup;
10 import org.kuali.student.lum.program.client.ProgramConstants;
11 import org.kuali.student.lum.program.client.properties.ProgramProperties;
12 import org.kuali.student.lum.program.client.widgets.EditableHeader;
13
14 import com.google.gwt.event.shared.HandlerManager;
15
16 public class ProgramRequirementsViewController extends BasicLayout {
17
18 public enum ProgramRequirementsViews {
19 PREVIEW,
20 MANAGE
21 }
22
23 public static final String PROGRAM_RULES_MODEL_ID = "programRulesModelId";
24 private ProgramRequirementsSummaryView preview;
25
26 public ProgramRequirementsViewController(Controller controller, HandlerManager eventBus, String name, Enum<?> viewType, boolean isReadOnly, EditableHeader header) {
27 super(ProgramRequirementsViewController.class.getName());
28 super.setController(controller);
29 super.setName(name);
30 super.setViewEnum(viewType);
31 super.setDefaultModelId(PROGRAM_RULES_MODEL_ID);
32 super.setParentController(controller);
33
34 this.setDefaultView(ProgramRequirementsViews.PREVIEW);
35
36
37 super.registerModel(PROGRAM_RULES_MODEL_ID, new ModelProvider<DataModel>() {
38 @Override
39 public void requestModel(final ModelRequestCallback<DataModel> callback) {
40 callback.onModelReady(new DataModel());
41 }
42 });
43
44
45 String previewTitle = ProgramProperties.get().program_menu_sections_requirements();
46 if (isReadOnly && (header != null)) {
47 preview = new ProgramRequirementsSummaryView(this, eventBus, ProgramRequirementsViews.PREVIEW, "", ProgramConstants.PROGRAM_MODEL_ID, isReadOnly, header);
48 } else {
49 preview = new ProgramRequirementsSummaryView(this, eventBus, ProgramRequirementsViews.PREVIEW, (isReadOnly ? previewTitle : ""), ProgramConstants.PROGRAM_MODEL_ID, isReadOnly);
50 }
51 super.addView(preview);
52
53 if (!isReadOnly) {
54 ProgramRequirementsManageView manageView = new ProgramRequirementsManageView(this, ProgramRequirementsViews.MANAGE, "Add and Combine Rules", PROGRAM_RULES_MODEL_ID);
55 super.addView(manageView);
56 }
57 }
58
59 @Override
60 public void updateModel() {
61 preview.updateModel();
62 }
63
64 @Override
65 public void beforeViewChange(final Enum<?> viewChangingTo, final Callback<Boolean> okToChange) {
66
67 if (getCurrentView() == null) {
68 okToChange.exec(true);
69 return;
70 }
71
72 super.beforeViewChange(viewChangingTo, new Callback<Boolean>() {
73
74 @Override
75 public void exec(Boolean result) {
76
77
78
79 if (viewChangingTo.name().equals(ProgramRequirementsViews.MANAGE.name()))
80 {
81 okToChange.exec(true);
82 return;
83 }
84
85
86
87 if (getCurrentView() instanceof ProgramRequirementsManageView) {
88 okToChange.exec(true);
89 return;
90 }
91
92
93
94
95
96 if (viewChangingTo.name().equals(ProgramRequirementsViews.PREVIEW.name())) {
97 preview.getRules().setupRules(ProgramRequirementsViewController.this, new Callback<Boolean>() {
98 @Override
99 public void exec(Boolean result) {
100 okToChange.exec(result);
101 return;
102 }
103 });
104 return;
105 }
106
107
108
109 if (!((SectionView) getCurrentView()).isDirty()) {
110 okToChange.exec(true);
111 return;
112 }
113
114
115
116 ButtonGroup<ButtonEnumerations.YesNoCancelEnum> buttonGroup = new YesNoCancelGroup();
117 final ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum> dialog =
118 new ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum>("Warning", "You may have unsaved changes. Save changes?", buttonGroup);
119 buttonGroup.addCallback(new Callback<ButtonEnumerations.YesNoCancelEnum>() {
120
121 @Override
122 public void exec(ButtonEnumerations.YesNoCancelEnum result) {
123 switch (result) {
124 case YES:
125 dialog.hide();
126 preview.storeRules(new Callback<Boolean>() {
127 @Override
128 public void exec(Boolean result) {
129 okToChange.exec(true);
130 }
131 });
132 break;
133 case NO:
134 dialog.hide();
135
136 okToChange.exec(true);
137 break;
138 case CANCEL:
139 okToChange.exec(false);
140 dialog.hide();
141 break;
142 }
143 }
144 });
145 dialog.show();
146 }
147 });
148 }
149
150 @Override
151 public void beforeShow(final Callback<Boolean> onReadyCallback) {
152 showDefaultView(onReadyCallback);
153 }
154
155 public ProgramRequirementsSummaryView getProgramRequirementsView() {
156 return preview;
157 }
158 }