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.resources.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.dialogs.*; 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.*; 26 import org.eclipse.ui.dialogs.WizardExportResourcesPage; 27 import org.eclipse.ui.help.WorkbenchHelp; 28 29 32 class WizardFileSystemResourceExportPage1 extends WizardExportResourcesPage implements Listener { 33 34 private Combo destinationNameField; 36 private Button destinationBrowseButton; 37 protected Button overwriteExistingFilesCheckbox; 38 protected Button createDirectoryStructureButton; 39 protected Button createSelectionOnlyButton; 40 41 private static final String STORE_DESTINATION_NAMES_ID = 43 "WizardFileSystemResourceExportPage1.STORE_DESTINATION_NAMES_ID"; private static final String STORE_OVERWRITE_EXISTING_FILES_ID = 45 "WizardFileSystemResourceExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID"; private static final String STORE_CREATE_STRUCTURE_ID = 47 "WizardFileSystemResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; private static final String SELECT_DESTINATION_MESSAGE = DataTransferMessages.getString("FileExport.selectDestinationMessage"); private static final String SELECT_DESTINATION_TITLE = DataTransferMessages.getString("FileExport.selectDestinationTitle"); 54 protected WizardFileSystemResourceExportPage1( 55 String name, 56 IStructuredSelection selection) { 57 super(name, selection); 58 } 59 62 public WizardFileSystemResourceExportPage1(IStructuredSelection selection) { 63 this("fileSystemExportPage1", selection); setTitle(DataTransferMessages.getString("DataTransfer.fileSystemTitle")); setDescription(DataTransferMessages.getString("FileExport.exportLocalFileSystem")); } 67 72 protected void addDestinationItem(String value) { 73 destinationNameField.add(value); 74 } 75 78 public void createControl(Composite parent) { 79 super.createControl(parent); 80 giveFocusToDestination(); 81 WorkbenchHelp.setHelp( 82 getControl(), 83 IDataTransferHelpContextIds.FILE_SYSTEM_EXPORT_WIZARD_PAGE); 84 } 85 90 protected void createDestinationGroup(Composite parent) { 91 92 Font font = parent.getFont(); 93 Composite destinationSelectionGroup = new Composite(parent, SWT.NONE); 95 GridLayout layout = new GridLayout(); 96 layout.numColumns = 3; 97 destinationSelectionGroup.setLayout(layout); 98 destinationSelectionGroup.setLayoutData( 99 new GridData( 100 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); 101 destinationSelectionGroup.setFont(font); 102 103 Label destinationLabel = new Label(destinationSelectionGroup, SWT.NONE); 104 destinationLabel.setText(getDestinationLabel()); 105 destinationLabel.setFont(font); 106 107 destinationNameField = 109 new Combo(destinationSelectionGroup, SWT.SINGLE | SWT.BORDER); 110 destinationNameField.addListener(SWT.Modify, this); 111 destinationNameField.addListener(SWT.Selection, this); 112 GridData data = 113 new GridData( 114 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 115 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 116 destinationNameField.setLayoutData(data); 117 destinationNameField.setFont(font); 118 119 destinationBrowseButton = 121 new Button(destinationSelectionGroup, SWT.PUSH); 122 destinationBrowseButton.setText(DataTransferMessages.getString("DataTransfer.browse")); destinationBrowseButton.addListener(SWT.Selection, this); 124 destinationBrowseButton.setFont(font); 125 setButtonLayoutData(destinationBrowseButton); 126 127 new Label(parent, SWT.NONE); } 129 130 133 134 protected void createOptionsGroupButtons(Group optionsGroup) { 135 136 Font font = optionsGroup.getFont(); 137 createOverwriteExisting(optionsGroup, font); 138 139 createDirectoryStructureOptions(optionsGroup, font); 140 } 141 142 148 protected void createDirectoryStructureOptions( 149 Group optionsGroup, 150 Font font) { 151 createDirectoryStructureButton = 153 new Button(optionsGroup, SWT.RADIO | SWT.LEFT); 154 createDirectoryStructureButton.setText(DataTransferMessages.getString("FileExport.createDirectoryStructure")); createDirectoryStructureButton.setSelection(false); 156 createDirectoryStructureButton.setFont(font); 157 158 createSelectionOnlyButton = 160 new Button(optionsGroup, SWT.RADIO | SWT.LEFT); 161 createSelectionOnlyButton.setText( 162 DataTransferMessages.getString( 163 "FileExport.createSelectedDirectories")); createSelectionOnlyButton.setSelection(true); 165 createSelectionOnlyButton.setFont(font); 166 } 167 168 174 protected void createOverwriteExisting(Group optionsGroup, Font font) { 175 overwriteExistingFilesCheckbox = 177 new Button(optionsGroup, SWT.CHECK | SWT.LEFT); 178 overwriteExistingFilesCheckbox.setText(DataTransferMessages.getString("ExportFile.overwriteExisting")); overwriteExistingFilesCheckbox.setFont(font); 180 } 181 182 189 protected boolean ensureDirectoryExists(File directory) { 190 if (!directory.exists()) { 191 if (!queryYesNoQuestion(DataTransferMessages.getString("DataTransfer.createTargetDirectory"))) return false; 193 194 if (!directory.mkdirs()) { 195 displayErrorDialog(DataTransferMessages.getString("DataTransfer.directoryCreationError")); giveFocusToDestination(); 197 return false; 198 } 199 } 200 201 return true; 202 } 203 210 protected boolean ensureTargetIsValid(File targetDirectory) { 211 if (targetDirectory.exists() && !targetDirectory.isDirectory()) { 212 displayErrorDialog(DataTransferMessages.getString("FileExport.directoryExists")); giveFocusToDestination(); 214 return false; 215 } 216 217 return ensureDirectoryExists(targetDirectory); 218 } 219 224 protected boolean executeExportOperation(FileSystemExportOperation op) { 225 op.setCreateLeadupStructure( 226 createDirectoryStructureButton.getSelection()); 227 op.setOverwriteFiles(overwriteExistingFilesCheckbox.getSelection()); 228 229 try { 230 getContainer().run(true, true, op); 231 } catch (InterruptedException e) { 232 return false; 233 } catch (InvocationTargetException e) { 234 displayErrorDialog(e.getTargetException()); 235 return false; 236 } 237 238 IStatus status = op.getStatus(); 239 if (!status.isOK()) { 240 ErrorDialog.openError(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.exportProblems"), null, status); 243 return false; 244 } 245 246 return true; 247 } 248 255 public boolean finish() { 256 if (!ensureTargetIsValid(new File (getDestinationValue()))) 257 return false; 258 259 List resourcesToExport = getWhiteCheckedResources(); 260 261 saveDirtyEditors(); 263 saveWidgetValues(); 265 266 if (resourcesToExport.size() > 0) 267 return executeExportOperation( 268 new FileSystemExportOperation( 269 null, 270 resourcesToExport, 271 getDestinationValue(), 272 this)); 273 274 MessageDialog.openInformation(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.information"), DataTransferMessages.getString("FileExport.noneSelected")); 277 return false; 278 } 279 284 protected String getDestinationLabel() { 285 return DataTransferMessages.getString("FileExport.toDirectory"); } 287 292 protected String getDestinationValue() { 293 return destinationNameField.getText().trim(); 294 } 295 298 protected void giveFocusToDestination() { 299 destinationNameField.setFocus(); 300 } 301 305 protected void handleDestinationBrowseButtonPressed() { 306 DirectoryDialog dialog = 307 new DirectoryDialog(getContainer().getShell(), SWT.SAVE); 308 dialog.setMessage(SELECT_DESTINATION_MESSAGE); 309 dialog.setText(SELECT_DESTINATION_TITLE); 310 dialog.setFilterPath(getDestinationValue()); 311 String selectedDirectoryName = dialog.open(); 312 313 if (selectedDirectoryName != null) { 314 setErrorMessage(null); 315 setDestinationValue(selectedDirectoryName); 316 } 317 } 318 322 public void handleEvent(Event e) { 323 Widget source = e.widget; 324 325 if (source == destinationBrowseButton) 326 handleDestinationBrowseButtonPressed(); 327 328 updatePageCompletion(); 329 } 330 334 protected void internalSaveWidgetValues() { 335 IDialogSettings settings = getDialogSettings(); 337 if (settings != null) { 338 String [] directoryNames = 339 settings.getArray(STORE_DESTINATION_NAMES_ID); 340 if (directoryNames == null) 341 directoryNames = new String [0]; 342 343 directoryNames = 344 addToHistory(directoryNames, getDestinationValue()); 345 settings.put(STORE_DESTINATION_NAMES_ID, directoryNames); 346 347 settings.put( 349 STORE_OVERWRITE_EXISTING_FILES_ID, 350 overwriteExistingFilesCheckbox.getSelection()); 351 352 settings.put( 353 STORE_CREATE_STRUCTURE_ID, 354 createDirectoryStructureButton.getSelection()); 355 356 } 357 } 358 362 protected void restoreWidgetValues() { 363 IDialogSettings settings = getDialogSettings(); 364 if (settings != null) { 365 String [] directoryNames = 366 settings.getArray(STORE_DESTINATION_NAMES_ID); 367 if (directoryNames == null) 368 return; 370 setDestinationValue(directoryNames[0]); 372 for (int i = 0; i < directoryNames.length; i++) 373 addDestinationItem(directoryNames[i]); 374 375 overwriteExistingFilesCheckbox.setSelection( 377 settings.getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID)); 378 379 boolean createDirectories = 380 settings.getBoolean(STORE_CREATE_STRUCTURE_ID); 381 createDirectoryStructureButton.setSelection(createDirectories); 382 createSelectionOnlyButton.setSelection(!createDirectories); 383 } 384 } 385 390 protected void setDestinationValue(String value) { 391 destinationNameField.setText(value); 392 } 393 397 protected boolean validateDestinationGroup() { 398 String destinationValue = getDestinationValue(); 399 if (destinationValue.length() == 0) { 400 setMessage(destinationEmptyMessage()); 401 return false; 402 } 403 404 String conflictingContainer = 405 getConflictingContainerNameFor(destinationValue); 406 if (conflictingContainer == null) 407 setErrorMessage(null); 408 else { 409 setErrorMessage(DataTransferMessages.format("FileExport.conflictingContainer", new Object [] { conflictingContainer })); 411 giveFocusToDestination(); 412 return false; 413 } 414 415 return true; 416 } 417 418 421 protected String destinationEmptyMessage(){ 422 return DataTransferMessages.getString("FileExport.destinationEmpty"); } 424 425 432 protected String getConflictingContainerNameFor(String targetDirectory) { 433 434 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 435 IPath testPath = new Path(targetDirectory); 436 437 if (root.getLocation().isPrefixOf(testPath)) 438 return DataTransferMessages.getString("FileExport.rootName"); 440 IProject[] projects = root.getProjects(); 441 442 for (int i = 0; i < projects.length; i++) { 443 if (projects[i].getLocation().isPrefixOf(testPath)) 444 return projects[i].getName(); 445 } 446 447 return null; 448 449 } 450 451 } 452 | Popular Tags |