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: import java.util.StringTokenizer;
027:
028: import org.beryl.gui.Controller;
029: import org.beryl.gui.GUIEvent;
030: import org.beryl.gui.GUIException;
031: import org.beryl.gui.MessageDialog;
032: import org.beryl.gui.Widget;
033: import org.beryl.gui.WidgetFactory;
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.FloatValidator;
039: import org.beryl.gui.validators.IntegerValidator;
040: import org.beryl.gui.validators.ValidationException;
041: import org.beryl.gui.validators.Validator;
042: import org.beryl.gui.widgets.Button;
043: import org.beryl.gui.widgets.Dialog;
044: import org.beryl.gui.widgets.Group;
045: import org.beryl.gui.widgets.LabeledWidget;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.NamedNodeMap;
048:
049: public class AnchorEditor extends Controller implements
050: ModelChangeListener {
051: private WidgetUserObject userObject = null;
052: private Widget parent = null;
053: private Dialog dialog = null;
054: private Group group = null;
055: private MapDataModel dataModel = null;
056: private Element anchorNode = null;
057: private ArrayList activeComponents = null;
058: private HashMap customComponents = null;
059: private Button okButton = null;
060: private MapDataModel editorModel = null;
061:
062: public AnchorEditor(Widget parent, MapDataModel editorModel)
063: throws GUIException {
064: this (parent, ((PropertyTableRow) editorModel.getValue("row"))
065: .getPropertyNode(), null);
066: this .editorModel = editorModel;
067: }
068:
069: public AnchorEditor(Widget parent, Element anchorNode,
070: WidgetUserObject userObject) throws GUIException {
071: this .anchorNode = anchorNode;
072: this .userObject = userObject;
073: this .parent = parent;
074:
075: dataModel = new MapDataModel();
076: dialog = constructDialog("AnchorEditor", dataModel);
077: group = (Group) dialog.getWidget("Group");
078: okButton = (Button) dialog.getWidget("OKButton");
079:
080: customComponents = new HashMap();
081: activeComponents = new ArrayList();
082:
083: loadCustomComponent("hig", "HIG_Row", new IntegerValidator());
084: loadCustomComponent("hig", "HIG_Column", new IntegerValidator());
085: loadCustomComponent("hig", "HIG_Width", new IntegerValidator());
086: loadCustomComponent("hig", "HIG_Height", new IntegerValidator());
087: loadCustomComponent("hig", "HIG_Alignment", null);
088:
089: loadCustomComponent("border", "Border_Alignment", null);
090:
091: loadCustomComponent("box", "Box_AlignX", new FloatValidator());
092: loadCustomComponent("box", "Box_AlignY", new FloatValidator());
093:
094: doLoad();
095: dataModel.addModelChangeListener(this );
096:
097: }
098:
099: public void show() {
100: try {
101: dialog.initDialog(parent);
102: dialog.show();
103: } catch (GUIException e) {
104: new MessageDialog(e);
105: }
106: }
107:
108: private void loadCustomComponent(String layoutName, String name,
109: Validator validator) throws GUIException {
110: Widget widget = WidgetFactory.getInstance().constructWidget(
111: getClass(), name, this , dataModel, group);
112: if (validator != null) {
113: ((LabeledWidget) widget).getDataWidget().addValidator(
114: validator);
115: }
116:
117: ArrayList components = (ArrayList) customComponents
118: .get(layoutName);
119:
120: if (components == null) {
121: components = new ArrayList();
122: customComponents.put(layoutName, components);
123: }
124:
125: components.add(widget);
126: }
127:
128: private void activateType(String type) throws GUIException {
129: for (int i = 0; i < activeComponents.size(); i++) {
130: group.removeChildWidget((Widget) activeComponents.get(i));
131: }
132:
133: ArrayList list = (ArrayList) customComponents.get(type);
134: if (list != null) {
135: for (int i = 0; i < list.size(); i++) {
136: group.addChild((Widget) list.get(i), null);
137: activeComponents.add(list.get(i));
138: }
139: }
140:
141: group.revalidate();
142: }
143:
144: public void modelChanged(ModelChangeEvent e) throws GUIException {
145: if (e instanceof MapChangeEvent) {
146: MapChangeEvent event = (MapChangeEvent) e;
147:
148: if (event.getKey().equals("type")) {
149: activateType(event.getNewValue().toString());
150: }
151: }
152:
153: try {
154: for (int i = 0; i < activeComponents.size(); i++) {
155: Widget widget = (Widget) activeComponents.get(i);
156:
157: widget.recursiveValidate();
158: }
159: okButton.setEnabled(true);
160: } catch (ValidationException ex) {
161: okButton.setEnabled(false);
162: }
163: }
164:
165: private void doLoad() throws GUIException {
166: String type = anchorNode.getAttribute("type");
167:
168: dataModel.setValue("box_alignx", "0.0");
169: dataModel.setValue("box_aligny", "0.0");
170: dataModel.setValue("hig_row", "1");
171: dataModel.setValue("hig_column", "1");
172: dataModel.setValue("hig_width", "1");
173: dataModel.setValue("hig_height", "1");
174: dataModel.setValue("hig_l", Boolean.TRUE);
175: dataModel.setValue("hig_r", Boolean.TRUE);
176: dataModel.setValue("hig_t", Boolean.TRUE);
177: dataModel.setValue("hig_b", Boolean.TRUE);
178: dataModel.setValue("border", dialog.getWidget("center"));
179:
180: if (type.equals("")) {
181: dataModel.setValue("type", dialog.getWidget("none"));
182: } else if (type.equals("hig")) {
183: dataModel.setValue("type", dialog.getWidget("hig"));
184:
185: StringTokenizer pos = new StringTokenizer(anchorNode
186: .getAttribute("pos"), ",");
187: String align = anchorNode.getAttribute("align");
188: int posCount = pos.countTokens();
189:
190: try {
191: if (align.equals("")
192: && anchorNode.getAttributes().getNamedItem(
193: "align") == null)
194: align = "lrtb";
195: if (posCount == 2) {
196: dataModel.setValue("hig_row", pos.nextToken());
197: dataModel.setValue("hig_column", pos.nextToken());
198: } else if (posCount == 4) {
199: dataModel.setValue("hig_row", pos.nextToken());
200: dataModel.setValue("hig_column", pos.nextToken());
201: dataModel.setValue("hig_width", pos.nextToken());
202: dataModel.setValue("hig_height", pos.nextToken());
203: } else {
204: throw new GUIException("Bad anchor argument count");
205: }
206:
207: dataModel.setValue("hig_l", new Boolean(align
208: .indexOf('l') != -1));
209: dataModel.setValue("hig_r", new Boolean(align
210: .indexOf('r') != -1));
211: dataModel.setValue("hig_t", new Boolean(align
212: .indexOf('t') != -1));
213: dataModel.setValue("hig_b", new Boolean(align
214: .indexOf('b') != -1));
215: } catch (NumberFormatException e) {
216: throw new GUIException("Invalid anchor data", e);
217: }
218: } else if (type.equals("border")) {
219: dataModel.setValue("type", dialog.getWidget("border"));
220:
221: dataModel.setValue("border", dialog.getWidget(anchorNode
222: .getAttribute("border")));
223: } else if (type.equals("box")) {
224: dataModel.setValue("type", dialog.getWidget("box"));
225:
226: dataModel.setValue("box_alignx", anchorNode
227: .getAttribute("alignx"));
228: dataModel.setValue("box_aligny", anchorNode
229: .getAttribute("aligny"));
230: }
231:
232: activateType(dataModel.getValue("type").toString());
233: }
234:
235: protected void doOK() throws GUIException {
236: NamedNodeMap map = anchorNode.getAttributes();
237: for (int i = 0; i < map.getLength(); i++) {
238: anchorNode.removeAttribute(map.item(i).getNodeName());
239: }
240:
241: String type = dataModel.getValue("type").toString();
242:
243: if (!type.equals("none")) {
244: anchorNode.setAttribute("type", type);
245:
246: if (type.equals("border")) {
247: anchorNode.setAttribute("border", dataModel.getValue(
248: "border").toString());
249: } else if (type.equals("box")) {
250: anchorNode.setAttribute("alignx", (String) dataModel
251: .getValue("box_alignx"));
252: anchorNode.setAttribute("aligny", (String) dataModel
253: .getValue("box_aligny"));
254: } else if (type.equals("hig")) {
255: String align = "", pos = "";
256:
257: if (((Boolean) dataModel.getValue("hig_l"))
258: .booleanValue())
259: align += "l";
260: if (((Boolean) dataModel.getValue("hig_r"))
261: .booleanValue())
262: align += "r";
263: if (((Boolean) dataModel.getValue("hig_t"))
264: .booleanValue())
265: align += "t";
266: if (((Boolean) dataModel.getValue("hig_b"))
267: .booleanValue())
268: align += "b";
269:
270: pos = dataModel.getValue("hig_row") + ","
271: + dataModel.getValue("hig_column");
272:
273: if (!dataModel.getValue("hig_width").equals("1")
274: || !dataModel.getValue("hig_height")
275: .equals("1"))
276: pos += "," + dataModel.getValue("hig_width") + ","
277: + dataModel.getValue("hig_height");
278:
279: anchorNode.setAttribute("pos", pos);
280: anchorNode.setAttribute("align", align);
281: }
282:
283: if (editorModel != null) {
284: PropertyTableRow row = (PropertyTableRow) editorModel
285: .getValue("row");
286: editorModel.setValue("value", AnchorAdapter
287: .toValue(anchorNode));
288: WidgetTree.doReInsert(row.getUserObject());
289: } else {
290: if (userObject != null) {
291: Object anchorValue = WidgetTree.createAnchor(
292: userObject.widget, anchorNode);
293: PropertyTableRow row = new PropertyTableRow(
294: userObject, anchorValue, anchorNode);
295: userObject.tableModel.addRow(row);
296: userObject.element.appendChild(anchorNode);
297: WidgetTree.doReInsert(userObject);
298: }
299: }
300: Builder.markModified();
301: } else {
302: if (editorModel != null) {
303: PropertyTableRow row = (PropertyTableRow) editorModel
304: .getValue("row");
305: WidgetTree.doDeleteProperty((PropertyTableRow) row);
306: Builder.markModified();
307: }
308: }
309: }
310:
311: public void eventOccured(GUIEvent event) {
312: String name = event.getName();
313: try {
314: if (name.equals("cancel")) {
315: dialog.dispose();
316: } else if (name.equals("ok")) {
317: doOK();
318: dialog.dispose();
319: }
320: } catch (Exception e) {
321: new MessageDialog(dialog, e);
322: }
323: }
324: }
|