KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javadocexport > JavadocWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> bug 38692
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.javadocexport;
13
14 import java.io.File JavaDoc;
15 import java.io.FileWriter JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Status;
28
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.IWorkspaceRoot;
32 import org.eclipse.core.resources.ResourcesPlugin;
33
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.Shell;
36
37 import org.eclipse.jface.dialogs.ErrorDialog;
38 import org.eclipse.jface.dialogs.IDialogConstants;
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.resource.JFaceResources;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.wizard.IWizardPage;
44 import org.eclipse.jface.wizard.Wizard;
45 import org.eclipse.jface.wizard.WizardDialog;
46
47 import org.eclipse.ui.IExportWizard;
48 import org.eclipse.ui.IWorkbench;
49 import org.eclipse.ui.IWorkbenchWindow;
50 import org.eclipse.ui.PlatformUI;
51
52 import org.eclipse.debug.core.DebugEvent;
53 import org.eclipse.debug.core.DebugPlugin;
54 import org.eclipse.debug.core.IDebugEventSetListener;
55 import org.eclipse.debug.core.ILaunch;
56 import org.eclipse.debug.core.ILaunchConfigurationType;
57 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
58 import org.eclipse.debug.core.ILaunchManager;
59 import org.eclipse.debug.core.Launch;
60 import org.eclipse.debug.core.model.IProcess;
61
62 import org.eclipse.debug.ui.IDebugUIConstants;
63
64 import org.eclipse.jdt.core.IJavaElement;
65 import org.eclipse.jdt.core.IJavaProject;
66
67 import org.eclipse.jdt.internal.corext.util.Messages;
68
69 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
70
71 import org.eclipse.jdt.ui.JavaUI;
72
73 import org.eclipse.jdt.internal.ui.JavaPlugin;
74 import org.eclipse.jdt.internal.ui.JavaPluginImages;
75 import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
76 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
77 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
78 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
79 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
80 import org.eclipse.jdt.internal.ui.util.PixelConverter;
81
82 public class JavadocWizard extends Wizard implements IExportWizard {
83
84     private JavadocTreeWizardPage fJTWPage;
85     private JavadocSpecificsWizardPage fJSWPage;
86     private JavadocStandardWizardPage fJSpWPage;
87
88     private IPath fDestination;
89
90     private boolean fWriteCustom;
91     private boolean fOpenInBrowser;
92
93     private final String JavaDoc TREE_PAGE_DESC= "JavadocTreePage"; //$NON-NLS-1$
94
private final String JavaDoc SPECIFICS_PAGE_DESC= "JavadocSpecificsPage"; //$NON-NLS-1$
95
private final String JavaDoc STANDARD_PAGE_DESC= "JavadocStandardPage"; //$NON-NLS-1$
96

97     private final int YES= 0;
98     private final int YES_TO_ALL= 1;
99     private final int NO= 2;
100     private final int NO_TO_ALL= 3;
101     private final String JavaDoc JAVADOC_ANT_INFORMATION_DIALOG= "javadocAntInformationDialog";//$NON-NLS-1$
102

103
104     private JavadocOptionsManager fStore;
105     private IWorkspaceRoot fRoot;
106
107     private IFile fXmlJavadocFile;
108     
109     private static final String JavaDoc ID_JAVADOC_PROCESS_TYPE= "org.eclipse.jdt.ui.javadocProcess"; //$NON-NLS-1$
110

111     public static void openJavadocWizard(JavadocWizard wizard, Shell shell, IStructuredSelection selection ) {
112         wizard.init(PlatformUI.getWorkbench(), selection);
113
114         WizardDialog dialog= new WizardDialog(shell, wizard);
115         PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
116         dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(100), converter.convertHeightInCharsToPixels(20));
117         dialog.open();
118     }
119     
120     
121     public JavadocWizard() {
122         this(null);
123     }
124
125     public JavadocWizard(IFile xmlJavadocFile) {
126         super();
127         setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_EXPORT_JAVADOC);
128         setWindowTitle(JavadocExportMessages.JavadocWizard_javadocwizard_title);
129
130         setDialogSettings(JavaPlugin.getDefault().getDialogSettings());
131
132         fRoot= ResourcesPlugin.getWorkspace().getRoot();
133         fXmlJavadocFile= xmlJavadocFile;
134
135         fWriteCustom= false;
136     }
137         
138     /*
139      * @see IWizard#performFinish()
140      */

