KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > jdev > RealWizard


1 package org.enhydra.kelp.jdev;
2
3 import org.enhydra.kelp.ant.node.AntProject;
4
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Image JavaDoc;
7
8 import java.io.*;
9
10 import java.net.URL JavaDoc;
11
12 import javax.swing.*;
13
14 import oracle.bali.ewt.wizard.ImageWizardPage;
15 import oracle.bali.ewt.wizard.Wizard;
16 import oracle.bali.ewt.wizard.WizardDialog;
17 import oracle.bali.ewt.wizard.WizardEvent;
18 import oracle.bali.ewt.wizard.WizardListener;
19
20 import oracle.ide.addin.Context;
21 import oracle.ide.model.NodeFactory;
22 import oracle.ide.model.Workspace;
23 import oracle.ide.net.URLPath;
24 import oracle.ide.util.GraphicsUtils;
25 import oracle.ide.wizard.WizardWelcomePage;
26
27 import oracle.ideimpl.explorer.ExplorerNode;
28 import oracle.ideimpl.navigator.NavigatorWindowImpl;
29
30 import oracle.jdeveloper.library.JDK;
31 import oracle.jdeveloper.library.JLibraryManager;
32 import oracle.jdeveloper.model.JProject;
33
34 import oracle.ide.Ide;
35 import oracle.jdeveloper.library.JProjectLibraryList;
36 import oracle.jdeveloper.library.JLibraryList;
37
38 import org.enhydra.kelp.jdev.codegen.wizard.JDevCodeGenWizard;
39 import org.enhydra.tool.ToolBoxInfo;
40 import java.util.LinkedList JavaDoc;
41 import oracle.jdeveloper.model.JProjectSettings;
42 import java.util.TreeMap JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import oracle.jdeveloper.model.JProjectConfiguration;
45 import oracle.jdeveloper.runner.RunConfiguration;
46 import oracle.jdeveloper.compiler.ant.AntConfiguration;
47 import javax.swing.JOptionPane JavaDoc;
48
49 //import org.enhydra.kelp.ant.node.AntProject;
50

