KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > datatransfer > WizardZipFileResourceExportPage1


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

11 package org.eclipse.ui.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.*;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.ui.help.WorkbenchHelp;
24
25 /**
26  * Page 1 of the base resource export-to-zip Wizard
27  */

28 /*package*/ class WizardZipFileResourceExportPage1 extends WizardFileSystemResourceExportPage1 {
29
30     // widgets
31
protected Button compressContentsCheckbox;
32
33     // dialog store id constants
34
private final static String JavaDoc STORE_DESTINATION_NAMES_ID = "WizardZipFileResourceExportPage1.STORE_DESTINATION_NAMES_ID"; //$NON-NLS-1$
35
private final static String JavaDoc STORE_CREATE_STRUCTURE_ID = "WizardZipFileResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; //$NON-NLS-1$
36
private final static String JavaDoc STORE_COMPRESS_CONTENTS_ID = "WizardZipFileResourceExportPage1.STORE_COMPRESS_CONTENTS_ID"; //$NON-NLS-1$
37
/**
38      * Create an instance of this class.
39      *
40      * @param name java.lang.String
41      */

42     protected WizardZipFileResourceExportPage1(
43         String JavaDoc name,
44         IStructuredSelection selection) {
45         super(name, selection);
46     }
47     /**
48      * Create an instance of this class
49      * @param IStructuredSelection selection
50      */

51     public WizardZipFileResourceExportPage1(IStructuredSelection selection) {
52         this("zipFileExportPage1", selection); //$NON-NLS-1$
53
setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); //$NON-NLS-1$
54
setDescription(DataTransferMessages.getString("ZipExport.description")); //$NON-NLS-1$
55
}
56     /** (non-Javadoc)
57      * Method declared on IDialogPage.
58      */

59     public void createControl(Composite parent) {
60         super.createControl(parent);
61         WorkbenchHelp.setHelp(
62             getControl(),
63             IDataTransferHelpContextIds.ZIP_FILE_EXPORT_WIZARD_PAGE);
64     }
65     /**
66      * Create the export options specification widgets.
67      *
68      */

69     protected void createOptionsGroupButtons(Group optionsGroup) {
70
71         Font font = optionsGroup.getFont();
72         // compress... checkbox
73
compressContentsCheckbox =
74             new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
75         compressContentsCheckbox.setText(DataTransferMessages.getString("ZipExport.compressContents")); //$NON-NLS-1$
76
compressContentsCheckbox.setFont(font);
77
78         createDirectoryStructureOptions(optionsGroup, font);
79
80         // initial setup
81
createDirectoryStructureButton.setSelection(true);
82         createSelectionOnlyButton.setSelection(false);
83         compressContentsCheckbox.setSelection(true);
84     }
85     /**
86      * Returns a boolean indicating whether the directory portion of the
87      * passed pathname is valid and available for use.
88      */

89     protected boolean ensureTargetDirectoryIsValid(String JavaDoc fullPathname) {
90         int separatorIndex = fullPathname.lastIndexOf(File.separator);
91
92         if (separatorIndex == -1) // ie.- default dir, which is fine
93
return true;
94
95         return ensureTargetIsValid(
96             new File JavaDoc(fullPathname.substring(0, separatorIndex)));
97     }
98     /**
99      * Returns a boolean indicating whether the passed File handle is
100      * is valid and available for use.
101      */

102     protected boolean ensureTargetFileIsValid(File JavaDoc targetFile) {
103         if (targetFile.exists() && targetFile.isDirectory()) {
104             displayErrorDialog(DataTransferMessages.getString("ZipExport.mustBeFile")); //$NON-NLS-1$
105
giveFocusToDestination();
106             return false;
107         }
108
109         if (targetFile.exists()) {
110             if (targetFile.canWrite()) {
111                 if (!queryYesNoQuestion(DataTransferMessages.getString("ZipExport.alreadyExists"))) //$NON-NLS-1$
112
return false;
113             }
114             else {
115                 displayErrorDialog(DataTransferMessages.getString("ZipExport.alreadyExistsError")); //$NON-NLS-1$
116
giveFocusToDestination();
117                 return false;
118             }
119         }
120
121         return true;
122     }
123     /**
124      * Ensures that the target output file and its containing directory are
125      * both valid and able to be used. Answer a boolean indicating validity.
126      */

127     protected boolean ensureTargetIsValid() {
128         String JavaDoc targetPath = getDestinationValue();
129
130         if (!ensureTargetDirectoryIsValid(targetPath))
131             return false;
132
133         if (!ensureTargetFileIsValid(new File JavaDoc(targetPath)))
134             return false;
135
136         return true;
137     }
138     /**
139      * Export the passed resource and recursively export all of its child resources
140      * (iff it's a container). Answer a boolean indicating success.
141      */

142     protected boolean executeExportOperation(ZipFileExportOperation op) {
143         op.setCreateLeadupStructure(
144             createDirectoryStructureButton.getSelection());
145         op.setUseCompression(compressContentsCheckbox.getSelection());
146
147         try {
148             getContainer().run(true, true, op);
149         } catch (InterruptedException JavaDoc e) {
150             return false;
151         } catch (InvocationTargetException JavaDoc e) {
152             displayErrorDialog(e.getTargetException());
153             return false;
154         }
155
156         IStatus status = op.getStatus();
157         if (!status.isOK()) {
158             ErrorDialog.openError(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.exportProblems"), //$NON-NLS-1$
159
null, // no special message
160
status);
161             return false;
162         }
163
164         return true;
165     }
166     /**
167      * The Finish button was pressed. Try to do the required work now and answer
168      * a boolean indicating success. If false is returned then the wizard will
169      * not close.
170      * @returns boolean
171      */

