KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > jbuilder > wizard > AbstractCodeGenWizard


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

23 package org.enhydra.kelp.jbuilder.wizard;
24
25 // ToolBox imports
26
import org.enhydra.tool.ToolBoxInfo;
27 import org.enhydra.tool.common.IconSet;
28 import org.enhydra.tool.common.ToolException;
29 import org.enhydra.tool.codegen.ValidationException;
30 import org.enhydra.tool.codegen.GeneratorException;
31 import org.enhydra.tool.codegen.wizard.CodeGenPanel;
32
33 // JBuilder imports
34
import com.borland.jbuilder.JBuilderHelp;
35 import com.borland.jbuilder.build.JavaBuildPropertyGroup;
36 import com.borland.jbuilder.node.JBProject;
37 import com.borland.jbuilder.node.HTMLFileNode;
38 import com.borland.jbuilder.paths.JDKPathSet;
39 import com.borland.jbuilder.paths.PathSetManager;
40 import com.borland.jbuilder.wizard.project.JBProjectWizard;
41 import com.borland.primetime.help.ZipHelpBook;
42 import com.borland.primetime.help.ZipHelpTopic;
43 import com.borland.primetime.ide.Browser;
44 import com.borland.primetime.ide.BrowserIcons;
45 import com.borland.primetime.node.Node;
46 import com.borland.primetime.node.Project;
47 import com.borland.primetime.node.DuplicateNodeException;
48 import com.borland.primetime.util.VetoException;
49 import com.borland.primetime.vfs.Url;
50 import com.borland.primetime.wizard.BasicWizard;
51 import com.borland.primetime.wizard.BasicWizardPage;
52 import com.borland.primetime.wizard.Wizard;
53 import com.borland.primetime.wizard.WizardAction;
54 import com.borland.primetime.wizard.WizardDialog;
55 import com.borland.primetime.wizard.WizardHost;
56 import com.borland.primetime.wizard.WizardManager;
57 import com.borland.primetime.wizard.WizardPage;
58
59 // AddinCore imports
60
import org.enhydra.kelp.KelpInfo;
61 import org.enhydra.kelp.common.Constants;
62 import org.enhydra.kelp.common.ResUtil;
63 import org.enhydra.kelp.common.node.OtterFolderNode;
64 import org.enhydra.kelp.common.node.OtterNodeFactory;
65 import org.enhydra.kelp.common.node.OtterProject;
66 import org.enhydra.kelp.common.codegen.ProjectGenUtil;
67 import org.enhydra.kelp.common.codegen.CodeGenException;
68
69 // AddinJBuilder imports
70
import org.enhydra.kelp.jbuilder.node.PrimeProject;
71 import org.enhydra.kelp.jbuilder.node.PrimeProject;
72
73 // Standard imports
74
import java.awt.BorderLayout JavaDoc;
75 import java.io.File JavaDoc;
76 import java.util.Properties JavaDoc;
77 import java.util.ArrayList JavaDoc;
78 import java.util.Iterator JavaDoc;
79 import javax.swing.JOptionPane JavaDoc;
80 import java.util.ResourceBundle JavaDoc;
81
82 //
83
abstract public class AbstractCodeGenWizard extends AbstractBasicWizard {
84     static ResourceBundle JavaDoc res =
85         ResourceBundle.getBundle("org.enhydra.kelp.jbuilder.Res"); // nores
86

87     //
88
private ProjectGenUtil util = null;
89     private JBProject nativeProject = null;
90
91     public abstract String JavaDoc getHelpTopic();
92     public abstract String JavaDoc getDisplayName();
93
94     /**
95      * Method declaration
96      *
97      *
98      * @param currentPage
99      * @param host
100      */

101     public void help(WizardPage wizardPage, WizardHost wizardHost) {
102         ZipHelpTopic topic = null;
103         ZipHelpBook book = null;
104
105         book = JBuilderHelp.createBook(Constants.KELP);
106         topic = new ZipHelpTopic(book, getHelpTopic());
107         topic.show(wizardPage.getPageComponent(wizardHost));
108     }
109
110     /**
111      * Method declaration
112      *
113      *
114      * @param host
115      *
116      * @return
117      */

118     abstract public WizardPage invokeWizard(WizardHost host);
119
120     /**
121      * Method declaration
122      *
123      * @param host
124      *
125      * @return
126      */

127     public WizardPage invokeCodeGenWizard(WizardHost host, ProjectGenUtil util) {
128         WizardPage wizardPage = null;
129         boolean invoke = true;
130
131         this.util = util;
132         if (!isProjectEmpty()) {
133             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134             int choice = JOptionPane.CANCEL_OPTION;
135
136             buf.append(res.getString("NotEmptyWarning1"));
137             buf.append(res.getString("NotEmptyWarning2"));
138             buf.append(res.getString("NotEmptyWarning3"));
139             choice =
140                 JOptionPane.showConfirmDialog(host.getDialogParent(),
141                                               buf.toString(),
142                                               ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()),
143                                               JOptionPane.WARNING_MESSAGE);
144             if (choice == JOptionPane.CANCEL_OPTION) {
145                 invoke = false;
146             }
147         }
148         if (invoke) {
149             try {
150                 setWizardTitle(ResUtil.format(res.getString("_0_Wizard"),
151                                               getDisplayName()));
152                 if ((nativeProject != null)
153                         && (nativeProject instanceof JBProject)) {
154                     for (int i = 0; i < util.getWizardPanels().length; i++) {
155                         LocalWizardPage page = new LocalWizardPage();
156
157                         page.setPanel(util.getWizardPanels()[i]);
158                         addWizardPage(page);
159                     }
160                     wizardPage = super.invokeWizard(host);
161                 }
162             } catch (CodeGenException e) {
163                 JOptionPane.showMessageDialog(host.getDialogParent(),
164                                               e.getMessage(),
165                                               ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()),
166                                               JOptionPane.WARNING_MESSAGE);
167             }
168         }
169         return wizardPage;
170     }
171
172     /**
173      * Method declaration
174      *
175      *
176      * @return
177      */

