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: package org.beryl.gui.builder;
022:
023: import javax.swing.ImageIcon;
024:
025: import org.beryl.gui.GUIException;
026: import org.beryl.gui.ImageIconFactory;
027: import org.beryl.gui.MessageDialog;
028: import org.beryl.gui.Widget;
029: import org.beryl.gui.XMLUtils;
030: import org.beryl.gui.model.MapDataModel;
031: import org.beryl.gui.model.TableRow;
032: import org.beryl.gui.table.ImageRenderer;
033: import org.beryl.gui.table.TableEditor;
034: import org.beryl.gui.table.TableRenderer;
035: import org.beryl.gui.widgets.Panel;
036: import org.beryl.gui.widgets.Table;
037: import org.beryl.gui.widgets.TextField;
038: import org.w3c.dom.Element;
039:
040: public class IconAdapter implements PropertyAdapter {
041: private TableRenderer iconRenderer = null;
042: private TableEditor iconEditor = null;
043:
044: private class IconEditor implements TableEditor {
045: public Widget getEditor(Table table, Object value,
046: TableRow row, String key) throws GUIException {
047: MapDataModel dataModel = new MapDataModel() {
048: public Object getValue(String key) {
049: if (key.equals("value")) {
050: try {
051: return ImageIconFactory
052: .getIcon((String) super
053: .getValue("value_str"));
054: } catch (Exception e) {
055: new MessageDialog(e);
056: }
057: }
058: return super .getValue(key);
059: }
060:
061: };
062:
063: dataModel.setValue("value", value);
064: dataModel.setValue("value_str", value.toString());
065:
066: Panel panel = new Panel(null, null);
067: TextField textField = new TextField(panel, null);
068: textField.setProperty("key", "value_str");
069: textField.finalizeConstruction();
070:
071: panel.addChild(textField, null);
072: panel.recursiveSetDataModel(dataModel);
073:
074: return panel;
075: }
076: }
077:
078: public IconAdapter() {
079: iconRenderer = new ImageRenderer();
080: iconEditor = new IconEditor();
081: }
082:
083: public TableEditor getEditor() {
084: return iconEditor;
085: }
086:
087: public TableRenderer getRenderer() {
088: return iconRenderer;
089: }
090:
091: public Object toValue(Object value, Element propertyNode) {
092: ((ImageIcon) value).setDescription(XMLUtils
093: .extractTextChildren(propertyNode));
094: return value;
095: }
096:
097: public void toDOM(Object value, Element propertyNode) {
098: try {
099: if (value instanceof String)
100: value = ImageIconFactory.getIcon((String) value);
101: } catch (Exception e) {
102: /* Should not happen */
103: throw new RuntimeException("Error while loading icon", e);
104: }
105: propertyNode.appendChild(propertyNode.getOwnerDocument()
106: .createTextNode(((ImageIcon) value).getDescription()));
107: }
108: }
|