KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > WizardArchiveFileResourceExportPage1


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.wizards.datatransfer;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.dialogs.ErrorDialog;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.FileDialog;
28 import org.eclipse.swt.widgets.Group;
29 import org.eclipse.ui.PlatformUI;
30
31 /**
32  * Page 1 of the base resource export-to-archive Wizard.
33  *
34  * @since 3.1
35  */

36 public class WizardArchiveFileResourceExportPage1 extends
37         WizardFileSystemResourceExportPage1 {
38
39     // widgets
40
protected Button compressContentsCheckbox;
41     
42     private Button zipFormatButton;
43     private Button targzFormatButton;
44
45     // dialog store id constants
46
private final static String JavaDoc STORE_DESTINATION_NAMES_ID = "WizardZipFileResourceExportPage1.STORE_DESTINATION_NAMES_ID"; //$NON-NLS-1$
47

48     private final static String JavaDoc STORE_CREATE_STRUCTURE_ID = "WizardZipFileResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; //$NON-NLS-1$
49

50     private final static String JavaDoc STORE_COMPRESS_CONTENTS_ID = "WizardZipFileResourceExportPage1.STORE_COMPRESS_CONTENTS_ID"; //$NON-NLS-1$
51

52     /**
53      * Create an instance of this class.
54      *
55      * @param name java.lang.String
56      */

57     protected WizardArchiveFileResourceExportPage1(String JavaDoc name,
58             IStructuredSelection selection) {
59         super(name, selection);
60     }
61
62     /**
63      * Create an instance of this class
64      * @param selection the selection
65      */

66     public WizardArchiveFileResourceExportPage1(IStructuredSelection selection) {
67         this("zipFileExportPage1", selection); //$NON-NLS-1$
68
setTitle(DataTransferMessages.ArchiveExport_exportTitle);
69         setDescription(DataTransferMessages.ArchiveExport_description);
70     }
71
72     /** (non-Javadoc)
73      * Method declared on IDialogPage.
74      */

75     public void createControl(Composite parent) {
76         super.createControl(parent);
77         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
78                 IDataTransferHelpContextIds.ZIP_FILE_EXPORT_WIZARD_PAGE);
79     }
80
81     /**
82      * Create the export options specification widgets.
83      *
84      */

85     protected void createOptionsGroupButtons(Group optionsGroup) {
86         Font font = optionsGroup.getFont();
87         optionsGroup.setLayout(new GridLayout(2, true));
88         
89         Composite left = new Composite(optionsGroup, SWT.NONE);
90         left.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false));
91         left.setLayout(new GridLayout(1, true));
92
93         createFileFormatOptions(left, font);
94         
95         // compress... checkbox
96
compressContentsCheckbox = new Button(left, SWT.CHECK
97                 | SWT.LEFT);
98         compressContentsCheckbox.setText(DataTransferMessages.ZipExport_compressContents);
99         compressContentsCheckbox.setFont(font);
100
101         Composite right = new Composite(optionsGroup, SWT.NONE);
102         right.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false));
103         right.setLayout(new GridLayout(1, true));
104
105         createDirectoryStructureOptions(right, font);
106
107         // initial setup
108
createDirectoryStructureButton.setSelection(true);
109         createSelectionOnlyButton.setSelection(false);
110         compressContentsCheckbox.setSelection(true);
111     }
112
113     /**
114      * Create the buttons for the group that determine if the entire or
115      * selected directory structure should be created.
116      * @param optionsGroup
117      * @param font
118      */

119     protected void createFileFormatOptions(Composite optionsGroup, Font font) {
120         // create directory structure radios
121
zipFormatButton = new Button(optionsGroup, SWT.RADIO | SWT.LEFT);
122         zipFormatButton.setText(DataTransferMessages.ArchiveExport_saveInZipFormat);
123         zipFormatButton.setSelection(true);
124         zipFormatButton.setFont(font);
125
126         // create directory structure radios
127
targzFormatButton = new Button(optionsGroup, SWT.RADIO | SWT.LEFT);
128         targzFormatButton.setText(DataTransferMessages.ArchiveExport_saveInTarFormat);
129         targzFormatButton.setSelection(false);
130         targzFormatButton.setFont(font);
131     }
132     
133     /**
134      * Returns a boolean indicating whether the directory portion of the
135      * passed pathname is valid and available for use.
136      */

137     protected boolean ensureTargetDirectoryIsValid(String JavaDoc fullPathname) {
138         int separatorIndex = fullPathname.lastIndexOf(File.separator);
139
140         if (separatorIndex == -1) {
141             return true;
142         }
143
144         return ensureTargetIsValid(new File JavaDoc(fullPathname.substring(0,
145                 separatorIndex)));
146     }
147
148     /**
149      * Returns a boolean indicating whether the passed File handle is
150      * is valid and available for use.
151      */

