1 package org.kuali.student.lum.program.client.widgets;
2
3 import java.util.Date;
4
5 import org.kuali.student.common.assembly.data.Metadata;
6 import org.kuali.student.common.assembly.data.QueryPath;
7 import org.kuali.student.common.ui.client.application.ViewContext;
8 import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
9 import org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding;
10 import org.kuali.student.common.ui.client.mvc.DataModel;
11 import org.kuali.student.common.ui.client.mvc.HasDataValue;
12 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
13 import org.kuali.student.lum.common.client.widgets.AppLocations;
14 import org.kuali.student.lum.program.client.ProgramConstants;
15 import org.kuali.student.lum.program.client.ProgramUtils;
16 import org.kuali.student.lum.program.client.events.AfterSaveEvent;
17 import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
18 import org.kuali.student.lum.program.client.major.MajorController;
19 import org.kuali.student.lum.program.client.properties.ProgramProperties;
20
21 import com.google.gwt.event.dom.client.ClickEvent;
22 import com.google.gwt.event.dom.client.ClickHandler;
23 import com.google.gwt.event.shared.HandlerManager;
24 import com.google.gwt.user.client.ui.Anchor;
25 import com.google.gwt.user.client.ui.Composite;
26 import com.google.gwt.user.client.ui.HorizontalPanel;
27 import com.google.gwt.user.client.ui.Label;
28 import com.google.gwt.user.client.ui.SimplePanel;
29 import com.google.gwt.user.client.ui.VerticalPanel;
30 import com.google.gwt.user.client.ui.Widget;
31
32
33
34
35 public class ProgramSideBar extends Composite {
36
37 private final VerticalPanel content = new VerticalPanel();
38
39 private State state = State.VIEW;
40
41 private Type type;
42
43 private Label versionLabel;
44 private Anchor viewVersion;
45 private Label historyLabel = new Label(ProgramProperties.get().sideBar_history());
46 private Label lastUpdatedDate = new Label();
47 private SimplePanel scheduledReviewDate = new SimplePanel();
48 private Label lastReviewDate = new Label();
49 private final HandlerManager eventBus;
50
51 private ViewContext viewContext;
52
53 private final SideBarDialogManager dialogManager;
54
55 public ProgramSideBar(HandlerManager eventBus, Type type) {
56 this.eventBus = eventBus;
57 this.type = type;
58 dialogManager = new SideBarDialogManager(eventBus);
59 initWidget(content);
60 setStyles();
61 buildLayout();
62 bind();
63 }
64
65 public void initialize(MajorController controller) {
66 DataModel model = controller.getProgramModel();
67 dialogManager.configureView(model.getDefinition(), controller);
68 viewContext = controller.getViewContext();
69 updateFields(model);
70 }
71
72 private void bind() {
73 eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
74 @Override
75 public void onEvent(ModelLoadedEvent event) {
76 updateFields(event.getModel());
77 }
78 });
79 eventBus.addHandler(AfterSaveEvent.TYPE, new AfterSaveEvent.Handler() {
80 @Override
81 public void onEvent(AfterSaveEvent event) {
82 DataModel model = event.getModel();
83 dialogManager.configureView(model.getDefinition(), event.getController());
84 updateFields(event.getModel());
85 }
86 });
87 }
88
89 private void updateFields(DataModel model) {
90 String programType = model.get(ProgramConstants.TYPE);
91
92
93 boolean doUpdate = (type == ProgramSideBar.Type.CORE && ProgramConstants.CORE_LU_TYPE_ID.equals(programType)) ||
94 (type == ProgramSideBar.Type.CREDENTIAL && programType.startsWith(ProgramConstants.CRED_LU_TYPE_PREFIX)) ||
95 (type == ProgramSideBar.Type.MAJOR && ProgramConstants.MAJOR_LU_TYPE_ID.equals(programType));
96
97 if (doUpdate){
98 setVersion((Long)model.get(ProgramConstants.VERSION), versionLabel);
99 setDate((Date) model.get(ProgramConstants.LAST_UPDATED_DATE), lastUpdatedDate);
100 lastReviewDate.setText((String) model.get(ProgramConstants.LAST_REVIEW_DATE));
101 setWidget(ProgramConstants.SCHEDULED_REVIEW_DATE, scheduledReviewDate, model);
102 }
103 }
104
105 private void setDate(Date updatedDate, Label lastUpdatedDate) {
106 if (updatedDate != null) {
107 lastUpdatedDate.setText(ProgramUtils.df.format(updatedDate));
108 } else {
109 lastUpdatedDate.setText("");
110 }
111 }
112
113 private void setVersion(Long version, Label versionaLabel){
114 if (version != null){
115 viewVersion.setVisible(version >= 1);
116 versionLabel.setText(ProgramProperties.get().sideBar_version(String.valueOf(version)));
117 } else {
118 versionLabel.setText(ProgramProperties.get().sideBar_version(""));
119 viewVersion.setVisible(false);
120 }
121 }
122
123 private void setWidget(String path, SimplePanel container, DataModel model) {
124 Metadata mData = model.getMetadata(QueryPath.parse(path));
125
126
127 if (null == mData && ProgramConstants.SCHEDULED_REVIEW_DATE.equals(path)) {
128 return;
129 }
130 Widget widget = DefaultWidgetFactory.getInstance().getReadOnlyWidget(model.getMetadata(QueryPath.parse(path)));
131 HasDataValueBinding.INSTANCE.setWidgetValue((HasDataValue) widget, model, path);
132 container.setWidget(widget);
133 }
134
135
136 private void buildLayout() {
137 content.clear();
138 Label history = new Label(ProgramProperties.get().sideBar_history());
139 history.addStyleName("KS-Program-History");
140 content.add(history);
141
142 content.add(createVersionPanel());
143 content.add(createDatePanel(ProgramProperties.get().sideBar_programLastUpdated(), lastUpdatedDate, false));
144 if (type == Type.MAJOR) {
145 content.add(createDatePanel(ProgramProperties.get().sideBar_scheduledReviewDate(), scheduledReviewDate, true));
146 content.add(createDatePanel(ProgramProperties.get().sideBar_lastReviewDate(), lastReviewDate, true));
147 }
148 content.add(createVersionHistoryPanel());
149 }
150
151 private Widget createDatePanel(String title, Widget widget, boolean showEdit) {
152 VerticalPanel verticalPanel = new VerticalPanel();
153 HorizontalPanel datePanel = new HorizontalPanel();
154 datePanel.addStyleName("datePanel");
155 datePanel.add(widget);
156 if (state == State.EDIT && showEdit) {
157 Anchor edit = new Anchor(ProgramProperties.get().common_edit());
158 edit.addClickHandler(new ClickHandler() {
159
160 @Override
161 public void onClick(ClickEvent event) {
162 dialogManager.show();
163 }
164 });
165 datePanel.add(edit);
166 }
167 verticalPanel.add(new Label(title));
168 verticalPanel.add(datePanel);
169 return verticalPanel;
170 }
171
172 private Widget createHistoryPanel(){
173 VerticalPanel verticalPanel = new VerticalPanel();
174 versionLabel = new Label(ProgramProperties.get().sideBar_version(""));
175 verticalPanel.add(versionLabel);
176 return verticalPanel;
177 }
178
179 private Widget createVersionPanel(){
180 VerticalPanel verticalPanel = new VerticalPanel();
181 versionLabel = new Label(ProgramProperties.get().sideBar_version(""));
182 verticalPanel.add(versionLabel);
183 return verticalPanel;
184 }
185
186 private Widget createVersionHistoryPanel(){
187 VerticalPanel verticalPanel = new VerticalPanel();
188 viewVersion = new Anchor(ProgramProperties.get().sideBar_viewHistory());
189 viewVersion.addClickHandler(new ClickHandler(){
190 @Override
191 public void onClick(ClickEvent event) {
192 switch (type){
193 case MAJOR: HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM_VERSIONS.getLocation()); break;
194 case CORE: HistoryManager.navigate(AppLocations.Locations.VIEW_CORE_VERSIONS.getLocation()); break;
195 case CREDENTIAL: HistoryManager.navigate(AppLocations.Locations.VIEW_BACC_VERSIONS.getLocation()); break;
196 }
197
198
199 }
200
201 });
202
203
204 verticalPanel.add(viewVersion);
205
206 return verticalPanel;
207 }
208
209 public void setState(State state) {
210 this.state = state;
211 buildLayout();
212 }
213
214 private void setStyles() {
215 content.addStyleName("sideBar");
216 content.addStyleName("KS-Program-History-Sidebar");
217 historyLabel.addStyleName("history");
218 }
219
220 public static enum State {
221 EDIT,
222 VIEW
223 }
224
225 public static enum Type {
226 CREDENTIAL,
227 CORE,
228 MAJOR
229 }
230 }