KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.zip.ZipException JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.dialogs.FileSystemElement;
28
29 /**
30  * Page 1 of the base resource import-from-zip Wizard.
31  *
32  * Note that importing from .jar is identical to importing from .zip, so
33  * all references to .zip in this class are equally applicable to .jar
34  * references.
35  * @deprecated use WizardZipFileResourceImportPage1
36  */

37 /*package*/ class WizardZipFileImportPage1 extends WizardFileSystemImportPage1 implements ISelectionChangedListener, Listener {
38     private ZipFileStructureProvider providerCache;
39
40     // constants
41
private static final String JavaDoc FILE_IMPORT_MASK = "*.jar;*.zip";//$NON-NLS-1$
42

43     // dialog store id constants
44
private final static String JavaDoc STORE_SOURCE_NAMES_ID = "WizardZipFileImportPage1.STORE_SOURCE_NAMES_ID";//$NON-NLS-1$
45
private final static String JavaDoc STORE_IMPORT_ALL_RESOURCES_ID = "WizardZipFileImportPage1.STORE_IMPORT_ALL_ENTRIES_ID";//$NON-NLS-1$
46
private final static String JavaDoc STORE_OVERWRITE_EXISTING_RESOURCES_ID = "WizardZipFileImportPage1.STORE_OVERWRITE_EXISTING_RESOURCES_ID";//$NON-NLS-1$
47
private final static String JavaDoc STORE_SELECTED_TYPES_ID = "WizardZipFileImportPage1.STORE_SELECTED_TYPES_ID";//$NON-NLS-1$
48
/**
49  * Creates an instance of this class
50  */

51 public WizardZipFileImportPage1(IWorkbench aWorkbench, IStructuredSelection selection) {
52     super("zipFileImportPage1", aWorkbench, selection);//$NON-NLS-1$
53
setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); //$NON-NLS-1$
54
setDescription(DataTransferMessages.getString("ZipImport.description")); //$NON-NLS-1$
55
}
56 /**
57  * Called when the user presses the Cancel button. Return a boolean
58  * indicating permission to close the wizard.
59  */

60 public boolean cancel() {
61     clearProviderCache();
62     return true;
63 }
64 /**
65  * Clears the cached structure provider after first finalizing
66  * it properly.
67  */

68 protected void clearProviderCache() {
69     if (providerCache != null) {
70         closeZipFile(providerCache.getZipFile());
71         providerCache = null;
72     }
73 }
74 /**
75  * Attempts to close the passed zip file, and answers a boolean indicating success.
76  */

77 protected boolean closeZipFile(ZipFile JavaDoc file) {
78     try {
79         file.close();
80     } catch (IOException JavaDoc e) {
81         displayErrorDialog(DataTransferMessages.format("ZipImport.couldNotClose", new Object JavaDoc [] {file.getName()})); //$NON-NLS-1$
82
return false;
83     }
84
85     return true;
86 }
87 /**
88  * Create the import options specification widgets.
89  */

90 protected void createOptionsGroup(Composite parent) {
91     // options group
92
Composite optionsGroup = new Composite(parent, SWT.NONE);
93     GridLayout layout = new GridLayout();
94     layout.marginHeight = 0;
95     optionsGroup.setLayout(layout);
96     optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
97
98     // overwrite... checkbox
99
overwriteExistingResourcesCheckbox = new Button(optionsGroup,SWT.CHECK);
100     overwriteExistingResourcesCheckbox.setText(DataTransferMessages.getString("FileImport.overwriteExisting")); //$NON-NLS-1$
101
}
102 /**
103  * Display the appropriate string representing a selection of the
104  * passed size.
105  */

106 protected void displaySelectedCount(int selectedEntryCount) {
107     if (selectedEntryCount == 1)
108         detailsDescriptionLabel.setText(DataTransferMessages.getString("ImportPage.oneSelected")); //$NON-NLS-1$
109
else
110         detailsDescriptionLabel.setText(DataTransferMessages.format("ZipImport.entriesSelected",new Object JavaDoc[] {String.valueOf(selectedEntryCount)})); //$NON-NLS-1$
111
}
112 /**
113  * Answer a boolean indicating whether the specified source currently exists
114  * and is valid (ie.- proper format)
115  */

