KickJava   Java API By Example, From Geeks To Geeks.

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


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.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.FileDialog;
27 import org.eclipse.swt.widgets.Group;
28 import org.eclipse.ui.PlatformUI;
29
30
31 /**
32  * Page 1 of the base resource export-to-zip Wizard
33  */

34 public class WizardZipFileResourceExportPage1 extends
35         WizardFileSystemResourceExportPage1 {
36
37     // widgets
38
protected Button compressContentsCheckbox;
39
40     // dialog store id constants
41
private final static String JavaDoc STORE_DESTINATION_NAMES_ID = "WizardZipFileResourceExportPage1.STORE_DESTINATION_NAMES_ID"; //$NON-NLS-1$
42

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

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

47     /**
48      * Create an instance of this class.
49      *
50      * @param name java.lang.String
51      */

52     protected WizardZipFileResourceExportPage1(String JavaDoc name,
53             IStructuredSelection selection) {
54         super(name, selection);
55     }
56
57     /**
58      * Create an instance of this class.
59      *
60      * @param selection the selection
61      */

62     public WizardZipFileResourceExportPage1(IStructuredSelection selection) {
63         this("zipFileExportPage1", selection); //$NON-NLS-1$
64
setTitle(DataTransferMessages.ZipExport_exportTitle);
65         setDescription(DataTransferMessages.ZipExport_description);
66     }
67
68     /** (non-Javadoc)
69      * Method declared on IDialogPage.
70      */

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

81     protected void createOptionsGroupButtons(Group optionsGroup) {
82
83         Font font = optionsGroup.getFont();
84         // compress... checkbox
85
compressContentsCheckbox = new Button(optionsGroup, SWT.CHECK
86                 | SWT.LEFT);
87         compressContentsCheckbox.setText(DataTransferMessages.ZipExport_compressContents);
88         compressContentsCheckbox.setFont(font);
89
90         createDirectoryStructureOptions(optionsGroup, font);
91
92         // initial setup
93
createDirectoryStructureButton.setSelection(true);
94         createSelectionOnlyButton.setSelection(false);
95         compressContentsCheckbox.setSelection(true);
96     }
97
98     /**
99      * Returns a boolean indicating whether the directory portion of the
100      * passed pathname is valid and available for use.
101      */

102     protected boolean ensureTargetDirectoryIsValid(String JavaDoc fullPathname) {
103         int separatorIndex = fullPathname.lastIndexOf(File.separator);
104
105         if (separatorIndex == -1) {
106             return true;
107         }
108
109         return ensureTargetIsValid(new File JavaDoc(fullPathname.substring(0,
110                 separatorIndex)));
111     }
112
113     /**
114      * Returns a boolean indicating whether the passed File handle is
115      * is valid and available for use.
116      */

117     protected boolean ensureTargetFileIsValid(File JavaDoc targetFile) {
118         if (targetFile.exists() && targetFile.isDirectory()) {
119             displayErrorDialog(DataTransferMessages.ZipExport_mustBeFile);
120             giveFocusToDestination();
121             return false;
122         }
123
124         if (targetFile.exists()) {
125             if (targetFile.canWrite()) {
126                 if (!queryYesNoQuestion(DataTransferMessages.ZipExport_alreadyExists)) {
127                     return false;
128                 }
129             } else {
130                 displayErrorDialog(DataTransferMessages.ZipExport_alreadyExistsError);
131                 giveFocusToDestination();
132                 return false;
133             }
134         }
135
136         return true;
137     }
138
139     /**
140      * Ensures that the target output file and its containing directory are
141      * both valid and able to be used. Answer a boolean indicating validity.
142      */

143     protected boolean ensureTargetIsValid() {
144         String JavaDoc targetPath = getDestinationValue();
145
146         if (!ensureTargetDirectoryIsValid(targetPath)) {
147             return false;
148         }
149
150         if (!ensureTargetFileIsValid(new File JavaDoc(targetPath))) {
151             return false;
152         }
153
154         return true;
155     }
156
157     /**
158      * Export the passed resource and recursively export all of its child resources
159      * (iff it's a container). Answer a boolean indicating success.
160      */

161     protected boolean executeExportOperation(ArchiveFileExportOperation op) {
162         op.setCreateLeadupStructure(createDirectoryStructureButton
163                 .getSelection());
164         op.setUseCompression(compressContentsCheckbox.getSelection());
165
166         try {
167             getContainer().run(true, true, op);
168         } catch (InterruptedException JavaDoc e) {
169             return false;
170         } catch (InvocationTargetException JavaDoc e) {
171             displayErrorDialog(e.getTargetException());
172             return false;
173         }
174
175         IStatus status = op.getStatus();
176         if (!status.isOK()) {
177             ErrorDialog.openError(getContainer().getShell(),
178                     DataTransferMessages.DataTransfer_exportProblems,
179                     null, // no special message
180
status);
181             return false;
182         }
183
184         return true;
185     }
186
187     /**
188      * The Finish button was pressed. Try to do the required work now and answer
189      * a boolean indicating success. If false is returned then the wizard will
190      * not close.
191      * @returns boolean
192      */

193     public boolean finish() {
194         if (!ensureTargetIsValid()) {
195             return false;
196         }
197
198         List JavaDoc resourcesToExport = getWhiteCheckedResources();
199
200         //Save dirty editors if possible but do not stop if not all are saved
201
saveDirtyEditors();
202         // about to invoke the operation so save our state
203
saveWidgetValues();
204
205         if (resourcesToExport.size() > 0) {
206             return executeExportOperation(new ArchiveFileExportOperation(null,
207                     resourcesToExport, getDestinationValue()));
208         }
209
210         MessageDialog.openInformation(getContainer().getShell(),
211                 DataTransferMessages.DataTransfer_information,
212                 DataTransferMessages.FileExport_noneSelected);
213
214         return false;
215     }
216
217     /**
218      * Answer the string to display in the receiver as the destination type
219      */

220     protected String JavaDoc getDestinationLabel() {
221         return DataTransferMessages.ZipExport_destinationLabel;
222     }
223
224     /**
225      * Answer the contents of self's destination specification widget. If this
226      * value does not have a suffix then add it first.
227      */

228     protected String JavaDoc getDestinationValue() {
229         String JavaDoc idealSuffix = getOutputSuffix();
230         String JavaDoc destinationText = super.getDestinationValue();
231
232         // only append a suffix if the destination doesn't already have a . in
233
// its last path segment.
234
// Also prevent the user from selecting a directory. Allowing this will
235
// create a ".zip" file in the directory
236
if (destinationText.length() != 0
237                 && !destinationText.endsWith(File.separator)) {
238             int dotIndex = destinationText.lastIndexOf('.');
239             if (dotIndex != -1) {
240                 // the last path seperator index
241
int pathSepIndex = destinationText.lastIndexOf(File.separator);
242                 if (pathSepIndex != -1 && dotIndex < pathSepIndex) {
243                     destinationText += idealSuffix;
244                 }
245             } else {
246                 destinationText += idealSuffix;
247             }
248         }
249
250         return destinationText;
251     }
252
253     /**
254      * Answer the suffix that files exported from this wizard should have.
255      * If this suffix is a file extension (which is typically the case)
256      * then it must include the leading period character.
257      *
258      */

259     protected String JavaDoc getOutputSuffix() {
260         return ".zip"; //$NON-NLS-1$
261
}
262
263     /**
264      * Open an appropriate destination browser so that the user can specify a source
265      * to import from
266      */

267     protected void handleDestinationBrowseButtonPressed() {
268         FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE);
269         dialog.setFilterExtensions(new String JavaDoc[] { "*.zip", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
270
dialog.setText(DataTransferMessages.ZipExport_selectDestinationTitle);
271         String JavaDoc currentSourceString = getDestinationValue();
272         int lastSeparatorIndex = currentSourceString
273                 .lastIndexOf(File.separator);
274         if (lastSeparatorIndex != -1) {
275             dialog.setFilterPath(currentSourceString.substring(0,
276                     lastSeparatorIndex));
277         }
278         String JavaDoc selectedFileName = dialog.open();
279
280         if (selectedFileName != null) {
281             setErrorMessage(null);
282             setDestinationValue(selectedFileName);
283         }
284     }
285
286     /**
287      * Hook method for saving widget values for restoration by the next instance
288      * of this class.
289      */

290     protected void internalSaveWidgetValues() {
291         // update directory names history
292
IDialogSettings settings = getDialogSettings();
293         if (settings != null) {
294             String JavaDoc[] directoryNames = settings
295                     .getArray(STORE_DESTINATION_NAMES_ID);
296             if (directoryNames == null) {
297                 directoryNames = new String JavaDoc[0];
298             }
299
300             directoryNames = addToHistory(directoryNames, getDestinationValue());
301             settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
302
303             settings.put(STORE_CREATE_STRUCTURE_ID,
304                     createDirectoryStructureButton.getSelection());
305
306             settings.put(STORE_COMPRESS_CONTENTS_ID, compressContentsCheckbox
307                     .getSelection());
308         }
309     }
310
311     /**
312      * Hook method for restoring widget values to the values that they held
313      * last time this wizard was used to completion.
314      */

315     protected void restoreWidgetValues() {
316         IDialogSettings settings = getDialogSettings();
317         if (settings != null) {
318             String JavaDoc[] directoryNames = settings
319                     .getArray(STORE_DESTINATION_NAMES_ID);
320             if (directoryNames == null || directoryNames.length == 0) {
321                 return; // ie.- no settings stored
322
}
323
324             // destination
325
setDestinationValue(directoryNames[0]);
326             for (int i = 0; i < directoryNames.length; i++) {
327                 addDestinationItem(directoryNames[i]);
328             }
329
330             boolean setStructure = settings
331                     .getBoolean(STORE_CREATE_STRUCTURE_ID);
332
333             createDirectoryStructureButton.setSelection(setStructure);
334             createSelectionOnlyButton.setSelection(!setStructure);
335
336             compressContentsCheckbox.setSelection(settings
337                     .getBoolean(STORE_COMPRESS_CONTENTS_ID));
338         }
339     }
340
341     /* (non-Javadoc)
342      * @see org.eclipse.ui.wizards.datatransfer.WizardFileSystemResourceExportPage1#destinationEmptyMessage()
343      */

344     protected String JavaDoc destinationEmptyMessage() {
345         return DataTransferMessages.ZipExport_destinationEmpty;
346     }
347
348 }
349
Popular Tags