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.ArrayList;
025: import java.util.Iterator;
026:
027: import org.beryl.gui.Controller;
028: import org.beryl.gui.GUIEvent;
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.LayoutFactory;
031: import org.beryl.gui.MessageDialog;
032: import org.beryl.gui.PropertyFactory;
033: import org.beryl.gui.WidgetInfo;
034: import org.beryl.gui.model.MapChangeEvent;
035: import org.beryl.gui.model.MapDataModel;
036: import org.beryl.gui.model.ModelChangeEvent;
037: import org.beryl.gui.model.ModelChangeListener;
038: import org.beryl.gui.model.TableDataModel;
039: import org.beryl.gui.model.TableRow;
040: import org.beryl.gui.widgets.Button;
041: import org.beryl.gui.widgets.ComboBox;
042: import org.beryl.gui.widgets.Dialog;
043: import org.beryl.gui.widgets.Frame;
044: import org.beryl.gui.widgets.Table;
045: import org.beryl.gui.widgets.TextField;
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048: import org.w3c.dom.Node;
049: import org.w3c.dom.NodeList;
050:
051: public class AddPropertyDialog extends Controller implements
052: ModelChangeListener {
053: private static final int IDX_ISTRING = 0;
054: private static final int IDX_STRING = 1;
055: private static final int IDX_INT = 2;
056: private static final int IDX_FLOAT = 3;
057: private static final int IDX_DOUBLE = 4;
058: private static final int IDX_BOOL = 5;
059:
060: private WidgetUserObject userObject = null;
061: private Dialog dialog = null;
062: private MapDataModel dataModel = null;
063: private ComboBox typeCombo = null;
064: private TextField valueField = null;
065: private TextField nameField = null;
066: private Table propertyTable = null;
067: private Button okButton = null;
068:
069: private class PropertyEntryTableRow extends TableRow {
070: private WidgetInfo.PropertyEntry entry = null;
071:
072: public PropertyEntryTableRow(WidgetInfo.PropertyEntry entry)
073: throws GUIException {
074: super ();
075: this .entry = entry;
076: setValue("name", entry.propertyName);
077: setValue("type", entry.propertyType);
078: setValue("description", entry.getDescription());
079: }
080:
081: public WidgetInfo.PropertyEntry getEntry() {
082: return entry;
083: }
084: };
085:
086: public AddPropertyDialog(Frame frame, WidgetUserObject userObject)
087: throws GUIException {
088: this .userObject = userObject;
089:
090: WidgetInfo info = userObject.widget.getWidgetInfo();
091:
092: if (info != null) {
093: ArrayList properties = new ArrayList();
094: NodeList childNodes = userObject.element.getChildNodes();
095:
096: for (int i = 0; i < childNodes.getLength(); i++) {
097: Node node = childNodes.item(i);
098:
099: if (node.getNodeType() == Node.ELEMENT_NODE) {
100: if (node.getNodeName().equals("property")) {
101: properties.add(((Element) node)
102: .getAttribute("name"));
103: } else if (node.getNodeName().equals("anchor")) {
104: properties.add("anchor");
105: } else if (node.getNodeName().equals("layout")) {
106: properties.add("layout");
107: }
108: }
109:
110: }
111:
112: TableDataModel tableModel = new TableDataModel();
113: for (Iterator i = info.getPropertyEntries().iterator(); i
114: .hasNext();) {
115: WidgetInfo.PropertyEntry entry = (WidgetInfo.PropertyEntry) i
116: .next();
117:
118: if (!properties.contains(entry.propertyName)) {
119:
120: if (entry.propertyName.equals("anchor")) {
121: if (info.getSupportsAnchor()) {
122: tableModel
123: .addRow(new PropertyEntryTableRow(
124: entry));
125: }
126: } else {
127: tableModel.addRow(new PropertyEntryTableRow(
128: entry));
129: }
130: }
131: }
132:
133: dataModel = new MapDataModel();
134:
135: dialog = constructDialog("AddPropertyDialog", dataModel);
136:
137: propertyTable = (Table) dialog.getWidget("PropertyTable");
138: okButton = (Button) dialog.getWidget("OKButton");
139: nameField = (TextField) dialog.getWidget("NameField");
140: valueField = (TextField) dialog.getWidget("ValueField");
141: typeCombo = (ComboBox) dialog.getWidget("TypeCombo");
142:
143: propertyTable.setTableDataModel(tableModel);
144:
145: dataModel.setValue("property", new TableRow[] {});
146: dataModel.setValue("type.index", new Integer(0));
147: dataModel.setValue("name", "");
148: dataModel.setValue("value", "");
149: dataModel.addModelChangeListener(this );
150: dataModel.setValue("mode", "common");
151:
152: dialog.initDialog(frame);
153: dialog.setInitialFocus(propertyTable);
154: dialog.show();
155: }
156: }
157:
158: public void eventOccured(GUIEvent event) {
159: try {
160: if (event.getName().equals("add")) {
161: Document document = userObject.element
162: .getOwnerDocument();
163:
164: if (dataModel.getValue("mode").equals("common")) {
165: TableRow[] entries = (TableRow[]) dialog
166: .getDataModel().getValue("property");
167:
168: for (int i = 0; i < entries.length; i++) {
169: WidgetInfo.PropertyEntry entry = ((PropertyEntryTableRow) entries[i])
170: .getEntry();
171:
172: Element propertyNode = null;
173:
174: if (entry.propertyName.equals("anchor")) {
175: propertyNode = document
176: .createElement("anchor");
177:
178: AnchorEditor anchorEditor = new AnchorEditor(
179: dialog, propertyNode, userObject);
180: anchorEditor.show();
181: } else {
182: if (entry.propertyName.equals("layout")) {
183: propertyNode = document
184: .createElement("layout");
185: } else {
186: propertyNode = document
187: .createElement("property");
188: propertyNode.setAttribute("name",
189: entry.propertyName);
190: }
191:
192: propertyNode.setAttribute("type",
193: entry.propertyType);
194:
195: new PropertyTableRow(userObject,
196: entry.defaultValue, propertyNode)
197: .getPropertyAdapter().toDOM(
198: entry.defaultValue,
199: propertyNode);
200:
201: Object value = null;
202: if (entry.propertyName.equals("layout")) {
203: value = LayoutFactory.getInstance()
204: .constructLayout(
205: userObject.widget,
206: propertyNode);
207: } else {
208: value = PropertyFactory
209: .getInstance()
210: .constructProperty(propertyNode);
211: }
212:
213: PropertyTableRow row = new PropertyTableRow(
214: userObject, value, propertyNode);
215:
216: userObject.widget.setProperty(
217: entry.propertyName, value);
218: WidgetTree.revalidate(userObject.widget);
219:
220: userObject.tableModel.addRow(row);
221: userObject.element
222: .appendChild(propertyNode);
223:
224: Builder.markModified();
225: }
226: }
227: } else {
228: String name = (String) dataModel.getValue("name"), type = null;
229:
230: Element propertyNode = document
231: .createElement("property");
232: propertyNode.setAttribute("name", name);
233:
234: switch (((Integer) dataModel.getValue("type.index"))
235: .intValue()) {
236: case IDX_ISTRING:
237: type = "istring";
238: break;
239: case IDX_STRING:
240: type = "string";
241: break;
242: case IDX_INT:
243: type = "int";
244: break;
245: case IDX_FLOAT:
246: type = "float";
247: break;
248: case IDX_DOUBLE:
249: type = "double";
250: break;
251: case IDX_BOOL:
252: type = "bool";
253: break;
254: }
255: propertyNode.setAttribute("type", type);
256: propertyNode.appendChild(document
257: .createTextNode((String) dataModel
258: .getValue("value")));
259: Object value = PropertyFactory.getInstance()
260: .constructProperty(propertyNode);
261: PropertyTableRow row = new PropertyTableRow(
262: userObject, value, propertyNode);
263: userObject.widget.setProperty(name, value);
264: WidgetTree.revalidate(userObject.widget);
265:
266: userObject.tableModel.addRow(null, row);
267: userObject.element.appendChild(propertyNode);
268: Builder.markModified();
269: }
270: dialog.dispose();
271: } else if (event.getName().equals("cancel")) {
272: dialog.dispose();
273: }
274: } catch (Exception e) {
275: new MessageDialog(dialog, e);
276: }
277: }
278:
279: public void modelChanged(ModelChangeEvent e) throws GUIException {
280: if (e instanceof MapChangeEvent) {
281: MapChangeEvent event = (MapChangeEvent) e;
282: if (event.getKey().equals("mode")) {
283: boolean common = event.getNewValue().equals("common");
284:
285: propertyTable.setEnabled(common);
286: nameField.setEnabled(!common);
287: typeCombo.setEnabled(!common);
288: valueField.setEnabled(!common);
289: }
290: if (dataModel.getValue("mode").equals("common")) {
291: okButton.setEnabled(((TableRow[]) dataModel
292: .getValue("property")).length > 0);
293: } else {
294: okButton.setEnabled(!dataModel.getValue("name").equals(
295: ""));
296: }
297: }
298: }
299: }
|