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.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.*; 24 25 30 class WizardZipFileExportPage1 extends WizardFileSystemExportPage1 { 31 32 protected Button overwriteExistingFileCheckbox; 34 protected Button createDirectoryStructureCheckbox; 35 protected Button compressContentsCheckbox; 36 37 protected static final int COMBO_HISTORY_LENGTH = 5; 39 40 private final static String STORE_DESTINATION_NAMES_ID = "WizardZipFileExportPage1.STORE_DESTINATION_NAMES_ID"; private final static String STORE_OVERWRITE_EXISTING_FILE_ID = "WizardZipFileExportPage1.STORE_OVERWRITE_EXISTING_FILE_ID"; private final static String STORE_CREATE_STRUCTURE_ID = "WizardZipFileExportPage1.STORE_CREATE_STRUCTURE_ID"; private final static String STORE_COMPRESS_CONTENTS_ID = "WizardZipFileExportPage1.STORE_COMPRESS_CONTENTS_ID"; 51 protected WizardZipFileExportPage1(String name, IStructuredSelection selection) { 52 super(name, selection); 53 } 54 57 public WizardZipFileExportPage1(IStructuredSelection selection) { 58 this("zipFileExportPage1", selection); setTitle(DataTransferMessages.getString("ZipExport.exportTitle")); setDescription(DataTransferMessages.getString("ZipExport.description")); } 62 67 protected void createOptionsGroup(Composite parent) { 68 Composite optionsGroup = new Composite(parent, SWT.NONE); 70 GridLayout layout = new GridLayout(); 71 layout.marginHeight = 0; 72 optionsGroup.setLayout(layout); 73 optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 74 75 overwriteExistingFileCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 77 overwriteExistingFileCheckbox.setText(DataTransferMessages.getString("ZipExport.overwriteFile")); 79 createDirectoryStructureCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 81 createDirectoryStructureCheckbox.setText(DataTransferMessages.getString("ExportFile.createDirectoryStructure")); 83 compressContentsCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 85 compressContentsCheckbox.setText(DataTransferMessages.getString("ZipExport.compressContents")); 87 createDirectoryStructureCheckbox.setSelection(true); 89 compressContentsCheckbox.setSelection(true); 90 } 91 97 protected boolean ensureTargetDirectoryIsValid(String fullPathname) { 98 int separatorIndex = fullPathname.lastIndexOf(File.separator); 99 100 if (separatorIndex == -1) return true; 102 103 return ensureTargetIsValid(new File (fullPathname.substring(0,separatorIndex))); 104 } 105 111 protected boolean ensureTargetFileIsValid(File targetFile) { 112 if (targetFile.exists() && targetFile.isDirectory()) { 113 displayErrorDialog(DataTransferMessages.getString("ZipExport.mustBeFile")); giveFocusToDestination(); 115 return false; 116 } 117 118 if (targetFile.exists()) { 119 if (!overwriteExistingFileCheckbox.getSelection() && targetFile.canWrite()) { 120 if (!queryYesNoQuestion(DataTransferMessages.getString("ZipExport.alreadyExists"))) return false; 122 } 123 124 if (!targetFile.canWrite()) { 125 displayErrorDialog(DataTransferMessages.getString("ZipExport.alreadyExistsError")); giveFocusToDestination(); 127 return false; 128 } 129 } 130 131 return true; 132 } 133 139 protected boolean ensureTargetIsValid() { 140 String targetPath = getDestinationValue(); 141 142 if (!ensureTargetDirectoryIsValid(targetPath)) 143 return false; 144 145 if (!ensureTargetFileIsValid(new File (targetPath))) 146 return false; 147 148 return true; 149 } 150 156 protected boolean executeExportOperation(ZipFileExportOperation op) { 157 op.setCreateLeadupStructure(createDirectoryStructureCheckbox.getSelection()); 158 op.setUseCompression(compressContentsCheckbox.getSelection()); 159 160 try { 161 getContainer().run(true, true, op); 162 } catch (InterruptedException e) { 163 return false; 164 } catch (InvocationTargetException e) { 165 displayErrorDialog(e.getTargetException().getMessage()); 166 return false; 167 } 168 169 IStatus status = op.getStatus(); 170 if (!status.isOK()) { 171 ErrorDialog.openError(getContainer().getShell(), 172 DataTransferMessages.getString("DataTransfer.exportProblems"), null, status); 175 return false; 176 } 177 178 return true; 179 } 180 187 public boolean finish() { 188 if (!ensureTargetIsValid()) 189 return false; 190 191 List resourcesToExport = getSelectedResources(); 192 193 saveWidgetValues(); 195 196 if (resourcesToExport.size() > 0) 197 return executeExportOperation( 198 new ZipFileExportOperation( 199 null, 200 resourcesToExport, 201 getDestinationValue())); 202 203 MessageDialog.openInformation( 204 getContainer().getShell(), 205 DataTransferMessages.getString("DataTransfer.information"), DataTransferMessages.getString("FileExport.noneSelected")); 208 return false; 209 } 210 215 protected String getDestinationLabel() { 216 return DataTransferMessages.getString("ZipExport.destinationLabel"); } 218 224 protected String getDestinationValue() { 225 String requiredSuffix = getOutputSuffix(); 226 String destinationText = super.getDestinationValue(); 227 228 if (!destinationText.toLowerCase().endsWith(requiredSuffix.toLowerCase())) 229 destinationText += requiredSuffix; 230 231 return destinationText; 232 } 233 240 protected String getOutputSuffix() { 241 return ".zip";} 243 247 protected void handleDestinationBrowseButtonPressed() { 248 FileDialog dialog = new FileDialog(getContainer().getShell(),SWT.SAVE); 249 dialog.setFilterExtensions(new String [] {"*.jar;*.zip"}); 251 String currentSourceString = getDestinationValue(); 252 int lastSeparatorIndex = currentSourceString.lastIndexOf(File.separator); 253 if (lastSeparatorIndex != -1) 254 dialog.setFilterPath(currentSourceString.substring(0,lastSeparatorIndex)); 255 String selectedFileName = dialog.open(); 256 257 if (selectedFileName != null) 258 setDestinationValue(selectedFileName); 259 } 260 264 protected void internalSaveWidgetValues() { 265 IDialogSettings settings = getDialogSettings(); 267 if(settings != null) { 268 String [] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID); 269 if (directoryNames == null) 270 directoryNames = new String [0]; 271 String [] newDirectoryNames = new String [directoryNames.length + 1]; 272 System.arraycopy(directoryNames,0,newDirectoryNames,1,directoryNames.length); 273 newDirectoryNames[0] = getDestinationValue(); 274 275 settings.put( 276 STORE_DESTINATION_NAMES_ID, 277 directoryNames); 278 279 settings.put( 281 STORE_OVERWRITE_EXISTING_FILE_ID, 282 overwriteExistingFileCheckbox.getSelection()); 283 284 settings.put( 285 STORE_CREATE_STRUCTURE_ID, 286 createDirectoryStructureCheckbox.getSelection()); 287 288 settings.put( 289 STORE_COMPRESS_CONTENTS_ID, 290 compressContentsCheckbox.getSelection()); 291 } 292 } 293 297 protected void restoreWidgetValues() { 298 IDialogSettings settings = getDialogSettings(); 299 if(settings != null) { 300 String [] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID); 301 if (directoryNames == null || directoryNames.length == 0) 302 return; 304 setDestinationValue(directoryNames[0]); 306 for (int i = 0; i < directoryNames.length; i++) 307 addDestinationItem(directoryNames[i]); 308 309 overwriteExistingFileCheckbox.setSelection( 311 settings.getBoolean(STORE_OVERWRITE_EXISTING_FILE_ID)); 312 313 createDirectoryStructureCheckbox.setSelection( 314 settings.getBoolean(STORE_CREATE_STRUCTURE_ID)); 315 316 compressContentsCheckbox.setSelection( 317 settings.getBoolean(STORE_COMPRESS_CONTENTS_ID)); 318 } 319 } 320 } 321 | Popular Tags |