141     public boolean performFinish() {
142
143         IJavaProject[] checkedProjects= fJTWPage.getCheckedProjects();
144         updateStore(checkedProjects);
145         
146         fStore.updateDialogSettings(getDialogSettings(), checkedProjects);
147
148         // Wizard should not run with dirty editors
149
if (!new RefactoringSaveHelper(RefactoringSaveHelper.SAVE_ALL_ALWAYS_ASK).saveEditors(getShell())) {
150             return false;
151         }
152
153         fDestination= Path.fromOSString(fStore.getDestination());
154         fDestination.toFile().mkdirs();
155
156         fOpenInBrowser= fStore.doOpenInBrowser();
157
158         //Ask if you wish to set the javadoc location for the projects (all) to
159
//the location of the newly generated javadoc
160
if (fStore.isFromStandard()) {
161             try {
162
163                 URL JavaDoc newURL= fDestination.toFile().toURL();
164                 List JavaDoc projs= new ArrayList JavaDoc();
165                 //get javadoc locations for all projects
166
for (int i= 0; i < checkedProjects.length; i++) {
167                     IJavaProject curr= checkedProjects[i];
168                     URL JavaDoc currURL= JavaUI.getProjectJavadocLocation(curr);
169                     if (!newURL.equals(currURL)) { // currURL can be null
170
//if not all projects have the same javadoc location ask if you want to change
171
//them to have the same javadoc location
172
projs.add(curr);
173                     }
174                 }
175                 if (!projs.isEmpty()) {
176                     setAllJavadocLocations((IJavaProject[]) projs.toArray(new IJavaProject[projs.size()]), newURL);
177                 }
178             } catch (MalformedURLException JavaDoc e) {
179                 JavaPlugin.log(e);
180             }
181         }
182
183         if (fJSWPage.generateAnt()) {
184             //@Improve: make a better message
185
OptionalMessageDialog.open(JAVADOC_ANT_INFORMATION_DIALOG, getShell(), JavadocExportMessages.JavadocWizard_antInformationDialog_title, null, JavadocExportMessages.JavadocWizard_antInformationDialog_message, MessageDialog.INFORMATION, new String JavaDoc[] { IDialogConstants.OK_LABEL }, 0);
186             try {
187                 File JavaDoc file= fStore.createXML(checkedProjects);
188                 if (file != null) {
189                     IFile[] files= fRoot.findFilesForLocation(Path.fromOSString(file.getPath()));
190                     if (files != null) {
191                         for (int i= 0; i < files.length; i++) {
192                             files[i].refreshLocal(IResource.DEPTH_ONE, null);
193                         }
194                     }
195                 }
196                 
197             } catch (CoreException e) {
198                 ExceptionHandler.handle(e, getShell(),JavadocExportMessages.JavadocWizard_error_writeANT_title, JavadocExportMessages.JavadocWizard_error_writeANT_message);
199             }
200         }
201
202         if (!executeJavadocGeneration())
203             return false;
204
205         return true;
206     }
207     
208     private void updateStore(IJavaProject[] checkedProjects) {
209         //writes the new settings to store
210
fJTWPage.updateStore(checkedProjects);
211         if (!fJTWPage.getCustom())
212             fJSpWPage.updateStore();
213         fJSWPage.updateStore();
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.jface.wizard.IWizard#performCancel()
218      */

219     public boolean performCancel() {
220         
221         IJavaProject[] checkedProjects= fJTWPage.getCheckedProjects();
222         updateStore(checkedProjects);
223         
224         //If the wizard was not launched from an ant file store the settings
225
if (fXmlJavadocFile == null) {
226             fStore.updateDialogSettings(getDialogSettings(), checkedProjects);
227         }
228         return super.performCancel();
229     }
230
231     private void setAllJavadocLocations(IJavaProject[] projects, URL JavaDoc newURL) {
232         Shell shell= getShell();
233         String JavaDoc[] buttonlabels= new String JavaDoc[] { IDialogConstants.YES_LABEL, IDialogConstants.YES_TO_ALL_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.NO_TO_ALL_LABEL };
234
235         for (int j= 0; j < projects.length; j++) {
236             IJavaProject iJavaProject= projects[j];
237             String JavaDoc message= Messages.format(JavadocExportMessages.JavadocWizard_updatejavadoclocation_message, new String JavaDoc[] { iJavaProject.getElementName(), fDestination.toOSString()});
238             MessageDialog dialog= new MessageDialog(shell, JavadocExportMessages.JavadocWizard_updatejavadocdialog_label, null, message, MessageDialog.QUESTION, buttonlabels, 1);
239
240             switch (dialog.open()) {
241                 case YES :
242                     JavaUI.setProjectJavadocLocation(iJavaProject, newURL);
243                     break;
244                 case YES_TO_ALL :
245                     for (int i= j; i < projects.length; i++) {
246                         iJavaProject= projects[i];
247                         JavaUI.setProjectJavadocLocation(iJavaProject, newURL);
248                         j++;
249                     }
250                     break;
251                 case NO_TO_ALL :
252                     j= projects.length;
253                     break;
254                 case NO :
255                 default :
256                     break;
257             }
258         }
259     }
260
261     private boolean executeJavadocGeneration() {
262         Process JavaDoc process= null;
263         try {
264             ArrayList JavaDoc vmArgs= new ArrayList JavaDoc();
265             ArrayList JavaDoc progArgs= new ArrayList JavaDoc();
266             
267             IStatus status= fStore.getArgumentArray(vmArgs, progArgs);
268             if (!status.isOK()) {
269                 ErrorDialog.openError(getShell(), JavadocExportMessages.JavadocWizard_error_title, JavadocExportMessages.JavadocWizard_warning_starting_message, status);
270             }
271             
272             File JavaDoc file= File.createTempFile("javadoc-arguments", ".tmp"); //$NON-NLS-1$//$NON-NLS-2$
273
vmArgs.add('@' + file.getAbsolutePath());
274
275             FileWriter JavaDoc writer= new FileWriter JavaDoc(file);
276             try {
277                 for (int i= 0; i < progArgs.size(); i++) {
278                     String JavaDoc curr= (String JavaDoc) progArgs.get(i);
279                     curr= checkForSpaces(curr);
280                     
281                     writer.write(curr);
282                     writer.write(' ');
283                 }
284             } finally {
285                 writer.close();
286             }
287             
288             String JavaDoc[] args= (String JavaDoc[]) vmArgs.toArray(new String JavaDoc[vmArgs.size()]);
289             process= Runtime.getRuntime().exec(args);
290             if (process != null) {
291                 // construct a formatted command line for the process properties
292
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
293                 for (int i= 0; i < args.length; i++) {
294                     buf.append(args[i]);
295                     buf.append(' ');
296                 }
297
298                 IDebugEventSetListener listener= new JavadocDebugEventListener(getShell().getDisplay(), file);
299                 DebugPlugin.getDefault().addDebugEventListener(listener);
300
301                 ILaunchConfigurationWorkingCopy wc= null;
302                 try {
303                     ILaunchConfigurationType lcType= DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
304                     String JavaDoc name= JavadocExportMessages.JavadocWizard_launchconfig_name;
305                     wc= lcType.newInstance(null, name);
306                     wc.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true);
307
308                     ILaunch newLaunch= new Launch(wc, ILaunchManager.RUN_MODE, null);
309                     IProcess iprocess= DebugPlugin.newProcess(newLaunch, process, JavadocExportMessages.JavadocWizard_javadocprocess_label);
310                     iprocess.setAttribute(IProcess.ATTR_CMDLINE, buf.toString());
311                     iprocess.setAttribute(IProcess.ATTR_PROCESS_TYPE, ID_JAVADOC_PROCESS_TYPE);
312
313                     DebugPlugin.getDefault().getLaunchManager().addLaunch(newLaunch);
314
315                 } catch (CoreException e) {
316                     String JavaDoc title= JavadocExportMessages.JavadocWizard_error_title;
317                     String JavaDoc message= JavadocExportMessages.JavadocWizard_launch_error_message;
318                     ExceptionHandler.handle(e, getShell(), title, message);
319                 }
320
321                 return true;
322
323             }
324         } catch (IOException JavaDoc e) {
325             String JavaDoc title= JavadocExportMessages.JavadocWizard_error_title;
326             String JavaDoc message= JavadocExportMessages.JavadocWizard_exec_error_message;
327
328             IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, e.getMessage(), e);
329             ExceptionHandler.handle(new CoreException(status), getShell(), title, message);
330             return false;
331         }
332         return false;
333
334     }
335
336     private String JavaDoc checkForSpaces(String JavaDoc curr) {
337         if (curr.indexOf(' ') == -1) {
338             return curr;
339         }
340         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
341         buf.append('\'');
342         for (int i= 0; i < curr.length(); i++) {
343             char ch= curr.charAt(i);
344             if (ch == '\\' || ch == '\'') {
345                 buf.append('\\');
346             }
347             buf.append(ch);
348         }
349         buf.append('\'');
350         return buf.toString();
351     }
352
353     /*
354      * @see IWizard#addPages()
355      */

