KickJava   Java API By Example, From Geeks To Geeks.

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


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.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.*;
24
25 /**
26  * Page 1 of the base resource export-to-zip Wizard
27  * WizardFileSystemExportPage1
28  * @deprecated use WizardZipFileResourceExportPage1
29  */

30 /*package*/ class WizardZipFileExportPage1 extends WizardFileSystemExportPage1 {
31
32     // widgets
33
protected Button overwriteExistingFileCheckbox;
34     protected Button createDirectoryStructureCheckbox;
35     protected Button compressContentsCheckbox;
36
37     // constants
38
protected static final int COMBO_HISTORY_LENGTH = 5;
39     
40     // dialog store id constants
41
private final static String JavaDoc STORE_DESTINATION_NAMES_ID = "WizardZipFileExportPage1.STORE_DESTINATION_NAMES_ID";//$NON-NLS-1$
42
private final static String JavaDoc STORE_OVERWRITE_EXISTING_FILE_ID = "WizardZipFileExportPage1.STORE_OVERWRITE_EXISTING_FILE_ID";//$NON-NLS-1$
43
private final static String JavaDoc STORE_CREATE_STRUCTURE_ID = "WizardZipFileExportPage1.STORE_CREATE_STRUCTURE_ID";//$NON-NLS-1$
44
private final static String JavaDoc STORE_COMPRESS_CONTENTS_ID = "WizardZipFileExportPage1.STORE_COMPRESS_CONTENTS_ID";//$NON-NLS-1$
45
/**
46  * Create an instance of this class. Note that this constructor
47  * is here primarily to keep the JarFileExportPage1 subclass happy
48  *
49  * @param name java.lang.String
50  */

51 protected WizardZipFileExportPage1(String JavaDoc name, IStructuredSelection selection) {
52     super(name, selection);
53 }
54 /**
55  * Create an instance of this class
56  */

57 public WizardZipFileExportPage1(IStructuredSelection selection) {
58     this("zipFileExportPage1", selection);//$NON-NLS-1$
59
setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); //$NON-NLS-1$
60
setDescription(DataTransferMessages.getString("ZipExport.description")); //$NON-NLS-1$
61
}
62 /**
63  * Create the export options specification widgets.
64  *
65  * @param parent org.eclipse.swt.widgets.Composite
66  */

67 protected void createOptionsGroup(Composite parent) {
68     // options group
69
Composite optionsGroup = new Composite(parent, SWT.NONE);
70     GridLayout layout = new GridLayout();
71     layout.marginHeight = 0;
72     optionsGroup.setLayout(layout);
73     optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
74
75     // overwrite... checkbox
76
overwriteExistingFileCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
77     overwriteExistingFileCheckbox.setText(DataTransferMessages.getString("ZipExport.overwriteFile")); //$NON-NLS-1$
78

79     // create directory structure checkbox
80
createDirectoryStructureCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
81     createDirectoryStructureCheckbox.setText(DataTransferMessages.getString("ExportFile.createDirectoryStructure")); //$NON-NLS-1$
82

83     // compress... checkbox
84
compressContentsCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
85     compressContentsCheckbox.setText(DataTransferMessages.getString("ZipExport.compressContents")); //$NON-NLS-1$
86

87     // initial setup
88
createDirectoryStructureCheckbox.setSelection(true);
89     compressContentsCheckbox.setSelection(true);
90 }
91 /**
92  * Returns a boolean indicating whether the directory portion of the
93  * passed pathname is valid and available for use.
94  *
95  * @return boolean
96  */

97 protected boolean ensureTargetDirectoryIsValid(String JavaDoc fullPathname) {
98     int separatorIndex = fullPathname.lastIndexOf(File.separator);
99
100     if (separatorIndex == -1) // ie.- default dir, which is fine
101
return true;
102         
103     return ensureTargetIsValid(new File JavaDoc(fullPathname.substring(0,separatorIndex)));
104 }
105 /**
106  * Returns a boolean indicating whether the passed File handle is
107  * is valid and available for use.
108  *
109  * @return boolean
110  */

111 protected boolean ensureTargetFileIsValid(File JavaDoc targetFile) {
112     if (targetFile.exists() && targetFile.isDirectory()) {
113         displayErrorDialog(DataTransferMessages.getString("ZipExport.mustBeFile")); //$NON-NLS-1$
114
giveFocusToDestination();
115         return false;
116     }
117
118     if (targetFile.exists()) {
119         if (!overwriteExistingFileCheckbox.getSelection() && targetFile.canWrite()) {
120             if (!queryYesNoQuestion(DataTransferMessages.getString("ZipExport.alreadyExists"))) //$NON-NLS-1$
121
return false;
122         }
123
124         if (!targetFile.canWrite()) {
125             displayErrorDialog(DataTransferMessages.getString("ZipExport.alreadyExistsError")); //$NON-NLS-1$
126
giveFocusToDestination();
127             return false;
128         }
129     }
130
131     return true;
132 }
133 /**
134  * Ensures that the target output file and its containing directory are
135  * both valid and able to be used. Answer a boolean indicating these.
136  *
137  * @return boolean
138  */

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

