KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > codegen > wizard > CodeGenWizard


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.codegen.wizard;
25
26 // ToolBox imports
27
import org.enhydra.tool.ToolBoxInfo;
28 import org.enhydra.tool.common.SwingUtil;
29 import org.enhydra.tool.common.wizard.TBWizard;
30 import org.enhydra.tool.codegen.CodeGen;
31 import org.enhydra.tool.codegen.Generator;
32 import org.enhydra.tool.codegen.GeneratorException;
33 import org.enhydra.tool.codegen.ValidationException;
34
35 //
36
// Standard imports
37
//
38
import javax.swing.*;
39 import javax.swing.border.*;
40 import java.awt.*;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.io.File JavaDoc;
44 import java.beans.*;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.ResourceBundle JavaDoc;
48
49 /**
50  * The CodeGenDialog defines a default container for presenting
51  * wizards. To use this class you need to a create one or more
52  * CodeGenPage objects and add them to this dialog. This dialog
53  * supplies action buttons for the Previous, Next, Finish,
54  * Cancel and Help.
55  * <P><I>
56  * This class is used when creating a standalone wizard. Use the
57  * OpenTools BasicWizard when developing JBuilder add-ins.
58  * </I></P>
59  */

60 public class CodeGenWizard extends TBWizard implements RootEditor {
61
62     //
63
private static ResourceBundle JavaDoc cgRes =
64         ResourceBundle.getBundle("org.enhydra.tool.codegen.Res"); // nores
65
private final static String JavaDoc CODEGEN_TITLE = "Kelp Application Wizard";
66
67     //
68
private File JavaDoc[] files = new File JavaDoc[0];
69     private GenSelectionPanel selectionPanel = null;
70     private CodeGen codeGen = null;
71     private GenSelectionListener[] selectionListeners =
72         new GenSelectionListener[0];
73
74     public static void main(String JavaDoc[] args) {
75         CodeGenWizard wizard = null;
76
77         try {
78             wizard = new CodeGenWizard();
79             wizard.showDialog(null);
80         } catch (GeneratorException e) {
81             e.printStackTrace(System.err);
82         }
83         System.exit(0);
84     }
85
86     public CodeGenWizard(CodeGen cg) {
87         super();
88         codeGenInit();
89         setCodeGen(cg);
90     }
91
92     public CodeGenWizard() throws GeneratorException {
93         super();
94         codeGenInit();
95         setCodeGen(new CodeGen());
96     }
97
98     // implements RootEditor
99
public boolean isAllowRootEdit() {
100         return getCodeGenDeck().isEnabled();
101     }
102
103     // implements RootEditor
104
public void setAllowRootEdit(boolean allow) {
105         getCodeGenDeck().setAllowRootEdit(allow);
106     }
107
108     /**
109      * Get an array of references to generated files.
110      *
111      * @return
112      * An array of generated files. May have a zero length or
113      * be null if no files have been generated.
114      *
115      */

116     public File JavaDoc[] getGeneratedFiles() {
117         return files;
118     }
119
120     /**
121      * Get the list of available generators.
122      *
123      * @return
124      * An array of generators from which a user can make a selection.
125      */

126     public Generator[] getGenerators() {
127         return codeGen.getGenerators();
128     }
129
130     /**
131      * Set the list of available generators.
132      *
133      * @param generators
134      * An array of generators from which a user can make a selection.
135      */

136     public void setCodeGen(CodeGen cd) {
137         codeGen = cd;
138         selectionPanel.setGenerators(codeGen.getGenerators());
139     }
140
141     public CodeGen getCodeGen() {
142         return codeGen;
143     }
144
145     /**
146      * Get the generator selected by the user.
147      *
148      * @return
149      * The generator to show options for and to use for generating a project.
150      */

151     public Generator getSelection() {
152         return getCodeGenDeck().getSelection();
153     }
154
155     /**
156      * Set the selected generator for a user and by pass the
157      * selection page.
158      *
159      * @param selection
160      * A generator containing option pages.
161      *
162      * @exception GeneratorException
163      * Thrown if unable to create option pages for the selection.
164      *
165      */

166     public void setSelection(Generator selection) throws GeneratorException {
167         getCodeGenDeck().setSelection(selection);
168         if (selection != null) {
169             getCodeGenDeck().setSelectionPageHidden(true);
170             getInnerPanel().getDialog().setTitle(selection.getWizardTitle());
171         }
172     }
173
174     // overrides TBWizard
175
public void back() {
176         if (getCodeGenDeck().isSelectionPageHidden()) {
177
178             // nothing special
179
} else if (getCodeGenDeck().getPageIndex() == 1) {
180             try {
181                 Generator select = getCodeGenDeck().getSelection();
182                 setCodeGen(new CodeGen());
183                 selectionPanel.setSelection(select);
184             } catch (GeneratorException e) {
185                 e.printStackTrace(System.err);
186             }
187         }
188         super.back();
189     }
190
191     // overrides TBWizard
192
public void next() {
193         if (getCodeGenDeck().isSelectionPageShowing()) {
194             try {
195                 Generator gen = selectionPanel.getSelection();
196
197                 codeGen.setSelection(gen);
198                 notifySelectionListeners(gen);
199
200                 // listeners my re-instatiate codegen with new selection;
201
gen = codeGen.getSelection();
202                 getCodeGenDeck().setSelection(gen);
203                 super.next();
204             } catch (GeneratorException e) {
205                 JOptionPane.showMessageDialog(getInnerPanel(),
206                                               e.getMessage(),
207                                               cgRes.getString("Generator_Error"),
208                                               JOptionPane.ERROR_MESSAGE);
209                 if (CodeGen.debug) {
210                     e.printStackTrace();
211                 }
212             }
213         } else {
214             super.next();
215         }
216     }
217
218     // overrides TBWizard
219
public void finish() {
220         Generator selection;
221
222         try {
223             getCodeGenDeck().writeOptionSet();
224             selection = selectionPanel.getSelection();
225             files = selection.generate();
226             super.finish();
227         } catch (ValidationException e) {
228             JOptionPane.showMessageDialog(getInnerPanel(),
229                                           e.getValidationMessage(),
230                                           cgRes.getString("Validation_Error"),
231                                           JOptionPane.ERROR_MESSAGE);
232         } catch (GeneratorException e) {
233             JOptionPane.showMessageDialog(getInnerPanel(), e.getMessage(),
234                                           cgRes.getString("Generation_Error"),
235                                           JOptionPane.ERROR_MESSAGE);
236         }
237     }
238
239     public static String JavaDoc getDefaultTitle() {
240         return CODEGEN_TITLE;
241     }
242
243     // override TBWizard
244
protected void clearAll() {
245         super.clearAll();
246         selectionPanel.removeAll();
247         selectionListeners = new GenSelectionListener[0];
248         selectionPanel = null;
249         codeGen = null;
250
251         // don't clear files
252
}
253
254     // implements TBWizard
255
public String JavaDoc getTitle() {
256         return CodeGenWizard.getDefaultTitle();
257     }
258
259     // implements TBWizard
260
protected String JavaDoc getProgressTitle() {
261         return "Component Generation Progress";
262     }
263
264     //
265
public synchronized void addSelectionListener(GenSelectionListener l) {
266         ArrayList JavaDoc list = null;
267
268         list = new ArrayList JavaDoc(Arrays.asList(selectionListeners));
269         if (!list.contains(l)) {
270             list.add(l);
271             list.trimToSize();
272             selectionListeners = new GenSelectionListener[list.size()];
273             selectionListeners =
274                 (GenSelectionListener[]) list.toArray(selectionListeners);
275         }
276         list.clear();
277     }
278
279     public synchronized void removeSelecitonListener(GenSelectionListener l) {
280         ArrayList JavaDoc list = null;
281
282         list = new ArrayList JavaDoc(Arrays.asList(selectionListeners));
283         if (list.contains(l)) {
284             list.remove(l);
285             list.trimToSize();
286             selectionListeners = new GenSelectionListener[list.size()];
287             selectionListeners =
288                 (GenSelectionListener[]) list.toArray(selectionListeners);
289         }
290         list.clear();
291     }
292
293     private void notifySelectionListeners(Generator gen) {
294         GenSelectionEvent event = null;
295
296         event = new GenSelectionEvent(this, gen);
297         for (int i = 0; i < selectionListeners.length; i++) {
298             selectionListeners[i].onSelection(event);
299         }
300     }
301
302     /**
303      * Add listeners and other items that are not generated by
304      * the JBuilder designer.
305      */

306     private void codeGenInit() {
307         CodeGenDeck newDeck = null;
308         CodeGenPage page = null;
309
310         newDeck = new CodeGenDeck();
311         selectionPanel = new GenSelectionPanel();
312         setDeck(newDeck);
313         page = new CodeGenPage();
314         page.setPageTitle(selectionPanel.getPageTitle());
315         page.setInstructions(selectionPanel.getInstructions());
316         page.add(selectionPanel);
317         addWizardPage(page);
318     }
319
320     private CodeGenDeck getCodeGenDeck() {
321         return (CodeGenDeck) getDeck();
322     }
323
324 }
325
Popular Tags