1 11 package org.eclipse.ui.internal.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.osgi.util.NLS; 23 import org.eclipse.swt.SWT; 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.Listener; 28 import org.eclipse.ui.IWorkbench; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.model.WorkbenchContentProvider; 31 import org.eclipse.ui.wizards.datatransfer.ImportOperation; 32 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; 33 34 35 42 public class WizardZipFileResourceImportPage1 extends 43 WizardFileSystemResourceImportPage1 implements Listener { 44 private ZipFileStructureProvider providerCache; 45 46 ZipFileStructureProvider currentProvider; 47 48 private static final String [] FILE_IMPORT_MASK = { "*.jar;*.zip", "*.*" }; 51 private final static String STORE_SOURCE_NAMES_ID = "WizardZipFileResourceImportPage1.STORE_SOURCE_NAMES_ID"; 54 private final static String STORE_OVERWRITE_EXISTING_RESOURCES_ID = "WizardZipFileResourceImportPage1.STORE_OVERWRITE_EXISTING_RESOURCES_ID"; 56 private final static String STORE_SELECTED_TYPES_ID = "WizardZipFileResourceImportPage1.STORE_SELECTED_TYPES_ID"; 58 63 public WizardZipFileResourceImportPage1(IWorkbench aWorkbench, 64 IStructuredSelection selection) { 65 super("zipFileImportPage1", aWorkbench, selection); setTitle(DataTransferMessages.ZipExport_exportTitle); 67 setDescription(DataTransferMessages.ZipImport_description); 68 } 69 70 76 public boolean cancel() { 77 clearProviderCache(); 78 return true; 79 } 80 81 85 protected void clearProviderCache() { 86 if (providerCache != null) { 87 closeZipFile(providerCache.getZipFile()); 88 providerCache = null; 89 } 90 } 91 92 95 protected boolean closeZipFile(ZipFile file) { 96 try { 97 file.close(); 98 } catch (IOException e) { 99 displayErrorDialog(NLS.bind(DataTransferMessages.ZipImport_couldNotClose, file.getName())); 100 return false; 101 } 102 103 return true; 104 } 105 106 109 public void createControl(Composite parent) { 110 super.createControl(parent); 111 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), 112 IDataTransferHelpContextIds.ZIP_FILE_IMPORT_WIZARD_PAGE); 113 } 114 115 121 protected void createOptionsGroup(Composite parent) { 122 123 overwriteExistingResourcesCheckbox = new Button(parent, SWT.CHECK); 125 overwriteExistingResourcesCheckbox.setText(DataTransferMessages.FileImport_overwriteExisting); 126 overwriteExistingResourcesCheckbox.setFont(parent.getFont()); 127 } 128 129 133 protected boolean ensureSourceIsValid() { 134 ZipFile specifiedFile = getSpecifiedSourceFile(); 135 136 if (specifiedFile == null) { 137 return false; 138 } 139 140 return closeZipFile(specifiedFile); 141 } 142 143 150 public boolean finish() { 151 if (!super.finish()) { 152 return false; 153 } 154 155 clearProviderCache(); 156 return true; 157 } 158 159 163 protected ITreeContentProvider getFileProvider() { 164 return new WorkbenchContentProvider() { 165 public Object [] getChildren(Object o) { 166 if (o instanceof MinimizedFileSystemElement) { 167 MinimizedFileSystemElement element = (MinimizedFileSystemElement) o; 168 return element.getFiles(currentProvider).getChildren( 169 element); 170 } 171 return new Object [0]; 172 } 173 }; 174 } 175 176 181 protected MinimizedFileSystemElement getFileSystemTree() { 182 183 ZipFile sourceFile = getSpecifiedSourceFile(); 184 if (sourceFile == null) { 185 this.currentProvider = null; 187 return null; 188 } 189 190 ZipFileStructureProvider provider = getStructureProvider(sourceFile); 191 this.currentProvider = provider; 192 return selectFiles(provider.getRoot(), provider); 193 } 194 195 199 protected ITreeContentProvider getFolderProvider() { 200 return new WorkbenchContentProvider() { 201 public Object [] getChildren(Object o) { 202 if (o instanceof MinimizedFileSystemElement) { 203 MinimizedFileSystemElement element = (MinimizedFileSystemElement) o; 204 return element.getFolders(currentProvider).getChildren( 205 element); 206 } 207 return new Object [0]; 208 } 209 210 public boolean hasChildren(Object o) { 211 if (o instanceof MinimizedFileSystemElement) { 212 MinimizedFileSystemElement element = (MinimizedFileSystemElement) o; 213 if (element.isPopulated()) { 214 return getChildren(element).length > 0; 215 } else { 216 return true; 218 } 219 } 220 return false; 221 } 222 }; 223 } 224 225 228 protected String getSourceLabel() { 229 return DataTransferMessages.ZipImport_fromFile; 230 } 231 232 236 protected ZipFile getSpecifiedSourceFile() { 237 return getSpecifiedSourceFile(sourceNameField.getText()); 238 } 239 240 244 private ZipFile getSpecifiedSourceFile(String fileName) { 245 if (fileName.length() == 0) { 246 return null; 247 } 248 249 try { 250 return new ZipFile (fileName); 251 } catch (ZipException e) { 252 displayErrorDialog(DataTransferMessages.ZipImport_badFormat); 253 } catch (IOException e) { 254 displayErrorDialog(DataTransferMessages.ZipImport_couldNotRead); 255 } 256 257 sourceNameField.setFocus(); 258 return null; 259 } 260 261 264 protected ZipFileStructureProvider getStructureProvider(ZipFile targetZip) { 265 if (providerCache == null) { 266 providerCache = new ZipFileStructureProvider(targetZip); 267 } else if (!providerCache.getZipFile().getName().equals( 268 targetZip.getName())) { 269 clearProviderCache(); 270 providerCache = new ZipFileStructureProvider(targetZip); 272 } else if (!providerCache.getZipFile().equals(targetZip)) { 273 closeZipFile(targetZip); } 275 276 return providerCache; 277 } 278 279 283 protected void handleSourceBrowseButtonPressed() { 284 String selectedFile = queryZipFileToImport(); 285 286 if (selectedFile != null) { 287 if (!selectedFile.equals(sourceNameField.getText())) { 288 ZipFile sourceFile = getSpecifiedSourceFile(selectedFile); 290 if (sourceFile != null) { 291 closeZipFile(sourceFile); 292 setSourceName(selectedFile); 293 selectionGroup.setFocus(); 294 } 295 } 296 } 297 } 298 299 302 protected boolean importResources(List fileSystemObjects) { 303 ZipFile zipFile = getSpecifiedSourceFile(); 304 ZipFileStructureProvider structureProvider = getStructureProvider(zipFile); 305 ImportOperation operation = new ImportOperation(getContainerFullPath(), 306 structureProvider.getRoot(), structureProvider, this, 307 fileSystemObjects); 308 309 operation.setContext(getShell()); 310 boolean result = executeImportOperation(operation); 311 312 closeZipFile(zipFile); 313 314 return result; 315 } 316 317 320 protected void initializeOperation(ImportOperation op) { 321 op.setOverwriteResources(overwriteExistingResourcesCheckbox 322 .getSelection()); 323 } 324 325 329 protected String queryZipFileToImport() { 330 FileDialog dialog = new FileDialog(sourceNameField.getShell(), SWT.OPEN); 331 dialog.setFilterExtensions(FILE_IMPORT_MASK); 332 dialog.setText(DataTransferMessages.ZipImportSource_title); 333 334 String currentSourceString = sourceNameField.getText(); 335 int lastSeparatorIndex = currentSourceString 336 .lastIndexOf(File.separator); 337 if (lastSeparatorIndex != -1) { 338 dialog.setFilterPath(currentSourceString.substring(0, 339 lastSeparatorIndex)); 340 } 341 342 return dialog.open(); 343 } 344 345 348 protected void resetSelection() { 349 350 super.resetSelection(); 351 setAllSelections(true); 352 } 353 354 358 protected void restoreWidgetValues() { 359 IDialogSettings settings = getDialogSettings(); 360 if (settings != null) { 361 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 362 if (sourceNames == null) { 363 return; } 365 366 for (int i = 0; i < sourceNames.length; i++) { 368 sourceNameField.add(sourceNames[i]); 369 } 370 371 overwriteExistingResourcesCheckbox.setSelection(settings 373 .getBoolean(STORE_OVERWRITE_EXISTING_RESOURCES_ID)); 374 } 375 } 376 377 384 protected void saveWidgetValues() { 385 IDialogSettings settings = getDialogSettings(); 386 if (settings != null) { 387 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 389 if (sourceNames == null) { 390 sourceNames = new String [0]; 391 } 392 393 sourceNames = addToHistory(sourceNames, sourceNameField.getText()); 394 settings.put(STORE_SOURCE_NAMES_ID, sourceNames); 395 396 String [] selectedTypesNames = settings 398 .getArray(STORE_SELECTED_TYPES_ID); 399 if (selectedTypesNames == null) { 400 selectedTypesNames = new String [0]; 401 } 402 403 settings.put(STORE_OVERWRITE_EXISTING_RESOURCES_ID, 404 overwriteExistingResourcesCheckbox.getSelection()); 405 } 406 } 407 408 412 protected boolean validateSourceGroup() { 413 414 if (this.currentProvider == null) { 416 setMessage(SOURCE_EMPTY_MESSAGE); 417 enableButtonGroup(false); 418 return false; 419 } else { 420 enableButtonGroup(true); 421 return true; 422 } 423 } 424 } 425 | Popular Tags |