1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.field.layout.element; |
17 | |
|
18 | |
import org.kuali.student.common.ui.client.application.Application; |
19 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.ValidationMessagePanel; |
20 | |
import org.kuali.student.common.ui.client.widgets.HasInputWidget; |
21 | |
import org.kuali.student.common.ui.client.widgets.HasWatermark; |
22 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
23 | |
import org.kuali.student.common.ui.client.widgets.KSTitleDescPanel; |
24 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; |
25 | |
import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayoutComponent; |
26 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
27 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
28 | |
|
29 | |
import com.google.gwt.user.client.Element; |
30 | |
import com.google.gwt.user.client.ui.Composite; |
31 | |
import com.google.gwt.user.client.ui.FlowPanel; |
32 | |
import com.google.gwt.user.client.ui.HTMLPanel; |
33 | |
import com.google.gwt.user.client.ui.Panel; |
34 | |
import com.google.gwt.user.client.ui.Widget; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public class FieldElement extends Composite implements FieldLayoutComponent{ |
68 | |
|
69 | |
|
70 | 0 | private KSTitleDescPanel titlePanel = new KSTitleDescPanel(); |
71 | 0 | private FlowPanel layout = new FlowPanel(); |
72 | |
private FieldTitle fieldTitle; |
73 | 0 | private SpanPanel instructions = new SpanPanel(); |
74 | 0 | private SpanPanel constraints = new SpanPanel(); |
75 | 0 | private AbbrPanel required = new AbbrPanel("Required", "ks-form-module-elements-required", " * "); |
76 | 0 | private AbbrButton help = new AbbrButton(AbbrButtonType.HELP); |
77 | |
private Widget fieldWidget; |
78 | 0 | private SpanPanel widgetSpan = new SpanPanel(); |
79 | |
private String fieldHTMLId; |
80 | 0 | private String watermarkText = null; |
81 | 0 | public static enum LineNum{SINGLE, DOUBLE, TRIPLE} |
82 | |
|
83 | |
private ValidationMessagePanel validationPanel; |
84 | |
private String fieldKey; |
85 | 0 | private boolean labelShown = true; |
86 | |
|
87 | |
private Panel parentPanel; |
88 | |
private Element parentElement; |
89 | |
|
90 | 0 | private ErrorLevel status = ErrorLevel.OK; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void setValidationPanel(ValidationMessagePanel validationPanel) { |
99 | 0 | this.validationPanel = validationPanel; |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
public Panel getParentPanel() { |
104 | 0 | return parentPanel; |
105 | |
} |
106 | |
|
107 | |
public void setParentPanel(Panel parentPanel) { |
108 | 0 | this.parentPanel = parentPanel; |
109 | 0 | this.parentElement = parentPanel.getElement(); |
110 | 0 | } |
111 | |
|
112 | |
public void setParentElement(Element element){ |
113 | 0 | parentElement = element; |
114 | 0 | } |
115 | |
|
116 | |
private String fieldName; |
117 | |
private String instructionText; |
118 | |
|
119 | |
public String getInstructionText() { |
120 | 0 | return instructionText; |
121 | |
} |
122 | |
|
123 | 0 | public FieldElement(String title, Widget widget) { |
124 | 0 | generateLayout(title, title, null, null, null, widget); |
125 | |
|
126 | 0 | } |
127 | |
|
128 | 0 | public FieldElement(String key, String title, Widget widget){ |
129 | 0 | generateLayout(key, title, null, null, null, widget); |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | 0 | public FieldElement(String key, MessageKeyInfo info){ |
138 | 0 | init(key, info, null); |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | 0 | public FieldElement(String key, MessageKeyInfo info, Widget widget){ |
148 | 0 | init(key, info, widget); |
149 | |
|
150 | 0 | } |
151 | |
|
152 | |
private void init(String key, MessageKeyInfo info, Widget widget){ |
153 | 0 | String title = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), info.getId()); |
154 | |
|
155 | 0 | String help = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), |
156 | |
info.getId() + HELP_MESSAGE_KEY); |
157 | 0 | if(help.equals(info.getId() + HELP_MESSAGE_KEY)){ |
158 | 0 | help = null; |
159 | |
} |
160 | |
|
161 | 0 | String instructions = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), |
162 | |
info.getId() + INSTRUCT_MESSAGE_KEY); |
163 | 0 | if(instructions.equals(info.getId() + INSTRUCT_MESSAGE_KEY)){ |
164 | 0 | instructions = null; |
165 | |
} |
166 | |
|
167 | 0 | String constraints = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), |
168 | |
info.getId() + CONSTRAINT_MESSAGE_KEY); |
169 | 0 | if(constraints.equals(info.getId() + CONSTRAINT_MESSAGE_KEY)){ |
170 | 0 | constraints = null; |
171 | |
} |
172 | |
|
173 | 0 | watermarkText = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), |
174 | |
info.getId() + WATERMARK_MESSAGE_KEY); |
175 | 0 | if(watermarkText.equals(info.getId() + WATERMARK_MESSAGE_KEY)){ |
176 | 0 | watermarkText = null; |
177 | |
} |
178 | |
|
179 | 0 | generateLayout(key, title, help, instructions, constraints, widget); |
180 | 0 | } |
181 | |
|
182 | |
private void generateLayout(String key, String title, String helpText, String instructText, String constraintText, Widget widget){ |
183 | 0 | this.setKey(key); |
184 | 0 | fieldName = title; |
185 | |
|
186 | 0 | fieldHTMLId = HTMLPanel.createUniqueId(); |
187 | 0 | fieldTitle = new LabelPanel(title, fieldHTMLId); |
188 | |
|
189 | 0 | required.setVisible(false); |
190 | 0 | fieldTitle.add(required); |
191 | 0 | if(helpText != null){ |
192 | 0 | this.setHelp(helpText); |
193 | |
} |
194 | |
else{ |
195 | 0 | help.setVisible(false); |
196 | |
} |
197 | |
|
198 | 0 | fieldTitle.add(help); |
199 | 0 | layout.add(fieldTitle); |
200 | 0 | if(instructText != null){ |
201 | 0 | this.setInstructions(instructText); |
202 | |
} |
203 | |
else{ |
204 | 0 | instructions.setVisible(false); |
205 | |
} |
206 | |
|
207 | 0 | if(constraintText != null){ |
208 | 0 | this.setConstraintText(constraintText); |
209 | |
} |
210 | |
else{ |
211 | 0 | constraints.setVisible(false); |
212 | |
} |
213 | 0 | instructions.setStyleName("ks-form-module-elements-instruction"); |
214 | 0 | layout.add(instructions); |
215 | 0 | layout.add(widgetSpan); |
216 | 0 | if(widget != null){ |
217 | 0 | this.setWidget(widget); |
218 | |
} |
219 | 0 | constraints.setStyleName("ks-form-module-elements-help-text"); |
220 | 0 | layout.add(constraints); |
221 | |
|
222 | |
|
223 | 0 | initWidget(layout); |
224 | 0 | layout.addStyleName("ks-form-module-elements"); |
225 | 0 | layout.addStyleName("ks-form-module-single-line-margin"); |
226 | 0 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public void setWidget(Widget w){ |
234 | 0 | if(fieldWidget != null){ |
235 | 0 | widgetSpan.remove(fieldWidget); |
236 | |
} |
237 | 0 | fieldWidget = w; |
238 | |
|
239 | 0 | if(fieldWidget != null){ |
240 | |
|
241 | |
|
242 | 0 | if(fieldWidget instanceof HasInputWidget){ |
243 | 0 | Widget input = ((HasInputWidget)fieldWidget).getInputWidget(); |
244 | 0 | if(input != null){ |
245 | 0 | if(input instanceof HasWatermark && watermarkText != null){ |
246 | 0 | ((HasWatermark)input).setWatermarkText(watermarkText); |
247 | |
} |
248 | 0 | input.getElement().setAttribute("id", fieldHTMLId); |
249 | |
} |
250 | |
else{ |
251 | 0 | fieldWidget.getElement().setAttribute("id", fieldHTMLId); |
252 | |
} |
253 | 0 | } |
254 | |
else{ |
255 | 0 | if(fieldWidget instanceof HasWatermark && watermarkText != null){ |
256 | 0 | ((HasWatermark)fieldWidget).setWatermarkText(watermarkText); |
257 | |
} |
258 | 0 | fieldWidget.getElement().setAttribute("id", fieldHTMLId); |
259 | |
} |
260 | |
|
261 | 0 | widgetSpan.add(fieldWidget); |
262 | |
} |
263 | 0 | } |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public void setRequiredString(String requiredKey){ |
272 | 0 | String requiredText = Application.getApplicationContext().getMessage(requiredKey); |
273 | 0 | required.setText(requiredText); |
274 | 0 | required.setVisible(true); |
275 | 0 | } |
276 | |
|
277 | |
public void setRequiredString(String requiredKey, String style){ |
278 | 0 | String requiredText = Application.getApplicationContext().getMessage(requiredKey); |
279 | 0 | required.setText(requiredText); |
280 | 0 | required.setStyleName(style); |
281 | 0 | required.setVisible(true); |
282 | 0 | } |
283 | |
|
284 | |
public AbbrPanel getRequiredPanel(){ |
285 | 0 | return required; |
286 | |
} |
287 | |
|
288 | |
public void clearRequiredText(){ |
289 | 0 | required.setText(""); |
290 | 0 | } |
291 | |
|
292 | |
public Widget getFieldWidget(){ |
293 | 0 | return fieldWidget; |
294 | |
} |
295 | |
|
296 | |
public FlowPanel getFieldDetailsLayout(){ |
297 | 0 | FlowPanel div = new FlowPanel(); |
298 | 0 | div.add(fieldTitle); |
299 | 0 | div.add(instructions); |
300 | 0 | div.addStyleName("ks-form-module-elements"); |
301 | 0 | return div; |
302 | |
} |
303 | |
|
304 | |
public FlowPanel getFieldWidgetAreaLayout(){ |
305 | 0 | FlowPanel div = new FlowPanel(); |
306 | 0 | div.add(fieldWidget); |
307 | 0 | div.add(constraints); |
308 | 0 | div.addStyleName("ks-form-module-elements"); |
309 | 0 | return div; |
310 | |
} |
311 | |
|
312 | |
public boolean isRequired() { |
313 | 0 | return required.isVisible(); |
314 | |
} |
315 | |
|
316 | |
public void setRequired(boolean isRequired){ |
317 | 0 | required.setVisible(isRequired); |
318 | 0 | } |
319 | |
|
320 | |
public void setInstructions(String text){ |
321 | 0 | instructionText = text; |
322 | 0 | if(instructionText != null && !instructionText.trim().equals("")){ |
323 | 0 | instructions.setHTML(text); |
324 | 0 | instructions.setVisible(true); |
325 | |
} |
326 | 0 | } |
327 | |
|
328 | |
public void setConstraintText(String text){ |
329 | 0 | if(text != null && !text.trim().equals("")){ |
330 | 0 | constraints.setHTML(text); |
331 | 0 | constraints.setVisible(true); |
332 | |
} |
333 | 0 | } |
334 | |
|
335 | |
public void setHelp(final String html){ |
336 | 0 | if(html != null && !html.trim().equals("")){ |
337 | 0 | help.setVisible(true); |
338 | 0 | help.setHoverHTML(html); |
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
} |
349 | |
else{ |
350 | 0 | help.setVisible(false); |
351 | |
} |
352 | 0 | } |
353 | |
|
354 | |
public Widget getTitleWidget() { |
355 | 0 | return titlePanel.getTitleWidget(); |
356 | |
} |
357 | |
|
358 | |
public ValidationMessagePanel getValidationPanel() { |
359 | 0 | return validationPanel; |
360 | |
} |
361 | |
|
362 | |
public Panel getEncapsulatingPanel() { |
363 | 0 | return parentPanel; |
364 | |
} |
365 | |
|
366 | |
public String getFieldName() { |
367 | 0 | return fieldName; |
368 | |
} |
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
public void setErrorState(boolean error){ |
376 | 0 | if(error){ |
377 | 0 | fieldTitle.addStyleName("invalid"); |
378 | 0 | if(parentPanel != null){ |
379 | 0 | parentPanel.addStyleName("error"); |
380 | |
} |
381 | 0 | else if(parentElement != null){ |
382 | 0 | parentElement.setClassName("error"); |
383 | |
} |
384 | |
|
385 | 0 | setWarnState(false); |
386 | |
} |
387 | |
else{ |
388 | 0 | fieldTitle.removeStyleName("invalid"); |
389 | 0 | if(parentPanel != null){ |
390 | 0 | parentPanel.removeStyleName("error"); |
391 | |
} |
392 | 0 | else if(parentElement != null){ |
393 | 0 | parentElement.setClassName(""); |
394 | |
} |
395 | |
|
396 | 0 | setWarnState(validationPanel.hasWarnings()); |
397 | |
} |
398 | 0 | } |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
public void setWarnState(boolean warn){ |
406 | 0 | if(warn){ |
407 | |
|
408 | 0 | if(parentPanel != null){ |
409 | 0 | parentPanel.addStyleName("warning"); |
410 | |
} |
411 | 0 | else if(parentElement != null){ |
412 | 0 | parentElement.setClassName("warning"); |
413 | |
} |
414 | |
|
415 | |
} |
416 | |
else{ |
417 | |
|
418 | 0 | if(parentPanel != null){ |
419 | 0 | parentPanel.removeStyleName("warning"); |
420 | |
} |
421 | 0 | else if(parentElement != null){ |
422 | 0 | parentElement.setClassName(""); |
423 | |
} |
424 | |
} |
425 | 0 | } |
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
public ErrorLevel processValidationResult(ValidationResultInfo vr) { |
434 | |
|
435 | 0 | if(getFieldWidget() instanceof ValidationProcessable){ |
436 | 0 | if(((ValidationProcessable)getFieldWidget()).shouldProcessValidationResult(vr)){ |
437 | 0 | return ((ValidationProcessable)getFieldWidget()).processValidationResult(vr); |
438 | |
} |
439 | |
} |
440 | |
|
441 | 0 | status = ErrorLevel.OK; |
442 | |
|
443 | 0 | if(vr.getLevel() == ErrorLevel.ERROR){ |
444 | 0 | String message = Application.getApplicationContext().getUILabel("validation", vr.getMessage()); |
445 | 0 | this.addValidationErrorMessage(message); |
446 | |
|
447 | 0 | if(status.getLevel() < ErrorLevel.ERROR.getLevel()){ |
448 | 0 | status = vr.getLevel(); |
449 | |
} |
450 | 0 | } |
451 | 0 | else if(vr.getLevel() == ErrorLevel.WARN){ |
452 | 0 | String message = Application.getApplicationContext().getUILabel("validation", vr.getMessage()); |
453 | 0 | this.addValidationWarningMessage(message); |
454 | |
|
455 | 0 | if(status.getLevel() < ErrorLevel.WARN.getLevel()){ |
456 | 0 | status = vr.getLevel(); |
457 | |
} |
458 | |
} |
459 | |
else{ |
460 | |
|
461 | |
} |
462 | 0 | return status; |
463 | |
} |
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
public void addValidationErrorMessage(String text){ |
470 | 0 | if(validationPanel != null){ |
471 | |
KSLabel message; |
472 | 0 | if(fieldName != null && !fieldName.trim().equals("")){ |
473 | 0 | message = new KSLabel(fieldName + " - " + text); |
474 | |
} |
475 | |
else{ |
476 | 0 | message = new KSLabel(text); |
477 | |
} |
478 | 0 | message.setStyleName("ks-form-error-label"); |
479 | 0 | this.setErrorState(true); |
480 | 0 | this.validationPanel.addErrorMessage(message); |
481 | |
} |
482 | 0 | } |
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
public void addValidationWarningMessage(String text){ |
489 | 0 | if(validationPanel != null){ |
490 | 0 | SpanPanel message = new SpanPanel(); |
491 | 0 | if(fieldName != null && !fieldName.trim().equals("")){ |
492 | 0 | message.setHTML("<b> Warning </b> " + fieldName + " - " + text); |
493 | |
} |
494 | |
else{ |
495 | 0 | message.setHTML("<b> Warning </b> " + text); |
496 | |
} |
497 | 0 | message.setStyleName("ks-form-warn-label"); |
498 | |
|
499 | 0 | this.setWarnState((status != ErrorLevel.ERROR)); |
500 | 0 | this.validationPanel.addWarnMessage(message); |
501 | |
} |
502 | 0 | } |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public void clearValidationErrors(){ |
508 | 0 | this.setErrorState(false); |
509 | 0 | if(validationPanel != null){ |
510 | 0 | this.validationPanel.clearErrors(); |
511 | |
} |
512 | 0 | } |
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
public void clearValidationWarnings(){ |
518 | 0 | this.setWarnState(false); |
519 | 0 | if(validationPanel != null){ |
520 | 0 | this.validationPanel.clearWarnings(); |
521 | |
} |
522 | 0 | } |
523 | |
|
524 | |
@Override |
525 | |
public String getKey() { |
526 | 0 | return fieldKey; |
527 | |
} |
528 | |
|
529 | |
@Override |
530 | |
public void setKey(String key) { |
531 | 0 | if(key == null){ |
532 | |
|
533 | 0 | key = HTMLPanel.createUniqueId(); |
534 | |
} |
535 | 0 | this.fieldKey = key; |
536 | 0 | } |
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
public void setTitleDescLineHeight(LineNum margin) { |
544 | 0 | layout.removeStyleName("ks-form-module-single-line-margin"); |
545 | 0 | switch(margin){ |
546 | |
case TRIPLE: |
547 | 0 | if(firstLineExists() && secondLineExists()){ |
548 | 0 | layout.addStyleName("ks-form-module-single-line-margin"); |
549 | |
} |
550 | 0 | else if((firstLineExists() || secondLineExists())){ |
551 | 0 | layout.addStyleName("ks-form-module-double-line-margin"); |
552 | |
} |
553 | |
else{ |
554 | 0 | layout.addStyleName("ks-form-module-triple-line-margin"); |
555 | |
} |
556 | |
|
557 | 0 | break; |
558 | |
case DOUBLE: |
559 | 0 | if((firstLineExists() || secondLineExists())){ |
560 | 0 | layout.addStyleName("ks-form-module-single-line-margin"); |
561 | |
} |
562 | |
else{ |
563 | 0 | layout.addStyleName("ks-form-module-double-line-margin"); |
564 | |
} |
565 | 0 | break; |
566 | |
case SINGLE: |
567 | 0 | layout.addStyleName("ks-form-module-single-line-margin"); |
568 | |
break; |
569 | |
} |
570 | |
|
571 | 0 | } |
572 | |
|
573 | |
private boolean firstLineExists(){ |
574 | 0 | boolean exists = false; |
575 | 0 | if((fieldName != null && !fieldName.equals("")) || required.isVisible() || help.isVisible()){ |
576 | 0 | exists = true; |
577 | |
} |
578 | 0 | return exists; |
579 | |
} |
580 | |
|
581 | |
private boolean secondLineExists(){ |
582 | 0 | boolean exists = false; |
583 | 0 | if(instructions.isVisible()){ |
584 | 0 | exists = true; |
585 | |
} |
586 | 0 | return exists; |
587 | |
} |
588 | |
|
589 | |
|
590 | |
|
591 | |
|
592 | |
public void hideLabel() { |
593 | 0 | layout.removeStyleName("ks-form-module-double-line-margin"); |
594 | 0 | layout.removeStyleName("ks-form-module-triple-line-margin"); |
595 | 0 | layout.addStyleName("ks-form-module-single-line-margin"); |
596 | 0 | fieldTitle.setStyleName("accessibility-hidden"); |
597 | 0 | instructions.setVisible(false); |
598 | 0 | labelShown = false; |
599 | 0 | } |
600 | |
|
601 | |
public boolean isLabelShown(){ |
602 | 0 | return labelShown; |
603 | |
} |
604 | |
} |