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.util.HashMap;
025:
026: import org.beryl.gui.GUIException;
027: import org.beryl.gui.LayoutFactory;
028: import org.beryl.gui.MessageDialog;
029: import org.beryl.gui.PropertyFactory;
030: import org.beryl.gui.Widget;
031: import org.beryl.gui.model.MapChangeEvent;
032: import org.beryl.gui.model.ModelChangeEvent;
033: import org.beryl.gui.model.ModelChangeListener;
034: import org.beryl.gui.model.TableRow;
035: import org.beryl.gui.table.TableEditor;
036: import org.beryl.gui.table.TableRenderer;
037: import org.beryl.gui.widgets.Table;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.NamedNodeMap;
040: import org.w3c.dom.NodeList;
041:
042: public class PropertyTableRow extends TableRow implements
043: ModelChangeListener {
044: private static HashMap propertyAdapters = null;
045:
046: static {
047: propertyAdapters = new HashMap();
048: PropertyAdapter istringAdapter = new IStringAdapter();
049: propertyAdapters.put("", istringAdapter);
050: propertyAdapters.put("istring", istringAdapter);
051: propertyAdapters.put("string", new StringAdapter());
052:
053: PropertyAdapter numberAdapter = new NumberAdapter();
054: propertyAdapters.put("int", numberAdapter);
055: propertyAdapters.put("long", numberAdapter);
056: propertyAdapters.put("float", numberAdapter);
057: propertyAdapters.put("double", numberAdapter);
058:
059: DimensionAdapter dimensionAdapter = new DimensionAdapter();
060: propertyAdapters.put("dimension", dimensionAdapter);
061: propertyAdapters.put("point", dimensionAdapter);
062:
063: propertyAdapters.put("enum", new EnumAdapter());
064: propertyAdapters.put("color", new ColorAdapter());
065: propertyAdapters.put("bool", new BoolAdapter());
066: propertyAdapters.put("icon", new IconAdapter());
067: propertyAdapters.put("iicon", new IconAdapter());
068: propertyAdapters.put("border", new BorderAdapter());
069: propertyAdapters.put("layout", new LayoutAdapter());
070: propertyAdapters.put("anchor", new AnchorAdapter());
071: propertyAdapters.put("font", new FontAdapter());
072: };
073:
074: private Element propertyNode = null;
075: private WidgetUserObject userObject = null;
076:
077: public PropertyTableRow(WidgetUserObject userObject, Object value,
078: Element propertyNode) throws GUIException {
079: this .userObject = userObject;
080: this .propertyNode = propertyNode;
081: String nodeName = propertyNode.getNodeName();
082:
083: if (nodeName.equals("property")) {
084: setValue("name", propertyNode.getAttribute("name"));
085: setValue("value", getPropertyAdapter().toValue(value,
086: propertyNode));
087: } else if (nodeName.equals("layout")) {
088: setValue("name", "layout");
089: setValue("value", getPropertyAdapter().toValue(value,
090: propertyNode));
091: } else if (nodeName.equals("anchor")) {
092: setValue("name", "anchor");
093: setValue("value", getPropertyAdapter().toValue(value,
094: propertyNode));
095: }
096: addModelChangeListener(this );
097: }
098:
099: public boolean hasCustomRenderer(String key) {
100: if (key.equals("value"))
101: return true;
102: return false;
103: }
104:
105: public Widget getRenderer(Table table, Object value,
106: boolean isSelected, boolean hasFocus, TableRow row,
107: String key) throws GUIException {
108: TableRenderer renderer = getPropertyAdapter().getRenderer();
109:
110: if (renderer != null)
111: return renderer.getRenderer(table, value, isSelected,
112: hasFocus, row, key);
113: return null;
114: }
115:
116: public WidgetUserObject getUserObject() {
117: return userObject;
118: }
119:
120: public Element getPropertyNode() {
121: return propertyNode;
122: }
123:
124: public Widget getEditor(Table table, Object value, TableRow row,
125: String key) throws GUIException {
126: TableEditor editor = getPropertyAdapter().getEditor();
127: if (editor != null)
128: return editor.getEditor(table, value, row, key);
129: return null;
130: }
131:
132: public boolean isEditable(String key) {
133: if (key.equals("value")) {
134: return true;
135: } else {
136: return false;
137: }
138: }
139:
140: public PropertyAdapter getPropertyAdapter() throws GUIException {
141: if (propertyNode.getNodeName().equals("layout"))
142: return (PropertyAdapter) propertyAdapters.get("layout");
143: else if (propertyNode.getNodeName().equals("anchor"))
144: return (PropertyAdapter) propertyAdapters.get("anchor");
145:
146: PropertyAdapter adapter = (PropertyAdapter) propertyAdapters
147: .get(propertyNode.getAttribute("type"));
148: if (adapter != null)
149: return adapter;
150: else
151: throw new GUIException("Unknown property type ["
152: + propertyNode.getAttribute("type") + "]");
153: }
154:
155: public void modelChanged(ModelChangeEvent e) throws GUIException {
156: MapChangeEvent event = (MapChangeEvent) e;
157:
158: if (event.getKey().equals("value")) {
159: if (propertyNode.getNodeName().equals("property")) {
160: NamedNodeMap map = propertyNode.getAttributes();
161: for (int i = 0; i < map.getLength(); i++) {
162: String name = map.item(i).getNodeName();
163: if (!name.equals("name") && !name.equals("type")) {
164: propertyNode.removeAttribute(map.item(i)
165: .getNodeName());
166: }
167: }
168:
169: if (!propertyNode.getAttribute("name").equals("border")) {
170: NodeList children = propertyNode.getChildNodes();
171: int length = children.getLength();
172: for (int i = 0; i < length; i++) {
173: propertyNode.removeChild(children.item(0));
174: }
175: }
176:
177: getPropertyAdapter().toDOM(event.getNewValue(),
178: propertyNode);
179:
180: try {
181: userObject.widget.setProperty(
182: (String) getValue("name"), PropertyFactory
183: .getInstance().constructProperty(
184: propertyNode));
185: } catch (Exception ex) {
186: new MessageDialog(ex);
187: }
188:
189: WidgetTree.revalidate(userObject.widget);
190: } else if (propertyNode.getNodeName().equals("layout")) {
191: getPropertyAdapter().toDOM(event.getNewValue(),
192: propertyNode);
193:
194: if (!propertyNode.getAttribute("type").equals("hig")
195: && !propertyNode.getAttribute("type")
196: .equals("")) {
197: propertyNode.removeAttribute("horiz");
198: propertyNode.removeAttribute("vert");
199: propertyNode.removeAttribute("hweights");
200: propertyNode.removeAttribute("vweights");
201: }
202:
203: try {
204: userObject.widget.setProperty("layout",
205: LayoutFactory.getInstance()
206: .constructLayout(userObject.widget,
207: propertyNode));
208: } catch (Exception ex) {
209: new MessageDialog(ex);
210: }
211:
212: WidgetTree.revalidate(userObject.widget);
213: }
214: }
215: }
216: };
|