001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.codegen;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.io.BufferedWriter;
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.io.OutputStreamWriter;
027: import java.util.HashMap;
028:
029: import com.jeta.forms.gui.common.FormUtils;
030: import com.jeta.forms.store.memento.FormMemento;
031: import com.jeta.open.gui.framework.JETADialog;
032: import com.jeta.open.gui.framework.JETADialogListener;
033: import com.jeta.open.gui.framework.JETAPanel;
034: import com.jeta.open.gui.utils.JETAToolbox;
035: import com.jeta.open.i18n.I18N;
036: import com.jeta.swingbuilder.codegen.builder.DefaultSourceBuilder;
037: import com.jeta.swingbuilder.codegen.gui.editor.SourceEditor;
038: import com.jeta.swingbuilder.gui.components.TSErrorDialog2;
039: import com.jeta.swingbuilder.gui.filechooser.FileChooserConfig;
040: import com.jeta.swingbuilder.gui.filechooser.TSFileChooserFactory;
041: import com.jeta.swingbuilder.gui.filechooser.TSFileFilter;
042: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
043: import com.jeta.swingbuilder.store.CodeModel;
044:
045: /**
046: * Front end class for invoking code generation.
047: *
048: * @author Jeff Tassin
049: */
050: public class ForwardEngineer {
051: /**
052: * This is a map of form ids to the last filename used to store generated
053: * code for that form. It is used to popuplate the file save dialog so the
054: * user does not have to re-type the file name every time.
055: */
056: private static HashMap m_file_names = new HashMap();
057:
058: public ForwardEngineer() {
059:
060: }
061:
062: public void generate(Component invoker, FormMemento fm) {
063: try {
064: FormUtils.setDesignMode(false);
065:
066: CodeModel cgenmodel = CodeModel.createInstance(fm);
067:
068: String txt = DefaultSourceBuilder
069: .buildSource(cgenmodel, fm);
070: final SourceEditor editor = new SourceEditor(txt, fm);
071: JETAPanel panel = new JETAPanel(new BorderLayout());
072: panel.add(editor, BorderLayout.CENTER);
073: panel.setPreferredSize(FormDesignerUtils
074: .getWindowDimension(panel, 420, 240));
075:
076: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
077: JETADialog.class, invoker, true);
078: dlg.setTitle(I18N.getLocalizedMessage("Code Generation"));
079: dlg.setPrimaryPanel(panel);
080: dlg.setSize(dlg.getPreferredSize());
081: dlg.setOkText(I18N.getLocalizedMessage("Save"));
082:
083: final Component parent = invoker;
084: final String formid = fm.getId();
085: dlg.addDialogListener(new JETADialogListener() {
086: public boolean cmdOk() {
087: FileChooserConfig fcc = new FileChooserConfig(
088: ".java", new TSFileFilter("java",
089: "Java Files(*.java)"));
090: fcc.setParentComponent(parent);
091: fcc.setInitialDirectory((String) m_file_names
092: .get(formid));
093: File file = TSFileChooserFactory
094: .showSaveDialog(fcc);
095: if (file == null)
096: return false;
097: else {
098: try {
099:
100: String path = file.getPath();
101: int pos = path.lastIndexOf(".java");
102: if (pos != path.length() - 5) {
103: path = path + ".java";
104: file = new File(path);
105: }
106:
107: FileOutputStream fos = new FileOutputStream(
108: file);
109: BufferedWriter bw = new BufferedWriter(
110: new OutputStreamWriter(fos));
111: bw.write(editor.getText());
112: bw.close();
113: m_file_names.put(formid, file
114: .getCanonicalPath());
115: return true;
116: } catch (Exception e) {
117: TSErrorDialog2 dlg = TSErrorDialog2
118: .createDialog(parent, "Error",
119: "Unable to save file", e);
120: dlg.showCenter();
121: return false;
122: }
123: }
124: }
125: });
126: dlg.showCenter();
127: } finally {
128: FormUtils.setDesignMode(true);
129: }
130:
131: }
132: }
|