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.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  public class CourseRequirementsViewController extends BasicLayout {
18  
19      public enum CourseRequirementsViews {
20          PREVIEW,
21          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  		super(CourseRequirementsViewController.class.getName());
31  		super.setController(controller);
32  		super.setName(name);
33  		super.setViewEnum(viewType);
34          super.setDefaultModelId(COURSE_RULES_MODEL_ID);
35          super.setParentController(controller);
36  
37  		this.setDefaultView(CourseRequirementsViews.PREVIEW);
38  
39          //not used
40          super.registerModel(COURSE_RULES_MODEL_ID, new ModelProvider<DataModel>() {
41              @Override
42              public void requestModel(final ModelRequestCallback<DataModel> callback) {
43                  callback.onModelReady(new DataModel());
44              }
45          });
46  
47          //no name for the view so that breadcrumbs do not extra link
48          preview = GWT.create(CourseRequirementsSummaryView.class);
49          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          super.addView(preview);
58  
59          if (!isReadOnly) {
60              CourseRequirementsManageView manageView = GWT.create(CourseRequirementsManageView.class);
61              manageView.init(this, CourseRequirementsViews.MANAGE, "Add and Combine Rules", COURSE_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                  //no dialog if user clicks on the 'Save' button on Manage Rule page
85                  if (getCurrentView() instanceof CourseRequirementsManageView) {
86                      okToChange.exec(true);
87                      return;
88                  }
89  
90                  //no dialog if user is going to the Manage Rule page
91                  if (viewChangingTo.name().equals(CourseRequirementsViewController.CourseRequirementsViews.MANAGE.name()))
92                  {
93                      //TODO show dialog if user clicks on a menu from Manage Rules page
94                      okToChange.exec(true);
95                      return;
96                  }
97  
98                  //user is moving to another course proposal section and no changes were made to the rules so allow it to happen
99                  if (!((SectionView) getCurrentView()).isDirty()) {
100                     okToChange.exec(true);
101                     return;
102                 }
103 
104                 //user is moving to another course proposal section and rules have been changed, user needs to either save rules or abondon changes before proceeding
105                 ButtonGroup<ButtonEnumerations.YesNoCancelEnum> buttonGroup = new YesNoCancelGroup();
106                 final ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum> dialog =
107                         new ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum>("Warning", "You may have unsaved changes.  Save changes?", buttonGroup);
108                 buttonGroup.addCallback(new Callback<ButtonEnumerations.YesNoCancelEnum>() {
109 
110                     @Override
111                     public void exec(ButtonEnumerations.YesNoCancelEnum result) {
112                         switch (result) {
113                             case YES:
114                                 dialog.hide();
115                                 preview.storeRules(true, new Callback<Boolean>() {
116                                     @Override
117                                     public void exec(Boolean result) {
118                                         okToChange.exec(result);
119                                     }
120                                 });
121                                 break;
122                             case NO:
123                                 dialog.hide();
124                                 preview.revertRuleChanges();                                
125                                 okToChange.exec(true);
126                                 break;
127                             case CANCEL:
128                                 okToChange.exec(false);
129                                 dialog.hide();
130                                 break;
131                         }
132                     }
133                 });
134                 dialog.show();
135             }
136         });
137     }
138     
139     @Override
140 	public void beforeShow(final Callback<Boolean> onReadyCallback){
141 		showDefaultView(onReadyCallback);
142 	}
143     
144     public void storeRules(Callback<Boolean> callback){
145     	preview.storeRules(true, callback);
146     }
147 }