172     public boolean finish() {
173         if (!ensureTargetIsValid())
174             return false;
175
176         List JavaDoc resourcesToExport = getWhiteCheckedResources();
177
178         //Save dirty editors if possible but do not stop if not all are saved
179
saveDirtyEditors();
180         // about to invoke the operation so save our state
181
saveWidgetValues();
182
183         if (resourcesToExport.size() > 0)
184             return executeExportOperation(
185                 new ZipFileExportOperation(
186                     null,
187                     resourcesToExport,
188                     getDestinationValue()));
189
190         MessageDialog.openInformation(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.information"), //$NON-NLS-1$
191
DataTransferMessages.getString("FileExport.noneSelected")); //$NON-NLS-1$
192

193         return false;
194     }
195     /**
196      * Answer the string to display in the receiver as the destination type
197      */

198     protected String JavaDoc getDestinationLabel() {
199         return DataTransferMessages.getString("ZipExport.destinationLabel"); //$NON-NLS-1$
200
}
201     /**
202      * Answer the contents of self's destination specification widget. If this
203      * value does not have a suffix then add it first.
204      */

205     protected String JavaDoc getDestinationValue() {
206         String JavaDoc idealSuffix = getOutputSuffix();
207         String JavaDoc destinationText = super.getDestinationValue();
208
209         // only append a suffix if the destination doesn't already have a . in
210
// its last path segment.
211
// Also prevent the user from selecting a directory. Allowing this will
212
// create a ".zip" file in the directory
213
if (destinationText.length() != 0 && !destinationText.endsWith(File.separator)) {
214             int dotIndex = destinationText.lastIndexOf('.');
215             if (dotIndex != -1) {
216                 // the last path seperator index
217
int pathSepIndex = destinationText.lastIndexOf(File.separator);
218                 if (pathSepIndex != -1 && dotIndex < pathSepIndex) {
219                     destinationText += idealSuffix;
220                 }
221             }
222             else {
223                 destinationText += idealSuffix;
224             }
225         }
226
227         return destinationText;
228     }
229     /**
230      * Answer the suffix that files exported from this wizard should have.
231      * If this suffix is a file extension (which is typically the case)
232      * then it must include the leading period character.
233      *
234      */

235     protected String JavaDoc getOutputSuffix() {
236         return ".zip"; //$NON-NLS-1$
237
}
238     /**
239      * Open an appropriate destination browser so that the user can specify a source
240      * to import from
241      */

242     protected void handleDestinationBrowseButtonPressed() {
243         FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE);
244         dialog.setFilterExtensions(new String JavaDoc[] { "*.zip", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
245
dialog.setText(DataTransferMessages.getString("ZipExport.selectDestinationTitle")); //$NON-NLS-1$
246
String JavaDoc currentSourceString = getDestinationValue();
247         int lastSeparatorIndex =
248             currentSourceString.lastIndexOf(File.separator);
249         if (lastSeparatorIndex != -1)
250             dialog.setFilterPath(
251                 currentSourceString.substring(0, lastSeparatorIndex));
252         String JavaDoc selectedFileName = dialog.open();
253
254         if (selectedFileName != null) {
255             setErrorMessage(null);
256             setDestinationValue(selectedFileName);
257         }
258     }
259     /**
260      * Hook method for saving widget values for restoration by the next instance
261      * of this class.
262      */

263     protected void internalSaveWidgetValues() {
264         // update directory names history
265
IDialogSettings settings = getDialogSettings();
266         if (settings != null) {
267             String JavaDoc[] directoryNames =
268                 settings.getArray(STORE_DESTINATION_NAMES_ID);
269             if (directoryNames == null)
270                 directoryNames = new String JavaDoc[0];
271
272             directoryNames =
273                 addToHistory(directoryNames, getDestinationValue());
274             settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
275
276             settings.put(
277                 STORE_CREATE_STRUCTURE_ID,
278                 createDirectoryStructureButton.getSelection());
279
280             settings.put(
281                 STORE_COMPRESS_CONTENTS_ID,
282                 compressContentsCheckbox.getSelection());
283         }
284     }
285     /**
286      * Hook method for restoring widget values to the values that they held
287      * last time this wizard was used to completion.
288      */

289     protected void restoreWidgetValues() {
290         IDialogSettings settings = getDialogSettings();
291         if (settings != null) {
292             String JavaDoc[] directoryNames =
293                 settings.getArray(STORE_DESTINATION_NAMES_ID);
294             if (directoryNames == null || directoryNames.length == 0)
295                 return; // ie.- no settings stored
296

297             // destination
298
setDestinationValue(directoryNames[0]);
299             for (int i = 0; i < directoryNames.length; i++)
300                 addDestinationItem(directoryNames[i]);
301
302             boolean setStructure =
303                 settings.getBoolean(STORE_CREATE_STRUCTURE_ID);
304
305             createDirectoryStructureButton.setSelection(setStructure);
306             createSelectionOnlyButton.setSelection(!setStructure);
307
308             compressContentsCheckbox.setSelection(
309                 settings.getBoolean(STORE_COMPRESS_CONTENTS_ID));
310         }
311     }
312     /* (non-Javadoc)
313      * @see org.eclipse.ui.wizards.datatransfer.WizardFileSystemResourceExportPage1#destinationEmptyMessage()
314      */

315     protected String JavaDoc destinationEmptyMessage() {
316         return DataTransferMessages.getString("ZipExport.destinationEmpty"); //$NON-NLS-1$
317
}
318
319 }
320
Popular Tags