001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.test;
023:
024: import org.beryl.gui.Controller;
025: import org.beryl.gui.GUIEvent;
026: import org.beryl.gui.GUIException;
027: import org.beryl.gui.MessageDialog;
028: import org.beryl.gui.model.MapChangeEvent;
029: import org.beryl.gui.model.MapDataModel;
030: import org.beryl.gui.model.ModelChangeEvent;
031: import org.beryl.gui.model.ModelChangeListener;
032: import org.beryl.gui.model.TableRow;
033: import org.beryl.gui.validators.DateFieldValidator;
034: import org.beryl.gui.validators.StrictTextFieldValidator;
035: import org.beryl.gui.validators.ValidationException;
036: import org.beryl.gui.widgets.Button;
037: import org.beryl.gui.widgets.Frame;
038:
039: /**
040: * Person editor
041: */
042:
043: public class PersonEditor extends Controller implements
044: ModelChangeListener {
045: private Frame frame = null;
046: private Button okButton = null;
047:
048: public PersonEditor(MapDataModel source) throws GUIException {
049: /* Create a copy of the table row */
050: MapDataModel model = (MapDataModel) source.clone();
051: model.setValue("source", source);
052:
053: frame = constructFrame("PersonEditor", model);
054: model.addModelChangeListener(this );
055:
056: /* Add date field validators */
057: StrictTextFieldValidator validator = new StrictTextFieldValidator();
058: frame.getWidget("FirstNameField").addValidator(validator);
059: frame.getWidget("LastNameField").addValidator(validator);
060: frame.getWidget("EmailField").addValidator(validator);
061: frame.getWidget("BirthDateField")
062: .addValidator(
063: new DateFieldValidator(
064: DateFieldValidator.EUROPEAN_DATE));
065:
066: okButton = (Button) frame.getWidget("OKButton");
067: frame.show();
068: }
069:
070: public void modelChanged(ModelChangeEvent e) {
071: MapChangeEvent event = (MapChangeEvent) e;
072: log.debug("Data model change : '" + event.getKey() + "' => '"
073: + event.getNewValue() + "'");
074:
075: try {
076: frame.recursiveValidate();
077: okButton.setEnabled(true);
078: } catch (ValidationException ex) {
079: try {
080: /* Disable the OK button if the data does not validate */
081: okButton.setEnabled(false);
082: } catch (GUIException exx) {
083: /* Won't happen since it is a JComponent */
084: }
085: } catch (GUIException ex) {
086: new MessageDialog(ex);
087: }
088: }
089:
090: public void eventOccured(GUIEvent event) {
091: try {
092: log.debug("Caught event : " + event.toString());
093: if (event.getName().equals("cancel")) {
094: frame.dispose();
095: } else if (event.getName().equals("save")) {
096: Frame frame = (Frame) event.getSource()
097: .getParentWidgetByClass(Frame.class);
098: MapDataModel model = frame.getDataModel();
099: model.removeModelChangeListener(this );
100: TableRow source = (TableRow) model.removeValueByKey(
101: null, "source");
102: source.replace(model);
103: frame.dispose();
104: }
105: } catch (GUIException e) {
106: new MessageDialog(e);
107: }
108: }
109: }
|