156 protected boolean executeExportOperation(ZipFileExportOperation op) {
157     op.setCreateLeadupStructure(createDirectoryStructureCheckbox.getSelection());
158     op.setUseCompression(compressContentsCheckbox.getSelection());
159     
160     try {
161         getContainer().run(true, true, op);
162     } catch (InterruptedException JavaDoc e) {
163         return false;
164     } catch (InvocationTargetException JavaDoc e) {
165         displayErrorDialog(e.getTargetException().getMessage());
166         return false;
167     }
168
169     IStatus status = op.getStatus();
170     if (!status.isOK()) {
171         ErrorDialog.openError(getContainer().getShell(),
172             DataTransferMessages.getString("DataTransfer.exportProblems"), //$NON-NLS-1$
173
null, // no special message
174
status);
175         return false;
176     }
177
178     return true;
179 }
180 /**
181  * The Finish button was pressed. Try to do the required work now and answer
182  * a boolean indicating success. If false is returned then the wizard will
183  * not close.
184  *
185  * @return boolean
186  */

187 public boolean finish() {
188     if (!ensureTargetIsValid())
189         return false;
190
191     List JavaDoc resourcesToExport = getSelectedResources();
192
193     // about to invoke the operation so save our state
194
saveWidgetValues();
195
196     if (resourcesToExport.size() > 0)
197         return executeExportOperation(
198             new ZipFileExportOperation(
199                 null,
200                 resourcesToExport,
201                 getDestinationValue()));
202
203     MessageDialog.openInformation(
204         getContainer().getShell(),
205         DataTransferMessages.getString("DataTransfer.information"), //$NON-NLS-1$
206
DataTransferMessages.getString("FileExport.noneSelected")); //$NON-NLS-1$
207

208     return false;
209 }
210 /**
211  * Answer the string to display in self as the destination type
212  *
213  * @return java.lang.String
214  */

215 protected String JavaDoc getDestinationLabel() {
216     return DataTransferMessages.getString("ZipExport.destinationLabel"); //$NON-NLS-1$
217
}
218 /**
219  * Answer the contents of self's destination specification widget. If this
220  * value does not have the required suffix then add it first.
221  *
222  * @return java.lang.String
223  */

224 protected String JavaDoc getDestinationValue() {
225     String JavaDoc requiredSuffix = getOutputSuffix();
226     String JavaDoc destinationText = super.getDestinationValue();
227
228     if (!destinationText.toLowerCase().endsWith(requiredSuffix.toLowerCase()))
229         destinationText += requiredSuffix;
230         
231     return destinationText;
232 }
233 /**
234  * Answer the suffix that files exported from this wizard must have.
235  * If this suffix is a file extension (which is typically the case)
236  * then it must include the leading period character.
237  *
238  * @return java.lang.String
239  */

240 protected String JavaDoc getOutputSuffix() {
241     return ".zip";//$NON-NLS-1$
242
}
243 /**
244  * Open an appropriate destination browser so that the user can specify a source
245  * to import from
246  */

247 protected void handleDestinationBrowseButtonPressed() {
248     FileDialog dialog = new FileDialog(getContainer().getShell(),SWT.SAVE);
249     dialog.setFilterExtensions(new String JavaDoc[] {"*.jar;*.zip"});//$NON-NLS-1$
250

251     String JavaDoc currentSourceString = getDestinationValue();
252     int lastSeparatorIndex = currentSourceString.lastIndexOf(File.separator);
253     if (lastSeparatorIndex != -1)
254         dialog.setFilterPath(currentSourceString.substring(0,lastSeparatorIndex));
255     String JavaDoc selectedFileName = dialog.open();
256
257     if (selectedFileName != null)
258         setDestinationValue(selectedFileName);
259 }
260 /**
261  * Hook method for saving widget values for restoration by the next instance
262  * of this class.
263  */

264 protected void internalSaveWidgetValues() {
265     // update directory names history
266
IDialogSettings settings = getDialogSettings();
267     if(settings != null) {
268         String JavaDoc[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
269         if (directoryNames == null)
270             directoryNames = new String JavaDoc[0];
271         String JavaDoc[] newDirectoryNames = new String JavaDoc[directoryNames.length + 1];
272         System.arraycopy(directoryNames,0,newDirectoryNames,1,directoryNames.length);
273         newDirectoryNames[0] = getDestinationValue();
274     
275         settings.put(
276             STORE_DESTINATION_NAMES_ID,
277             directoryNames);
278
279         // options
280
settings.put(
281             STORE_OVERWRITE_EXISTING_FILE_ID,
282             overwriteExistingFileCheckbox.getSelection());
283
284         settings.put(
285             STORE_CREATE_STRUCTURE_ID,
286             createDirectoryStructureCheckbox.getSelection());
287
288         settings.put(
289             STORE_COMPRESS_CONTENTS_ID,
290             compressContentsCheckbox.getSelection());
291     }
292 }
293 /**
294  * Hook method for restoring widget values to the values that they held
295  * last time this wizard was used to completion.
296  */

297 protected void restoreWidgetValues() {
298     IDialogSettings settings = getDialogSettings();
299     if(settings != null) {
300         String JavaDoc[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
301         if (directoryNames == null || directoryNames.length == 0)
302             return; // ie.- no settings stored
303

304         // destination
305
setDestinationValue(directoryNames[0]);
306         for (int i = 0; i < directoryNames.length; i++)
307             addDestinationItem(directoryNames[i]);
308
309         // options
310
overwriteExistingFileCheckbox.setSelection(
311             settings.getBoolean(STORE_OVERWRITE_EXISTING_FILE_ID));
312
313         createDirectoryStructureCheckbox.setSelection(
314             settings.getBoolean(STORE_CREATE_STRUCTURE_ID));
315
316         compressContentsCheckbox.setSelection(
317             settings.getBoolean(STORE_COMPRESS_CONTENTS_ID));
318     }
319 }
320 }
321
Popular Tags