001: /*
002: * Beryl - A web platform based on XML, XSLT and Java This file is part of the
003: * 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 modify it under
008: * the terms of the GNU Lesser General Public License as published by the Free
009: * Software Foundation; either version 2.1 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
015: * details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this program; if not, write to the Free Software Foundation, Inc.,
019: * 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: import java.awt.Font;
026: import javax.swing.JLabel;
027: import org.beryl.gui.GUIEvent;
028: import org.beryl.gui.GUIEventListener;
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.MessageDialog;
031: import org.beryl.gui.Widget;
032: import org.beryl.gui.model.MapDataModel;
033: import org.beryl.gui.model.TableRow;
034: import org.beryl.gui.table.ButtonEditor;
035: import org.beryl.gui.table.TableEditor;
036: import org.beryl.gui.table.TableRenderer;
037: import org.beryl.gui.widgets.Frame;
038: import org.beryl.gui.widgets.Label;
039: import org.beryl.gui.widgets.Panel;
040: import org.beryl.gui.widgets.Table;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043: import say.swing.JFontChooser;
044:
045: public class FontAdapter implements PropertyAdapter, GUIEventListener {
046: private TableEditor fontEditor = null;
047:
048: public class FontRenderer implements TableRenderer {
049: public Widget getRenderer(Table table, Object value,
050: boolean isSelected, boolean hasFocus, TableRow row,
051: String key) throws GUIException {
052: Font font = (Font) value;
053: Panel panel = new Panel(null, null);
054: Label label = new Label(panel, null);
055: label.setProperty("font", font);
056: label.setProperty("text", font.getName());
057: label.setProperty("horizontalAlignment", new Integer(
058: JLabel.CENTER));
059: panel.setProperty("layout", new BorderLayout());
060: panel.addChild(label, BorderLayout.CENTER);
061: return panel;
062: }
063: }
064:
065: public FontAdapter() {
066: fontEditor = new ButtonEditor("edit", this ) {
067: public Widget getEditor(Table table, Object value,
068: TableRow row, String key) throws GUIException {
069: Widget widget = super .getEditor(table, value, row, key);
070: MapDataModel dataModel = widget.getDataModel();
071: dataModel.setValue("frame", table
072: .getParentWidgetByClass(Frame.class));
073: return widget;
074: }
075: };
076: }
077:
078: public TableRenderer getRenderer() {
079: return new FontRenderer();
080: }
081:
082: public TableEditor getEditor() {
083: return fontEditor;
084: }
085:
086: public Object toValue(Object value, Element propertyNode) {
087: return value;
088: }
089:
090: public void toDOM(Object value, Element propertyNode) {
091: Font font = (Font) value;
092: Document document = propertyNode.getOwnerDocument();
093: Element nameNode = document.createElement("name");
094: Element styleNode = document.createElement("style");
095: Element sizeNode = document.createElement("size");
096: int styleID = font.getStyle();
097: String style = "";
098: if ((styleID & Font.BOLD) != 0 && (styleID & Font.ITALIC) != 0) {
099: style = "bolditalic";
100: } else if ((styleID & Font.BOLD) != 0) {
101: style = "bold";
102: } else if ((styleID & Font.ITALIC) != 0) {
103: style = "italic";
104: }
105: nameNode.appendChild(document.createTextNode(font.getName()));
106: styleNode.appendChild(document.createTextNode(style));
107: sizeNode.appendChild(document.createTextNode(String
108: .valueOf(font.getSize())));
109: propertyNode.appendChild(nameNode);
110: propertyNode.appendChild(styleNode);
111: propertyNode.appendChild(sizeNode);
112: }
113:
114: public void eventOccured(GUIEvent event) {
115: try {
116: JFontChooser fontChooser = new JFontChooser();
117: fontChooser.setSelectedFont((Font) event.getSource()
118: .getDataModel().getValue("value"));
119:
120: int result = fontChooser.showDialog(((Frame) event
121: .getSource().getDataModel().getValue("frame"))
122: .getRealWidget());
123: if (result == JFontChooser.OK_OPTION) {
124: event.getSource().getDataModel().setValue("value",
125: fontChooser.getSelectedFont());
126: }
127: } catch (Exception e) {
128: new MessageDialog(e);
129: }
130: }
131: }
|