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
5 * Contributors: Team in a Box Ltd http://www.teaminabox.co.uk/
8 package net.sourceforge.phpeclipse.wiki.export;
10 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
12 import org.eclipse.core.resources.IContainer;
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IFolder;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.preference.StringFieldEditor;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.jface.wizard.WizardPage;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.DirectoryDialog;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
40 public final class WikiExportWizardPage extends WizardPage implements IPropertyChangeListener, SelectionListener {
41 private StringFieldEditor folderText;
43 private StringFieldEditor exportDirectoryText;
45 private ISelection selection;
47 public WikiExportWizardPage(ISelection selection) {
48 super(WikiEditorPlugin.getResourceString("Export.wizardTitle"));
49 setTitle(WikiEditorPlugin.getResourceString("Export.wizardTitle"));
50 setDescription(WikiEditorPlugin.getResourceString("Export.wizardDescription"));
51 this.selection = selection;
54 public void createControl(Composite parent) {
55 Composite rootComposite = createControlsContainer(parent);
59 } catch (RuntimeException rex) {
61 } catch (CoreException cex) {
62 WikiEditorPlugin.getDefault().log("", cex);
63 throw new RuntimeException("Caught CoreException. See log for details.");
66 setControl(rootComposite);
69 private Composite createControlsContainer(Composite parent) {
70 Composite container = new Composite(parent, SWT.NULL);
71 GridLayout layout = new GridLayout();
72 layout.numColumns = 1;
73 layout.verticalSpacing = 20;
74 container.setLayout(layout);
75 container.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
77 createCommonControls(container);
81 private void createCommonControls(Composite parent) {
82 Composite container = new Composite(parent, SWT.NULL);
83 GridLayout layout = new GridLayout();
84 layout.numColumns = 3;
85 layout.verticalSpacing = 9;
86 container.setLayout(layout);
87 container.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
89 createFolderControls(container);
90 createExportDirectoryControls(container);
93 private void createExportDirectoryControls(Composite container) {
94 exportDirectoryText = addStringFieldEditor(container, WikiEditorPlugin.getResourceString("Export.wizardExportDirectory"));
96 Button button = new Button(container, SWT.PUSH);
97 button.setText(WikiEditorPlugin.getResourceString("Export.wizardBrowse"));
98 button.addSelectionListener(new SelectionAdapter() {
99 public void widgetSelected(SelectionEvent e) {
100 handleBrowseHtmlExportLocation();
105 private void createFolderControls(Composite container) {
106 folderText = addStringFieldEditor(container, WikiEditorPlugin.getResourceString("Export.wizardFolder"));
108 Button button = new Button(container, SWT.PUSH);
109 button.setText(WikiEditorPlugin.getResourceString("Export.wizardBrowse"));
110 button.addSelectionListener(new SelectionAdapter() {
111 public void widgetSelected(SelectionEvent e) {
113 handleBrowseFolders();
114 } catch (CoreException cex) {
115 WikiEditorPlugin.getDefault().log("", cex);
116 throw new RuntimeException("Caught CoreException. See log for details.");
122 private StringFieldEditor addStringFieldEditor(Composite container, String labelText) {
123 Label label = new Label(container, SWT.NULL);
124 label.setText(labelText);
126 Composite editorComposite = new Composite(container, SWT.NULL);
127 editorComposite.setLayout(new GridLayout());
128 editorComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
129 StringFieldEditor editor = new StringFieldEditor("", "", editorComposite);
131 editor.setPropertyChangeListener(this);
136 private void initialize() throws CoreException {
137 if (selection == null || selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
141 IStructuredSelection ssel = (IStructuredSelection) selection;
142 if (ssel.size() == 1) {
143 initialiseFromSelectedObject(ssel.getFirstElement());
147 private void initialiseFromSelectedObject(Object obj) throws CoreException {
148 if (obj instanceof IFolder || obj instanceof IProject) {
149 initialiseFolder(((IResource) obj));
153 private void initialiseFolder(IResource resource) throws CoreException {
154 folderText.setStringValue(resource.getFullPath().toString());
155 initialiseExportDirectoryText(resource);
158 private void initialiseExportDirectoryText(IResource resource) throws CoreException {
159 String exportDir = resource.getProject().getPersistentProperty(WikiExportWizard.DIRECTORY_QUALIFIED_NAME);
160 if (exportDir != null) {
161 exportDirectoryText.setStringValue(exportDir);
163 exportDirectoryText.setStringValue("");
167 private void handleBrowseHtmlExportLocation() {
168 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SINGLE | SWT.OPEN);
169 String path = dialog.open();
171 exportDirectoryText.setStringValue(path);
175 private void handleBrowseFolders() throws CoreException {
176 ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
177 WikiEditorPlugin.getResourceString("Export.wizardSelectFolder"));
178 if (dialog.open() == Window.OK) {
179 Object[] result = dialog.getResult();
180 if (result != null && result.length == 1) {
181 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember((IPath) result[0]);
182 if (resource instanceof IFile) {
185 initialiseFolder(resource);
190 private void dialogChanged() {
191 if (getFolderText().length() == 0) {
192 updateStatus("Folder must be specified");
193 } else if (getExportDirectoryPath().length() == 0) {
194 updateStatus("Directory must be specified");
200 private void updateStatus(String message) {
201 setErrorMessage(message);
202 setPageComplete(message == null);
205 public String getExportDirectoryPath() {
206 return exportDirectoryText.getStringValue();
209 public void propertyChange(PropertyChangeEvent event) {
213 public void widgetSelected(SelectionEvent e) {
217 public void widgetDefaultSelected(SelectionEvent e) {
221 String getFolderText() {
222 return folderText.getStringValue();
225 public IContainer getFolder() {
226 return (IContainer) ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getFolderText()));