c3250fb8e2609fae136a9745484907d3a767530a
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / WikiExportWizardPage.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 net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
11
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;
39
40 public final class WikiExportWizardPage extends WizardPage implements IPropertyChangeListener, SelectionListener {
41   private StringFieldEditor folderText;
42
43   private StringFieldEditor exportDirectoryText;
44
45   private ISelection selection;
46
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;
52   }
53
54   public void createControl(Composite parent) {
55     Composite rootComposite = createControlsContainer(parent);
56
57     try {
58       initialize();
59     } catch (RuntimeException rex) {
60       throw rex;
61     } catch (CoreException cex) {
62       WikiEditorPlugin.getDefault().log("", cex);
63       throw new RuntimeException("Caught CoreException. See log for details.");
64     }
65     dialogChanged();
66     setControl(rootComposite);
67   }
68
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));
76
77     createCommonControls(container);
78     return container;
79   }
80
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));
88
89     createFolderControls(container);
90     createExportDirectoryControls(container);
91   }
92
93   private void createExportDirectoryControls(Composite container) {
94     exportDirectoryText = addStringFieldEditor(container, WikiEditorPlugin.getResourceString("Export.wizardExportDirectory"));
95
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();
101       }
102     });
103   }
104
105   private void createFolderControls(Composite container) {
106     folderText = addStringFieldEditor(container, WikiEditorPlugin.getResourceString("Export.wizardFolder"));
107
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) {
112         try {
113           handleBrowseFolders();
114         } catch (CoreException cex) {
115           WikiEditorPlugin.getDefault().log("", cex);
116           throw new RuntimeException("Caught CoreException. See log for details.");
117         }
118       }
119     });
120   }
121
122   private StringFieldEditor addStringFieldEditor(Composite container, String labelText) {
123     Label label = new Label(container, SWT.NULL);
124     label.setText(labelText);
125
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);
130
131     editor.setPropertyChangeListener(this);
132
133     return editor;
134   }
135
136   private void initialize() throws CoreException {
137     if (selection == null || selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
138       return;
139     }
140
141     IStructuredSelection ssel = (IStructuredSelection) selection;
142     if (ssel.size() == 1) {
143       initialiseFromSelectedObject(ssel.getFirstElement());
144     }
145   }
146
147   private void initialiseFromSelectedObject(Object obj) throws CoreException {
148     if (obj instanceof IFolder || obj instanceof IProject) {
149       initialiseFolder(((IResource) obj));
150     }
151   }
152
153   private void initialiseFolder(IResource resource) throws CoreException {
154     folderText.setStringValue(resource.getFullPath().toString());
155     initialiseExportDirectoryText(resource);
156   }
157
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);
162     } else {
163       exportDirectoryText.setStringValue("");
164     }
165   }
166
167   private void handleBrowseHtmlExportLocation() {
168     DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SINGLE | SWT.OPEN);
169     String path = dialog.open();
170     if (path != null) {
171       exportDirectoryText.setStringValue(path);
172     }
173   }
174
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) {
183           return;
184         }
185         initialiseFolder(resource);
186       }
187     }
188   }
189
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");
195     } else {
196       updateStatus(null);
197     }
198   }
199
200   private void updateStatus(String message) {
201     setErrorMessage(message);
202     setPageComplete(message == null);
203   }
204
205   public String getExportDirectoryPath() {
206     return exportDirectoryText.getStringValue();
207   }
208
209   public void propertyChange(PropertyChangeEvent event) {
210     dialogChanged();
211   }
212
213   public void widgetSelected(SelectionEvent e) {
214     dialogChanged();
215   }
216
217   public void widgetDefaultSelected(SelectionEvent e) {
218     dialogChanged();
219   }
220
221   String getFolderText() {
222     return folderText.getStringValue();
223   }
224
225   public IContainer getFolder() {
226     return (IContainer) ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getFolderText()));
227   }
228 }