initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / WikiExportWizard.java
1 /*
2  * Copyright (c) 2002 Team in a Box Ltd. All rights reserved. This file is made available under the terms and conditions of the
3  * Common Public License v 1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v1.0.html
4  * 
5  * Contributors: Team in a Box Ltd http://www.teaminabox.co.uk/
6  */
7
8 package net.sourceforge.phpeclipse.wiki.export;
9
10 import java.io.File;
11 import java.lang.reflect.InvocationTargetException;
12
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14 import net.sourceforge.phpeclipse.wiki.preferences.Util;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.QualifiedName;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.wizard.Wizard;
28 import org.eclipse.ui.INewWizard;
29 import org.eclipse.ui.IWorkbench;
30
31 public final class WikiExportWizard extends Wizard implements INewWizard {
32   static final QualifiedName DIRECTORY_QUALIFIED_NAME = new QualifiedName(WikiEditorPlugin.PLUGIN_ID, "exportDirectory");
33
34   private WikiExportWizardPage page;
35
36   private ISelection selection;
37
38   public WikiExportWizard() {
39     super();
40     setNeedsProgressMonitor(true);
41   }
42
43   public void addPages() {
44     page = new WikiExportWizardPage(selection);
45     addPage(page);
46   }
47
48   public boolean performFinish() {
49     persistExportProperties();
50
51     final IContainer folder = page.getFolder();
52     final String exportDirectory = page.getExportDirectoryPath();
53
54     return runOperationForContainer(new IRunnableWithProgress() {
55       public void run(IProgressMonitor monitor) throws InvocationTargetException {
56         try {
57           startExport(monitor, folder, exportDirectory);
58         } catch (Exception e) {
59           throw new InvocationTargetException(e);
60         } finally {
61           monitor.done();
62         }
63       }
64     });
65   }
66
67   private boolean runOperationForContainer(IRunnableWithProgress op) {
68     try {
69       getContainer().run(true, true, op);
70     } catch (InterruptedException e) {
71       return false;
72     } catch (InvocationTargetException e) {
73       WikiEditorPlugin.getDefault().log("", e);
74       MessageDialog.openError(getShell(), "Error", e.getTargetException().getMessage());
75       return false;
76     }
77
78     return true;
79   }
80
81   private void startExport(IProgressMonitor monitor, IContainer folder, String exportDirectory) throws CoreException {
82     try {
83       final String srcBasePath = Util.getWikiTextsPath(folder);  
84       new WikiExporter().export(folder, exportDirectory, srcBasePath, monitor);
85     } catch (Exception ioex) {
86       throw new CoreException(new Status(IStatus.ERROR, "Failed to write Wiki Documents", IStatus.OK, ioex.getMessage(), ioex));
87     }
88   }
89
90   private void persistExportProperties() {
91     IProject project = page.getFolder().getProject();
92     try {
93       project.setPersistentProperty(WikiExportWizard.DIRECTORY_QUALIFIED_NAME, new File(page.getExportDirectoryPath())
94           .getAbsolutePath());
95     } catch (CoreException cex) {
96       noteException(cex);
97     }
98   }
99
100   private void noteException(CoreException cex) {
101     WikiEditorPlugin.getDefault().log("Export Error", cex);
102     throw new RuntimeException("An error occurred. Please see the log for details.");
103   }
104
105   public void init(IWorkbench workbench, IStructuredSelection selection) {
106     this.selection = selection;
107   }
108 }