1 package org.kuali.student.lum.program.client.widgets;
2
3 import java.util.List;
4
5 import org.kuali.student.common.ui.client.application.Application;
6 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
7 import org.kuali.student.common.ui.client.mvc.Callback;
8 import org.kuali.student.common.ui.client.mvc.DataModel;
9 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
10 import org.kuali.student.common.ui.client.widgets.KSButton;
11 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
12 import org.kuali.student.common.ui.client.widgets.notification.KSNotification;
13 import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
14 import org.kuali.student.common.ui.client.widgets.KSLightBox;
15 import org.kuali.student.common.validation.dto.ValidationResultInfo;
16 import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel;
17 import org.kuali.student.lum.common.client.widgets.AppLocations;
18 import org.kuali.student.lum.program.client.ProgramConstants;
19 import org.kuali.student.lum.program.client.ProgramMsgConstants;
20 import org.kuali.student.lum.program.client.ProgramStatus;
21 import org.kuali.student.lum.program.client.events.AfterSaveEvent;
22 import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
23 import org.kuali.student.lum.program.client.events.StateChangeEvent;
24
25 import com.google.gwt.event.dom.client.ClickEvent;
26 import com.google.gwt.event.dom.client.ClickHandler;
27 import com.google.gwt.event.shared.HandlerManager;
28 import com.google.gwt.user.client.ui.Anchor;
29 import com.google.gwt.user.client.ui.Composite;
30 import com.google.gwt.user.client.ui.FlowPanel;
31 import com.google.gwt.user.client.ui.HasVerticalAlignment;
32 import com.google.gwt.user.client.ui.HorizontalPanel;
33 import com.google.gwt.user.client.ui.Widget;
34
35
36
37
38 public class SummaryActionPanel extends Composite {
39
40 private final HorizontalPanel content = new HorizontalPanel();
41
42 private final KSButton approveButton = new KSButton(getLabel(ProgramMsgConstants.BUTTON_APPROVE));
43
44 private final KSButton activateButton = new KSButton(getLabel(ProgramMsgConstants.BUTTON_ACTIVATE), ButtonStyle.SECONDARY);
45
46 private final Anchor exitAnchor = new Anchor(getLabel(ProgramMsgConstants.LINK_BACKCURRICULUM));
47
48 private final KSLightBox activateDialog = new KSLightBox();
49 private Section activateSection;
50
51 private DataModel dataModel;
52
53 private HandlerManager eventBus;
54
55 public SummaryActionPanel(Section activateSection, HandlerManager eventBus) {
56 initWidget(content);
57 this.activateSection = activateSection;
58 this.eventBus = eventBus;
59 buildLayout();
60 setStyles();
61 bind();
62 }
63
64 private void bind() {
65 eventBus.addHandler(AfterSaveEvent.TYPE, new AfterSaveEvent.Handler() {
66 @Override
67 public void onEvent(AfterSaveEvent event) {
68 dataModel = event.getModel();
69 processStatus();
70 }
71 });
72 eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
73 @Override
74 public void onEvent(ModelLoadedEvent event) {
75 dataModel = event.getModel();
76 processStatus();
77 }
78 });
79 approveButton.addClickHandler(new ClickHandler() {
80 @Override
81 public void onClick(ClickEvent event) {
82 processButtonClick(ProgramStatus.APPROVED);
83 }
84 });
85 activateButton.addClickHandler(new ClickHandler() {
86 @Override
87 public void onClick(ClickEvent event) {
88 processActivateClick();
89 }
90 });
91 exitAnchor.addClickHandler(new ClickHandler() {
92 @Override
93 public void onClick(ClickEvent event) {
94 HistoryManager.navigate(AppLocations.Locations.CURRICULUM_MANAGEMENT.getLocation());
95 }
96 });
97 }
98
99 private void processStatus() {
100 processStatus(ProgramStatus.of(dataModel));
101 }
102
103 private void processButtonClick(ProgramStatus status) {
104 eventBus.fireEvent(new StateChangeEvent(status));
105 }
106
107 private void processActivateClick() {
108 String versionFromId = dataModel.get(ProgramConstants.VERSION_FROM_ID);
109 if (versionFromId != null) {
110 activateSection.updateWidgetData(dataModel);
111 activateDialog.show();
112 } else {
113 processButtonClick(ProgramStatus.ACTIVE);
114 }
115 }
116
117 private void processStatus(ProgramStatus programStatus) {
118 if (programStatus == ProgramStatus.DRAFT) {
119 enableButtons(true, false);
120 } else if (programStatus == ProgramStatus.APPROVED) {
121 enableButtons(false, true);
122 } else if (programStatus == ProgramStatus.ACTIVE) {
123 enableButtons(false, false);
124 } else if (programStatus == ProgramStatus.SUPERSEDED) {
125 content.setVisible(false);
126 }
127 }
128
129 private void setStyles() {
130 content.addStyleName("programActionPanel");
131 content.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
132 }
133
134 private void buildLayout() {
135 content.add(approveButton);
136 content.add(activateButton);
137 content.add(exitAnchor);
138
139 buildActivateDialog();
140 }
141
142 private void buildActivateDialog() {
143 FlowPanel panel = new FlowPanel();
144
145 panel.add((Widget) activateSection);
146
147 KSButton activate = new KSButton(getLabel(ProgramMsgConstants.BUTTON_ACTIVATE), new ClickHandler() {
148 public void onClick(ClickEvent event) {
149 activateSection.updateModel(dataModel);
150 dataModel.validate(new Callback<List<ValidationResultInfo>>(){
151 @Override
152 public void exec(List<ValidationResultInfo> results) {
153
154 if (ErrorLevel.OK.equals(activateSection.processValidationResults(results))) {
155
156 processButtonClick(ProgramStatus.ACTIVE);
157 activateDialog.hide();
158 } else {
159 KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000));
160 }
161
162 }
163 });
164 }
165 });
166 activateDialog.addButton(activate);
167
168 KSButton cancel = new KSButton(getLabel(ProgramMsgConstants.COMMON_CANCEL), ButtonStyle.ANCHOR_LARGE_CENTERED, new ClickHandler() {
169 public void onClick(ClickEvent event) {
170 activateDialog.hide();
171 }
172 });
173 activateDialog.addButton(cancel);
174
175 activateDialog.setWidget(panel);
176 }
177
178 private void enableButtons(boolean enableApprove, boolean enableActivate) {
179 approveButton.setEnabled(enableApprove);
180 activateButton.setEnabled(enableActivate);
181 }
182
183 protected String getLabel(String messageKey) {
184 return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey);
185 }
186 }