001    package org.kuali.student.lum.program.client.requirements;
002    
003    import org.kuali.student.common.ui.client.application.Application;
004    import org.kuali.student.common.ui.client.configurable.mvc.layouts.BasicLayout;
005    import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
006    import org.kuali.student.common.ui.client.mvc.*;
007    import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations;
008    import org.kuali.student.common.ui.client.widgets.dialog.ButtonMessageDialog;
009    import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup;
010    import org.kuali.student.common.ui.client.widgets.field.layout.button.YesNoCancelGroup;
011    import org.kuali.student.lum.program.client.ProgramConstants;
012    import org.kuali.student.lum.program.client.ProgramMsgConstants;
013    import org.kuali.student.lum.program.client.widgets.EditableHeader;
014    
015    import com.google.gwt.event.shared.HandlerManager;
016    
017    public class ProgramRequirementsViewController extends BasicLayout {
018    
019        public enum ProgramRequirementsViews {
020            PREVIEW,
021            MANAGE
022        }
023    
024        public static final String PROGRAM_RULES_MODEL_ID = "programRulesModelId";
025        private ProgramRequirementsSummaryView preview;
026        boolean reloadFlag;
027    
028        public ProgramRequirementsViewController(Controller controller, HandlerManager eventBus, String name, Enum<?> viewType, boolean isReadOnly, EditableHeader header, boolean reloadFlag){
029            this(controller, eventBus, name, viewType, isReadOnly, header);
030            this.reloadFlag = reloadFlag;
031        }
032    
033        public ProgramRequirementsViewController(Controller controller, HandlerManager eventBus, String name, Enum<?> viewType, boolean isReadOnly, EditableHeader header) {
034            super(ProgramRequirementsViewController.class.getName());
035            super.setController(controller);
036            super.setName(name);
037            super.setViewEnum(viewType);
038            super.setDefaultModelId(PROGRAM_RULES_MODEL_ID);
039            super.setParentController(controller);
040    
041            this.setDefaultView(ProgramRequirementsViews.PREVIEW);
042    
043            //not used
044            super.registerModel(PROGRAM_RULES_MODEL_ID, new ModelProvider<DataModel>() {
045                @Override
046                public void requestModel(final ModelRequestCallback<DataModel> callback) {
047                    callback.onModelReady(new DataModel());
048                }
049            });
050    
051            //no name for the view so that breadcrumbs do not extra link
052            String previewTitle = getLabel(ProgramMsgConstants.PROGRAM_MENU_SECTIONS_REQUIREMENTS);
053            if (isReadOnly && (header != null)) {
054                preview = new ProgramRequirementsSummaryView(this, eventBus, ProgramRequirementsViews.PREVIEW, "", ProgramConstants.PROGRAM_MODEL_ID, isReadOnly, header);                                                            
055            } else {
056                preview = new ProgramRequirementsSummaryView(this, eventBus, ProgramRequirementsViews.PREVIEW, (isReadOnly ? previewTitle : ""), ProgramConstants.PROGRAM_MODEL_ID, isReadOnly);
057            }
058            super.addView(preview);
059    
060            if (!isReadOnly) {
061                ProgramRequirementsManageView manageView = new ProgramRequirementsManageView(this, ProgramRequirementsViews.MANAGE, "Add and Combine Rules", PROGRAM_RULES_MODEL_ID);
062                super.addView(manageView);
063            }
064        }
065    
066        @Override
067        public void updateModel() {
068            preview.updateModel();
069        }
070    
071        @Override
072        public void beforeViewChange(final Enum<?> viewChangingTo, final Callback<Boolean> okToChange) {
073    
074            if (getCurrentView() == null) {
075                okToChange.exec(true);
076                return;
077            }
078    
079            super.beforeViewChange(viewChangingTo, new Callback<Boolean>() {
080    
081                @Override
082                public void exec(Boolean result) {
083    
084                    //moving from PREVIEW to MANAGE page
085                    //no dialog if user is going to the Manage Rule page
086                    if (viewChangingTo.name().equals(ProgramRequirementsViews.MANAGE.name()))
087                    {
088                        okToChange.exec(true);
089                        return;
090                    }
091    
092                    //moving from MANAGE to PREVIEW page
093                    //no dialog if user clicks on the 'Save' or cancel button on the Manage Rule page
094                    if (getCurrentView() instanceof ProgramRequirementsManageView) {
095                        okToChange.exec(true);
096                        return;
097                    }
098    
099                    //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    }