Improved PDF export (every article is a chapter, outline, FileDialog)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / pdf / WikiPDFExportWizardPage.java
index 729effe..a71bcf6 100644 (file)
@@ -13,25 +13,17 @@ import java.util.List;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
 
 import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.dialogs.DialogSettings;
 import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.dialogs.IDialogSettings;
 import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.operation.IRunnableWithProgress;
-import org.eclipse.jface.preference.StringFieldEditor;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
@@ -41,18 +33,16 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.DirectoryDialog;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Widget;
-import org.eclipse.ui.dialogs.ContainerSelectionDialog;
 import org.eclipse.ui.dialogs.WizardExportResourcesPage;
 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
 
 public final class WikiPDFExportWizardPage extends WizardExportResourcesPage implements IPropertyChangeListener, SelectionListener {
-  private StringFieldEditor folderText;
+//  private StringFieldEditor folderText;
 
   private static final String[] PDF_EXTENSION = { "pdf" };
 
@@ -61,7 +51,7 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
 
   private static final String STORE_OVERWRITE_EXISTING_FILES_ID = "WikiPDFExportWizardPage.STORE_OVERWRITE_EXISTING_FILES_ID"; //$NON-NLS-1$
 
-  private StringFieldEditor exportFileText;
+//  private StringFieldEditor exportFileText;
 
   private Combo destinationNameField;
 
@@ -70,6 +60,7 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
   protected Button overwriteExistingFilesCheckbox;
 
   private ISelection selection;
+  private IDialogSettings fSettings = null;
 
   public WikiPDFExportWizardPage(IStructuredSelection selection) {
     super(WikiEditorPlugin.getResourceString("Export.wizardTitle"), selection);
@@ -117,36 +108,38 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
 
     new Label(parent, SWT.NONE); // vertical spacer
   }
+
   /**
-        *      Create the options specification widgets.
-        *
-        *      @param parent org.eclipse.swt.widgets.Composite
-        */
-       protected void createOptionsGroup(Composite parent) {
-               // options group
-               Group optionsGroup = new Group(parent, SWT.NONE);
-               GridLayout layout = new GridLayout();
-               optionsGroup.setLayout(layout);
-               optionsGroup.setLayoutData(
-                       new GridData(
-                               GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
-               optionsGroup.setText(IDEWorkbenchMessages.getString("WizardExportPage.options")); //$NON-NLS-1$
-               optionsGroup.setFont(parent.getFont());
-
-               createOptionsGroupButtons(optionsGroup);
-
-       }
+   * Create the options specification widgets.
+   * 
+   * @param parent
+   *          org.eclipse.swt.widgets.Composite
+   */
+  protected void createOptionsGroup(Composite parent) {
+    // options group
+    Group optionsGroup = new Group(parent, SWT.NONE);
+    GridLayout layout = new GridLayout();
+    optionsGroup.setLayout(layout);
+    optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
+    optionsGroup.setText(IDEWorkbenchMessages.getString("WizardExportPage.options")); //$NON-NLS-1$
+    optionsGroup.setFont(parent.getFont());
+
+    createOptionsGroupButtons(optionsGroup);
+
+  }
+
   /**
-        * Create the buttons in the options group.
-        */
+   * Create the buttons in the options group.
+   */
 
-       protected void createOptionsGroupButtons(Group optionsGroup) {
+  protected void createOptionsGroupButtons(Group optionsGroup) {
 
-               Font font = optionsGroup.getFont();
-               createOverwriteExisting(optionsGroup, font);
+    Font font = optionsGroup.getFont();
+    createOverwriteExisting(optionsGroup, font);
+
+    //         createDirectoryStructureOptions(optionsGroup, font);
+  }
 
-//             createDirectoryStructureOptions(optionsGroup, font);
-       }
   /**
    * Create the button for checking if we should ask if we are going to overwrite existing files.
    * 
@@ -179,15 +172,15 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
    * Open an appropriate destination browser so that the user can specify a source to import from
    */
   protected void handleDestinationBrowseButtonPressed() {
-    DirectoryDialog dialog = new DirectoryDialog(getContainer().getShell(), SWT.SAVE);
-    dialog.setMessage("Select destination");
-    dialog.setText("Select title");
+    FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE);
+    dialog.setFilterExtensions(PDF_EXTENSION);
+    dialog.setText("Select pdf file");
     dialog.setFilterPath(getDestinationValue());
-    String selectedDirectoryName = dialog.open();
+    String selectedFileName = dialog.open();
 
-    if (selectedDirectoryName != null) {
+    if (selectedFileName != null) {
       setErrorMessage(null);
-      setDestinationValue(selectedDirectoryName);
+      setDestinationValue(selectedFileName);
     }
   }
 
@@ -215,12 +208,12 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
     // update directory names history
     IDialogSettings settings = getDialogSettings();
     if (settings != null) {
-      String[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
-      if (directoryNames == null)
-        directoryNames = new String[0];
+      String[] fileNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
+      if (fileNames == null)
+        fileNames = new String[0];
 
-      directoryNames = addToHistory(directoryNames, getDestinationValue());
-      settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
+      fileNames = addToHistory(fileNames, getDestinationValue());
+      settings.put(STORE_DESTINATION_NAMES_ID, fileNames);
 
       // options
       settings.put(STORE_OVERWRITE_EXISTING_FILES_ID, overwriteExistingFilesCheckbox.getSelection());
@@ -238,14 +231,14 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
   protected void restoreWidgetValues() {
     IDialogSettings settings = getDialogSettings();
     if (settings != null) {
-      String[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
-      if (directoryNames == null)
+      String[] fileNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
+      if (fileNames == null)
         return; // ie.- no settings stored
 
       // destination
-      setDestinationValue(directoryNames[0]);
-      for (int i = 0; i < directoryNames.length; i++)
-        addDestinationItem(directoryNames[i]);
+      setDestinationValue(fileNames[0]);
+      for (int i = 0; i < fileNames.length; i++)
+        addDestinationItem(fileNames[i]);
 
       // options
       overwriteExistingFilesCheckbox.setSelection(settings.getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID));
@@ -256,14 +249,17 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
       //                       createSelectionOnlyButton.setSelection(!createDirectories);
     }
   }
+
   /**
-        *      Add the passed value to self's destination widget's history
-        *
-        *      @param value java.lang.String
-        */
-       protected void addDestinationItem(String value) {
-               destinationNameField.add(value);
-       }
+   * Add the passed value to self's destination widget's history
+   * 
+   * @param value
+   *          java.lang.String
+   */
+  protected void addDestinationItem(String value) {
+    destinationNameField.add(value);
+  }
+
   /**
    * (non-Javadoc) Method declared on IDialogPage.
    */
@@ -349,66 +345,74 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
   //    return editor;
   //  }
 
-  private void initialize() throws CoreException {
-    if (selection == null || selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
-      return;
-    }
-
-    IStructuredSelection ssel = (IStructuredSelection) selection;
-    if (ssel.size() == 1) {
-      initialiseFromSelectedObject(ssel.getFirstElement());
-    }
-  }
-
-  private void initialiseFromSelectedObject(Object obj) throws CoreException {
-    if (obj instanceof IFolder || obj instanceof IProject) {
-      initialiseFolder(((IResource) obj));
-    }
-  }
-
-  private void initialiseFolder(IResource resource) throws CoreException {
-    folderText.setStringValue(resource.getFullPath().toString());
-    initialiseExportDirectoryText(resource);
-  }
-
-  private void initialiseExportDirectoryText(IResource resource) throws CoreException {
-    String exportDir = resource.getProject().getPersistentProperty(WikiPDFExportWizard.DIRECTORY_QUALIFIED_NAME);
-    if (exportDir != null) {
-      exportFileText.setStringValue(exportDir);
-    } else {
-      exportFileText.setStringValue("");
-    }
-  }
-
-  private void handleBrowseHtmlExportLocation() {
-    FileDialog dialog = new FileDialog(getShell(), SWT.SINGLE | SWT.OPEN);
-    dialog.setFilterExtensions(PDF_EXTENSION);
-    String path = dialog.open();
-    if (path != null) {
-      exportFileText.setStringValue(path);
-    }
-  }
-
-  private void handleBrowseFolders() throws CoreException {
-    ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
-        WikiEditorPlugin.getResourceString("Export.wizardSelectFolder"));
-    if (dialog.open() == Window.OK) {
-      Object[] result = dialog.getResult();
-      if (result != null && result.length == 1) {
-        IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember((IPath) result[0]);
-        if (resource instanceof IFile) {
-          return;
-        }
-        initialiseFolder(resource);
-      }
-    }
-  }
-
+//  private void initialize() throws CoreException {
+//    if (selection == null || selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
+//      return;
+//    }
+//
+//    IStructuredSelection ssel = (IStructuredSelection) selection;
+//    if (ssel.size() == 1) {
+//      initialiseFromSelectedObject(ssel.getFirstElement());
+//    }
+//  }
+
+//  private void initialiseFromSelectedObject(Object obj) throws CoreException {
+//    if (obj instanceof IFolder || obj instanceof IProject) {
+//      initialiseFolder(((IResource) obj));
+//    }
+//  }
+
+//  private void initialiseFolder(IResource resource) throws CoreException {
+//    folderText.setStringValue(resource.getFullPath().toString());
+//    initialiseExportDirectoryText(resource);
+//  }
+
+//  private void initialiseExportDirectoryText(IResource resource) throws CoreException {
+//    String exportDir = resource.getProject().getPersistentProperty(WikiPDFExportWizard.DIRECTORY_QUALIFIED_NAME);
+//    if (exportDir != null) {
+//      exportFileText.setStringValue(exportDir);
+//    } else {
+//      exportFileText.setStringValue("");
+//    }
+//  }
+
+//  private void handleBrowseHtmlExportLocation() {
+//    FileDialog dialog = new FileDialog(getShell(), SWT.SINGLE | SWT.OPEN);
+//    dialog.setFilterExtensions(PDF_EXTENSION);
+//    String path = dialog.open();
+//    if (path != null) {
+//      setDestinationValue(path);
+////      exportFileText.setStringValue(path);
+//    }
+//  }
+//
+//  private void handleBrowseFolders() throws CoreException {
+//    FileDialog dialog = new FileDialog(getShell());
+//    //, ResourcesPlugin.getWorkspace().getRoot(), false,
+//    //  WikiEditorPlugin.getResourceString("Export.wizardSelectFolder"));
+//    String filePath = dialog.open();
+//    //    if (dialog.open() == Window.OK) {
+//    //      Object[] result = dialog.getResult();
+//    if (filePath != null) {
+//      //      if (result != null && result.length == 1) {
+//      //        IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember((IPath) result[0]);
+//      IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(filePath);
+//      if (!(resource instanceof IFile)) {
+//        return;
+//      }
+////      initialiseFolder(resource);
+//      //      }
+//    }
+//  }
+
+  
   private void dialogChanged() {
-    if (getFolderText().length() == 0) {
-      updateStatus("Folder must be specified");
-    } else if (getExportDirectoryPath().length() == 0) {
-      updateStatus("Directory must be specified");
+//    if (getFolderText().length() == 0) {
+//      updateStatus("File must be specified");
+//    } else 
+      
+    if (getDestinationValue().length() == 0) {
+      updateStatus("PDF export file must be specified");
     } else {
       updateStatus(null);
     }
@@ -419,9 +423,9 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
     setPageComplete(message == null);
   }
 
-  public String getExportDirectoryPath() {
-    return exportFileText.getStringValue();
-  }
+//  public String getExportDirectoryPath() {
+//    return exportFileText.getStringValue();
+//  }
 
   public void propertyChange(PropertyChangeEvent event) {
     dialogChanged();
@@ -435,12 +439,12 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
     dialogChanged();
   }
 
-  String getFolderText() {
-    return folderText.getStringValue();
-  }
+//  String getFolderText() {
+//    return folderText.getStringValue();
+//  }
 
-  public IContainer getFolder() {
-    return (IContainer) ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getFolderText()));
+  public IContainer getFile() {
+    return (IContainer) ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getDestinationValue()));
   }
 
   protected boolean executeExportOperation(WikiPDFExporter op) {
@@ -493,4 +497,20 @@ public final class WikiPDFExportWizardPage extends WizardExportResourcesPage imp
 
     return false;
   }
+  /* (non-Javadoc)
+   * @see org.eclipse.jface.wizard.WizardPage#getDialogSettings()
+   */
+//  protected IDialogSettings getDialogSettings() {
+//    IDialogSettings dialogBounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
+//     if (dialogBounds == null) {
+//             dialogBounds= new DialogSettings(DIALOG_BOUNDS_KEY);
+//             fSettings.addSection(dialogBounds);
+//     }
+//     dialogBounds.put(X, bounds.x);
+//     dialogBounds.put(Y, bounds.y);
+//     dialogBounds.put(WIDTH, bounds.width);
+//     dialogBounds.put(HEIGHT, bounds.height);
+//    // TODO Auto-generated method stub
+//    return super.getDialogSettings();
+//  }
 }
\ No newline at end of file