178     private boolean isProjectEmpty() {
179         Node[] nodes = null;
180         boolean empty = true;
181
182         if (nativeProject != null) {
183             nodes = nativeProject.getChildren();
184         }
185         if (nodes != null) {
186             if (nodes.length > 1) {
187                 empty = false;
188             } else if (nodes.length == 1) {
189                 if (!(nodes[0] instanceof HTMLFileNode)) {
190                     empty = false;
191                 }
192             }
193         }
194         return empty;
195     }
196
197     /**
198      * Method declaration
199      *
200      *
201      * @param host
202      */

203     protected OtterProject initProject(WizardHost host) {
204         OtterProject otterProj = null;
205
206         if (host.getBrowser().getActiveProject() == null) {
207             nativeProject = null;
208         } else if (host.getBrowser().getActiveProject()
209                    instanceof JBProject) {
210             nativeProject = (JBProject) host.getBrowser().getActiveProject();
211         }
212         if (nativeProject == null) {
213             WizardDialog dlg = null;
214             JBProjectWizard wiz = null;
215
216             wiz = new JBProjectWizard();
217             dlg = new WizardDialog(host.getBrowser(), wiz);
218             dlg.show();
219             if (host.getBrowser().getActiveProject() == null) {
220                 nativeProject = null;
221             } else if (host.getBrowser().getActiveProject()
222                        instanceof JBProject) {
223                 nativeProject =
224                     (JBProject) host.getBrowser().getActiveProject();
225             }
226         }
227         if (nativeProject != null) {
228             if (nativeProject instanceof JBProject) {
229                 otterProj = new PrimeProject((JBProject) nativeProject);
230             }
231         }
232         return otterProj;
233     }
234
235     /**
236      * Method declaration
237      *
238      *
239      * @throws VetoException
240      */

241     public WizardPage finish(WizardPage currentPage,
242                              WizardHost host) throws VetoException {
243         JDKPathSet jdk = null;
244         boolean continueBuild = true;
245
246         jdk = getTargetJDK();
247         if (currentPage instanceof BasicWizardPage) {
248             BasicWizardPage basicPage = (BasicWizardPage) currentPage;
249
250             basicPage.checkPage();
251         }
252         if (nativeProject != null && util != null) {
253             try {
254                 if (getSteps().length > 0) {
255                   util.getGenerator().setAddinSteps(getSteps());
256                 }
257                 if (jdk != null) {
258                     String JavaDoc javaPath = new String JavaDoc();
259
260                     javaPath =
261                         jdk.getHomePath().getFileObject().getAbsolutePath();
262                     util.getGenerator().setJavaPath(javaPath);
263                 }
264                 util.generate();
265                 nativeSetupProject(jdk);
266                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
267
268                 buf.append(ResUtil.format(res.getString("AddedFiles"),
269                                           getDisplayName()));
270                 buf.append('\n');
271                 buf.append(Constants.TAB4);
272                 buf.append(nativeProject.getDisplayName());
273                 buf.append('\n');
274                 buf.append(res.getString("ReferReadme1"));
275                 buf.append(ResUtil.format(res.getString("ReferReadme2"),
276                                           getDisplayName()));
277                 JOptionPane.showMessageDialog(host.getDialogParent(),
278                                               buf.toString());
279                 openReadme(host.getBrowser());
280             } catch (CodeGenException e) {
281                 JOptionPane.showMessageDialog(this.wizardHost.getDialogParent(),
282                                               e.getMessage(),
283                                               ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()),
284                                               JOptionPane.WARNING_MESSAGE);
285                 throw new VetoException();
286             }
287         }
288         return null;
289     }
290
291     /**
292      * Method declaration
293      *
294      */

