01: package net.xoetrope.samples.advanced;
02:
03: import java.awt.Component;
04: import java.awt.event.MouseEvent;
05:
06: import net.xoetrope.awt.XButton;
07: import net.xoetrope.awt.XEdit;
08: import net.xoetrope.xui.XPage;
09: import net.xoetrope.xui.XResourceManager;
10: import net.xoetrope.xui.helper.BuddyHelper;
11: import net.xoetrope.xui.style.XStyleFactory;
12: import net.xoetrope.xui.validation.XValidationExceptionHandler;
13: import net.xoetrope.xui.validation.XValidationFactory;
14: import net.xoetrope.xui.validation.XValidator;
15:
16: public class Validation extends XPage implements
17: XValidationExceptionHandler {
18: XButton validateButton;
19: BuddyHelper buddy;
20: XEdit txtFirstname, txtSurname, txtAge;
21: private boolean pageValidation = false;
22: private String validationText = "";
23:
24: public Validation() {
25: setExceptionHandler(this );
26: buddy = new BuddyHelper((XStyleFactory) componentFactory);
27:
28: try {
29: setValidationFactory(new XValidationFactory(
30: XResourceManager.getBufferedReader(
31: "samplevalidate.xml", null)));
32: } catch (Exception ex) {
33: ex.printStackTrace();
34: }
35:
36: txtFirstname = (XEdit) buddy.addComponent(XPage.EDIT, 20, 50,
37: 130, 25, "Firstname", "", null);
38: txtSurname = (XEdit) buddy.addComponent(XPage.EDIT, 20, 80,
39: 130, 25, "Surname", "", null);
40: txtAge = (XEdit) buddy.addComponent(XPage.EDIT, 20, 95, 130,
41: 110, 355, 25, "Age", "", "Years", null);
42: validateButton = (XButton) componentFactory.addComponent(
43: XPage.BUTTON, 10, 180, 130, 25, "Validate");
44: addMouseHandler(validateButton, "validate");
45: mapValidations();
46: }
47:
48: public void mapValidations() {
49: addValidation(txtFirstname, "firstname");
50: addValidation(txtSurname, "surname");
51: addValidation(txtAge, "age");
52: }
53:
54: public void validate() {
55: MouseEvent evt = (MouseEvent) getCurrentEvent();
56: if ((evt != null) && (evt.getID() == evt.MOUSE_CLICKED)) {
57: pageValidation = true;
58: int ret = checkValidations();
59: pageValidation = false;
60: if (ret > XValidator.LEVEL_WARNING) {
61: showMessage("Input error", validationText);
62: validationText = "";
63: }
64: }
65:
66: }
67:
68: public boolean handleException(Component comp, Exception ex,
69: XValidator validator) {
70: String msg = validator.getMessage();
71: System.out.println(msg);
72: if (!pageValidation)
73: showMessage("Input error", msg);
74: else if (validationText.length() > 0)
75: validationText += "\n";
76: validationText += msg;
77: return true;
78: }
79:
80: }
|