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.HashMap;
026:
027: import org.beryl.gui.Controller;
028: import org.beryl.gui.GUIEvent;
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.MessageDialog;
031: import org.beryl.gui.Widget;
032: import org.beryl.gui.WidgetFactory;
033: import org.beryl.gui.XMLUtils;
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.validators.IntegerValidator;
039: import org.beryl.gui.validators.ValidationException;
040: import org.beryl.gui.validators.Validator;
041: import org.beryl.gui.widgets.Button;
042: import org.beryl.gui.widgets.Dialog;
043: import org.beryl.gui.widgets.Group;
044: import org.beryl.gui.widgets.LabeledWidget;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.NodeList;
048:
049: public class BorderEditor extends Controller implements
050: ModelChangeListener {
051: private Widget parent = null;
052: private Dialog dialog = null;
053: private Group group = null;
054: private MapDataModel dataModel = null;
055: private Element borderNode = null;
056: private ArrayList activeComponents = null;
057: private HashMap customComponents = null;
058: private Button okButton = null;
059: private MapDataModel editorModel = null;
060:
061: public BorderEditor(Widget parent, MapDataModel editorModel)
062: throws GUIException {
063: this .borderNode = ((PropertyTableRow) editorModel
064: .getValue("row")).getPropertyNode();
065: this .parent = parent;
066: this .editorModel = editorModel;
067:
068: dataModel = new MapDataModel();
069: dialog = (Dialog) constructDialog("BorderEditor", dataModel);
070: group = (Group) dialog.getWidget("Group");
071: okButton = (Button) dialog.getWidget("OKButton");
072:
073: customComponents = new HashMap();
074: activeComponents = new ArrayList();
075:
076: loadCustomComponent("empty", "Empty_Top",
077: new IntegerValidator());
078: loadCustomComponent("empty", "Empty_Bottom",
079: new IntegerValidator());
080: loadCustomComponent("empty", "Empty_Left",
081: new IntegerValidator());
082: loadCustomComponent("empty", "Empty_Right",
083: new IntegerValidator());
084: loadCustomComponent("titled", "Titled_Title", null);
085:
086: doLoad();
087: dataModel.addModelChangeListener(this );
088:
089: }
090:
091: public void show() {
092: try {
093: dialog.initDialog(parent);
094: dialog.show();
095: } catch (GUIException e) {
096: new MessageDialog(e);
097: }
098: }
099:
100: private void loadCustomComponent(String layoutName, String name,
101: Validator validator) throws GUIException {
102: Widget widget = WidgetFactory.getInstance().constructWidget(
103: getClass(), name, this , dataModel, group);
104: if (validator != null) {
105: ((LabeledWidget) widget).getDataWidget().addValidator(
106: validator);
107: }
108:
109: ArrayList components = (ArrayList) customComponents
110: .get(layoutName);
111:
112: if (components == null) {
113: components = new ArrayList();
114: customComponents.put(layoutName, components);
115: }
116:
117: components.add(widget);
118: }
119:
120: private void activateType(String type) throws GUIException {
121: for (int i = 0; i < activeComponents.size(); i++) {
122: group.removeChildWidget((Widget) activeComponents.get(i));
123: }
124:
125: ArrayList list = (ArrayList) customComponents.get(type);
126: if (list != null) {
127: for (int i = 0; i < list.size(); i++) {
128: group.addChild((Widget) list.get(i), null);
129: activeComponents.add(list.get(i));
130: }
131: }
132:
133: group.revalidate();
134: }
135:
136: public void modelChanged(ModelChangeEvent e) throws GUIException {
137: if (e instanceof MapChangeEvent) {
138: MapChangeEvent event = (MapChangeEvent) e;
139:
140: if (event.getKey().equals("type")) {
141: activateType(event.getNewValue().toString());
142: }
143: }
144:
145: try {
146: for (int i = 0; i < activeComponents.size(); i++) {
147: Widget widget = (Widget) activeComponents.get(i);
148:
149: widget.recursiveValidate();
150: }
151: okButton.setEnabled(true);
152: } catch (ValidationException ex) {
153: okButton.setEnabled(false);
154: }
155: }
156:
157: private void doLoad() throws GUIException {
158: String type = borderNode.getAttribute("border");
159:
160: dataModel.setValue("empty_top", "0");
161: dataModel.setValue("empty_bottom", "0");
162: dataModel.setValue("empty_left", "0");
163: dataModel.setValue("empty_right", "0");
164: dataModel.setValue("titled_title", "");
165:
166: if (type.equals("none")) {
167: dataModel.setValue("type", dialog.getWidget("none"));
168: } else if (type.equals("empty")) {
169: dataModel.setValue("type", dialog.getWidget("empty"));
170:
171: dataModel.setValue("empty_top", XMLUtils
172: .getStringFromChild(borderNode, "top"));
173: dataModel.setValue("empty_bottom", XMLUtils
174: .getStringFromChild(borderNode, "bottom"));
175: dataModel.setValue("empty_left", XMLUtils
176: .getStringFromChild(borderNode, "left"));
177: dataModel.setValue("empty_right", XMLUtils
178: .getStringFromChild(borderNode, "right"));
179: } else if (type.equals("titled")) {
180: dataModel.setValue("type", dialog.getWidget("titled"));
181:
182: dataModel.setValue("titled_title", XMLUtils
183: .getStringFromChild(borderNode, "title"));
184: } else if (type.equals("raised")) {
185: dataModel.setValue("type", dialog.getWidget("raised"));
186: } else if (type.equals("lowered")) {
187: dataModel.setValue("type", dialog.getWidget("lowered"));
188: } else if (type.equals("etched")) {
189: dataModel.setValue("type", dialog.getWidget("etched"));
190: } else {
191: throw new GUIException("Unknown border type [" + type + "]");
192: }
193:
194: activateType(dataModel.getValue("type").toString());
195: }
196:
197: protected void doOK() throws GUIException {
198: NodeList children = borderNode.getChildNodes();
199: int length = children.getLength();
200: for (int i = 0; i < length; i++) {
201: borderNode.removeChild(children.item(0));
202: }
203:
204: String type = dataModel.getValue("type").toString();
205: borderNode.setAttribute("border", type);
206: Document document = borderNode.getOwnerDocument();
207:
208: if (type.equals("empty")) {
209: Element top = document.createElement("top");
210: top.appendChild(document.createTextNode((String) dataModel
211: .getValue("empty_top")));
212: borderNode.appendChild(top);
213:
214: Element bottom = document.createElement("bottom");
215: bottom.appendChild(document
216: .createTextNode((String) dataModel
217: .getValue("empty_bottom")));
218: borderNode.appendChild(bottom);
219:
220: Element left = document.createElement("left");
221: left.appendChild(document.createTextNode((String) dataModel
222: .getValue("empty_left")));
223: borderNode.appendChild(left);
224:
225: Element right = document.createElement("right");
226: right.appendChild(document
227: .createTextNode((String) dataModel
228: .getValue("empty_right")));
229: borderNode.appendChild(right);
230: } else if (type.equals("titled")) {
231: Element title = document.createElement("title");
232: title.appendChild(document
233: .createTextNode((String) dataModel
234: .getValue("titled_title")));
235: borderNode.appendChild(title);
236: }
237:
238: PropertyTableRow row = (PropertyTableRow) editorModel
239: .getValue("row");
240: WidgetUserObject object = row.getUserObject();
241: editorModel.setValue("value", type);
242: }
243:
244: public void eventOccured(GUIEvent event) {
245: String name = event.getName();
246: try {
247: if (name.equals("cancel")) {
248: dialog.dispose();
249: } else if (name.equals("ok")) {
250: doOK();
251: dialog.dispose();
252: }
253: } catch (Exception e) {
254: new MessageDialog(e);
255: }
256: }
257: }
|