001: package net.xoetrope.samples.controls;
002:
003: import java.awt.Component;
004: import java.awt.Frame;
005: import java.awt.event.MouseEvent;
006:
007: import net.xoetrope.awt.XButton;
008: import net.xoetrope.awt.XEdit;
009: import net.xoetrope.xui.XPage;
010: import net.xoetrope.xui.XResourceManager;
011: import net.xoetrope.xui.helper.BuddyHelper;
012: import net.xoetrope.xui.style.XStyleFactory;
013: import net.xoetrope.xui.validation.XValidationExceptionHandler;
014: import net.xoetrope.xui.validation.XValidationFactory;
015: import net.xoetrope.xui.validation.XValidator;
016:
017: /**
018: * <p>Title: Xui</p>
019: * <p>Description: </p>
020: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
021: * <p>Company: Xoetrope Ltd.</p>
022: * @author not attributable
023: * @version 1.0
024: */
025:
026: public class ValidationSample extends XPage implements
027: XValidationExceptionHandler {
028: XButton closeButton, validateButton;
029: Frame frame;
030: BuddyHelper buddy;
031: XEdit txtFirstname, txtSurname, txtAge;
032: private boolean pageValidation = false;
033: private String validationText = "";
034:
035: public ValidationSample() {
036: setExceptionHandler(this );
037: try {
038: setValidationFactory(new XValidationFactory(
039: XResourceManager.getBufferedReader(
040: "SampleValidate.xml", null)));
041: } catch (Exception ex) {
042: ex.printStackTrace();
043: }
044:
045: buddy = new BuddyHelper((XStyleFactory) componentFactory);
046:
047: frame = new Frame("Validation Sample");
048: frame.setLayout(null);
049: frame.setSize(640, 580);
050: String desc = "This example shows how validation can be associated with input controls. First we need to set the";
051: desc += " ValidationFactory with the name of the file which contains the validation we require. Then we call the";
052: desc += " addValidation function of XPage to associate the validation with the control. Validations can be inline";
053: desc += " on the focuslost of the control or can be done in bulk such as when you wish to move screen.";
054:
055: componentFactory.addComponent(XPage.LABEL, 10, 50, 530, 100,
056: desc);
057:
058: txtFirstname = (XEdit) buddy.addComponent(XPage.EDIT, 20, 250,
059: 130, 25, "Firstname", "Joe", null);
060: txtSurname = (XEdit) buddy.addComponent(XPage.EDIT, 20, 280,
061: 130, 25, "Surname", "Bloggs", null);
062: txtAge = (XEdit) buddy.addComponent(XPage.EDIT, 20, 95, 130,
063: 310, 355, 25, "Age", "29", "Years", null);
064: validateButton = (XButton) componentFactory.addComponent(
065: XPage.BUTTON, 10, 440, 130, 25, "Validate");
066: closeButton = (XButton) componentFactory.addComponent(
067: XPage.BUTTON, 10, 540, 130, 25, "Close");
068: setSize(frame.getSize());
069: mapEvents();
070: mapValidations();
071:
072: frame.add(this );
073: frame.setVisible(true);
074: frame.show();
075: }
076:
077: private void mapEvents() {
078: addMouseHandler(closeButton, "Close");
079: addMouseHandler(validateButton, "validate");
080: }
081:
082: public void mapValidations() {
083: addValidation(txtFirstname, "firstname");
084: addValidation(txtSurname, "surname");
085: addValidation(txtAge, "age");
086: }
087:
088: public static void main(String args[]) {
089: ValidationSample compSample = new ValidationSample();
090: }
091:
092: public void Close() {
093: if (wasMouseClicked()) {
094: frame.setVisible(false);
095: }
096: }
097:
098: public void validate() {
099: MouseEvent evt = (MouseEvent) getCurrentEvent();
100: if (evt.getID() == evt.MOUSE_CLICKED) {
101: pageValidation = true;
102: int ret = checkValidations();
103: pageValidation = false;
104: if (ret > XValidator.LEVEL_WARNING) {
105: showMessage("Input error", validationText);
106: validationText = "";
107: }
108: }
109:
110: }
111:
112: public boolean handleException(Component comp, Exception ex,
113: XValidator validator) {
114: String msg = validator.getMessage();
115: System.out.println(msg);
116: if (!pageValidation)
117: showMessage("Input error", msg);
118: else if (validationText.length() > 0)
119: validationText += "\n";
120: validationText += msg;
121: return true;
122: }
123:
124: }
|