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.dialogs.MessageDialog; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.graphics.Font; 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.Group; 28 import org.eclipse.ui.PlatformUI; 29 30 31 34 public class WizardZipFileResourceExportPage1 extends 35 WizardFileSystemResourceExportPage1 { 36 37 protected Button compressContentsCheckbox; 39 40 private final static String STORE_DESTINATION_NAMES_ID = "WizardZipFileResourceExportPage1.STORE_DESTINATION_NAMES_ID"; 43 private final static String STORE_CREATE_STRUCTURE_ID = "WizardZipFileResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; 45 private final static String STORE_COMPRESS_CONTENTS_ID = "WizardZipFileResourceExportPage1.STORE_COMPRESS_CONTENTS_ID"; 47 52 protected WizardZipFileResourceExportPage1(String name, 53 IStructuredSelection selection) { 54 super(name, selection); 55 } 56 57 62 public WizardZipFileResourceExportPage1(IStructuredSelection selection) { 63 this("zipFileExportPage1", selection); setTitle(DataTransferMessages.ZipExport_exportTitle); 65 setDescription(DataTransferMessages.ZipExport_description); 66 } 67 68 71 public void createControl(Composite parent) { 72 super.createControl(parent); 73 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), 74 IDataTransferHelpContextIds.ZIP_FILE_EXPORT_WIZARD_PAGE); 75 } 76 77 81 protected void createOptionsGroupButtons(Group optionsGroup) { 82 83 Font font = optionsGroup.getFont(); 84 compressContentsCheckbox = new Button(optionsGroup, SWT.CHECK 86 | SWT.LEFT); 87 compressContentsCheckbox.setText(DataTransferMessages.ZipExport_compressContents); 88 compressContentsCheckbox.setFont(font); 89 90 createDirectoryStructureOptions(optionsGroup, font); 91 92 createDirectoryStructureButton.setSelection(true); 94 createSelectionOnlyButton.setSelection(false); 95 compressContentsCheckbox.setSelection(true); 96 } 97 98 102 protected boolean ensureTargetDirectoryIsValid(String fullPathname) { 103 int separatorIndex = fullPathname.lastIndexOf(File.separator); 104 105 if (separatorIndex == -1) { 106 return true; 107 } 108 109 return ensureTargetIsValid(new File (fullPathname.substring(0, 110 separatorIndex))); 111 } 112 113 117 protected boolean ensureTargetFileIsValid(File targetFile) { 118 if (targetFile.exists() && targetFile.isDirectory()) { 119 displayErrorDialog(DataTransferMessages.ZipExport_mustBeFile); 120 giveFocusToDestination(); 121 return false; 122 } 123 124 if (targetFile.exists()) { 125 if (targetFile.canWrite()) { 126 if (!queryYesNoQuestion(DataTransferMessages.ZipExport_alreadyExists)) { 127 return false; 128 } 129 } else { 130 displayErrorDialog(DataTransferMessages.ZipExport_alreadyExistsError); 131 giveFocusToDestination(); 132 return false; 133 } 134 } 135 136 return true; 137 } 138 139 143 protected boolean ensureTargetIsValid() { 144 String targetPath = getDestinationValue(); 145 146 if (!ensureTargetDirectoryIsValid(targetPath)) { 147 return false; 148 } 149 150 if (!ensureTargetFileIsValid(new File (targetPath))) { 151 return false; 152 } 153 154 return true; 155 } 156 157 161 protected boolean executeExportOperation(ArchiveFileExportOperation op) { 162 op.setCreateLeadupStructure(createDirectoryStructureButton 163 .getSelection()); 164 op.setUseCompression(compressContentsCheckbox.getSelection()); 165 166 try { 167 getContainer().run(true, true, op); 168 } catch (InterruptedException e) { 169 return false; 170 } catch (InvocationTargetException e) { 171 displayErrorDialog(e.getTargetException()); 172 return false; 173 } 174 175 IStatus status = op.getStatus(); 176 if (!status.isOK()) { 177 ErrorDialog.openError(getContainer().getShell(), 178 DataTransferMessages.DataTransfer_exportProblems, 179 null, status); 181 return false; 182 } 183 184 return true; 185 } 186 187 193 public boolean finish() { 194 if (!ensureTargetIsValid()) { 195 return false; 196 } 197 198 List resourcesToExport = getWhiteCheckedResources(); 199 200 saveDirtyEditors(); 202 saveWidgetValues(); 204 205 if (resourcesToExport.size() > 0) { 206 return executeExportOperation(new ArchiveFileExportOperation(null, 207 resourcesToExport, getDestinationValue())); 208 } 209 210 MessageDialog.openInformation(getContainer().getShell(), 211 DataTransferMessages.DataTransfer_information, 212 DataTransferMessages.FileExport_noneSelected); 213 214 return false; 215 } 216 217 220 protected String getDestinationLabel() { 221 return DataTransferMessages.ZipExport_destinationLabel; 222 } 223 224 228 protected String getDestinationValue() { 229 String idealSuffix = getOutputSuffix(); 230 String destinationText = super.getDestinationValue(); 231 232 if (destinationText.length() != 0 237 && !destinationText.endsWith(File.separator)) { 238 int dotIndex = destinationText.lastIndexOf('.'); 239 if (dotIndex != -1) { 240 int pathSepIndex = destinationText.lastIndexOf(File.separator); 242 if (pathSepIndex != -1 && dotIndex < pathSepIndex) { 243 destinationText += idealSuffix; 244 } 245 } else { 246 destinationText += idealSuffix; 247 } 248 } 249 250 return destinationText; 251 } 252 253 259 protected String getOutputSuffix() { 260 return ".zip"; } 262 263 267 protected void handleDestinationBrowseButtonPressed() { 268 FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE); 269 dialog.setFilterExtensions(new String [] { "*.zip", "*.*" }); dialog.setText(DataTransferMessages.ZipExport_selectDestinationTitle); 271 String currentSourceString = getDestinationValue(); 272 int lastSeparatorIndex = currentSourceString 273 .lastIndexOf(File.separator); 274 if (lastSeparatorIndex != -1) { 275 dialog.setFilterPath(currentSourceString.substring(0, 276 lastSeparatorIndex)); 277 } 278 String selectedFileName = dialog.open(); 279 280 if (selectedFileName != null) { 281 setErrorMessage(null); 282 setDestinationValue(selectedFileName); 283 } 284 } 285 286 290 protected void internalSaveWidgetValues() { 291 IDialogSettings settings = getDialogSettings(); 293 if (settings != null) { 294 String [] directoryNames = settings 295 .getArray(STORE_DESTINATION_NAMES_ID); 296 if (directoryNames == null) { 297 directoryNames = new String [0]; 298 } 299 300 directoryNames = addToHistory(directoryNames, getDestinationValue()); 301 settings.put(STORE_DESTINATION_NAMES_ID, directoryNames); 302 303 settings.put(STORE_CREATE_STRUCTURE_ID, 304 createDirectoryStructureButton.getSelection()); 305 306 settings.put(STORE_COMPRESS_CONTENTS_ID, compressContentsCheckbox 307 .getSelection()); 308 } 309 } 310 311 315 protected void restoreWidgetValues() { 316 IDialogSettings settings = getDialogSettings(); 317 if (settings != null) { 318 String [] directoryNames = settings 319 .getArray(STORE_DESTINATION_NAMES_ID); 320 if (directoryNames == null || directoryNames.length == 0) { 321 return; } 323 324 setDestinationValue(directoryNames[0]); 326 for (int i = 0; i < directoryNames.length; i++) { 327 addDestinationItem(directoryNames[i]); 328 } 329 330 boolean setStructure = settings 331 .getBoolean(STORE_CREATE_STRUCTURE_ID); 332 333 createDirectoryStructureButton.setSelection(setStructure); 334 createSelectionOnlyButton.setSelection(!setStructure); 335 336 compressContentsCheckbox.setSelection(settings 337 .getBoolean(STORE_COMPRESS_CONTENTS_ID)); 338 } 339 } 340 341 344 protected String destinationEmptyMessage() { 345 return DataTransferMessages.ZipExport_destinationEmpty; 346 } 347 348 } 349 | Popular Tags |