* added a first PDF Exporter (menu File->Export...)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / html / WikiHTMLExportWizard.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/export/html/WikiHTMLExportWizard.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/export/html/WikiHTMLExportWizard.java
new file mode 100644 (file)
index 0000000..97e7f94
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2002 Team in a Box Ltd. All rights reserved. This file is made available under the terms and conditions of the
+ * Common Public License v 1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v1.0.html
+ * 
+ * Contributors: Team in a Box Ltd http://www.teaminabox.co.uk/
+ */
+
+package net.sourceforge.phpeclipse.wiki.export.html;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.QualifiedName;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.INewWizard;
+import org.eclipse.ui.IWorkbench;
+
+public final class WikiHTMLExportWizard extends Wizard implements INewWizard {
+  static final QualifiedName DIRECTORY_QUALIFIED_NAME = new QualifiedName(WikiEditorPlugin.PLUGIN_ID, "exportDirectory");
+
+  private WikiHTMLExportWizardPage page;
+
+  private ISelection selection;
+
+  public WikiHTMLExportWizard() {
+    super();
+    setNeedsProgressMonitor(true);
+  }
+
+  public void addPages() {
+    page = new WikiHTMLExportWizardPage(selection);
+    addPage(page);
+  }
+
+  public boolean performFinish() {
+    persistExportProperties();
+
+    final IContainer folder = page.getFolder();
+    final String exportDirectory = page.getExportDirectoryPath();
+
+    return runOperationForContainer(new IRunnableWithProgress() {
+      public void run(IProgressMonitor monitor) throws InvocationTargetException {
+        try {
+          startExport(monitor, folder, exportDirectory);
+        } catch (Exception e) {
+          throw new InvocationTargetException(e);
+        } finally {
+          monitor.done();
+        }
+      }
+    });
+  }
+
+  private boolean runOperationForContainer(IRunnableWithProgress op) {
+    try {
+      getContainer().run(true, true, op);
+    } catch (InterruptedException e) {
+      return false;
+    } catch (InvocationTargetException e) {
+      WikiEditorPlugin.getDefault().log("", e);
+      MessageDialog.openError(getShell(), "Error", e.getTargetException().getMessage());
+      return false;
+    }
+
+    return true;
+  }
+
+  private void startExport(IProgressMonitor monitor, IContainer folder, String exportDirectory) throws CoreException {
+    try {
+      final String srcBasePath = Util.getWikiTextsPath(folder);  
+      new WikiHTMLExporter().export(folder, exportDirectory, srcBasePath, monitor);
+    } catch (Exception ioex) {
+      throw new CoreException(new Status(IStatus.ERROR, "Failed to write Wiki Documents", IStatus.OK, ioex.getMessage(), ioex));
+    }
+  }
+
+  private void persistExportProperties() {
+    IProject project = page.getFolder().getProject();
+    try {
+      project.setPersistentProperty(WikiHTMLExportWizard.DIRECTORY_QUALIFIED_NAME, new File(page.getExportDirectoryPath())
+          .getAbsolutePath());
+    } catch (CoreException cex) {
+      noteException(cex);
+    }
+  }
+
+  private void noteException(CoreException cex) {
+    WikiEditorPlugin.getDefault().log("Export Error", cex);
+    throw new RuntimeException("An error occurred. Please see the log for details.");
+  }
+
+  public void init(IWorkbench workbench, IStructuredSelection selection) {
+    this.selection = selection;
+  }
+}
\ No newline at end of file