356     public void addPages() {
357         
358         fJTWPage= new JavadocTreeWizardPage(TREE_PAGE_DESC, fStore);
359         fJSWPage= new JavadocSpecificsWizardPage(SPECIFICS_PAGE_DESC, fJTWPage, fStore);
360         fJSpWPage= new JavadocStandardWizardPage(STANDARD_PAGE_DESC, fJTWPage, fStore);
361
362         super.addPage(fJTWPage);
363         super.addPage(fJSpWPage);
364         super.addPage(fJSWPage);
365
366         fJTWPage.init();
367         fJSpWPage.init();
368         fJSWPage.init();
369
370     }
371
372     public void init(IWorkbench workbench, IStructuredSelection structuredSelection) {
373         IWorkbenchWindow window= workbench.getActiveWorkbenchWindow();
374         List JavaDoc selected= Collections.EMPTY_LIST;
375         if (window != null) {
376             ISelection selection= window.getSelectionService().getSelection();
377             if (selection instanceof IStructuredSelection) {
378                 selected= ((IStructuredSelection) selection).toList();
379             } else {
380                 IJavaElement element= EditorUtility.getActiveEditorJavaInput();
381                 if (element != null) {
382                     selected= new ArrayList JavaDoc();
383                     selected.add(element);
384                 }
385             }
386         }
387         fStore= new JavadocOptionsManager(fXmlJavadocFile, getDialogSettings(), selected);
388     }
389
390     private void refresh(IPath path) {
391         if (fRoot.findContainersForLocation(path).length > 0) {
392             try {
393                 fRoot.refreshLocal(IResource.DEPTH_INFINITE, null);
394             } catch (CoreException e) {
395                 JavaPlugin.log(e);
396             }
397         }
398     }
399
400     private void spawnInBrowser(Display display) {
401         if (fOpenInBrowser) {
402             try {
403                 IPath indexFile= fDestination.append("index.html"); //$NON-NLS-1$
404
URL JavaDoc url= indexFile.toFile().toURL();
405                 OpenBrowserUtil.open(url, display, getWindowTitle());
406             } catch (MalformedURLException JavaDoc e) {
407                 JavaPlugin.log(e);
408             }
409         }
410     }
411
412     private class JavadocDebugEventListener implements IDebugEventSetListener {
413         private Display fDisplay;
414         private File JavaDoc fFile;
415
416         public JavadocDebugEventListener(Display display, File JavaDoc file) {
417             fDisplay= display;
418             fFile= file;
419         }
420         
421         public void handleDebugEvents(DebugEvent[] events) {
422             for (int i= 0; i < events.length; i++) {
423                 if (events[i].getKind() == DebugEvent.TERMINATE) {
424                     try {
425                         if (!fWriteCustom) {
426                             fFile.delete();
427                             refresh(fDestination); //If destination of javadoc is in workspace then refresh workspace
428
spawnInBrowser(fDisplay);
429                         }
430                     } finally {
431                         DebugPlugin.getDefault().removeDebugEventListener(this);
432                     }
433                     return;
434                 }
435             }
436         }
437     }
438
439     public IWizardPage getNextPage(IWizardPage page) {
440         if (page instanceof JavadocTreeWizardPage) {
441             if (!fJTWPage.getCustom()) {
442                 return fJSpWPage;
443             }
444             return fJSWPage;
445         } else if (page instanceof JavadocSpecificsWizardPage) {
446             return null;
447         } else if (page instanceof JavadocStandardWizardPage)
448             return fJSWPage;
449         else
450             return null;
451     }
452
453     public IWizardPage getPreviousPage(IWizardPage page) {
454         if (page instanceof JavadocSpecificsWizardPage) {
455             if (!fJTWPage.getCustom()) {
456                 return fJSpWPage;
457             }
458             return fJSWPage;
459         } else if (page instanceof JavadocTreeWizardPage) {
460             return null;
461         } else if (page instanceof JavadocStandardWizardPage)
462             return fJTWPage;
463         else
464             return null;
465     }
466     
467 }
468
Popular Tags