View Javadoc

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          //not used
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          //no name for the view so that breadcrumbs do not extra link
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                  //moving from PREVIEW to MANAGE page
85                  //no dialog if user is going to the Manage Rule page
86                  if (viewChangingTo.name().equals(ProgramRequirementsViews.MANAGE.name()))
87                  {
88                      okToChange.exec(true);
89                      return;
90                  }
91  
92                  //moving from MANAGE to PREVIEW page
93                  //no dialog if user clicks on the 'Save' or cancel button on the Manage Rule page
94                  if (getCurrentView() instanceof ProgramRequirementsManageView) {
95                      okToChange.exec(true);
96                      return;
97                  }
98  
99                  //moving from MANAGE to other page
100                 //TODO show dialog if user clicks on a menu from Manage Rules page                
101 
102                 //moving from other page to PREVIEW page
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                 //moving from PREVIEW to other page (rules have NOT changed)
115                 //user is moving to another program section and no changes were made to the rules so allow it to happen
116                 if (!((SectionView) getCurrentView()).isDirty()) {
117                     okToChange.exec(true);
118                     return;
119                 }
120 
121                 //moving from PREVIEW to other page (rules have changed)
122                 //user is moving to another program section and rules have been changed, user needs to either save rules or abandon changes before proceeding
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                                 //preview.revertRuleChanges();     //TODO put back if we will NOT reset rules every time user comes to PREVIEW page above...
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 }