116 protected boolean ensureSourceIsValid() {
117     ZipFile JavaDoc specifiedFile = getSpecifiedSourceFile();
118
119     if (specifiedFile == null)
120         return false;
121
122     return closeZipFile(specifiedFile);
123 }
124 /**
125  * The Finish button was pressed. Try to do the required work now and answer
126  * a boolean indicating success. If <code>false</code> is returned then the
127  * wizard will not close.
128  */

129 public boolean finish() {
130     if (!super.finish())
131         return false;
132
133     clearProviderCache();
134     return true;
135 }
136 /**
137  * Answer the root FileSystemElement that represents the contents of the
138  * currently-specified .zip file. If this FileSystemElement is not
139  * currently defined then create and return it.
140  */

141 protected FileSystemElement getFileSystemTree() {
142     if (getRoot() != null)
143         return getRoot();
144
145     ZipFile JavaDoc sourceFile = getSpecifiedSourceFile();
146     if (sourceFile == null)
147         return null;
148
149     ZipFileStructureProvider provider = getStructureProvider(sourceFile);
150     return selectFiles(provider.getRoot(),provider);
151 }
152 /**
153  * Answer the string to display as the label for the source specification field
154  */

155 protected String JavaDoc getSourceLabel() {
156     return DataTransferMessages.getString("ZipExport.destinationLabel"); //$NON-NLS-1$
157
}
158 /**
159  * Answer a handle to the zip file currently specified as being the source.
160  * Return null if this file does not exist or is not of valid format.
161  */

162 protected ZipFile JavaDoc getSpecifiedSourceFile() {
163     try {
164         return new ZipFile JavaDoc(sourceNameField.getText());
165     } catch (ZipException JavaDoc e) {
166         displayErrorDialog(DataTransferMessages.getString("ZipImport.badFormat")); //$NON-NLS-1$
167
} catch (IOException JavaDoc e) {
168         displayErrorDialog(DataTransferMessages.getString("ZipImport.couldNotRead")); //$NON-NLS-1$
169
}
170     
171     sourceNameField.setFocus();
172     return null;
173 }
174 /**
175  * Returns a structure provider for the specified zip file.
176  */

177 protected ZipFileStructureProvider getStructureProvider(ZipFile JavaDoc targetZip) {
178     if (providerCache == null)
179         providerCache = new ZipFileStructureProvider(targetZip);
180     else if (!providerCache.getZipFile().getName().equals(targetZip.getName())) {
181         clearProviderCache(); // ie.- new value, so finalize&remove old value
182
providerCache = new ZipFileStructureProvider(targetZip);
183     } else if (!providerCache.getZipFile().equals(targetZip))
184         closeZipFile(targetZip); // ie.- duplicate handle to same .zip
185

186     return providerCache;
187 }
188 /**
189  * Open a FileDialog so that the user can specify the source
190  * file to import from
191  */

192 protected void handleSourceBrowseButtonPressed() {
193     String JavaDoc selectedFile = queryZipFileToImport();
194
195     if (selectedFile != null) {
196         if (!selectedFile.equals(sourceNameField.getText())) {
197             resetSelection();
198             sourceNameField.setText(selectedFile);
199         }
200     }
201 }
202 /**
203  * Recursively import all resources starting at the user-specified source location.
204  * Answer a boolean indicating success.
205  */

206 protected boolean importAllResources() {
207     ZipFileStructureProvider structureProvider = getStructureProvider(getSpecifiedSourceFile());
208     
209     return executeImportOperation(
210         new ImportOperation(
211             getContainerFullPath(),
212             structureProvider.getRoot(),
213             structureProvider,
214             this));
215 }
216 /**
217  * Import the resources with extensions as specified by the user
218  */

219 protected boolean importResources(List JavaDoc fileSystemObjects) {
220     ZipFileStructureProvider structureProvider = getStructureProvider(getSpecifiedSourceFile());
221     
222     return executeImportOperation(
223         new ImportOperation(
224             getContainerFullPath(),
225             structureProvider.getRoot(),
226             structureProvider,
227             this,
228             fileSystemObjects));
229 }
230 /**
231  * Initializes the specified operation appropriately.
232  */