152     protected boolean ensureTargetFileIsValid(File JavaDoc targetFile) {
153         if (targetFile.exists() && targetFile.isDirectory()) {
154             displayErrorDialog(DataTransferMessages.ZipExport_mustBeFile);
155             giveFocusToDestination();
156             return false;
157         }
158
159         if (targetFile.exists()) {
160             if (targetFile.canWrite()) {
161                 if (!queryYesNoQuestion(DataTransferMessages.ZipExport_alreadyExists)) {
162                     return false;
163                 }
164             } else {
165                 displayErrorDialog(DataTransferMessages.ZipExport_alreadyExistsError);
166                 giveFocusToDestination();
167                 return false;
168             }
169         }
170
171         return true;
172     }
173
174     /**
175      * Ensures that the target output file and its containing directory are
176      * both valid and able to be used. Answer a boolean indicating validity.
177      */

178     protected boolean ensureTargetIsValid() {
179         String JavaDoc targetPath = getDestinationValue();
180
181         if (!ensureTargetDirectoryIsValid(targetPath)) {
182             return false;
183         }
184
185         if (!ensureTargetFileIsValid(new File JavaDoc(targetPath))) {
186             return false;
187         }
188
189         return true;
190     }
191
192     /**
193      * Export the passed resource and recursively export all of its child resources
194      * (iff it's a container). Answer a boolean indicating success.
195      */

196     protected boolean executeExportOperation(ArchiveFileExportOperation op) {
197         op.setCreateLeadupStructure(createDirectoryStructureButton
198                 .getSelection());
199         op.setUseCompression(compressContentsCheckbox.getSelection());
200         op.setUseTarFormat(targzFormatButton.getSelection());
201
202         try {
203             getContainer().run(true, true, op);
204         } catch (InterruptedException JavaDoc e) {
205             return false;
206         } catch (InvocationTargetException JavaDoc e) {
207             displayErrorDialog(e.getTargetException());
208             return false;
209         }
210
211         IStatus status = op.getStatus();
212         if (!status.isOK()) {
213             ErrorDialog.openError(getContainer().getShell(),
214                     DataTransferMessages.DataTransfer_exportProblems,
215                     null, // no special message
216
status);
217             return false;
218         }
219
220         return true;
221     }
222
223     /**
224      * The Finish button was pressed. Try to do the required work now and answer
225      * a boolean indicating success. If false is returned then the wizard will
226      * not close.
227      * @returns boolean
228      */

229     public boolean finish() {
230         List JavaDoc resourcesToExport = getWhiteCheckedResources();
231         
232         if (!ensureTargetIsValid()) {
233             return false;
234         }
235
236         //Save dirty editors if possible but do not stop if not all are saved
237
saveDirtyEditors();
238         // about to invoke the operation so save our state
239
saveWidgetValues();
240
241         return executeExportOperation(new ArchiveFileExportOperation(null,
242                 resourcesToExport, getDestinationValue()));
243     }
244
245     /**
246      * Answer the string to display in the receiver as the destination type
247      */

248     protected String JavaDoc getDestinationLabel() {
249         return DataTransferMessages.ArchiveExport_destinationLabel;
250     }
251
252     /**
253      * Answer the contents of self's destination specification widget. If this
254      * value does not have a suffix then add it first.
255      */

256     protected String JavaDoc getDestinationValue() {
257         String JavaDoc idealSuffix = getOutputSuffix();
258         String JavaDoc destinationText = super.getDestinationValue();
259
260         // only append a suffix if the destination doesn't already have a . in
261
// its last path segment.
262
// Also prevent the user from selecting a directory. Allowing this will
263
// create a ".zip" file in the directory
264
if (destinationText.length() != 0
265                 && !destinationText.endsWith(File.separator)) {
266             int dotIndex = destinationText.lastIndexOf('.');
267             if (dotIndex != -1) {
268                 // the last path seperator index
269
int pathSepIndex = destinationText.lastIndexOf(File.separator);
270                 if (pathSepIndex != -1 && dotIndex < pathSepIndex) {
271                     destinationText += idealSuffix;
272                 }
273             } else {
274                 destinationText += idealSuffix;
275             }
276         }
277
278         return destinationText;
279     }
280
281     /**
282      * Answer the suffix that files exported from this wizard should have.
283      * If this suffix is a file extension (which is typically the case)
284      * then it must include the leading period character.
285      *
286      */