295     private void nativeSetupProject(JDKPathSet jdk) {
296         File JavaDoc sourceDir = null;
297         if (!nativeProject.getPaths().putClassOnFullPath(ToolBoxInfo.getEnhydraMainClass())) {
298
299             // no enhydra library
300
}
301         if (!ToolBoxInfo.isEnhydra3()) {
302             if (jdk != null) {
303                 nativeProject.getPaths().setJDKPathSet(jdk);
304             }
305         }
306         JavaBuildPropertyGroup.SYNC_DIR.setValue(nativeProject,
307                                                  String.valueOf(false));
308     }
309
310     private JDKPathSet getTargetJDK() {
311         ArrayList JavaDoc jdkList = PathSetManager.getJDKs();
312         Iterator JavaDoc jdkIterator = jdkList.iterator();
313         JDKPathSet jdk = null;
314
315         while (jdkIterator.hasNext()) {
316             Object JavaDoc next = jdkIterator.next();
317
318             if (next instanceof JDKPathSet) {
319                 jdk = (JDKPathSet) next;
320                 if (jdk.getName().indexOf(Constants.JDK13) > 0) {
321                     break;
322                 }
323             }
324         }
325         return jdk;
326     }
327
328     private void openReadme(Browser browser) {
329         Node[] list = new Node[0];
330         Node node = null;
331
332         list = nativeProject.getChildren();
333         for (int i = 0; i < list.length; i++) {
334             if (list[i].getDisplayName().toLowerCase().endsWith(Constants.FILE_README)) {
335                 node = list[i];
336                 break;
337             }
338         }
339         list = new Node[0];
340         try {
341             browser.setActiveNode(node, true);
342         } catch (Exception JavaDoc e) {
343             e.printStackTrace();
344         }
345     }
346
347     /**
348      * Method declaration
349      *
350      *
351      * @return
352      */

353     abstract public String JavaDoc[] getSteps();
354
355     /**
356      * Class declaration
357      *
358      *
359      * @author Paul Mahar
360      */

361     private class LocalWizardPage extends BasicWizardPage {
362         private CodeGenPanel panel;
363
364         /**
365          * Constructor declaration
366          *
367          */

368         public LocalWizardPage() {
369             BorderLayout JavaDoc layout = new BorderLayout JavaDoc();
370
371             layout.setHgap(5);
372             layout.setVgap(5);
373             this.setLayout(layout);
374             if (KelpInfo.isToolBoxVersionInSynch()) {
375               this.setLargeIcon(IconSet.getOtterWizard());
376             }
377         }
378
379         /**
380          * Method declaration
381          *
382          *
383          * @param host
384          */

385         public void activated(WizardHost host) {
386             host.setFinishEnabled(true);
387         }
388
389         public void checkPage() throws VetoException {
390             try {
391                 panel.validateOptionSet();
392             } catch (ValidationException e) {
393                 JOptionPane.showMessageDialog(panel.getTopLevelAncestor(),
394                                               e.getValidationMessage(),
395                                               ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()),
396                                               JOptionPane.WARNING_MESSAGE);
397                 throw new VetoException(e.getValidationMessage());
398             }
399         }
400
401         protected void setPanel(CodeGenPanel p) {
402             panel = p;
403             setPageTitle(panel.getPageTitle());
404             setInstructions(panel.getInstructions());
405             add(panel, BorderLayout.CENTER);
406         }
407
408         protected CodeGenPanel getPanel() {
409             return panel;
410         }
411
412     }
413 }
414
Popular Tags