1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.List ; 16 import java.util.zip.ZipException ; 17 import java.util.zip.ZipFile ; 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 37 class WizardZipFileImportPage1 extends WizardFileSystemImportPage1 implements ISelectionChangedListener, Listener { 38 private ZipFileStructureProvider providerCache; 39 40 private static final String FILE_IMPORT_MASK = "*.jar;*.zip"; 43 private final static String STORE_SOURCE_NAMES_ID = "WizardZipFileImportPage1.STORE_SOURCE_NAMES_ID"; private final static String STORE_IMPORT_ALL_RESOURCES_ID = "WizardZipFileImportPage1.STORE_IMPORT_ALL_ENTRIES_ID"; private final static String STORE_OVERWRITE_EXISTING_RESOURCES_ID = "WizardZipFileImportPage1.STORE_OVERWRITE_EXISTING_RESOURCES_ID"; private final static String STORE_SELECTED_TYPES_ID = "WizardZipFileImportPage1.STORE_SELECTED_TYPES_ID"; 51 public WizardZipFileImportPage1(IWorkbench aWorkbench, IStructuredSelection selection) { 52 super("zipFileImportPage1", aWorkbench, selection); setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); setDescription(DataTransferMessages.getString("ZipImport.description")); } 56 60 public boolean cancel() { 61 clearProviderCache(); 62 return true; 63 } 64 68 protected void clearProviderCache() { 69 if (providerCache != null) { 70 closeZipFile(providerCache.getZipFile()); 71 providerCache = null; 72 } 73 } 74 77 protected boolean closeZipFile(ZipFile file) { 78 try { 79 file.close(); 80 } catch (IOException e) { 81 displayErrorDialog(DataTransferMessages.format("ZipImport.couldNotClose", new Object [] {file.getName()})); return false; 83 } 84 85 return true; 86 } 87 90 protected void createOptionsGroup(Composite parent) { 91 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 overwriteExistingResourcesCheckbox = new Button(optionsGroup,SWT.CHECK); 100 overwriteExistingResourcesCheckbox.setText(DataTransferMessages.getString("FileImport.overwriteExisting")); } 102 106 protected void displaySelectedCount(int selectedEntryCount) { 107 if (selectedEntryCount == 1) 108 detailsDescriptionLabel.setText(DataTransferMessages.getString("ImportPage.oneSelected")); else 110 detailsDescriptionLabel.setText(DataTransferMessages.format("ZipImport.entriesSelected",new Object [] {String.valueOf(selectedEntryCount)})); } 112 116 protected boolean ensureSourceIsValid() { 117 ZipFile specifiedFile = getSpecifiedSourceFile(); 118 119 if (specifiedFile == null) 120 return false; 121 122 return closeZipFile(specifiedFile); 123 } 124 129 public boolean finish() { 130 if (!super.finish()) 131 return false; 132 133 clearProviderCache(); 134 return true; 135 } 136 141 protected FileSystemElement getFileSystemTree() { 142 if (getRoot() != null) 143 return getRoot(); 144 145 ZipFile sourceFile = getSpecifiedSourceFile(); 146 if (sourceFile == null) 147 return null; 148 149 ZipFileStructureProvider provider = getStructureProvider(sourceFile); 150 return selectFiles(provider.getRoot(),provider); 151 } 152 155 protected String getSourceLabel() { 156 return DataTransferMessages.getString("ZipExport.destinationLabel"); } 158 162 protected ZipFile getSpecifiedSourceFile() { 163 try { 164 return new ZipFile (sourceNameField.getText()); 165 } catch (ZipException e) { 166 displayErrorDialog(DataTransferMessages.getString("ZipImport.badFormat")); } catch (IOException e) { 168 displayErrorDialog(DataTransferMessages.getString("ZipImport.couldNotRead")); } 170 171 sourceNameField.setFocus(); 172 return null; 173 } 174 177 protected ZipFileStructureProvider getStructureProvider(ZipFile targetZip) { 178 if (providerCache == null) 179 providerCache = new ZipFileStructureProvider(targetZip); 180 else if (!providerCache.getZipFile().getName().equals(targetZip.getName())) { 181 clearProviderCache(); providerCache = new ZipFileStructureProvider(targetZip); 183 } else if (!providerCache.getZipFile().equals(targetZip)) 184 closeZipFile(targetZip); 186 return providerCache; 187 } 188 192 protected void handleSourceBrowseButtonPressed() { 193 String selectedFile = queryZipFileToImport(); 194 195 if (selectedFile != null) { 196 if (!selectedFile.equals(sourceNameField.getText())) { 197 resetSelection(); 198 sourceNameField.setText(selectedFile); 199 } 200 } 201 } 202 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 219 protected boolean importResources(List 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 233 protected void initializeOperation(ImportOperation op) { 234 op.setOverwriteResources(overwriteExistingResourcesCheckbox.getSelection()); 235 } 236 240 protected String queryZipFileToImport() { 241 FileDialog dialog = new FileDialog(sourceNameField.getShell(),SWT.OPEN); 242 dialog.setFilterExtensions(new String [] {FILE_IMPORT_MASK}); 243 244 String 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 255 protected void restoreWidgetValues() { 256 IDialogSettings settings = getDialogSettings(); 257 if(settings != null) { 258 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 259 if (sourceNames == null) 260 return; 262 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 sourceNameField.setText(sourceNames[0]); 273 for (int i = 0; i < sourceNames.length; i++) 274 sourceNameField.add(sourceNames[i]); 275 276 String [] selectedTypes = settings.getArray(STORE_SELECTED_TYPES_ID); 278 if (selectedTypes.length > 0) 279 typesToImportField.setText((String )selectedTypes[0]); 280 for (int i = 0; i < selectedTypes.length; i++) 281 typesToImportField.add((String )selectedTypes[i]); 282 283 overwriteExistingResourcesCheckbox.setSelection( 285 settings.getBoolean(STORE_OVERWRITE_EXISTING_RESOURCES_ID)); 286 } 287 } 288 295 protected void saveWidgetValues() { 296 IDialogSettings settings = getDialogSettings(); 297 if(settings != null) { 298 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 300 if (sourceNames == null) 301 sourceNames = new String [0]; 302 303 sourceNames = addToHistory(sourceNames,sourceNameField.getText()); 304 settings.put( 305 STORE_SOURCE_NAMES_ID, 306 sourceNames); 307 308 String [] selectedTypesNames = settings.getArray(STORE_SELECTED_TYPES_ID); 310 if (selectedTypesNames == null) 311 selectedTypesNames = new String [0]; 312 313 if (importTypedResourcesRadio.getSelection()) 314 selectedTypesNames = addToHistory(selectedTypesNames,typesToImportField.getText()); 315 settings.put( 316 STORE_SELECTED_TYPES_ID, 317 selectedTypesNames); 318 319 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 |