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