287     protected String JavaDoc getOutputSuffix() {
288         if(zipFormatButton.getSelection()) {
289             return ".zip"; //$NON-NLS-1$
290
} else if(compressContentsCheckbox.getSelection()) {
291             return ".tar.gz"; //$NON-NLS-1$
292
} else {
293             return ".tar"; //$NON-NLS-1$
294
}
295     }
296
297     /**
298      * Open an appropriate destination browser so that the user can specify a source
299      * to import from
300      */

301     protected void handleDestinationBrowseButtonPressed() {
302         FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE);
303         dialog.setFilterExtensions(new String JavaDoc[] { "*.zip;*.tar.gz;*.tar;*.tgz", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
304
dialog.setText(DataTransferMessages.ArchiveExport_selectDestinationTitle);
305         String JavaDoc currentSourceString = getDestinationValue();
306         int lastSeparatorIndex = currentSourceString
307                 .lastIndexOf(File.separator);
308         if (lastSeparatorIndex != -1) {
309             dialog.setFilterPath(currentSourceString.substring(0,
310                     lastSeparatorIndex));
311         }
312         String JavaDoc selectedFileName = dialog.open();
313
314         if (selectedFileName != null) {
315             setErrorMessage(null);
316             setDestinationValue(selectedFileName);
317         }
318     }
319
320     /**
321      * Hook method for saving widget values for restoration by the next instance
322      * of this class.
323      */

324     protected void internalSaveWidgetValues() {
325         // update directory names history
326
IDialogSettings settings = getDialogSettings();
327         if (settings != null) {
328             String JavaDoc[] directoryNames = settings
329                     .getArray(STORE_DESTINATION_NAMES_ID);
330             if (directoryNames == null) {
331                 directoryNames = new String JavaDoc[0];
332             }
333
334             directoryNames = addToHistory(directoryNames, getDestinationValue());
335             settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
336
337             settings.put(STORE_CREATE_STRUCTURE_ID,
338                     createDirectoryStructureButton.getSelection());
339
340             settings.put(STORE_COMPRESS_CONTENTS_ID, compressContentsCheckbox
341                     .getSelection());
342         }
343     }
344
345     /**
346      * Hook method for restoring widget values to the values that they held
347      * last time this wizard was used to completion.
348      */

349     protected void restoreWidgetValues() {
350         IDialogSettings settings = getDialogSettings();
351         if (settings != null) {
352             String JavaDoc[] directoryNames = settings
353                     .getArray(STORE_DESTINATION_NAMES_ID);
354             if (directoryNames == null || directoryNames.length == 0) {
355                 return; // ie.- no settings stored
356
}
357
358             // destination
359
setDestinationValue(directoryNames[0]);
360             for (int i = 0; i < directoryNames.length; i++) {
361                 addDestinationItem(directoryNames[i]);
362             }
363
364             boolean setStructure = settings
365                     .getBoolean(STORE_CREATE_STRUCTURE_ID);
366
367             createDirectoryStructureButton.setSelection(setStructure);
368             createSelectionOnlyButton.setSelection(!setStructure);
369
370             compressContentsCheckbox.setSelection(settings
371                     .getBoolean(STORE_COMPRESS_CONTENTS_ID));
372         }
373     }
374
375     /* (non-Javadoc)
376      * @see org.eclipse.ui.wizards.datatransfer.WizardFileSystemResourceExportPage1#destinationEmptyMessage()
377      */

378     protected String JavaDoc destinationEmptyMessage() {
379         return DataTransferMessages.ArchiveExport_destinationEmpty;
380     }
381     
382     /**
383      * Answer a boolean indicating whether the receivers destination specification
384      * widgets currently all contain valid values.
385      */

386     protected boolean validateDestinationGroup() {
387         String JavaDoc destinationValue = getDestinationValue();
388         if (destinationValue.endsWith(".tar")) { //$NON-NLS-1$
389
compressContentsCheckbox.setSelection(false);
390             targzFormatButton.setSelection(true);
391             zipFormatButton.setSelection(false);
392         } else if (destinationValue.endsWith(".tar.gz") //$NON-NLS-1$
393
|| destinationValue.endsWith(".tgz")) { //$NON-NLS-1$
394
compressContentsCheckbox.setSelection(true);
395             targzFormatButton.setSelection(true);
396             zipFormatButton.setSelection(false);
397         } else if (destinationValue.endsWith(".zip")) { //$NON-NLS-1$
398
zipFormatButton.setSelection(true);
399             targzFormatButton.setSelection(false);
400         }
401         
402         return super.validateDestinationGroup();
403     }
404 }
405
Popular Tags