/* * 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.pdf; import java.lang.reflect.InvocationTargetException; 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.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; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.layout.GridData; 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 static final String[] PDF_EXTENSION = { "pdf" }; //dialog store id constants private static final String STORE_DESTINATION_NAMES_ID = "WikiPDFExportWizardPage.STORE_DESTINATION_NAMES_ID"; //$NON-NLS-1$ private static final String STORE_OVERWRITE_EXISTING_FILES_ID = "WikiPDFExportWizardPage.STORE_OVERWRITE_EXISTING_FILES_ID"; //$NON-NLS-1$ private StringFieldEditor exportFileText; private Combo destinationNameField; private Button destinationBrowseButton; protected Button overwriteExistingFilesCheckbox; private ISelection selection; public WikiPDFExportWizardPage(IStructuredSelection selection) { super(WikiEditorPlugin.getResourceString("Export.wizardTitle"), selection); setTitle(WikiEditorPlugin.getResourceString("Export.wizardTitle")); setDescription(WikiEditorPlugin.getResourceString("Export.wizardDescription")); this.selection = selection; } /** * Create the export destination specification widgets * * @param parent * org.eclipse.swt.widgets.Composite */ protected void createDestinationGroup(Composite parent) { Font font = parent.getFont(); // destination specification group Composite destinationSelectionGroup = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); layout.numColumns = 3; destinationSelectionGroup.setLayout(layout); destinationSelectionGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); destinationSelectionGroup.setFont(font); Label destinationLabel = new Label(destinationSelectionGroup, SWT.NONE); destinationLabel.setText("PDF output filename:"); destinationLabel.setFont(font); // destination name entry field destinationNameField = new Combo(destinationSelectionGroup, SWT.SINGLE | SWT.BORDER); destinationNameField.addListener(SWT.Modify, this); destinationNameField.addListener(SWT.Selection, this); GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); data.widthHint = SIZING_TEXT_FIELD_WIDTH; destinationNameField.setLayoutData(data); destinationNameField.setFont(font); // destination browse button destinationBrowseButton = new Button(destinationSelectionGroup, SWT.PUSH); destinationBrowseButton.setText("Browse..."); //$NON-NLS-1$ destinationBrowseButton.addListener(SWT.Selection, this); destinationBrowseButton.setFont(font); setButtonLayoutData(destinationBrowseButton); 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 buttons in the options group. */ protected void createOptionsGroupButtons(Group optionsGroup) { Font font = optionsGroup.getFont(); createOverwriteExisting(optionsGroup, font); // createDirectoryStructureOptions(optionsGroup, font); } /** * Create the button for checking if we should ask if we are going to overwrite existing files. * * @param optionsGroup * @param font */ protected void createOverwriteExisting(Group optionsGroup, Font font) { // overwrite... checkbox overwriteExistingFilesCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT); overwriteExistingFilesCheckbox.setText("Overwrite existing files"); //$NON-NLS-1$ overwriteExistingFilesCheckbox.setFont(font); } /** * Handle all events and enablements for widgets in this page * * @param e * Event */ public void handleEvent(Event e) { Widget source = e.widget; if (source == destinationBrowseButton) handleDestinationBrowseButtonPressed(); updatePageCompletion(); } /** * 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"); dialog.setFilterPath(getDestinationValue()); String selectedDirectoryName = dialog.open(); if (selectedDirectoryName != null) { setErrorMessage(null); setDestinationValue(selectedDirectoryName); } } /** * Answer the contents of self's destination specification widget * * @return java.lang.String */ protected String getDestinationValue() { return destinationNameField.getText().trim(); } /** * Set the contents of the receivers destination specification widget to the passed value * */ protected void setDestinationValue(String value) { destinationNameField.setText(value); } /** * Hook method for saving widget values for restoration by the next instance of this class. */ protected void internalSaveWidgetValues() { // 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]; directoryNames = addToHistory(directoryNames, getDestinationValue()); settings.put(STORE_DESTINATION_NAMES_ID, directoryNames); // options settings.put(STORE_OVERWRITE_EXISTING_FILES_ID, overwriteExistingFilesCheckbox.getSelection()); // settings.put( // STORE_CREATE_STRUCTURE_ID, // createDirectoryStructureButton.getSelection()); } } /** * Hook method for restoring widget values to the values that they held last time this wizard was used to completion. */ protected void restoreWidgetValues() { IDialogSettings settings = getDialogSettings(); if (settings != null) { String[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID); if (directoryNames == null) return; // ie.- no settings stored // destination setDestinationValue(directoryNames[0]); for (int i = 0; i < directoryNames.length; i++) addDestinationItem(directoryNames[i]); // options overwriteExistingFilesCheckbox.setSelection(settings.getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID)); // boolean createDirectories = // settings.getBoolean(STORE_CREATE_STRUCTURE_ID); // createDirectoryStructureButton.setSelection(createDirectories); // 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); } /** * (non-Javadoc) Method declared on IDialogPage. */ public void createControl(Composite parent) { super.createControl(parent); giveFocusToDestination(); // WorkbenchHelp.setHelp( // getControl(), // IDataTransferHelpContextIds.FILE_SYSTEM_EXPORT_WIZARD_PAGE); } /** * Set the current input focus to self's destination entry field */ protected void giveFocusToDestination() { destinationNameField.setFocus(); } // private Composite createControlsContainer(Composite parent) { // Composite container = new Composite(parent, SWT.NULL); // GridLayout layout = new GridLayout(); // layout.numColumns = 1; // layout.verticalSpacing = 20; // container.setLayout(layout); // container.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); // // createCommonControls(container); // return container; // } // private void createCommonControls(Composite parent) { // Composite container = new Composite(parent, SWT.NULL); // GridLayout layout = new GridLayout(); // layout.numColumns = 3; // layout.verticalSpacing = 9; // container.setLayout(layout); // container.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); // // createFolderControls(container); // createExportDirectoryControls(container); // } // private void createExportDirectoryControls(Composite container) { // exportFileText = addStringFieldEditor(container, WikiEditorPlugin.getResourceString("Export.wizardExportDirectory")); // // Button button = new Button(container, SWT.PUSH); // button.setText(WikiEditorPlugin.getResourceString("Export.wizardBrowse")); // button.addSelectionListener(new SelectionAdapter() { // public void widgetSelected(SelectionEvent e) { // handleBrowseHtmlExportLocation(); // } // }); // } // private void createFolderControls(Composite container) { // folderText = addStringFieldEditor(container, WikiEditorPlugin.getResourceString("Export.wizardFolder")); // // Button button = new Button(container, SWT.PUSH); // button.setText(WikiEditorPlugin.getResourceString("Export.wizardBrowse")); // button.addSelectionListener(new SelectionAdapter() { // public void widgetSelected(SelectionEvent e) { // try { // handleBrowseFolders(); // } catch (CoreException cex) { // WikiEditorPlugin.getDefault().log("", cex); // throw new RuntimeException("Caught CoreException. See log for details."); // } // } // }); // } // private StringFieldEditor addStringFieldEditor(Composite container, String labelText) { // Label label = new Label(container, SWT.NULL); // label.setText(labelText); // // Composite editorComposite = new Composite(container, SWT.NULL); // editorComposite.setLayout(new GridLayout()); // editorComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); // StringFieldEditor editor = new StringFieldEditor("", "", editorComposite); // // editor.setPropertyChangeListener(this); // // 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 dialogChanged() { if (getFolderText().length() == 0) { updateStatus("Folder must be specified"); } else if (getExportDirectoryPath().length() == 0) { updateStatus("Directory must be specified"); } else { updateStatus(null); } } private void updateStatus(String message) { setErrorMessage(message); setPageComplete(message == null); } public String getExportDirectoryPath() { return exportFileText.getStringValue(); } public void propertyChange(PropertyChangeEvent event) { dialogChanged(); } public void widgetSelected(SelectionEvent e) { dialogChanged(); } public void widgetDefaultSelected(SelectionEvent e) { dialogChanged(); } String getFolderText() { return folderText.getStringValue(); } public IContainer getFolder() { return (IContainer) ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getFolderText())); } protected boolean executeExportOperation(WikiPDFExporter op) { // op.setCreateLeadupStructure( // createDirectoryStructureButton.getSelection()); op.setOverwriteFiles(overwriteExistingFilesCheckbox.getSelection()); try { getContainer().run(true, true, op); } catch (InterruptedException e) { return true; } catch (InvocationTargetException e) { displayErrorDialog(e.getTargetException()); return true; } IStatus status = op.getStatus(); if (!status.isOK()) { ErrorDialog.openError(getContainer().getShell(), "PDF export problems", //$NON-NLS-1$ null, // no special message status); return true; } return true; } /** * The Finish button was pressed. Try to do the required work now and answer a boolean indicating success. If false is returned * then the wizard will not close. * * @return boolean */ public boolean finish() { // if (!ensureTargetIsValid(new File(getDestinationValue()))) // return false; List resourcesToExport = getWhiteCheckedResources(); //Save dirty editors if possible but do not stop if not all are saved saveDirtyEditors(); // about to invoke the operation so save our state saveWidgetValues(); if (resourcesToExport.size() > 0) return executeExportOperation(new WikiPDFExporter(resourcesToExport, getDestinationValue(), this)); MessageDialog.openInformation(getContainer().getShell(), "PDF export", //$NON-NLS-1$ "No file selected"); //$NON-NLS-1$ return false; } }