1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.io.File ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.*; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.dialogs.*; 20 import org.eclipse.jface.viewers.*; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.*; 25 import org.eclipse.ui.*; 26 import org.eclipse.ui.dialogs.*; 27 28 32 class WizardFileSystemImportPage1 extends WizardImportPage implements ISelectionChangedListener, Listener { 33 private List selectedResources; 34 private FileSystemElement root; 35 36 protected Combo typesToImportField; 38 protected Button typesToImportEditButton; 39 protected Combo sourceNameField; 40 protected Button sourceBrowseButton; 41 protected Button importAllResourcesRadio; 42 protected Button importTypedResourcesRadio; 43 protected Button detailsButton; 44 protected Label detailsDescriptionLabel; 45 protected Button overwriteExistingResourcesCheckbox; 46 protected Button createContainerStructureCheckbox; 47 48 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 50 private static final String TYPE_DELIMITER = DataTransferMessages.getString("DataTransfer.typeDelimiter"); 52 private final static String STORE_SOURCE_NAMES_ID = "WizardFileSystemImportPage1.STORE_SOURCE_NAMES_ID"; private final static String STORE_IMPORT_ALL_RESOURCES_ID = "WizardFileSystemImportPage1.STORE_IMPORT_ALL_FILES_ID"; private final static String STORE_OVERWRITE_EXISTING_RESOURCES_ID = "WizardFileSystemImportPage1.STORE_OVERWRITE_EXISTING_RESOURCES_ID"; private final static String STORE_CREATE_CONTAINER_STRUCTURE_ID = "WizardFileSystemImportPage1.STORE_CREATE_CONTAINER_STRUCTURE_ID"; private final static String STORE_SELECTED_TYPES_ID = "WizardFileSystemImportPage1.STORE_SELECTED_TYPES_ID"; 61 protected WizardFileSystemImportPage1(String name, IWorkbench aWorkbench, IStructuredSelection selection) { 62 super(name,selection); 63 } 64 67 public WizardFileSystemImportPage1(IWorkbench aWorkbench, IStructuredSelection selection) { 68 this("fileSystemImportPage1", aWorkbench, selection); setTitle(DataTransferMessages.getString("DataTransfer.fileSystemTitle")); setDescription(DataTransferMessages.getString("FileImport.importFileSystem")); } 72 76 protected void addToSelectedResources(FileSystemElement element) { 77 if (element.isDirectory()) { 78 Object [] children = element.getFolders().getChildren(element); 79 for (int i = 0; i < children.length; ++i) { 80 addToSelectedResources((FileSystemElement) children[i]); 81 } 82 children = element.getFiles().getChildren(element); 83 for (int i = 0; i < children.length; ++i) { 84 addToSelectedResources((FileSystemElement) children[i]); 85 } 86 } else 87 selectedResources.add(element); 88 } 89 92 protected void createOptionsGroup(Composite parent) { 93 Composite optionsGroup = new Composite(parent, SWT.NONE); 95 GridLayout layout = new GridLayout(); 96 layout.marginHeight = 0; 97 optionsGroup.setLayout(layout); 98 optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 99 100 overwriteExistingResourcesCheckbox = new Button(optionsGroup,SWT.CHECK); 102 overwriteExistingResourcesCheckbox.setText(DataTransferMessages.getString("FileImport.overwriteExisting")); 104 createContainerStructureCheckbox = new Button(optionsGroup,SWT.CHECK); 106 createContainerStructureCheckbox.setText(DataTransferMessages.getString("FileImport.createComplete")); } 108 111 protected void createSourceGroup(Composite parent) { 112 Composite sourceContainerGroup = new Composite(parent,SWT.NONE); 113 GridLayout layout = new GridLayout(); 114 layout.numColumns = 3; 115 sourceContainerGroup.setLayout(layout); 116 sourceContainerGroup.setLayoutData( 117 new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 118 119 new Label(sourceContainerGroup,SWT.NONE).setText(getSourceLabel()); 120 121 sourceNameField = new Combo(sourceContainerGroup,SWT.BORDER); 123 sourceNameField.addListener(SWT.Modify,this); 124 sourceNameField.addListener(SWT.Selection,this); 125 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 126 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 127 sourceNameField.setLayoutData(data); 128 129 sourceBrowseButton = new Button(sourceContainerGroup,SWT.PUSH); 131 sourceBrowseButton.setText(DataTransferMessages.getString("DataTransfer.browse")); sourceBrowseButton.addListener(SWT.Selection,this); 133 sourceBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 134 135 Composite sourceTypesGroup = new Composite(parent,SWT.NONE); 137 layout = new GridLayout(); 138 layout.numColumns = 3; 139 sourceTypesGroup.setLayout(layout); 140 sourceTypesGroup.setLayoutData( 141 new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 142 143 importAllResourcesRadio = new Button(sourceTypesGroup,SWT.RADIO); 145 importAllResourcesRadio.setText(DataTransferMessages.getString("DataTransfer.allTypes")); importAllResourcesRadio.addListener(SWT.Selection,this); 147 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 148 data.horizontalSpan = 3; 149 importAllResourcesRadio.setLayoutData(data); 150 151 importTypedResourcesRadio = new Button(sourceTypesGroup,SWT.RADIO); 153 importTypedResourcesRadio.setText(DataTransferMessages.getString("FileImport.filesofType")); importTypedResourcesRadio.addListener(SWT.Selection,this); 155 156 typesToImportField = new Combo(sourceTypesGroup, SWT.NONE); 158 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 159 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 160 typesToImportField.setLayoutData(data); 161 typesToImportField.addListener(SWT.Modify, this); 162 163 typesToImportEditButton = new Button(sourceTypesGroup, SWT.PUSH); 165 typesToImportEditButton.setText(DataTransferMessages.getString("FileImport.edit")); typesToImportEditButton.setLayoutData(new GridData( 167 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_END)); 168 typesToImportEditButton.addListener(SWT.Selection, this); 169 170 Composite fileDetailsGroup = new Composite(parent, SWT.NONE); 172 layout = new GridLayout(); 173 layout.numColumns = 2; 174 fileDetailsGroup.setLayout(layout); 175 fileDetailsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 176 177 detailsButton = new Button(fileDetailsGroup,SWT.PUSH); 179 detailsButton.setText(DataTransferMessages.getString("DataTransfer.details")); detailsButton.addListener(SWT.Selection,this); 181 182 detailsDescriptionLabel = new Label(fileDetailsGroup,SWT.NONE); 184 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 185 detailsDescriptionLabel.setLayoutData(data); 186 187 typesToImportField.setEnabled(false); 189 typesToImportEditButton.setEnabled(false); 190 importAllResourcesRadio.setSelection(true); 191 resetSelection(); 192 sourceNameField.setFocus(); 193 } 194 198 protected void displaySelectedCount(int selectedFileCount) { 199 if (selectedFileCount == 1) 200 detailsDescriptionLabel.setText( 201 DataTransferMessages.getString("DataTransfer.oneSelected")); else 203 detailsDescriptionLabel.setText(DataTransferMessages.format("FileImport.filesSelected",new Object [] {String.valueOf(selectedFileCount)})); } 205 209 protected boolean ensureSourceIsValid() { 210 if (new File (getSourceDirectoryName()).isDirectory()) 211 return true; 212 213 displayErrorDialog(DataTransferMessages.getString("FileImport.invalidSource")); sourceNameField.setFocus(); 215 return false; 216 } 217 220 protected boolean executeImportOperation(ImportOperation op) { 221 initializeOperation(op); 222 223 try { 224 getContainer().run(true, true, op); 225 } catch (InterruptedException e) { 226 return false; 227 } catch (InvocationTargetException e) { 228 displayErrorDialog(e.getTargetException().getMessage()); 229 return false; 230 } 231 232 IStatus status = op.getStatus(); 233 if (!status.isOK()) { 234 ErrorDialog.openError(getContainer().getShell(), 235 DataTransferMessages.getString("FileImport.importProblems"), null, status); 238 return false; 239 } 240 241 return true; 242 } 243 248 public boolean finish() { 249 if (!ensureSourceIsValid()) 250 return false; 251 252 if (selectedResources == null && importAllResourcesRadio.getSelection()) { 253 saveWidgetValues(); 255 256 return importAllResources(); 257 } else { 258 if (selectedResources == null) { 260 if (getFileSystemTree() == null) 261 return false; 262 } 263 264 saveWidgetValues(); 266 267 if (selectedResources.size() > 0) { 268 List fileSystemObjects = new ArrayList(selectedResources.size()); 269 Iterator resourcesEnum = selectedResources.iterator(); 270 while (resourcesEnum.hasNext()) 271 fileSystemObjects.add( 272 ((FileSystemElement)resourcesEnum.next()).getFileSystemObject()); 273 274 return importResources(fileSystemObjects); 275 } 276 277 MessageDialog.openInformation( 278 getContainer().getShell(), 279 DataTransferMessages.getString("DataTransfer.information"), DataTransferMessages.getString("FileImport.noneSelected")); 282 return false; 283 } 284 } 285 290 protected FileSystemElement getFileSystemTree() { 291 if (root != null) 292 return root; 293 294 File sourceDirectory = getSourceDirectory(); 295 if (sourceDirectory == null) 296 return null; 297 298 return selectFiles(sourceDirectory, FileSystemStructureProvider.INSTANCE); 299 } 300 303 protected FileSystemElement getRoot() { 304 return root; 305 } 306 309 protected List getSelectedResources() { 310 return selectedResources; 311 } 312 316 protected File getSourceDirectory() { 317 File sourceDirectory = new File (getSourceDirectoryName()); 318 if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) { 319 displayErrorDialog(DataTransferMessages.getString("FileImport.invalidSource")); sourceNameField.setFocus(); 321 return null; 322 } 323 324 return sourceDirectory; 325 } 326 331 private String getSourceDirectoryName() { 332 IPath result = new Path(sourceNameField.getText().trim()); 333 334 if (result.getDevice() != null && result.segmentCount() == 0) result = result.addTrailingSeparator(); 336 else 337 result = result.removeTrailingSeparator(); 338 339 return result.toOSString(); 340 } 341 344 protected String getSourceLabel() { 345 return DataTransferMessages.getString("FileImport.sourceTitle"); } 347 351 protected List getTypesToImport() { 352 if (importAllResourcesRadio.getSelection()) 353 return null; 354 355 List result = new ArrayList(); 356 StringTokenizer tokenizer = new StringTokenizer(typesToImportField.getText(),TYPE_DELIMITER); 357 358 while (tokenizer.hasMoreTokens()) { 359 String currentExtension = tokenizer.nextToken().trim(); 360 if (!currentExtension.equals("")) result.add(currentExtension); 362 } 363 364 return result; 365 } 366 370 protected String [] getTypesToImportArray() { 371 List typesToImport = getTypesToImport(); 372 if (typesToImport == null) 373 return null; 374 375 String result[] = new String [typesToImport.size()]; 376 typesToImport.toArray(result); 377 378 return result; 379 } 380 383 protected void handleDetailsButtonPressed() { 384 FileSystemElement rootElement = getFileSystemTree(); 385 386 if (rootElement != null) { 387 List newSelections = queryResourcesToImport(rootElement); 388 389 if (newSelections != null) { 390 selectedResources = newSelections; 391 displaySelectedCount(selectedResources.size()); 392 } 393 } 394 } 395 398 public void handleEvent(Event e) { 399 Widget source = e.widget; 400 401 if (source == sourceNameField) 402 resetSelection(); 403 else if (source == sourceBrowseButton) 404 handleSourceBrowseButtonPressed(); 405 else if (source == importAllResourcesRadio) 406 resetSelection(); 407 else if (source == importTypedResourcesRadio) { 408 resetSelection(); 409 typesToImportField.setFocus(); 410 } else if (source == detailsButton) 411 handleDetailsButtonPressed(); 412 else if (source == typesToImportField) 413 resetSelection(); 414 else if (source == typesToImportEditButton) 415 handleTypesEditButtonPressed(); 416 417 super.handleEvent(e); 418 } 419 423 protected void handleSourceBrowseButtonPressed() { 424 DirectoryDialog dialog = new DirectoryDialog(sourceNameField.getShell(),SWT.SAVE); 425 dialog.setMessage(DataTransferMessages.getString("FileImport.selectSource")); dialog.setFilterPath(getSourceDirectoryName()); 427 428 String selectedDirectory = dialog.open(); 429 if (selectedDirectory != null) { 430 if (!selectedDirectory.equals(getSourceDirectoryName())) { 431 resetSelection(); 432 sourceNameField.setText(selectedDirectory); 433 } 434 } 435 } 436 440 protected void handleTypesEditButtonPressed() { 441 IFileEditorMapping editorMappings[] = 442 PlatformUI.getWorkbench().getEditorRegistry().getFileEditorMappings(); 443 444 List selectedTypes = getTypesToImport(); 445 List initialSelections = new ArrayList(); 446 for (int i = 0; i < editorMappings.length; i++) { 447 IFileEditorMapping mapping = editorMappings[i]; 448 if (selectedTypes.contains(mapping.getExtension())) 449 initialSelections.add(mapping); 450 } 451 452 ListSelectionDialog dialog = 453 new ListSelectionDialog( 454 getContainer().getShell(), 455 editorMappings, 456 FileEditorMappingContentProvider.INSTANCE, 457 FileEditorMappingLabelProvider.INSTANCE, 458 DataTransferMessages.getString("FileImport.selectTypes")); 460 dialog.setInitialSelections(initialSelections.toArray()); 461 dialog.setTitle(DataTransferMessages.getString("FileImport.typeSelectionTitle")); dialog.open(); 463 464 Object [] newSelectedTypes = dialog.getResult(); 465 if (newSelectedTypes != null) { List result = new ArrayList(newSelectedTypes.length); 467 for (int i = 0; i < newSelectedTypes.length; i++) 468 result.add(((IFileEditorMapping)newSelectedTypes[i]).getExtension()); 469 setTypesToImport(result); 470 } 471 } 472 476 protected boolean importAllResources() { 477 return executeImportOperation( 478 new ImportOperation( 479 getContainerFullPath(), 480 getSourceDirectory(), 481 FileSystemStructureProvider.INSTANCE, 482 this)); 483 } 484 487 protected boolean importResources(List fileSystemObjects) { 488 return executeImportOperation( 489 new ImportOperation( 490 getContainerFullPath(), 491 getSourceDirectory(), 492 FileSystemStructureProvider.INSTANCE, 493 this, 494 fileSystemObjects)); 495 } 496 499 protected void initializeOperation(ImportOperation op) { 500 op.setCreateContainerStructure(createContainerStructureCheckbox.getSelection()); 501 op.setOverwriteResources(overwriteExistingResourcesCheckbox.getSelection()); 502 } 503 508 protected List queryResourcesToImport(FileSystemElement rootElement) { 509 FileSelectionDialog dialog = new FileSelectionDialog(getContainer().getShell(), rootElement, DataTransferMessages.getString("FileImport.selectResources")); dialog.setInitialSelections(selectedResources.toArray()); 511 dialog.setExpandAllOnOpen(true); 512 dialog.open(); 513 if (dialog.getResult() == null) 514 return null; 515 return Arrays.asList(dialog.getResult()); 516 517 } 518 521 protected void resetSelection() { 522 detailsDescriptionLabel.setText(DataTransferMessages.getString("DataTransfer.allFiles")); selectedResources = null; 524 root = null; 525 } 526 530 protected void restoreWidgetValues() { 531 IDialogSettings settings = getDialogSettings(); 532 if(settings != null) { 533 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 534 if (sourceNames == null) 535 return; 537 boolean importAll = settings.getBoolean(STORE_IMPORT_ALL_RESOURCES_ID); 539 importAllResourcesRadio.setSelection(importAll); 540 importTypedResourcesRadio.setSelection(!importAll); 541 542 sourceNameField.setText(sourceNames[0]); 544 for (int i = 0; i < sourceNames.length; i++) 545 sourceNameField.add(sourceNames[i]); 546 547 String [] selectedTypes = settings.getArray(STORE_SELECTED_TYPES_ID); 549 if (selectedTypes.length > 0) 550 typesToImportField.setText(selectedTypes[0]); 551 for (int i = 0; i < selectedTypes.length; i++) 552 typesToImportField.add(selectedTypes[i]); 553 554 overwriteExistingResourcesCheckbox.setSelection( 556 settings.getBoolean(STORE_OVERWRITE_EXISTING_RESOURCES_ID)); 557 558 createContainerStructureCheckbox.setSelection( 559 settings.getBoolean(STORE_CREATE_CONTAINER_STRUCTURE_ID)); 560 561 } 562 } 563 567 protected void saveWidgetValues() { 568 IDialogSettings settings = getDialogSettings(); 569 if(settings != null) { 570 String [] sourceNames = settings.getArray(STORE_SOURCE_NAMES_ID); 572 if (sourceNames == null) 573 sourceNames = new String [0]; 574 575 sourceNames = addToHistory(sourceNames,getSourceDirectoryName()); 576 settings.put( 577 STORE_SOURCE_NAMES_ID, 578 sourceNames); 579 580 String [] selectedTypesNames = settings.getArray(STORE_SELECTED_TYPES_ID); 582 if (selectedTypesNames == null) 583 selectedTypesNames = new String [0]; 584 585 if (importTypedResourcesRadio.getSelection()) 586 selectedTypesNames = addToHistory(selectedTypesNames,typesToImportField.getText()); 587 588 settings.put( 589 STORE_SELECTED_TYPES_ID, 590 selectedTypesNames); 591 592 settings.put( 594 STORE_IMPORT_ALL_RESOURCES_ID, 595 importAllResourcesRadio.getSelection()); 596 597 settings.put( 598 STORE_OVERWRITE_EXISTING_RESOURCES_ID, 599 overwriteExistingResourcesCheckbox.getSelection()); 600 601 settings.put( 602 STORE_CREATE_CONTAINER_STRUCTURE_ID, 603 createContainerStructureCheckbox.getSelection()); 604 605 } 606 } 607 612 protected FileSystemElement selectFiles(Object rootFileSystemObject,IImportStructureProvider structureProvider) { 613 try { 614 SelectFilesOperation op = 615 new SelectFilesOperation(rootFileSystemObject,structureProvider); 616 op.setDesiredExtensions(getTypesToImportArray()); 617 getContainer().run(true, true, op); 618 root = op.getResult(); 619 setSelectedResources(new ArrayList()); 620 addToSelectedResources(root); 621 } catch (InterruptedException e) { 622 return null; 623 } catch (InvocationTargetException e) { 624 displayErrorDialog(e.getTargetException().getMessage()); 625 return null; 626 } 627 628 return root; 629 } 630 634 public void selectionChanged(SelectionChangedEvent event) { 635 if (importTypedResourcesRadio.getSelection()) 636 resetSelection(); 637 } 638 641 protected void setRoot(FileSystemElement value) { 642 root = value; 643 } 644 647 protected void setSelectedResources(List value) { 648 selectedResources = value; 649 } 650 653 protected void setTypesToImport(List types) { 654 StringBuffer result = new StringBuffer (); 655 for (int i = 0; i < types.size(); ++i) { 656 if (i != 0) { 657 result.append(TYPE_DELIMITER); 658 result.append(" "); } 660 result.append(types.get(i)); 661 } 662 typesToImportField.setText(result.toString()); 663 } 664 667 protected void updateWidgetEnablements() { 668 typesToImportField.setEnabled(importTypedResourcesRadio.getSelection()); 669 typesToImportEditButton.setEnabled(importTypedResourcesRadio.getSelection()); 670 } 671 675 protected boolean validateSourceGroup() { 676 return !getSourceDirectoryName().equals("");} 678 } 679 | Popular Tags |