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 import org.eclipse.ui.dialogs.WizardExportPage; 25 26 30 class WizardFileSystemExportPage1 extends WizardExportPage implements Listener { 31 32 private Combo destinationNameField; 34 private Button destinationBrowseButton; 35 private Button overwriteExistingFilesCheckbox; 36 private Button createDirectoryStructureCheckbox; 37 private Button createDirectoriesForSelectedContainersCheckbox; 38 39 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 41 42 private static final String STORE_DESTINATION_NAMES_ID = "WizardFileSystemExportPage1.STORE_DESTINATION_NAMES_ID"; private static final String STORE_OVERWRITE_EXISTING_FILES_ID = "WizardFileSystemExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID"; private static final String STORE_CREATE_STRUCTURE_ID = "WizardFileSystemExportPage1.STORE_CREATE_STRUCTURE_ID"; private static final String STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID = "WizardFileSystemExportPage1.STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID"; 50 protected WizardFileSystemExportPage1(String name, IStructuredSelection selection) { 51 super(name, selection); 52 } 53 56 public WizardFileSystemExportPage1(IStructuredSelection selection) { 57 this("fileSystemExportPage1", selection); setTitle(DataTransferMessages.getString("DataTransfer.fileSystemTitle")); setDescription(DataTransferMessages.getString("FileExport.exportLocalFileSystem")); } 61 66 protected void addDestinationItem(String value) { 67 destinationNameField.add(value); 68 } 69 72 public void createControl(Composite parent) { 73 super.createControl(parent); 74 giveFocusToDestination(); 75 } 76 81 protected void createDestinationGroup(Composite parent) { 82 Composite destinationSelectionGroup = new Composite(parent, SWT.NONE); 84 GridLayout layout = new GridLayout(); 85 layout.numColumns = 3; 86 destinationSelectionGroup.setLayout(layout); 87 destinationSelectionGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); 88 89 new Label(destinationSelectionGroup, SWT.NONE).setText(getDestinationLabel()); 90 91 destinationNameField = new Combo(destinationSelectionGroup, SWT.SINGLE | SWT.BORDER); 93 destinationNameField.addListener(SWT.Modify, this); 94 destinationNameField.addListener(SWT.Selection, this); 95 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 96 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 97 destinationNameField.setLayoutData(data); 98 99 destinationBrowseButton = new Button(destinationSelectionGroup, SWT.PUSH); 101 destinationBrowseButton.setText(DataTransferMessages.getString("DataTransfer.browse")); destinationBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 103 destinationBrowseButton.addListener(SWT.Selection, this); 104 105 new Label(parent, SWT.NONE); } 107 112 protected void createOptionsGroup(Composite parent) { 113 Composite optionsGroup = new Composite(parent, SWT.NONE); 115 GridLayout layout = new GridLayout(); 116 layout.marginHeight = 0; 117 optionsGroup.setLayout(layout); 118 optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 119 120 overwriteExistingFilesCheckbox = new Button(optionsGroup,SWT.CHECK|SWT.LEFT); 122 overwriteExistingFilesCheckbox.setText(DataTransferMessages.getString("ExportFile.overwriteExisting")); 124 createDirectoryStructureCheckbox = new Button(optionsGroup,SWT.CHECK|SWT.LEFT); 126 createDirectoryStructureCheckbox.setText(DataTransferMessages.getString("ExportFile.createDirectoryStructure")); createDirectoryStructureCheckbox.addListener(SWT.Selection,this); 128 129 createDirectoriesForSelectedContainersCheckbox = new Button(optionsGroup,SWT.CHECK|SWT.LEFT); 131 createDirectoriesForSelectedContainersCheckbox.setText(DataTransferMessages.getString("ExportFile.createDirectoriesForSelected")); 133 createDirectoryStructureCheckbox.setSelection(true); 135 } 136 143 protected boolean ensureDirectoryExists(File directory) { 144 if (!directory.exists()) { 145 if (!queryYesNoQuestion(DataTransferMessages.getString("DataTransfer.createTargetDirectory"))) return false; 147 148 if (!directory.mkdirs()) { 149 displayErrorDialog(DataTransferMessages.getString("DataTransfer.directoryCreationError")); giveFocusToDestination(); 151 return false; 152 } 153 } 154 155 return true; 156 } 157 164 protected boolean ensureTargetIsValid(File targetDirectory) { 165 if (targetDirectory.exists() && !targetDirectory.isDirectory()) { 166 displayErrorDialog(DataTransferMessages.getString("FileExport.directoryExists")); giveFocusToDestination(); 168 return false; 169 } 170 171 return ensureDirectoryExists(targetDirectory); 172 } 173 178 protected boolean executeExportOperation(FileSystemExportOperation op) { 179 op.setCreateContainerDirectories(createDirectoriesForSelectedContainersCheckbox.getSelection()); 180 op.setCreateLeadupStructure(createDirectoryStructureCheckbox.getSelection()); 181 op.setOverwriteFiles(overwriteExistingFilesCheckbox.getSelection()); 182 183 try { 184 getContainer().run(true, true, op); 185 } catch (InterruptedException e) { 186 return false; 187 } catch (InvocationTargetException e) { 188 displayErrorDialog(e.getTargetException().getMessage()); 189 return false; 190 } 191 192 IStatus status = op.getStatus(); 193 if (!status.isOK()) { 194 ErrorDialog.openError(getContainer().getShell(), 195 DataTransferMessages.getString("DataTransfer.exportProblems"), null, status); 198 return false; 199 } 200 201 return true; 202 } 203 210 public boolean finish() { 211 if (!ensureTargetIsValid(new File (getDestinationValue()))) 212 return false; 213 214 List resourcesToExport = getSelectedResources(); 215 216 saveWidgetValues(); 218 219 if (resourcesToExport.size() > 0) 220 return executeExportOperation( 221 new FileSystemExportOperation( 222 getSourceResource(), 223 resourcesToExport, 224 getDestinationValue(), 225 this)); 226 227 MessageDialog.openInformation( 228 getContainer().getShell(), 229 DataTransferMessages.getString("DataTransfer.information"), DataTransferMessages.getString("FileExport.noneSelected")); 232 return false; 233 } 234 239 protected String getDestinationLabel() { 240 return DataTransferMessages.getString("DataTransfer.directory"); } 242 247 protected String getDestinationValue() { 248 return destinationNameField.getText().trim(); 249 } 250 253 protected void giveFocusToDestination() { 254 destinationNameField.setFocus(); 255 } 256 260 protected void handleDestinationBrowseButtonPressed() { 261 DirectoryDialog dialog = new DirectoryDialog(getContainer().getShell(),SWT.SAVE); 262 dialog.setMessage(DataTransferMessages.getString("DataTransfer.selectDestination")); dialog.setFilterPath(getDestinationValue()); 264 String selectedDirectoryName = dialog.open(); 265 266 if (selectedDirectoryName != null) 267 setDestinationValue(selectedDirectoryName); 268 } 269 274 public void handleEvent(Event e) { 275 Widget source = e.widget; 276 277 if (source == destinationBrowseButton) 278 handleDestinationBrowseButtonPressed(); 279 280 super.handleEvent(e); 281 } 282 286 protected void internalSaveWidgetValues() { 287 IDialogSettings settings = getDialogSettings(); 289 if(settings != null) { 290 String [] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID); 291 if (directoryNames == null) 292 directoryNames = new String [0]; 293 294 directoryNames = addToHistory(directoryNames,getDestinationValue()); 295 settings.put( 296 STORE_DESTINATION_NAMES_ID, 297 directoryNames); 298 299 settings.put( 301 STORE_OVERWRITE_EXISTING_FILES_ID, 302 overwriteExistingFilesCheckbox.getSelection()); 303 304 settings.put( 305 STORE_CREATE_STRUCTURE_ID, 306 createDirectoryStructureCheckbox.getSelection()); 307 308 settings.put( 309 STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID, 310 createDirectoriesForSelectedContainersCheckbox.getSelection()); 311 312 } 313 } 314 318 protected void restoreWidgetValues() { 319 IDialogSettings settings = getDialogSettings(); 320 if(settings != null) { 321 String [] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID); 322 if (directoryNames == null) 323 return; 325 setDestinationValue(directoryNames[0]); 327 for (int i = 0; i < directoryNames.length; i++) 328 addDestinationItem(directoryNames[i]); 329 330 overwriteExistingFilesCheckbox.setSelection( 332 settings.getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID)); 333 334 createDirectoryStructureCheckbox.setSelection( 335 settings.getBoolean(STORE_CREATE_STRUCTURE_ID)); 336 337 createDirectoriesForSelectedContainersCheckbox.setSelection( 338 settings.getBoolean(STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID)); 339 } 340 } 341 347 protected void setDestinationValue(String value) { 348 destinationNameField.setText(value); 349 } 350 356 protected boolean validateDestinationGroup() { 357 return !getDestinationValue().equals("");} 359 } 360 | Popular Tags |