51 public class RealWizard extends Wizard
52                         implements WizardListener
53 {
54   private final static String JavaDoc WIZARD_LEFT_IMAGE = "/media/otterwizard.jpg";
55
56   private Context context;
57   private WizardDialog wDialog = null;
58
59   private String JavaDoc enProjRoot = null;
60   private String JavaDoc enProjName = null;
61
62   ImageWizardPage[] wizardPage;
63
64   WizardWelcomePage wwp;
65   ImageWizardPage iwpOne;
66
67   private Model model;
68
69   public RealWizard(Context ctx)
70   {
71     this.context = ctx;
72     this.model = new Model();
73     String JavaDoc wsPath = Ide.getActiveWorkspace().getURL().getFile();
74
75     if ((wsPath.charAt(0) == '/')&&('/' != java.io.File.separatorChar)){
76       wsPath=wsPath.substring(1);
77     }
78
79     model.setProjectRoot(wsPath.substring(0, wsPath.lastIndexOf("/")));
80
81     this.setPreferredSize(new Dimension JavaDoc(166, 387));
82
83     wwp = createWelcomePage();
84     this.addPage(wwp.getWizardPage());
85
86     iwpOne = createWizardPageOne(model);
87     this.addPage(iwpOne);
88     PanelOne panelOne = (PanelOne)iwpOne.getInteractiveArea();
89     panelOne.setDefaultValues();
90     iwpOne.addWizardValidateListener(panelOne);
91
92     if (!wwp.isHidden())
93       this.setSelectedPage(wwp.getWizardPage());
94     else
95       this.setSelectedPage(iwpOne);
96
97     this.setMustFinish(true);
98
99     wizardPage = new ImageWizardPage[]
100     {
101       wwp.getWizardPage(),
102       iwpOne,
103     };
104     // Create wizard Dialog
105
wDialog = createWizardDialog();
106     wDialog.setTitle("Create Enhydra Application");
107
108     this.addWizardListener(this);
109     wDialog.runDialog();
110   }
111
112   public void wizardApplyState(WizardEvent p0)
113   {
114   }
115   public void wizardCanceled(WizardEvent p0)
116   {
117   }
118   public void wizardFinished(WizardEvent p0)
119   {
120     JDevCodeGenWizard wizard = null;
121     JProject enhydraProj = null;
122     int canceled = 0;
123     try
124     {
125       wizard = new JDevCodeGenWizard(model.getProjectName(),
126                                      model.getProjectRoot());
127       wizard.getInnerPanel().initPreferredSize();
128       canceled = wizard.showDialog(null);
129     } catch (Exception JavaDoc ex)
130     {
131       ex.printStackTrace();
132     }
133     // create enhydra project now
134
if (canceled != JOptionPane.CANCEL_OPTION)
135     {
136       enhydraProj = createEnhydraProject();
137       if (enhydraProj == null) {
138         String JavaDoc msg = "Error creating Enhydra project";
139         oracle.ide.Ide.getLogManager().getMsgPage().log(msg + "\n");
140         JOptionPane.showMessageDialog(null,
141                                       msg,
142                                       "Ooops",
143                                       JOptionPane.ERROR_MESSAGE);
144         return;
145       }
146       Workspace ws = Ide.getActiveWorkspace();
147       ws.add(enhydraProj);
148
149       ws.setActiveProject(enhydraProj);
150       refreshNavigator(ws, enhydraProj);
151     }
152   }
153
154   private JProject createEnhydraProject()
155   {
156     setEnProjName(this.model.getProjectName());
157     setEnProjRoot((this.model.getProjectRoot()).replace(File.separatorChar, '/'));
158     JProject enhydraProj = null;
159     String JavaDoc prjPath = enProjRoot + "/" + enProjName;
160
161     AntProject antProject = null;
162     try
163     {
164       antProject = new AntProject(prjPath);
165       enhydraProj = (JProject)NodeFactory.findOrCreate(JProject.class,
166          new URL JavaDoc("file:///" + enProjRoot + "/" + enProjName + "/" + enProjName + ".jpr"));
167       enhydraProj.setUsesDynamicPaths(true);
168       URL JavaDoc[] enProjSrc = null;
169       enProjSrc = new URL JavaDoc[] {new URL JavaDoc("file:///" +
170                       antProject.getProperty(AntProject.SRC_DIR)),
171                   new URL JavaDoc("file:///" +
172                       antProject.getProperty(AntProject.XMLC_GENERETED_SRC_DIR)),
173                   new URL JavaDoc("file:///" +
174                       antProject.getProperty(AntProject.DODS_GENERATED_SRC_DIR)),
175                   new URL JavaDoc("file:///" +
176                       antProject.getProperty(AntProject.INPUT_DIR))};
177       enhydraProj.setProjectSourcePath(new URLPath(enProjSrc));
178       enhydraProj.setOutputDirectory(new URL JavaDoc("file:///" +
179         antProject.getProperty(AntProject.CLASSES_DIR)));
180       enhydraProj.setHtmlRootDirectory(new URL JavaDoc("file:///" +
181         antProject.getProperty(AntProject.RESOURCES_DIR)));
182
183     } catch (Exception JavaDoc ex)
184     {
185       ex.printStackTrace();
186     }
187       JDK jdk = setJDK1_4(enhydraProj);
188
189       JProjectConfiguration enProjConfig = enhydraProj.getActiveConfiguration();
190       String JavaDoc enhydraRoot = ToolBoxInfo.getEnhydraRoot();
191       // set project's classpath
192
String JavaDoc jdkBinDir = "";
193       if (jdk != null)
194         jdkBinDir = jdk.getSDKBinDir().getPath();
195       URLPath enProjLibs = createAllLibs(enhydraRoot, jdkBinDir);
196       enProjConfig.setAdditionalClassPath(enProjLibs);
197
198       enProjConfig.setJdkName(
199         (((JDK)(enhydraProj.getProjectLibraryList().getJdkList().get(0))).getName()) );
200       enProjConfig.setJdkVersionNumber(
201         (((JDK)(enhydraProj.getProjectLibraryList().getJdkList().get(0))).getJavaVersion()) );
202
203       // set ant config
204
AntConfiguration antConfig = (AntConfiguration)enhydraProj.getProjectSettings().
205           getCommonData().get("oracle.jdeveloper.compiler.ant.AntConfiguration");
206       try
207       {
208         antConfig.setProjectBuildfile(
209             new URL JavaDoc("file:///" + enProjRoot + "/" + enProjName + "/build.xml"));
210       } catch (Exception JavaDoc ex)
211       {
212         ex.printStackTrace();
213       }
214       antConfig.setMakeTarget("make");
215       antConfig.setRebuildTarget("rebuild");
216
217       setRunConfig(antProject, enProjConfig);
218
219     return enhydraProj;
220   }
221
222   private void refreshNavigator(Workspace ws, JProject enhydraProj)
223   {
224     NavigatorWindowImpl navWin = (NavigatorWindowImpl)
225             Ide.getNavigatorManager().getSystemNavigator();
226     navWin.show();
227     ExplorerNode eNode = (ExplorerNode)navWin.getTreeExplorer().getRoot();
228     eNode = (ExplorerNode)navWin.findTNode(ws, eNode);
229     navWin.refresh(eNode);
230     eNode = (ExplorerNode)navWin.findTNode(enhydraProj, eNode);
231     navWin.expand(eNode, true);
232     navWin.show();
233   }
234
235   private JDK setJDK1_4(JProject enhydraProj)
236   {
237     JProjectLibraryList jProjLibList = new JProjectLibraryList();
238     java.util.Iterator JavaDoc jdkIterator = JLibraryManager.getJDKs().iterator();
239     JDK jdk = null;
240     String JavaDoc version = null;
241     boolean found1_4 = false;
242     while ( jdkIterator.hasNext() &&
243             !(found1_4) )
244     {
245       jdk = (JDK)jdkIterator.next();
246       version = jdk.getJavaVersion().toString().substring(0,3);
247       if (Float.parseFloat(version) >= Float.parseFloat("1.4"))
248       {
249         LinkedList JavaDoc lList = new LinkedList JavaDoc();
250         lList.add(jdk);
251         jProjLibList.setJdkList(lList);
252         jProjLibList.setOwner(null);
253         jProjLibList.setLibraryList(null);
254         found1_4 = true;
255       }
256     }
257     if (found1_4)
258     {
259       enhydraProj.setProjectLibraryList(jProjLibList);
260     } else
261     {
262       String JavaDoc msg = "Enhydra Applications need JDK 1.4 or higher to run.\nPlease install JDK 1.4 or higher.";
263       JOptionPane.showMessageDialog(null,
264                                     msg,
265                                     "Ooops",
266                                     JOptionPane.ERROR_MESSAGE);
267
268     }
269     return jdk;
270   }
271
272   private URLPath createAllLibs(String JavaDoc enhydraRoot, String JavaDoc jdkBinDir)
273   {
274     URLPath enLibs = new URLPath();
275     File file = new File(enhydraRoot + "/lib");
276     enLibs.add(createEnhydraLibURLs(file));
277     file = new File(enhydraRoot + "/dods/lib");
278     enLibs.add(createEnhydraLibURLs(file));
279     file = new File(enhydraRoot + "/lib/build");
280     enLibs.add(createEnhydraLibURLs(file));
281     enLibs.add(createEnhydraLibURLs(file));
282     
283     file = new File(enhydraRoot + "/lib/dom");
284     enLibs.add(createEnhydraLibURLs(file));
285     try
286     {
287       enLibs.add(new URL JavaDoc("jar:file:///" + enhydraRoot + "/build/anttask.jar!/"));
288       enLibs.add(new URL JavaDoc("jar:file:///" + enhydraRoot + "/build/xmlc-taskdef.jar!/"));//SV
289
if (!jdkBinDir.equals(""))
290         enLibs.add(new URL JavaDoc("jar:file:///" + jdkBinDir + "../lib/tools.jar!/"));
291     } catch (Exception JavaDoc ex)
292     {
293       ex.printStackTrace();
294     }
295     return enLibs;
296   }
297
298   private URLPath createEnhydraLibURLs(File file)
299   {
300                 // filter for dirs; jar and zip files
301
URLPath dirLibs = new URLPath();
302     ArchiveFilter archives = new ArchiveFilter();
303                 File[] fileList = null;
304                 if (file.isDirectory()) {
305                         fileList = file.listFiles(archives);
306                         for (int i = 0; i < fileList.length; i++) {
307                                 File fileListElem = fileList[i];
308                                 if (!fileListElem.isDirectory()) {
309                                         try {
310             dirLibs.add(new URL JavaDoc("jar:file:///" + fileListElem.getPath().replace('\\', '/') + "!/"));
311                                         } catch (Exception JavaDoc e) {
312                                                 e.printStackTrace();
313                                         }
314                                 }
315                         }
316                 } else {
317                         System.err.println(file.getName() +" is not a directory.");
318                 }
319     return dirLibs;
320         }
321
322   private void setRunConfig(AntProject antProject, JProjectConfiguration enProjConfig)
323   {
324     // set project's run configuration
325
RunConfiguration enRunConfig =
326       (RunConfiguration)enProjConfig.getConfigData().get(
327           "oracle.jdeveloper.runner.RunConfiguration");
328     enRunConfig.setRunActiveFile(false);
329     String JavaDoc enhydraRoot = ToolBoxInfo.getEnhydraRoot();
330     File file = new File(enhydraRoot);
331     try
332     {
333       URL JavaDoc multiserver = new URL JavaDoc("jar:file:///" + file.getPath() +
334           "/lib/enhydra.jar!/com/lutris/multiServer/MultiServer.class");
335       enRunConfig.setTargetURL(multiserver);
336       enRunConfig.setRunDirectoryURL(new URL JavaDoc("file:///" +
337                                               antProject.getProperty(AntProject.OUTPUT_DIR)));
338     } catch (Exception JavaDoc ex)
339     {
340       ex.printStackTrace();
341     }
342     enRunConfig.setProgramArguments(
343                                     antProject.getProperty(AntProject.OUTPUT_DIR) +
344                                     "/conf/multiserver.conf");
345     enRunConfig.setClearLogBeforeRun(true);
346     enRunConfig.setJavaOptions("-Denhydra.home=\"" + enhydraRoot + "\"");
347   }
348   public void wizardSelectionChanged(WizardEvent wEvent)
349   {
350
351     int pageNo = 0;
352     try { pageNo = wEvent.getPage().getIndex(); }
353     catch (Exception JavaDoc ignore) { }
354
355     if (pageNo == 0) // to Welcome
356
;
357     else if (pageNo == 1)
358     {
359       PanelOne p1 = (PanelOne)iwpOne.getInteractiveArea();
360       p1.setDefaultValues();
361     }
362   }
363
364   private WizardWelcomePage createWelcomePage()
365   {
366     // This key is used to remember the "Show next time" check box!
367
final String JavaDoc strShowAgainKey = "APP_WIZARD";
368     WizardWelcomePage wp = new WizardWelcomePage(strShowAgainKey);
369     // Set the image
370
java.awt.Image JavaDoc img = GraphicsUtils.loadFromResource(WIZARD_LEFT_IMAGE, this.getClass());
371
372     if (img != null)
373       wp.setImage(img);
374     else
375       System.out.println(WIZARD_LEFT_IMAGE + " not found");
376     // Set the Title
377
final String JavaDoc welcomeTitle = "Creating Enhydra Application";
378     wp.setTitle(welcomeTitle);
379     // Set the Description Text for first page
380
final String JavaDoc welcomeDesc = "\n\n" +
381       "This wizard will help you to create Enhydra application.\n" +
382       "Please, enter project name and path to the project root folder.\n" +
383       "A new folder with the name of the project will be created\n" +
384       "under the root.\n" +
385       "\n\n" +
386       "Press Next to continue\n";
387     wp.setDescription(welcomeDesc);
388     // Return results
389
return wp;
390   }
391
392   private ImageWizardPage createWizardPageOne(Model m)
393   {
394     PanelOne panel = new PanelOne(m);
395     final String JavaDoc title = "Input parameters";
396     Image JavaDoc img = GraphicsUtils.createImageIcon(GraphicsUtils.loadFromResource(WIZARD_LEFT_IMAGE, this.getClass())).getImage();
397     ImageWizardPage iwp = new ImageWizardPage(panel, img, title);
398     return iwp;
399   }
400
401   private WizardDialog createWizardDialog()
402   {
403     WizardDialog wdlg = new WizardDialog(this, Ide.getMainWindow());
404     return wdlg;
405   }
406
407   public void setEnProjRoot(String JavaDoc root)
408   {
409     enProjRoot = root;
410   }
411
412   public String JavaDoc getEnProjRoot()
413   {
414     return enProjRoot;
415   }
416
417   public void setEnProjName(String JavaDoc root)
418   {
419     enProjName = root;
420   }
421
422   public String JavaDoc getEnProjName()
423   {
424     return enProjName;
425   }
426
427 }
428
Popular Tags