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.IStructuredSelection; 21 import org.eclipse.jface.viewers.ITreeContentProvider; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.widgets.*; 24 import org.eclipse.ui.IWorkbench; 25 import org.eclipse.ui.help.WorkbenchHelp; 26 import org.eclipse.ui.model.WorkbenchContentProvider; 27 28 35 36 class WizardZipFileResourceImportPage1 37 extends WizardFileSystemResourceImportPage1 38 implements Listener { 39 private ZipFileStructureProvider providerCache; 40 ZipFileStructureProvider currentProvider; 41 42 private static final String FILE_IMPORT_MASK = "*.jar;*.zip"; 45 private final static String STORE_SOURCE_NAMES_ID = 47 "WizardZipFileResourceImportPage1.STORE_SOURCE_NAMES_ID"; private final static String STORE_OVERWRITE_EXISTING_RESOURCES_ID = 49 "WizardZipFileResourceImportPage1.STORE_OVERWRITE_EXISTING_RESOURCES_ID"; private final static String STORE_SELECTED_TYPES_ID = 51 "WizardZipFileResourceImportPage1.STORE_SELECTED_TYPES_ID"; 57 public WizardZipFileResourceImportPage1( 58 IWorkbench aWorkbench, 59 IStructuredSelection selection) { 60 super("zipFileImportPage1", aWorkbench, selection); setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); setDescription(DataTransferMessages.getString("ZipImport.description")); } 64 70 public boolean cancel() { 71 clearProviderCache(); 72 return true; 73 } 74 78 protected void clearProviderCache() { 79 if (providerCache != null) { 80 closeZipFile(providerCache.getZipFile()); 81 providerCache = null; 82 } 83 } 84 87 protected boolean closeZipFile(ZipFile file) { 88 try { 89 file.close(); 90 } catch (IOException e) { 91 displayErrorDialog(DataTransferMessages.format("ZipImport.couldNotClose", new Object [] { file.getName()})); return false; 93 } 94 95 return true; 96 } 97 100 public void createControl(Composite parent) { 101 super.createControl(parent); 102 WorkbenchHelp.setHelp( 103 getControl(), 104 IDataTransferHelpContextIds.ZIP_FILE_IMPORT_WIZARD_PAGE); 105 } 106 112 protected void createOptionsGroup(Composite parent) { 113 114 overwriteExistingResourcesCheckbox = new Button(parent, SWT.CHECK); 116 overwriteExistingResourcesCheckbox.setText(DataTransferMessages.getString("FileImport.overwriteExisting")); overwriteExistingResourcesCheckbox.setFont(parent.getFont()); 118 } 119 123 protected boolean ensureSourceIsValid() { 124 ZipFile specifiedFile = getSpecifiedSourceFile(); 125 126 if (specifiedFile == null) 127 return false; 128 129 return closeZipFile(specifiedFile); 130 } 131 138 public boolean finish() { 139 if (!super.finish()) 140 return false; 141 142 clearProviderCache(); 143 return true; 144 } 145 149 protected ITreeContentProvider getFileProvider() { 150 return new WorkbenchContentProvider() { 151 public Object [] getChildren(Object o) { 152 if (o instanceof MinimizedFileSystemElement) { 153 MinimizedFileSystemElement element = 154 (MinimizedFileSystemElement) o; 155 return element.getFiles(currentProvider).getChildren( 156 element); 157 } 158 return new Object [0]; 159 } 160 }; 161 } 162 167 protected MinimizedFileSystemElement getFileSystemTree() { 168 169 ZipFile sourceFile = getSpecifiedSourceFile(); 170 if (sourceFile == null) { 171 this.currentProvider = null; 173 return null; 174 } 175 176 ZipFileStructureProvider provider = getStructureProvider(sourceFile); 177 this.currentProvider = provider; 178 return selectFiles(provider.getRoot(), provider); 179 } 180 184 protected ITreeContentProvider getFolderProvider() { 185 return new WorkbenchContentProvider() { 186 public Object [] getChildren(Object o) { 187 if (o instanceof MinimizedFileSystemElement) { 188 MinimizedFileSystemElement element = 189 (MinimizedFileSystemElement) o; 190 return element.getFolders(currentProvider).getChildren( 191 element); 192 } 193 return new Object [0]; 194 } 195 public boolean hasChildren(Object o) { 196 if (o instanceof MinimizedFileSystemElement) { 197 MinimizedFileSystemElement element = 198 (MinimizedFileSystemElement) o; 199 if (element.isPopulated()) 200 return getChildren(element).length > 0; 201 else { 202 return true; 204 } 205 } 206 return false; 207 } 208 }; 209 } 210 213 protected String getSourceLabel() { 214 return DataTransferMessages.getString("ZipImport.fromFile"); } 216 220 protected ZipFile getSpecifiedSourceFile() { 221 return getSpecifiedSourceFile(sourceNameField.getText()); 222 } 223 227 private ZipFile getSpecifiedSourceFile(String fileName) { 228 if (fileName.length() == 0) 229 return null; 230 231 try { 232 return new ZipFile (fileName); 233 } catch (ZipException e) { 234 displayErrorDialog(DataTransferMessages.getString("ZipImport.badFormat")); } catch (IOException e) { 236 displayErrorDialog(DataTransferMessages.getString("ZipImport.couldNotRead")); } 238 239 sourceNameField.setFocus(); 240 return null; 241 } 242 245 protected ZipFileStructureProvider getStructureProvider(ZipFile targetZip) { 246 if (providerCache == null) 247 providerCache = new ZipFileStructureProvider(targetZip); 248 else if ( 249 !providerCache.getZipFile().getName().equals( 250 targetZip.getName())) { 251 clearProviderCache(); 252 providerCache = new ZipFileStructureProvider(targetZip); 254 } else if (!providerCache.getZipFile().equals(targetZip)) 255 closeZipFile(targetZip); 257 return providerCache; 258 } 259 263 protected void handleSourceBrowseButtonPressed() { 264 String selectedFile = queryZipFileToImport(); 265 266 if (selectedFile != null) { 267 if (!selectedFile.equals(sourceNameField.getText())) { 268 ZipFile sourceFile = getSpecifiedSourceFile(selectedFile); 270 if (sourceFile != null) { 271 closeZipFile(sourceFile); 272 setSourceName(selectedFile); 273 selectionGroup.setFocus(); 274 } 275 } 276 } 277 } 278 281 protected boolean importResources(List fileSystemObjects) { 282 ZipFile zipFile = getSpecifiedSourceFile(); 283 ZipFileStructureProvider structureProvider = 284 getStructureProvider(zipFile); 285 ImportOperation operation = 286 new ImportOperation( 287 getContainerFullPath(), 288 structureProvider.getRoot(), 289 structureProvider, 290 this, 291 fileSystemObjects); 292 293 operation.setContext(getShell()); 294 boolean result = executeImportOperation(operation); 295 296 closeZipFile(zipFile); 297 298 return result; 299 } 300 303 protected void initializeOperation(ImportOperation op) { 304 op.setOverwriteResources( 305 overwriteExistingResourcesCheckbox.getSelection()); 306 } 307 311 protected String queryZipFileToImport() { 312 FileDialog dialog = 313 new FileDialog(sourceNameField.getShell(), SWT.OPEN); 314 dialog.setFilterExtensions(new String [] { FILE_IMPORT_MASK }); 315 dialog.setText(DataTransferMessages.getString("ZipImportSource.title")); 317 String currentSourceString = sourceNameField.getText(); 318 int lastSeparatorIndex = 319 currentSourceString.lastIndexOf(File.separator); 320 if (lastSeparatorIndex != -1) 321 dialog.setFilterPath( 322 currentSourceString.substring(0, lastSeparatorIndex)); 323 324 return dialog.open(); 325 } 326 329 protected void resetSelection() { 330 331 super.resetSelection(); 332 setAllSelections(true); 333 } 334 338 protected void restoreWidgetValues() { 339 IDialogSettings settings = getDialogSettings(); 340 if (settings != null) { 341 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 342 if (sourceNames == null) 343 return; 345 for (int i = 0; i < sourceNames.length; i++) 347 sourceNameField.add(sourceNames[i]); 348 349 overwriteExistingResourcesCheckbox.setSelection( 351 settings.getBoolean(STORE_OVERWRITE_EXISTING_RESOURCES_ID)); 352 } 353 } 354 361 protected void saveWidgetValues() { 362 IDialogSettings settings = getDialogSettings(); 363 if (settings != null) { 364 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 366 if (sourceNames == null) 367 sourceNames = new String [0]; 368 369 sourceNames = addToHistory(sourceNames, sourceNameField.getText()); 370 settings.put(STORE_SOURCE_NAMES_ID, sourceNames); 371 372 String [] selectedTypesNames = 374 settings.getArray(STORE_SELECTED_TYPES_ID); 375 if (selectedTypesNames == null) 376 selectedTypesNames = new String [0]; 377 378 settings.put( 379 STORE_OVERWRITE_EXISTING_RESOURCES_ID, 380 overwriteExistingResourcesCheckbox.getSelection()); 381 } 382 } 383 387 protected boolean validateSourceGroup() { 388 389 if (this.currentProvider == null) { 391 setMessage(SOURCE_EMPTY_MESSAGE); 392 enableButtonGroup(false); 393 return false; 394 } else { 395 enableButtonGroup(true); 396 return true; 397 } 398 } 399 } 400 | Popular Tags |