1 11 package org.eclipse.ui.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.*; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.widgets.*; 23 import org.eclipse.ui.help.WorkbenchHelp; 24 25 28 class WizardZipFileResourceExportPage1 extends WizardFileSystemResourceExportPage1 { 29 30 protected Button compressContentsCheckbox; 32 33 private final static String STORE_DESTINATION_NAMES_ID = "WizardZipFileResourceExportPage1.STORE_DESTINATION_NAMES_ID"; private final static String STORE_CREATE_STRUCTURE_ID = "WizardZipFileResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; private final static String STORE_COMPRESS_CONTENTS_ID = "WizardZipFileResourceExportPage1.STORE_COMPRESS_CONTENTS_ID"; 42 protected WizardZipFileResourceExportPage1( 43 String name, 44 IStructuredSelection selection) { 45 super(name, selection); 46 } 47 51 public WizardZipFileResourceExportPage1(IStructuredSelection selection) { 52 this("zipFileExportPage1", selection); setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); setDescription(DataTransferMessages.getString("ZipExport.description")); } 56 59 public void createControl(Composite parent) { 60 super.createControl(parent); 61 WorkbenchHelp.setHelp( 62 getControl(), 63 IDataTransferHelpContextIds.ZIP_FILE_EXPORT_WIZARD_PAGE); 64 } 65 69 protected void createOptionsGroupButtons(Group optionsGroup) { 70 71 Font font = optionsGroup.getFont(); 72 compressContentsCheckbox = 74 new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 75 compressContentsCheckbox.setText(DataTransferMessages.getString("ZipExport.compressContents")); compressContentsCheckbox.setFont(font); 77 78 createDirectoryStructureOptions(optionsGroup, font); 79 80 createDirectoryStructureButton.setSelection(true); 82 createSelectionOnlyButton.setSelection(false); 83 compressContentsCheckbox.setSelection(true); 84 } 85 89 protected boolean ensureTargetDirectoryIsValid(String fullPathname) { 90 int separatorIndex = fullPathname.lastIndexOf(File.separator); 91 92 if (separatorIndex == -1) return true; 94 95 return ensureTargetIsValid( 96 new File (fullPathname.substring(0, separatorIndex))); 97 } 98 102 protected boolean ensureTargetFileIsValid(File targetFile) { 103 if (targetFile.exists() && targetFile.isDirectory()) { 104 displayErrorDialog(DataTransferMessages.getString("ZipExport.mustBeFile")); giveFocusToDestination(); 106 return false; 107 } 108 109 if (targetFile.exists()) { 110 if (targetFile.canWrite()) { 111 if (!queryYesNoQuestion(DataTransferMessages.getString("ZipExport.alreadyExists"))) return false; 113 } 114 else { 115 displayErrorDialog(DataTransferMessages.getString("ZipExport.alreadyExistsError")); giveFocusToDestination(); 117 return false; 118 } 119 } 120 121 return true; 122 } 123 127 protected boolean ensureTargetIsValid() { 128 String targetPath = getDestinationValue(); 129 130 if (!ensureTargetDirectoryIsValid(targetPath)) 131 return false; 132 133 if (!ensureTargetFileIsValid(new File (targetPath))) 134 return false; 135 136 return true; 137 } 138 142 protected boolean executeExportOperation(ZipFileExportOperation op) { 143 op.setCreateLeadupStructure( 144 createDirectoryStructureButton.getSelection()); 145 op.setUseCompression(compressContentsCheckbox.getSelection()); 146 147 try { 148 getContainer().run(true, true, op); 149 } catch (InterruptedException e) { 150 return false; 151 } catch (InvocationTargetException e) { 152 displayErrorDialog(e.getTargetException()); 153 return false; 154 } 155 156 IStatus status = op.getStatus(); 157 if (!status.isOK()) { 158 ErrorDialog.openError(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.exportProblems"), null, status); 161 return false; 162 } 163 164 return true; 165 } 166 172 public boolean finish() { 173 if (!ensureTargetIsValid()) 174 return false; 175 176 List resourcesToExport = getWhiteCheckedResources(); 177 178 saveDirtyEditors(); 180 saveWidgetValues(); 182 183 if (resourcesToExport.size() > 0) 184 return executeExportOperation( 185 new ZipFileExportOperation( 186 null, 187 resourcesToExport, 188 getDestinationValue())); 189 190 MessageDialog.openInformation(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.information"), DataTransferMessages.getString("FileExport.noneSelected")); 193 return false; 194 } 195 198 protected String getDestinationLabel() { 199 return DataTransferMessages.getString("ZipExport.destinationLabel"); } 201 205 protected String getDestinationValue() { 206 String idealSuffix = getOutputSuffix(); 207 String destinationText = super.getDestinationValue(); 208 209 if (destinationText.length() != 0 && !destinationText.endsWith(File.separator)) { 214 int dotIndex = destinationText.lastIndexOf('.'); 215 if (dotIndex != -1) { 216 int pathSepIndex = destinationText.lastIndexOf(File.separator); 218 if (pathSepIndex != -1 && dotIndex < pathSepIndex) { 219 destinationText += idealSuffix; 220 } 221 } 222 else { 223 destinationText += idealSuffix; 224 } 225 } 226 227 return destinationText; 228 } 229 235 protected String getOutputSuffix() { 236 return ".zip"; } 238 242 protected void handleDestinationBrowseButtonPressed() { 243 FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE); 244 dialog.setFilterExtensions(new String [] { "*.zip", "*.*" }); dialog.setText(DataTransferMessages.getString("ZipExport.selectDestinationTitle")); String currentSourceString = getDestinationValue(); 247 int lastSeparatorIndex = 248 currentSourceString.lastIndexOf(File.separator); 249 if (lastSeparatorIndex != -1) 250 dialog.setFilterPath( 251 currentSourceString.substring(0, lastSeparatorIndex)); 252 String selectedFileName = dialog.open(); 253 254 if (selectedFileName != null) { 255 setErrorMessage(null); 256 setDestinationValue(selectedFileName); 257 } 258 } 259 263 protected void internalSaveWidgetValues() { 264 IDialogSettings settings = getDialogSettings(); 266 if (settings != null) { 267 String [] directoryNames = 268 settings.getArray(STORE_DESTINATION_NAMES_ID); 269 if (directoryNames == null) 270 directoryNames = new String [0]; 271 272 directoryNames = 273 addToHistory(directoryNames, getDestinationValue()); 274 settings.put(STORE_DESTINATION_NAMES_ID, directoryNames); 275 276 settings.put( 277 STORE_CREATE_STRUCTURE_ID, 278 createDirectoryStructureButton.getSelection()); 279 280 settings.put( 281 STORE_COMPRESS_CONTENTS_ID, 282 compressContentsCheckbox.getSelection()); 283 } 284 } 285 289 protected void restoreWidgetValues() { 290 IDialogSettings settings = getDialogSettings(); 291 if (settings != null) { 292 String [] directoryNames = 293 settings.getArray(STORE_DESTINATION_NAMES_ID); 294 if (directoryNames == null || directoryNames.length == 0) 295 return; 297 setDestinationValue(directoryNames[0]); 299 for (int i = 0; i < directoryNames.length; i++) 300 addDestinationItem(directoryNames[i]); 301 302 boolean setStructure = 303 settings.getBoolean(STORE_CREATE_STRUCTURE_ID); 304 305 createDirectoryStructureButton.setSelection(setStructure); 306 createSelectionOnlyButton.setSelection(!setStructure); 307 308 compressContentsCheckbox.setSelection( 309 settings.getBoolean(STORE_COMPRESS_CONTENTS_ID)); 310 } 311 } 312 315 protected String destinationEmptyMessage() { 316 return DataTransferMessages.getString("ZipExport.destinationEmpty"); } 318 319 } 320 | Popular Tags |