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.BorderLayout;
025:
026: import org.beryl.gui.GUIEvent;
027: import org.beryl.gui.GUIEventListener;
028: import org.beryl.gui.GUIException;
029: import org.beryl.gui.MessageDialog;
030: import org.beryl.gui.Widget;
031: import org.beryl.gui.model.ListDataModel;
032: import org.beryl.gui.model.MapDataModel;
033: import org.beryl.gui.model.ModelChangeEvent;
034: import org.beryl.gui.model.ModelChangeListener;
035: import org.beryl.gui.model.TableRow;
036: import org.beryl.gui.table.TableEditor;
037: import org.beryl.gui.table.TableRenderer;
038: import org.beryl.gui.widgets.Button;
039: import org.beryl.gui.widgets.ComboBox;
040: import org.beryl.gui.widgets.Frame;
041: import org.beryl.gui.widgets.Panel;
042: import org.beryl.gui.widgets.Table;
043: import org.w3c.dom.Element;
044:
045: public class LayoutAdapter implements PropertyAdapter, GUIEventListener {
046: /**
047: * List data model containing the available layout types
048: */
049: private static ListDataModel layoutModel = null;
050:
051: private TableEditor layoutEditor = null;
052:
053: static {
054: try {
055: layoutModel = new ListDataModel();
056: layoutModel.addValue(null, "border");
057: layoutModel.addValue(null, "hig");
058: layoutModel.addValue(null, "vbox");
059: layoutModel.addValue(null, "hbox");
060: layoutModel.addValue(null, "flow");
061: layoutModel.addValue(null, "lflow");
062: layoutModel.addValue(null, "rflow");
063: } catch (GUIException e) {
064: /* Won't happen */
065: throw new RuntimeException(e);
066: }
067: };
068:
069: private class LayoutEditor implements TableEditor {
070: public Widget getEditor(Table table, Object value,
071: TableRow row, String key) throws GUIException {
072: Panel panel = new Panel(null, null);
073:
074: ComboBox comboBox = new ComboBox(panel, null);
075: final Button button = new Button(panel, null);
076: final MapDataModel dataModel = new MapDataModel();
077: dataModel.setValue("userobject", ((PropertyTableRow) row)
078: .getUserObject());
079: dataModel.setValue("frame", table
080: .getParentWidgetByClass(Frame.class));
081: dataModel.setValue("node", ((PropertyTableRow) row)
082: .getPropertyNode());
083:
084: button.setProperty("text", "...");
085: button.addListener("clicked", "modify", LayoutAdapter.this );
086: panel.addChild(comboBox, "Center");
087: panel.addChild(button, "East");
088: comboBox.setProperty("valuekey", "value");
089: comboBox.setListDataModel(layoutModel);
090: panel.recursiveSetDataModel(dataModel);
091: dataModel.addModelChangeListener(new ModelChangeListener() {
092: public void modelChanged(ModelChangeEvent e)
093: throws GUIException {
094: button.setEnabled("hig".equals(dataModel
095: .getValue("value")));
096: }
097: });
098: dataModel.setValue("value", value);
099: return panel;
100: }
101: }
102:
103: public LayoutAdapter() {
104: layoutEditor = new LayoutEditor();
105: }
106:
107: public TableRenderer getRenderer() {
108: return null;
109: }
110:
111: public TableEditor getEditor() {
112: return layoutEditor;
113: }
114:
115: public Object toValue(Object value, Element propertyNode) {
116: String type = propertyNode.getAttribute("type");
117: if (type.equals(""))
118: type = "hig";
119: return type;
120: }
121:
122: public void toDOM(Object value, Element propertyNode) {
123: if (value instanceof BorderLayout)
124: propertyNode.setAttribute("type", "border"); // Default layout
125: else
126: propertyNode.setAttribute("type", (String) value);
127: }
128:
129: public void eventOccured(GUIEvent event) {
130: try {
131: String layout = (String) event.getSource().getDataModel()
132: .getValue("value");
133: if (layout.equals("hig")) {
134: new HIGEditor((Frame) event.getSource().getDataModel()
135: .getValue("frame"), (Element) event.getSource()
136: .getDataModel().getValue("node"),
137: (WidgetUserObject) event.getSource()
138: .getDataModel().getValue("userobject"));
139: }
140: } catch (Exception e) {
141: new MessageDialog(e);
142: }
143: }
144: }
|