1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.File ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.jface.dialogs.ErrorDialog; 19 import org.eclipse.jface.dialogs.IDialogSettings; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.Font; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.FileDialog; 28 import org.eclipse.swt.widgets.Group; 29 import org.eclipse.ui.PlatformUI; 30 31 36 public class WizardArchiveFileResourceExportPage1 extends 37 WizardFileSystemResourceExportPage1 { 38 39 protected Button compressContentsCheckbox; 41 42 private Button zipFormatButton; 43 private Button targzFormatButton; 44 45 private final static String STORE_DESTINATION_NAMES_ID = "WizardZipFileResourceExportPage1.STORE_DESTINATION_NAMES_ID"; 48 private final static String STORE_CREATE_STRUCTURE_ID = "WizardZipFileResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; 50 private final static String STORE_COMPRESS_CONTENTS_ID = "WizardZipFileResourceExportPage1.STORE_COMPRESS_CONTENTS_ID"; 52 57 protected WizardArchiveFileResourceExportPage1(String name, 58 IStructuredSelection selection) { 59 super(name, selection); 60 } 61 62 66 public WizardArchiveFileResourceExportPage1(IStructuredSelection selection) { 67 this("zipFileExportPage1", selection); setTitle(DataTransferMessages.ArchiveExport_exportTitle); 69 setDescription(DataTransferMessages.ArchiveExport_description); 70 } 71 72 75 public void createControl(Composite parent) { 76 super.createControl(parent); 77 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), 78 IDataTransferHelpContextIds.ZIP_FILE_EXPORT_WIZARD_PAGE); 79 } 80 81 85 protected void createOptionsGroupButtons(Group optionsGroup) { 86 Font font = optionsGroup.getFont(); 87 optionsGroup.setLayout(new GridLayout(2, true)); 88 89 Composite left = new Composite(optionsGroup, SWT.NONE); 90 left.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false)); 91 left.setLayout(new GridLayout(1, true)); 92 93 createFileFormatOptions(left, font); 94 95 compressContentsCheckbox = new Button(left, SWT.CHECK 97 | SWT.LEFT); 98 compressContentsCheckbox.setText(DataTransferMessages.ZipExport_compressContents); 99 compressContentsCheckbox.setFont(font); 100 101 Composite right = new Composite(optionsGroup, SWT.NONE); 102 right.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false)); 103 right.setLayout(new GridLayout(1, true)); 104 105 createDirectoryStructureOptions(right, font); 106 107 createDirectoryStructureButton.setSelection(true); 109 createSelectionOnlyButton.setSelection(false); 110 compressContentsCheckbox.setSelection(true); 111 } 112 113 119 protected void createFileFormatOptions(Composite optionsGroup, Font font) { 120 zipFormatButton = new Button(optionsGroup, SWT.RADIO | SWT.LEFT); 122 zipFormatButton.setText(DataTransferMessages.ArchiveExport_saveInZipFormat); 123 zipFormatButton.setSelection(true); 124 zipFormatButton.setFont(font); 125 126 targzFormatButton = new Button(optionsGroup, SWT.RADIO | SWT.LEFT); 128 targzFormatButton.setText(DataTransferMessages.ArchiveExport_saveInTarFormat); 129 targzFormatButton.setSelection(false); 130 targzFormatButton.setFont(font); 131 } 132 133 137 protected boolean ensureTargetDirectoryIsValid(String fullPathname) { 138 int separatorIndex = fullPathname.lastIndexOf(File.separator); 139 140 if (separatorIndex == -1) { 141 return true; 142 } 143 144 return ensureTargetIsValid(new File (fullPathname.substring(0, 145 separatorIndex))); 146 } 147 148 152 protected boolean ensureTargetFileIsValid(File targetFile) { 153 if (targetFile.exists() && targetFile.isDirectory()) { 154 displayErrorDialog(DataTransferMessages.ZipExport_mustBeFile); 155 giveFocusToDestination(); 156 return false; 157 } 158 159 if (targetFile.exists()) { 160 if (targetFile.canWrite()) { 161 if (!queryYesNoQuestion(DataTransferMessages.ZipExport_alreadyExists)) { 162 return false; 163 } 164 } else { 165 displayErrorDialog(DataTransferMessages.ZipExport_alreadyExistsError); 166 giveFocusToDestination(); 167 return false; 168 } 169 } 170 171 return true; 172 } 173 174 178 protected boolean ensureTargetIsValid() { 179 String targetPath = getDestinationValue(); 180 181 if (!ensureTargetDirectoryIsValid(targetPath)) { 182 return false; 183 } 184 185 if (!ensureTargetFileIsValid(new File (targetPath))) { 186 return false; 187 } 188 189 return true; 190 } 191 192 196 protected boolean executeExportOperation(ArchiveFileExportOperation op) { 197 op.setCreateLeadupStructure(createDirectoryStructureButton 198 .getSelection()); 199 op.setUseCompression(compressContentsCheckbox.getSelection()); 200 op.setUseTarFormat(targzFormatButton.getSelection()); 201 202 try { 203 getContainer().run(true, true, op); 204 } catch (InterruptedException e) { 205 return false; 206 } catch (InvocationTargetException e) { 207 displayErrorDialog(e.getTargetException()); 208 return false; 209 } 210 211 IStatus status = op.getStatus(); 212 if (!status.isOK()) { 213 ErrorDialog.openError(getContainer().getShell(), 214 DataTransferMessages.DataTransfer_exportProblems, 215 null, status); 217 return false; 218 } 219 220 return true; 221 } 222 223 229 public boolean finish() { 230 List resourcesToExport = getWhiteCheckedResources(); 231 232 if (!ensureTargetIsValid()) { 233 return false; 234 } 235 236 saveDirtyEditors(); 238 saveWidgetValues(); 240 241 return executeExportOperation(new ArchiveFileExportOperation(null, 242 resourcesToExport, getDestinationValue())); 243 } 244 245 248 protected String getDestinationLabel() { 249 return DataTransferMessages.ArchiveExport_destinationLabel; 250 } 251 252 256 protected String getDestinationValue() { 257 String idealSuffix = getOutputSuffix(); 258 String destinationText = super.getDestinationValue(); 259 260 if (destinationText.length() != 0 265 && !destinationText.endsWith(File.separator)) { 266 int dotIndex = destinationText.lastIndexOf('.'); 267 if (dotIndex != -1) { 268 int pathSepIndex = destinationText.lastIndexOf(File.separator); 270 if (pathSepIndex != -1 && dotIndex < pathSepIndex) { 271 destinationText += idealSuffix; 272 } 273 } else { 274 destinationText += idealSuffix; 275 } 276 } 277 278 return destinationText; 279 } 280 281 287 protected String getOutputSuffix() { 288 if(zipFormatButton.getSelection()) { 289 return ".zip"; } else if(compressContentsCheckbox.getSelection()) { 291 return ".tar.gz"; } else { 293 return ".tar"; } 295 } 296 297 301 protected void handleDestinationBrowseButtonPressed() { 302 FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE); 303 dialog.setFilterExtensions(new String [] { "*.zip;*.tar.gz;*.tar;*.tgz", "*.*" }); dialog.setText(DataTransferMessages.ArchiveExport_selectDestinationTitle); 305 String currentSourceString = getDestinationValue(); 306 int lastSeparatorIndex = currentSourceString 307 .lastIndexOf(File.separator); 308 if (lastSeparatorIndex != -1) { 309 dialog.setFilterPath(currentSourceString.substring(0, 310 lastSeparatorIndex)); 311 } 312 String selectedFileName = dialog.open(); 313 314 if (selectedFileName != null) { 315 setErrorMessage(null); 316 setDestinationValue(selectedFileName); 317 } 318 } 319 320 324 protected void internalSaveWidgetValues() { 325 IDialogSettings settings = getDialogSettings(); 327 if (settings != null) { 328 String [] directoryNames = settings 329 .getArray(STORE_DESTINATION_NAMES_ID); 330 if (directoryNames == null) { 331 directoryNames = new String [0]; 332 } 333 334 directoryNames = addToHistory(directoryNames, getDestinationValue()); 335 settings.put(STORE_DESTINATION_NAMES_ID, directoryNames); 336 337 settings.put(STORE_CREATE_STRUCTURE_ID, 338 createDirectoryStructureButton.getSelection()); 339 340 settings.put(STORE_COMPRESS_CONTENTS_ID, compressContentsCheckbox 341 .getSelection()); 342 } 343 } 344 345 349 protected void restoreWidgetValues() { 350 IDialogSettings settings = getDialogSettings(); 351 if (settings != null) { 352 String [] directoryNames = settings 353 .getArray(STORE_DESTINATION_NAMES_ID); 354 if (directoryNames == null || directoryNames.length == 0) { 355 return; } 357 358 setDestinationValue(directoryNames[0]); 360 for (int i = 0; i < directoryNames.length; i++) { 361 addDestinationItem(directoryNames[i]); 362 } 363 364 boolean setStructure = settings 365 .getBoolean(STORE_CREATE_STRUCTURE_ID); 366 367 createDirectoryStructureButton.setSelection(setStructure); 368 createSelectionOnlyButton.setSelection(!setStructure); 369 370 compressContentsCheckbox.setSelection(settings 371 .getBoolean(STORE_COMPRESS_CONTENTS_ID)); 372 } 373 } 374 375 378 protected String destinationEmptyMessage() { 379 return DataTransferMessages.ArchiveExport_destinationEmpty; 380 } 381 382 386 protected boolean validateDestinationGroup() { 387 String destinationValue = getDestinationValue(); 388 if (destinationValue.endsWith(".tar")) { compressContentsCheckbox.setSelection(false); 390 targzFormatButton.setSelection(true); 391 zipFormatButton.setSelection(false); 392 } else if (destinationValue.endsWith(".tar.gz") || destinationValue.endsWith(".tgz")) { compressContentsCheckbox.setSelection(true); 395 targzFormatButton.setSelection(true); 396 zipFormatButton.setSelection(false); 397 } else if (destinationValue.endsWith(".zip")) { zipFormatButton.setSelection(true); 399 targzFormatButton.setSelection(false); 400 } 401 402 return super.validateDestinationGroup(); 403 } 404 } 405 | Popular Tags |