1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.element;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang3.StringUtils;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22 import org.kuali.rice.krad.messages.MessageService;
23 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24 import org.kuali.rice.krad.uif.CssConstants;
25 import org.kuali.rice.krad.uif.UifConstants;
26 import org.kuali.rice.krad.uif.util.LifecycleElement;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39
40
41 @BeanTag(name = "stepProgressBar-bean", parent = "Uif-StepProgressBar")
42 public class StepProgressBar extends ProgressBar {
43 private static final long serialVersionUID = 1053164737424481519L;
44
45 private Map<String, String> steps;
46 private List<String> stepLabelClasses;
47 private List<String> accessibilityText;
48
49 private String currentStep;
50 private String completeStep;
51
52 private Integer verticalHeight;
53 private Integer verticalStepHeight;
54
55 public StepProgressBar() {
56 steps = new LinkedHashMap<String, String>();
57 accessibilityText = new ArrayList<String>();
58 stepLabelClasses = new ArrayList<String>();
59 }
60
61
62
63
64
65
66
67 @Override
68 public void performFinalize(Object model, LifecycleElement parent) {
69
70
71 if (this.getPercentComplete() != null && currentStep == null) {
72 calculateCurrentStepFromPercentage();
73 } else if (currentStep != null) {
74 this.setPercentComplete(null);
75 }
76
77 super.performFinalize(model, parent);
78
79 MessageService messageService = KRADServiceLocatorWeb.getMessageService();
80
81
82 String cssDimension = CssConstants.WIDTH;
83 if (this.isVertical()) {
84 cssDimension = CssConstants.HEIGHT;
85 this.addStyleClass(CssConstants.ProgressBar.VERTICAL_STEP_PROGRESS_BAR);
86 }
87
88 int totalSteps = steps.size();
89 if (totalSteps == 0) {
90 throw new RuntimeException(
91 "At least one step is required for a StepProgressBar: " + this.getId() + " with parent: " + parent
92 .getId());
93 }
94
95 boolean explicitlySetPercentages = CollectionUtils.isNotEmpty(getSegmentPercentages());
96 boolean explicitlySetClasses = CollectionUtils.isNotEmpty(this.getSegmentClasses());
97 if (explicitlySetPercentages && explicitlySetClasses && this.getSegmentClasses().size() != this
98 .getSegmentPercentages().size()) {
99 throw new RuntimeException(
100 "If segmentPercentages are set on a StepProgressBar type, and segmentClasses are also "
101 + "set, the lists MUST contain the same number of items");
102 }
103
104
105 populateProgressBarRenderingLists(totalSteps, cssDimension, explicitlySetPercentages, explicitlySetClasses);
106
107
108
109 if (this.isVertical() && getVerticalHeight() == null) {
110 setVerticalHeight(getSegmentSizes().size() * verticalStepHeight);
111 }
112
113
114 if (currentStep != null && currentStep.equals(completeStep)) {
115 this.addAriaAttribute(UifConstants.AriaAttributes.VALUE_NOW, Integer.toString(steps.size()));
116 this.addAriaAttribute(UifConstants.AriaAttributes.VALUE_TEXT, messageService.getMessageText(
117 "accessibility.progressBar.complete"));
118 }
119
120
121 this.addAriaAttribute(UifConstants.AriaAttributes.VALUE_MIN, "0");
122 this.addAriaAttribute(UifConstants.AriaAttributes.VALUE_MAX, Integer.toString(totalSteps));
123 }
124
125
126
127
128
129
130 private String calculateCurrentStepFromPercentage() {
131 if (getPercentComplete() == 0) {
132 return "";
133 } else if (getPercentComplete() == 100) {
134 return completeStep;
135 }
136
137 int size = steps.size();
138 double currentStep = Math.ceil(size * this.getPercentComplete());
139
140 String key = "";
141 Iterator<String> stepIterator = steps.keySet().iterator();
142 for (int step = 0; stepIterator.hasNext() && step <= currentStep; step++) {
143 key = stepIterator.next();
144 }
145
146 this.setPercentComplete(null);
147
148 return key;
149 }
150
151
152
153
154
155
156
157
158
159
160 public void populateProgressBarRenderingLists(int totalSteps, String cssDimension, boolean explicitlySetPercentages,
161 boolean explicitlySetClasses) {
162 MessageService messageService = KRADServiceLocatorWeb.getMessageService();
163
164 double percentage = Math.floor(100 / totalSteps);
165 double percentTotal = 0;
166 boolean currentStepFound = false;
167
168
169
170 if (StringUtils.isBlank(currentStep) || (!steps.containsKey(currentStep) && !currentStep.equals(
171 completeStep))) {
172 currentStepFound = true;
173 }
174
175 Iterator<String> stepIterator = steps.keySet().iterator();
176 for (int step = 0; stepIterator.hasNext() && step <= totalSteps; step++) {
177 String stepKey = stepIterator.next();
178
179 double stepPercentage;
180
181
182 if (explicitlySetPercentages) {
183 Integer percentageValue = getSegmentPercentages().get(step);
184 stepPercentage = percentageValue;
185 percentTotal += percentageValue;
186 } else {
187 stepPercentage = percentage;
188 percentTotal += percentage;
189 }
190
191
192
193 if (!stepIterator.hasNext() && percentTotal < 100) {
194 stepPercentage = (percentage + (100 - percentTotal));
195 percentTotal = 100;
196 }
197
198 String dimensionValue = stepPercentage + "%";
199
200
201 String cssClasses =
202 CssConstants.ProgressBar.PROGRESS_BAR + " " + CssConstants.ProgressBar.SUCCESS_PROGRESS_BAR;
203 String labelCssClasses = CssConstants.ProgressBar.STEP_LABEL + " " + CssConstants.ProgressBar.COMPLETE;
204 String srText = messageService.getMessageText("accessibility.progressBar.completeStep");
205
206
207
208 if (stepKey.equals(currentStep)) {
209 currentStepFound = true;
210 cssClasses = CssConstants.ProgressBar.PROGRESS_BAR + " " + CssConstants.ProgressBar.INFO_PROGRESS_BAR;
211 labelCssClasses = CssConstants.ProgressBar.STEP_LABEL + " " + CssConstants.ProgressBar.ACTIVE;
212 srText = messageService.getMessageText("accessibility.progressBar.currentStep");
213
214
215 this.addAriaAttribute(UifConstants.AriaAttributes.VALUE_NOW, Integer.toString(step));
216 this.addAriaAttribute(UifConstants.AriaAttributes.VALUE_TEXT, srText + steps.get(stepKey));
217 } else if (currentStepFound) {
218 cssClasses = CssConstants.ProgressBar.PROGRESS_BAR + " " + CssConstants.ProgressBar.EMPTY_PROGRESS_BAR;
219 labelCssClasses = CssConstants.ProgressBar.STEP_LABEL;
220 srText = messageService.getMessageText("accessibility.progressBar.futureStep");
221 }
222
223 this.getSegmentSizes().add(cssDimension + dimensionValue);
224
225
226 if (!explicitlySetClasses) {
227 this.getSegmentClasses().add(cssClasses);
228 }
229
230 this.getStepLabelClasses().add(labelCssClasses);
231 this.accessibilityText.add(srText);
232 }
233 }
234
235
236
237
238
239
240 @BeanTagAttribute(name = "steps", type = BeanTagAttribute.AttributeType.MAPVALUE)
241 public Map<String, String> getSteps() {
242 return steps;
243 }
244
245
246
247
248 public void setSteps(Map<String, String> steps) {
249 this.steps = steps;
250 }
251
252
253
254
255
256
257 public Collection<String> getStepCollection() {
258 return steps.values();
259 }
260
261
262
263
264
265
266 public List<String> getStepLabelClasses() {
267 return stepLabelClasses;
268 }
269
270
271
272
273
274
275
276 public List<String> getAccessibilityText() {
277 return accessibilityText;
278 }
279
280
281
282
283
284
285 @BeanTagAttribute(name = "currentStep")
286 public String getCurrentStep() {
287 return currentStep;
288 }
289
290
291
292
293 public void setCurrentStep(String currentStep) {
294 this.currentStep = currentStep;
295 }
296
297
298
299
300
301
302
303 @BeanTagAttribute(name = "completeStep")
304 public String getCompleteStep() {
305 return completeStep;
306 }
307
308
309
310
311 public void setCompleteStep(String completeStep) {
312 this.completeStep = completeStep;
313 }
314
315
316
317
318
319
320
321 @BeanTagAttribute(name = "verticalHeight")
322 public Integer getVerticalHeight() {
323 return verticalHeight;
324 }
325
326
327
328
329 public void setVerticalHeight(Integer verticalHeight) {
330 this.verticalHeight = verticalHeight;
331 }
332
333
334
335
336
337
338
339 @BeanTagAttribute(name = "verticalStepHeight")
340 public Integer getVerticalStepHeight() {
341 return verticalStepHeight;
342 }
343
344
345
346
347 public void setVerticalStepHeight(Integer verticalStepHeight) {
348 this.verticalStepHeight = verticalStepHeight;
349 }
350 }