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.io.File;
025: import java.net.URL;
026:
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.dom.DOMSource;
030: import javax.xml.transform.stream.StreamResult;
031: import javax.xml.transform.stream.StreamSource;
032:
033: import org.beryl.gui.Controller;
034: import org.beryl.gui.DialogUtils;
035: import org.beryl.gui.GUIEvent;
036: import org.beryl.gui.GUIException;
037: import org.beryl.gui.MessageDialog;
038: import org.beryl.gui.model.MapDataModel;
039: import org.beryl.gui.model.ModelChangeEvent;
040: import org.beryl.gui.model.ModelChangeListener;
041: import org.beryl.gui.widgets.Button;
042: import org.beryl.gui.widgets.Dialog;
043: import org.beryl.gui.widgets.Frame;
044: import org.w3c.dom.Document;
045:
046: public class SkeletonDialog extends Controller {
047: private static String SKELETON_XSL = "/resources/builder/skeleton.xsl";
048: private Dialog dialog = null;
049: private Document document = null;
050: private MapDataModel dataModel = null;
051:
052: public SkeletonDialog(Frame parent, Document document)
053: throws GUIException {
054: this .document = document;
055: dataModel = new MapDataModel();
056: dialog = constructDialog("SkeletonDialog", dataModel);
057: final Button okButton = (Button) dialog.getWidget("OKButton");
058:
059: dataModel.addModelChangeListener(new ModelChangeListener() {
060: public void modelChanged(ModelChangeEvent e)
061: throws GUIException {
062: okButton.setEnabled(dataModel.getValue("file") != null
063: && dataModel.getValue("package") != null
064: && !((String) dataModel.getValue("package"))
065: .trim().equals(""));
066: }
067: });
068:
069: okButton.setEnabled(false);
070: dialog.initDialog(parent);
071: dialog.show();
072: }
073:
074: public void eventOccured(GUIEvent e) {
075: String eventName = e.getName();
076:
077: try {
078: if (eventName.equals("cancel")) {
079: dialog.dispose();
080: } else if (eventName.equals("ok")) {
081: String pkg = (String) dataModel.getValue("package");
082: File file = (File) dataModel.getValue("file");
083:
084: URL url = this .getClass().getResource(SKELETON_XSL);
085: Transformer transformer = TransformerFactory
086: .newInstance().newTransformer(
087: new StreamSource(url.openStream()));
088: if (pkg.indexOf('.') != -1) {
089: transformer.setParameter("package", pkg.substring(
090: 0, pkg.lastIndexOf('.')));
091: transformer.setParameter("class", pkg.substring(pkg
092: .lastIndexOf(".") + 1, pkg.length()));
093: } else {
094: transformer.setParameter("package", "");
095: transformer.setParameter("class", pkg);
096: }
097:
098: transformer.transform(new DOMSource(document),
099: new StreamResult(file));
100:
101: dialog.dispose();
102: } else {
103: File newFile = DialogUtils.showSaveFileDialog(dialog,
104: "java");
105: if (newFile != null) {
106: dataModel.setValue("file", newFile);
107: }
108: }
109: } catch (Exception ex) {
110: new MessageDialog(dialog, ex);
111: }
112: }
113: }
|