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