View Javadoc

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.*;
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  
11  public class CourseRequirementsViewController extends BasicLayout {
12  
13      public enum CourseRequirementsViews {
14          PREVIEW,
15          MANAGE
16      }
17  
18      public static final String COURSE_RULES_MODEL_ID = "courseRulesModelId";
19      public static final String CLU_PROPOSAL_MODEL = "cluProposalModel";    
20  
21      private CourseRequirementsSummaryView preview;
22  
23      public CourseRequirementsViewController(Controller controller, String name, Enum<?> viewType, boolean isReadOnly) {
24  		super(CourseRequirementsViewController.class.getName());
25  		super.setController(controller);
26  		super.setName(name);
27  		super.setViewEnum(viewType);
28          super.setDefaultModelId(COURSE_RULES_MODEL_ID);
29          super.setParentController(controller);
30  
31  		this.setDefaultView(CourseRequirementsViews.PREVIEW);
32  
33          //not used
34          super.registerModel(COURSE_RULES_MODEL_ID, new ModelProvider<DataModel>() {
35              @Override
36              public void requestModel(final ModelRequestCallback<DataModel> callback) {
37                  callback.onModelReady(new DataModel());
38              }
39          });
40  
41          //no name for the view so that breadcrumbs do not extra link
42          preview = new CourseRequirementsSummaryView(this, CourseRequirementsViews.PREVIEW, (isReadOnly ? "Course Requirements" : ""), CLU_PROPOSAL_MODEL,
43                                                  new CourseRequirementsDataModel(this), isReadOnly);
44          super.addView(preview);
45  
46          if (!isReadOnly) {
47              CourseRequirementsManageView manageView = new CourseRequirementsManageView(this, CourseRequirementsViews.MANAGE,
48                                                  "Add and Combine Rules", COURSE_RULES_MODEL_ID);
49              super.addView(manageView);
50          }
51      }
52  
53      @Override
54      public void updateModel() {
55          preview.updateModel();    
56      }
57  
58      @Override
59      public void beforeViewChange(final Enum<?> viewChangingTo, final Callback<Boolean> okToChange) {
60  
61          if (getCurrentView() == null) {
62              okToChange.exec(true);
63              return;
64          }
65  
66          super.beforeViewChange(viewChangingTo, new Callback<Boolean>() {
67  
68              @Override
69              public void exec(Boolean result) {
70  
71                  //no dialog if user clicks on the 'Save' button on Manage Rule page
72                  if (getCurrentView() instanceof CourseRequirementsManageView) {
73                      okToChange.exec(true);
74                      return;
75                  }
76  
77                  //no dialog if user is going to the Manage Rule page
78                  if (viewChangingTo.name().equals(CourseRequirementsViewController.CourseRequirementsViews.MANAGE.name()))
79                  {
80                      //TODO show dialog if user clicks on a menu from Manage Rules page
81                      okToChange.exec(true);
82                      return;
83                  }
84  
85                  //user is moving to another course proposal section and no changes were made to the rules so allow it to happen
86                  if (!((SectionView) getCurrentView()).isDirty()) {
87                      okToChange.exec(true);
88                      return;
89                  }
90  
91                  //user is moving to another course proposal section and rules have been changed, user needs to either save rules or abondon changes before proceeding
92                  ButtonGroup<ButtonEnumerations.YesNoCancelEnum> buttonGroup = new YesNoCancelGroup();
93                  final ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum> dialog =
94                          new ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum>("Warning", "You may have unsaved changes.  Save changes?", buttonGroup);
95                  buttonGroup.addCallback(new Callback<ButtonEnumerations.YesNoCancelEnum>() {
96  
97                      @Override
98                      public void exec(ButtonEnumerations.YesNoCancelEnum result) {
99                          switch (result) {
100                             case YES:
101                                 dialog.hide();
102                                 preview.storeRules(true, new Callback<Boolean>() {
103                                     @Override
104                                     public void exec(Boolean result) {
105                                         okToChange.exec(result);
106                                     }
107                                 });
108                                 break;
109                             case NO:
110                                 dialog.hide();
111                                 preview.revertRuleChanges();                                
112                                 okToChange.exec(true);
113                                 break;
114                             case CANCEL:
115                                 okToChange.exec(false);
116                                 dialog.hide();
117                                 break;
118                         }
119                     }
120                 });
121                 dialog.show();
122             }
123         });
124     }
125     
126     @Override
127 	public void beforeShow(final Callback<Boolean> onReadyCallback){
128 		showDefaultView(onReadyCallback);
129 	}
130 }