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