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 java.awt.Dimension;
025: import java.awt.GridLayout;
026: import java.awt.Point;
027:
028: import org.beryl.gui.GUIException;
029: import org.beryl.gui.Widget;
030: import org.beryl.gui.model.MapDataModel;
031: import org.beryl.gui.model.ModelChangeEvent;
032: import org.beryl.gui.model.ModelChangeListener;
033: import org.beryl.gui.model.TableRow;
034: import org.beryl.gui.table.TableEditor;
035: import org.beryl.gui.table.TableRenderer;
036: import org.beryl.gui.validators.IntegerValidator;
037: import org.beryl.gui.widgets.Label;
038: import org.beryl.gui.widgets.Panel;
039: import org.beryl.gui.widgets.Table;
040: import org.beryl.gui.widgets.TextField;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043:
044: public class DimensionAdapter implements PropertyAdapter {
045: private TableRenderer dimensionRenderer = null;
046: private TableEditor dimensionEditor = null;
047:
048: private class DimensionRenderer implements TableRenderer {
049: public Widget getRenderer(Table table, Object value,
050: boolean isSelected, boolean hasFocus, TableRow row,
051: String key) throws GUIException {
052: Label label = new Label(null, null);
053: if (value instanceof Dimension) {
054: Dimension dimension = (Dimension) value;
055: label.setProperty("text", (int) dimension.getWidth()
056: + ", " + (int) dimension.getHeight());
057: } else {
058: Point point = (Point) value;
059: label.setProperty("text", point.x + ", " + point.y);
060: }
061:
062: return label;
063: }
064: };
065:
066: private class DimensionEditor implements TableEditor {
067: public Widget getEditor(Table table, Object value,
068: TableRow row, String key) throws GUIException {
069: final MapDataModel dataModel = new MapDataModel();
070:
071: if (value instanceof Dimension) {
072: final Dimension dimension = (Dimension) value;
073:
074: dataModel.setValue("width", String
075: .valueOf((int) dimension.getWidth()));
076: dataModel.setValue("height", String
077: .valueOf((int) dimension.getHeight()));
078: dataModel.setValue("value", value);
079:
080: dataModel
081: .addModelChangeListener(new ModelChangeListener() {
082: public void modelChanged(ModelChangeEvent e)
083: throws GUIException {
084: try {
085: int width = Integer
086: .parseInt((String) dataModel
087: .getValue("width"));
088: int height = Integer
089: .parseInt((String) dataModel
090: .getValue("height"));
091:
092: dimension.setSize(width, height);
093: } catch (NumberFormatException ex) {
094: /* Ignore */
095: }
096: }
097: });
098: } else {
099: final Point point = (Point) value;
100:
101: dataModel.setValue("width", String.valueOf(point.x));
102: dataModel.setValue("height", String.valueOf(point.y));
103: dataModel.setValue("value", value);
104:
105: dataModel
106: .addModelChangeListener(new ModelChangeListener() {
107: public void modelChanged(ModelChangeEvent e)
108: throws GUIException {
109: try {
110: int x = Integer
111: .parseInt((String) dataModel
112: .getValue("width"));
113: int y = Integer
114: .parseInt((String) dataModel
115: .getValue("height"));
116:
117: point.setLocation(x, y);
118: } catch (NumberFormatException ex) {
119: /* Ignore */
120: }
121: }
122: });
123: }
124:
125: Panel panel = new Panel(null, null);
126: panel.setProperty("layout", new GridLayout(1, 2));
127: TextField widthField = new TextField(panel, null);
128: widthField.addValidator(new IntegerValidator());
129: widthField.setProperty("key", "width");
130: TextField heightField = new TextField(panel, null);
131: heightField.addValidator(new IntegerValidator());
132: heightField.setProperty("key", "height");
133: panel.addChild(widthField, null);
134: panel.addChild(heightField, null);
135: panel.recursiveSetDataModel(dataModel);
136:
137: widthField.finalizeConstruction();
138: heightField.finalizeConstruction();
139:
140: return panel;
141: }
142: };
143:
144: public DimensionAdapter() {
145: dimensionRenderer = new DimensionRenderer();
146: dimensionEditor = new DimensionEditor();
147: }
148:
149: public TableEditor getEditor() {
150: return dimensionEditor;
151: }
152:
153: public TableRenderer getRenderer() {
154: return dimensionRenderer;
155: }
156:
157: public Object toValue(Object value, Element propertyNode) {
158: return value;
159: }
160:
161: public void toDOM(Object value, Element propertyNode) {
162: Document document = propertyNode.getOwnerDocument();
163:
164: if (value instanceof Dimension) {
165: Dimension dimension = (Dimension) value;
166:
167: Element widthNode = document.createElement("width");
168: Element heightNode = document.createElement("height");
169: propertyNode.appendChild(widthNode);
170: propertyNode.appendChild(heightNode);
171: widthNode.appendChild(document.createTextNode(String
172: .valueOf((int) dimension.getWidth())));
173: heightNode.appendChild(document.createTextNode(String
174: .valueOf((int) dimension.getHeight())));
175: } else {
176: Point point = (Point) value;
177:
178: Element xNode = document.createElement("x");
179: Element yNode = document.createElement("y");
180: propertyNode.appendChild(xNode);
181: propertyNode.appendChild(yNode);
182: xNode.appendChild(document.createTextNode(String
183: .valueOf(point.x)));
184: yNode.appendChild(document.createTextNode(String
185: .valueOf(point.y)));
186: }
187: }
188: }
|