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