233 protected void initializeOperation(ImportOperation op) {
234     op.setOverwriteResources(overwriteExistingResourcesCheckbox.getSelection());
235 }
236 /**
237  * Opens a file selection dialog and returns a string representing the
238  * selected file, or <code>null</code> if the dialog was canceled.
239  */

240 protected String JavaDoc queryZipFileToImport() {
241     FileDialog dialog = new FileDialog(sourceNameField.getShell(),SWT.OPEN);
242     dialog.setFilterExtensions(new String JavaDoc[] {FILE_IMPORT_MASK});
243     
244     String JavaDoc currentSourceString = sourceNameField.getText();
245     int lastSeparatorIndex = currentSourceString.lastIndexOf(File.separator);
246     if (lastSeparatorIndex != -1)
247         dialog.setFilterPath(currentSourceString.substring(0,lastSeparatorIndex));
248         
249     return dialog.open();
250 }
251 /**
252  * Use the dialog store to restore widget values to the values that they held
253  * last time this wizard was used to completion
254  */

255 protected void restoreWidgetValues() {
256     IDialogSettings settings = getDialogSettings();
257     if(settings != null) {
258         String JavaDoc[] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID);
259         if (sourceNames == null)
260             return; // ie.- no settings stored
261

262         // set all/specific types radios and related enablements
263
if (settings.getBoolean(STORE_IMPORT_ALL_RESOURCES_ID)) {
264             importAllResourcesRadio.setSelection(true);
265             importTypedResourcesRadio.setSelection(false);
266         } else {
267             importTypedResourcesRadio.setSelection(true);
268             importAllResourcesRadio.setSelection(false);
269         }
270
271         // set filenames history
272
sourceNameField.setText(sourceNames[0]);
273         for (int i = 0; i < sourceNames.length; i++)
274             sourceNameField.add(sourceNames[i]);
275
276         // set selected types
277
String JavaDoc[] selectedTypes = settings.getArray(STORE_SELECTED_TYPES_ID);
278         if (selectedTypes.length > 0)
279             typesToImportField.setText((String JavaDoc)selectedTypes[0]);
280         for (int i = 0; i < selectedTypes.length; i++)
281             typesToImportField.add((String JavaDoc)selectedTypes[i]);
282             
283         // radio buttons and checkboxes
284
overwriteExistingResourcesCheckbox.setSelection(
285             settings.getBoolean(STORE_OVERWRITE_EXISTING_RESOURCES_ID));
286     }
287 }
288 /**
289  * Since Finish was pressed, write widget values to the dialog store so that they
290  * will persist into the next invocation of this wizard page.
291  *
292  * Note that this method is identical to the one that appears in the superclass.
293  * This is necessary because proper overriding of instance variables is not occurring.
294  */

295 protected void saveWidgetValues() {
296     IDialogSettings settings = getDialogSettings();
297     if(settings != null) {
298         // update source names history
299
String JavaDoc[] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID);
300         if (sourceNames == null)
301             sourceNames = new String JavaDoc[0];
302
303         sourceNames = addToHistory(sourceNames,sourceNameField.getText());
304         settings.put(
305             STORE_SOURCE_NAMES_ID,
306             sourceNames);
307
308         // update specific types to import history
309
String JavaDoc[] selectedTypesNames = settings.getArray(STORE_SELECTED_TYPES_ID);
310         if (selectedTypesNames == null)
311             selectedTypesNames = new String JavaDoc[0];
312
313         if (importTypedResourcesRadio.getSelection())
314             selectedTypesNames = addToHistory(selectedTypesNames,typesToImportField.getText());
315         settings.put(
316             STORE_SELECTED_TYPES_ID,
317             selectedTypesNames);
318
319         // radio buttons and checkboxes
320
settings.put(
321             STORE_IMPORT_ALL_RESOURCES_ID,
322             importAllResourcesRadio.getSelection());
323     
324         settings.put(
325             STORE_OVERWRITE_EXISTING_RESOURCES_ID,
326             overwriteExistingResourcesCheckbox.getSelection());
327     }
328 }
329 }
330
Popular Tags