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.builder;
023:
024: import org.beryl.gui.GUIException;
025: import org.beryl.gui.Widget;
026: import org.beryl.gui.model.MapChangeEvent;
027: import org.beryl.gui.model.MapDataModel;
028: import org.beryl.gui.model.ModelChangeEvent;
029: import org.beryl.gui.model.ModelChangeListener;
030: import org.beryl.gui.model.TableRow;
031: import org.beryl.gui.table.TableEditor;
032: import org.beryl.gui.table.TableRenderer;
033: import org.beryl.gui.validators.DoubleValidator;
034: import org.beryl.gui.validators.FloatValidator;
035: import org.beryl.gui.validators.IntegerValidator;
036: import org.beryl.gui.validators.LongValidator;
037: import org.beryl.gui.widgets.Panel;
038: import org.beryl.gui.widgets.Table;
039: import org.beryl.gui.widgets.TextField;
040: import org.w3c.dom.Element;
041:
042: public class NumberAdapter implements PropertyAdapter {
043: private TableEditor numberEditor = null;
044:
045: private class NumberEditor implements TableEditor {
046: public Widget getEditor(Table table, Object value,
047: TableRow row, String key) throws GUIException {
048: final MapDataModel dataModel = new MapDataModel();
049:
050: final String type = ((PropertyTableRow) row)
051: .getPropertyNode().getAttribute("type");
052:
053: dataModel.setValue("value", value);
054: dataModel.setValue("value_str", value.toString());
055:
056: dataModel.addModelChangeListener(new ModelChangeListener() {
057: public void modelChanged(ModelChangeEvent e)
058: throws GUIException {
059: if (e instanceof MapChangeEvent) {
060: MapChangeEvent event = (MapChangeEvent) e;
061: if (event.getKey().equals("value_str")) {
062: try {
063: if (type.equals("int"))
064: dataModel
065: .setValue(
066: "value",
067: new Integer(
068: (String) dataModel
069: .getValue("value_str")));
070: else if (type.equals("long"))
071: dataModel
072: .setValue(
073: "value",
074: new Long(
075: (String) dataModel
076: .getValue("value_str")));
077: else if (type.equals("float"))
078: dataModel
079: .setValue(
080: "value",
081: new Float(
082: (String) dataModel
083: .getValue("value_str")));
084: else if (type.equals("double"))
085: dataModel
086: .setValue(
087: "value",
088: new Double(
089: (String) dataModel
090: .getValue("value_str")));
091: } catch (Exception ex) {
092: /* Ignore */
093: }
094: }
095: }
096: }
097: });
098:
099: Panel panel = new Panel(null, null);
100: TextField textField = new TextField(panel, null);
101: textField.setProperty("key", "value_str");
102: textField.finalizeConstruction();
103:
104: if (type.equals("int"))
105: textField.addValidator(new IntegerValidator());
106: else if (type.equals("long"))
107: textField.addValidator(new LongValidator());
108: else if (type.equals("float"))
109: textField.addValidator(new FloatValidator());
110: else if (type.equals("double"))
111: textField.addValidator(new DoubleValidator());
112: else
113: throw new GUIException("Unknown numeric type");
114:
115: panel.addChild(textField, null);
116: panel.recursiveSetDataModel(dataModel);
117:
118: return panel;
119: }
120: };
121:
122: public NumberAdapter() {
123: numberEditor = new NumberEditor();
124: }
125:
126: public TableEditor getEditor() {
127: return numberEditor;
128: }
129:
130: public TableRenderer getRenderer() {
131: return null;
132: }
133:
134: public Object toValue(Object value, Element propertyNode) {
135: return value;
136: }
137:
138: public void toDOM(Object value, Element propertyNode) {
139: propertyNode.appendChild(propertyNode.getOwnerDocument()
140: .createTextNode(value.toString()));
141: }
142: }
|