1 11 package org.eclipse.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.StringTokenizer ; 18 import java.util.Vector ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspace; 25 import org.eclipse.core.resources.IWorkspaceRoot; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IAdaptable; 29 import org.eclipse.core.runtime.IPath; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.jface.dialogs.IDialogSettings; 32 import org.eclipse.jface.dialogs.MessageDialog; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.StructuredSelection; 35 import org.eclipse.osgi.util.NLS; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.widgets.Button; 40 import org.eclipse.swt.widgets.Combo; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Event; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.swt.widgets.Text; 45 import org.eclipse.swt.widgets.Widget; 46 import org.eclipse.ui.IFileEditorMapping; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 49 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 50 51 79 public abstract class WizardExportPage extends WizardDataTransferPage { 80 private IStructuredSelection currentResourceSelection; 81 82 private List selectedResources; 83 84 private List selectedTypes; 85 86 private boolean exportCurrentSelection = false; 87 88 private boolean exportAllResourcesPreSet = false; 89 90 private Combo typesToExportField; 92 93 private Button typesToExportEditButton; 94 95 private Button exportAllTypesRadio; 96 97 private Button exportSpecifiedTypesRadio; 98 99 private Button resourceDetailsButton; 100 101 private Label resourceDetailsDescription; 102 103 private Text resourceNameField; 104 105 private Button resourceBrowseButton; 106 107 private boolean initialExportAllTypesValue = true; 109 110 private String initialExportFieldValue; 111 112 private String initialTypesFieldValue; 113 114 private static final String CURRENT_SELECTION = "<current selection>"; 117 private static final String TYPE_DELIMITER = ","; 119 private static final String STORE_SELECTED_TYPES_ID = "WizardFileSystemExportPage1.STORE_SELECTED_TYPES_ID."; 122 private static final String STORE_EXPORT_ALL_RESOURCES_ID = "WizardFileSystemExportPage1.STORE_EXPORT_ALL_RESOURCES_ID."; 124 132 protected WizardExportPage(String pageName, IStructuredSelection selection) { 133 super(pageName); 134 this.currentResourceSelection = selection; 135 } 136 137 142 protected boolean allowNewContainerName() { 143 return false; 144 } 145 146 149 public void createControl(Composite parent) { 150 Composite composite = new Composite(parent, SWT.NULL); 151 composite.setLayout(new GridLayout()); 152 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 153 | GridData.HORIZONTAL_ALIGN_FILL)); 154 155 createBoldLabel(composite, IDEWorkbenchMessages.WizardExportPage_whatLabel); 156 createSourceGroup(composite); 157 158 createSpacer(composite); 159 160 createBoldLabel(composite, IDEWorkbenchMessages.WizardExportPage_whereLabel); 161 createDestinationGroup(composite); 162 163 createSpacer(composite); 164 165 createBoldLabel(composite, IDEWorkbenchMessages.WizardExportPage_options); 166 createOptionsGroup(composite); 167 168 restoreResourceSpecificationWidgetValues(); restoreWidgetValues(); if (currentResourceSelection != null) { 171 setupBasedOnInitialSelections(); 172 } 173 174 updateWidgetEnablements(); 175 setPageComplete(determinePageCompletion()); 176 177 setControl(composite); 178 } 179 180 188 protected abstract void createDestinationGroup(Composite parent); 189 190 195 protected final void createSourceGroup(Composite parent) { 196 Composite sourceGroup = new Composite(parent, SWT.NONE); 198 GridLayout layout = new GridLayout(); 199 layout.numColumns = 3; 200 sourceGroup.setLayout(layout); 201 sourceGroup.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 202 | GridData.HORIZONTAL_ALIGN_FILL)); 203 204 new Label(sourceGroup, SWT.NONE).setText(IDEWorkbenchMessages.WizardExportPage_folder); 206 207 resourceNameField = new Text(sourceGroup, SWT.SINGLE | SWT.BORDER); 209 resourceNameField.addListener(SWT.KeyDown, this); 210 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 211 | GridData.GRAB_HORIZONTAL); 212 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 213 resourceNameField.setLayoutData(data); 214 215 resourceBrowseButton = new Button(sourceGroup, SWT.PUSH); 217 resourceBrowseButton.setText(IDEWorkbenchMessages.WizardExportPage_browse); 218 resourceBrowseButton.addListener(SWT.Selection, this); 219 resourceBrowseButton.setLayoutData(new GridData( 220 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 221 222 exportAllTypesRadio = new Button(sourceGroup, SWT.RADIO); 224 exportAllTypesRadio.setText(IDEWorkbenchMessages.WizardExportPage_allTypes); 225 exportAllTypesRadio.addListener(SWT.Selection, this); 226 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 227 | GridData.GRAB_HORIZONTAL); 228 data.horizontalSpan = 3; 229 exportAllTypesRadio.setLayoutData(data); 230 231 exportSpecifiedTypesRadio = new Button(sourceGroup, SWT.RADIO); 233 exportSpecifiedTypesRadio.setText(IDEWorkbenchMessages.WizardExportPage_specificTypes); 234 exportSpecifiedTypesRadio.addListener(SWT.Selection, this); 235 236 typesToExportField = new Combo(sourceGroup, SWT.NONE); 238 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 239 | GridData.GRAB_HORIZONTAL); 240 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 241 typesToExportField.setLayoutData(data); 242 typesToExportField.addListener(SWT.Modify, this); 243 244 typesToExportEditButton = new Button(sourceGroup, SWT.PUSH); 246 typesToExportEditButton.setText(IDEWorkbenchMessages.WizardExportPage_edit); 247 typesToExportEditButton.setLayoutData(new GridData( 248 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL 249 | GridData.VERTICAL_ALIGN_END)); 250 typesToExportEditButton.addListener(SWT.Selection, this); 251 252 resourceDetailsButton = new Button(sourceGroup, SWT.PUSH); 254 resourceDetailsButton.setText(IDEWorkbenchMessages.WizardExportPage_details); 255 resourceDetailsButton.addListener(SWT.Selection, this); 256 257 resourceDetailsDescription = new Label(sourceGroup, SWT.NONE); 259 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 260 | GridData.GRAB_HORIZONTAL); 261 data.horizontalSpan = 2; 262 resourceDetailsDescription.setLayoutData(data); 263 264 resetSelectedResources(); 266 exportAllTypesRadio.setSelection(initialExportAllTypesValue); 267 exportSpecifiedTypesRadio.setSelection(!initialExportAllTypesValue); 268 typesToExportField.setEnabled(!initialExportAllTypesValue); 269 typesToExportEditButton.setEnabled(!initialExportAllTypesValue); 270 271 if (initialExportFieldValue != null) { 272 resourceNameField.setText(initialExportFieldValue); 273 } 274 if (initialTypesFieldValue != null) { 275 typesToExportField.setText(initialTypesFieldValue); 276 } 277 } 278 279 284 protected void displayErrorDialog(String message) { 285 MessageDialog.openError(getContainer().getShell(), IDEWorkbenchMessages.WizardExportPage_errorDialogTitle, message); 286 } 287 288 294 protected void displayResourcesSelectedCount(int selectedResourceCount) { 295 if (selectedResourceCount == 1) { 296 resourceDetailsDescription.setText(IDEWorkbenchMessages.WizardExportPage_oneResourceSelected); 297 } else { 298 resourceDetailsDescription 299 .setText(NLS.bind(IDEWorkbenchMessages.WizardExportPage_resourceCountMessage, new Integer (selectedResourceCount))); 300 } 301 } 302 303 311 protected boolean ensureResourcesLocal(List resources) { 312 return true; 313 } 314 315 324 protected List extractNonLocalResources(List originalList) { 325 Vector result = new Vector (originalList.size()); 326 Iterator resourcesEnum = originalList.iterator(); 327 328 while (resourcesEnum.hasNext()) { 329 IResource currentResource = (IResource) resourcesEnum.next(); 330 if (!currentResource.isLocal(IResource.DEPTH_ZERO)) { 331 result.addElement(currentResource); 332 } 333 } 334 335 return result; 336 } 337 338 345 public boolean getExportAllTypesValue() { 346 if (exportAllTypesRadio == null) { 347 return initialExportAllTypesValue; 348 } 349 350 return exportAllTypesRadio.getSelection(); 351 } 352 353 361 public String getResourceFieldValue() { 362 if (resourceNameField == null) { 363 return initialExportFieldValue; 364 } 365 366 return resourceNameField.getText(); 367 } 368 369 373 protected IPath getResourcePath() { 374 return getPathFromText(this.resourceNameField); 375 } 376 377 385 protected List getSelectedResources() { 386 if (selectedResources == null) { 387 IResource sourceResource = getSourceResource(); 388 389 if (sourceResource != null) { 390 selectAppropriateResources(sourceResource); 391 } 392 } 393 394 return selectedResources; 395 } 396 397 404 protected IResource getSourceResource() { 405 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 406 IPath testPath = getResourcePath(); 408 409 IStatus result = workspace.validatePath(testPath.toString(), 410 IResource.ROOT | IResource.PROJECT | IResource.FOLDER 411 | IResource.FILE); 412 413 if (result.isOK() && workspace.getRoot().exists(testPath)) { 414 return workspace.getRoot().findMember(testPath); 415 } 416 417 return null; 418 } 419 420 427 public String getTypesFieldValue() { 428 if (typesToExportField == null) { 429 return initialTypesFieldValue; 430 } 431 432 return typesToExportField.getText(); 433 } 434 435 441 protected List getTypesToExport() { 442 List result = new ArrayList (); 443 StringTokenizer tokenizer = new StringTokenizer (typesToExportField 444 .getText(), TYPE_DELIMITER); 445 446 while (tokenizer.hasMoreTokens()) { 447 String currentExtension = tokenizer.nextToken().trim(); 448 if (!currentExtension.equals("")) { result.add(currentExtension); 450 } 451 } 452 453 return result; 454 } 455 456 461 public void handleEvent(Event event) { 462 Widget source = event.widget; 463 464 if (source == exportAllTypesRadio || source == typesToExportField 465 || source == resourceNameField) { 466 resetSelectedResources(); 467 } else if (source == exportSpecifiedTypesRadio) { 468 resetSelectedResources(); 469 typesToExportField.setFocus(); 470 } else if (source == resourceDetailsButton) { 471 handleResourceDetailsButtonPressed(); 472 } else if (source == resourceBrowseButton) { 473 handleResourceBrowseButtonPressed(); 474 } else if (source == typesToExportEditButton) { 475 handleTypesEditButtonPressed(); 476 } 477 478 setPageComplete(determinePageCompletion()); 479 updateWidgetEnablements(); 480 } 481 482 486 protected void handleResourceBrowseButtonPressed() { 487 IResource currentFolder = getSourceResource(); 488 if (currentFolder != null && currentFolder.getType() == IResource.FILE) { 489 currentFolder = currentFolder.getParent(); 490 } 491 492 IPath containerPath = queryForContainer((IContainer) currentFolder, 493 IDEWorkbenchMessages.WizardExportPage_selectResourcesToExport); 494 if (containerPath != null) { String relativePath = containerPath.makeRelative().toString(); 496 if (!relativePath.toString().equals(resourceNameField.getText())) { 497 resetSelectedResources(); 498 resourceNameField.setText(relativePath); 499 } 500 } 501 } 502 503 507 protected void handleResourceDetailsButtonPressed() { 508 IAdaptable source = getSourceResource(); 509 510 if (source == null) { 511 source = ResourcesPlugin.getWorkspace().getRoot(); 512 } 513 514 selectAppropriateResources(source); 515 516 if (source instanceof IFile) { 517 source = ((IFile) source).getParent(); 518 setResourceToDisplay((IResource) source); 519 } 520 521 Object [] newlySelectedResources = queryIndividualResourcesToExport(source); 522 523 if (newlySelectedResources != null) { 524 selectedResources = Arrays.asList(newlySelectedResources); 525 displayResourcesSelectedCount(selectedResources.size()); 526 } 527 } 528 529 533 protected void handleTypesEditButtonPressed() { 534 Object [] newSelectedTypes = queryResourceTypesToExport(); 535 536 if (newSelectedTypes != null) { List result = new ArrayList (newSelectedTypes.length); 538 for (int i = 0; i < newSelectedTypes.length; i++) { 539 result.add(((IFileEditorMapping) newSelectedTypes[i]) 540 .getExtension()); 541 } 542 setTypesToExport(result); 543 } 544 } 545 546 554 protected boolean hasExportableExtension(String resourceName) { 555 if (selectedTypes == null) { 556 return true; 557 } 558 559 int separatorIndex = resourceName.lastIndexOf("."); if (separatorIndex == -1) { 561 return false; 562 } 563 564 String extension = resourceName.substring(separatorIndex + 1); 565 566 Iterator it = selectedTypes.iterator(); 567 while (it.hasNext()) { 568 if (extension.equalsIgnoreCase((String ) it.next())) { 569 return true; 570 } 571 } 572 573 return false; 574 } 575 576 584 protected void internalSaveWidgetValues() { 585 } 586 587 596 protected Object [] queryIndividualResourcesToExport(IAdaptable rootResource) { 597 ResourceSelectionDialog dialog = new ResourceSelectionDialog( 598 getContainer().getShell(), rootResource, IDEWorkbenchMessages.WizardExportPage_selectResourcesTitle); 599 dialog.setInitialSelections(selectedResources 600 .toArray(new Object [selectedResources.size()])); 601 dialog.open(); 602 return dialog.getResult(); 603 } 604 605 613 protected Object [] queryResourceTypesToExport() { 614 IFileEditorMapping editorMappings[] = PlatformUI.getWorkbench() 615 .getEditorRegistry().getFileEditorMappings(); 616 617 int mappingsSize = editorMappings.length; 618 List selectedTypes = getTypesToExport(); 619 List initialSelections = new ArrayList (selectedTypes.size()); 620 621 for (int i = 0; i < mappingsSize; i++) { 622 IFileEditorMapping currentMapping = editorMappings[i]; 623 if (selectedTypes.contains(currentMapping.getExtension())) { 624 initialSelections.add(currentMapping); 625 } 626 } 627 628 ListSelectionDialog dialog = new ListSelectionDialog(getContainer() 629 .getShell(), editorMappings, 630 FileEditorMappingContentProvider.INSTANCE, 631 FileEditorMappingLabelProvider.INSTANCE, IDEWorkbenchMessages.WizardExportPage_selectionDialogMessage); 632 633 dialog.setTitle(IDEWorkbenchMessages.WizardExportPage_resourceTypeDialog); 634 dialog.open(); 635 636 return dialog.getResult(); 637 } 638 639 643 protected void resetSelectedResources() { 644 resourceDetailsDescription.setText(IDEWorkbenchMessages.WizardExportPage_detailsMessage); 645 selectedResources = null; 646 647 if (exportCurrentSelection) { 648 exportCurrentSelection = false; 649 650 if (resourceNameField.getText().length() > CURRENT_SELECTION 651 .length()) { 652 resourceNameField.setText(resourceNameField.getText() 653 .substring(CURRENT_SELECTION.length())); 654 } else { 655 resourceNameField.setText(""); } 657 } 658 } 659 660 665 protected void restoreResourceSpecificationWidgetValues() { 666 IDialogSettings settings = getDialogSettings(); 667 if (settings != null) { 668 String pageName = getName(); 669 boolean exportAllResources = settings 670 .getBoolean(STORE_EXPORT_ALL_RESOURCES_ID + pageName); 671 672 if (!exportAllResourcesPreSet) { 674 exportAllTypesRadio.setSelection(exportAllResources); 675 exportSpecifiedTypesRadio.setSelection(!exportAllResources); 676 } 677 678 if (initialTypesFieldValue == null) { 680 String [] selectedTypes = settings 681 .getArray(STORE_SELECTED_TYPES_ID + pageName); 682 if (selectedTypes != null) { 683 if (selectedTypes.length > 0) { 684 typesToExportField.setText(selectedTypes[0]); 685 } 686 for (int i = 0; i < selectedTypes.length; i++) { 687 typesToExportField.add(selectedTypes[i]); 688 } 689 } 690 } 691 } 692 } 693 694 700 protected void saveWidgetValues() { 701 IDialogSettings settings = getDialogSettings(); 702 if (settings != null) { 703 String pageName = getName(); 704 705 String [] selectedTypesNames = settings 707 .getArray(STORE_SELECTED_TYPES_ID + pageName); 708 if (selectedTypesNames == null) { 709 selectedTypesNames = new String [0]; 710 } 711 712 if (exportSpecifiedTypesRadio.getSelection()) { 713 selectedTypesNames = addToHistory(selectedTypesNames, 714 typesToExportField.getText()); 715 } 716 717 settings 718 .put(STORE_SELECTED_TYPES_ID + pageName, selectedTypesNames); 719 720 settings.put(STORE_EXPORT_ALL_RESOURCES_ID + pageName, 722 exportAllTypesRadio.getSelection()); 723 } 724 725 internalSaveWidgetValues(); 727 728 } 729 730 736 protected void selectAppropriateFolderContents(IContainer resource) { 737 try { 738 IResource[] members = resource.members(); 739 740 for (int i = 0; i < members.length; i++) { 741 if (members[i].getType() == IResource.FILE) { 742 IFile currentFile = (IFile) members[i]; 743 if (hasExportableExtension(currentFile.getFullPath() 744 .toString())) { 745 selectedResources.add(currentFile); 746 } 747 } 748 if (members[i].getType() == IResource.FOLDER) { 749 selectAppropriateFolderContents((IContainer) members[i]); 750 } 751 } 752 } catch (CoreException e) { 753 } 755 } 756 757 763 protected void selectAppropriateResources(Object resource) { 764 if (selectedResources == null) { 765 766 if (exportSpecifiedTypesRadio.getSelection()) { 767 selectedTypes = getTypesToExport(); 768 } else { 769 selectedTypes = null; } 771 772 selectedResources = new ArrayList (); 773 if (resource instanceof IWorkspaceRoot) { 774 IProject[] projects = ((IWorkspaceRoot) resource).getProjects(); 775 for (int i = 0; i < projects.length; i++) { 776 selectAppropriateFolderContents(projects[i]); 777 } 778 } else if (resource instanceof IFile) { 779 IFile file = (IFile) resource; 780 if (hasExportableExtension(file.getFullPath().toString())) { 781 selectedResources.add(file); 782 } 783 } else { 784 selectAppropriateFolderContents((IContainer) resource); 785 } 786 } 787 } 788 789 795 public void setExportAllTypesValue(boolean value) { 796 if (exportAllTypesRadio == null) { 797 initialExportAllTypesValue = value; 798 exportAllResourcesPreSet = true; 799 } else { 800 exportAllTypesRadio.setSelection(value); 801 exportSpecifiedTypesRadio.setSelection(!value); 802 } 803 } 804 805 811 public void setResourceFieldValue(String value) { 812 if (resourceNameField == null) { 813 initialExportFieldValue = value; 814 } else { 815 resourceNameField.setText(value); 816 } 817 } 818 819 823 protected void setResourceToDisplay(IResource resource) { 824 setResourceFieldValue(resource.getFullPath().makeRelative().toString()); 825 } 826 827 833 public void setTypesFieldValue(String value) { 834 if (typesToExportField == null) { 835 initialTypesFieldValue = value; 836 } else { 837 typesToExportField.setText(value); 838 } 839 } 840 841 848 protected void setTypesToExport(List typeStrings) { 849 StringBuffer result = new StringBuffer (); 850 Iterator typesEnum = typeStrings.iterator(); 851 852 while (typesEnum.hasNext()) { 853 result.append(typesEnum.next()); 854 result.append(TYPE_DELIMITER); 855 result.append(" "); } 857 858 typesToExportField.setText(result.toString()); 859 } 860 861 864 protected void setupBasedOnInitialSelections() { 865 if (initialExportFieldValue != null) { 866 IResource specifiedSourceResource = getSourceResource(); 869 if (specifiedSourceResource == null) { 870 currentResourceSelection = new StructuredSelection(); 871 } else { 872 currentResourceSelection = new StructuredSelection( 873 specifiedSourceResource); 874 } 875 } 876 877 if (currentResourceSelection.isEmpty()) { 878 return; } 880 881 List selections = new ArrayList (); 882 Iterator it = currentResourceSelection.iterator(); 883 while (it.hasNext()) { 884 IResource currentResource = (IResource) it.next(); 885 if (currentResource.isAccessible()) { 887 selections.add(currentResource); 888 } 889 } 890 891 if (selections.isEmpty()) { 892 return; } 894 895 int selectedResourceCount = selections.size(); 896 if (selectedResourceCount == 1) { 897 IResource resource = (IResource) selections.get(0); 898 setResourceToDisplay(resource); 899 } else { 900 selectedResources = selections; 901 exportAllTypesRadio.setSelection(true); 902 exportSpecifiedTypesRadio.setSelection(false); 903 resourceNameField.setText(CURRENT_SELECTION); 904 exportCurrentSelection = true; 905 displayResourcesSelectedCount(selectedResourceCount); 906 } 907 } 908 909 912 protected void updateWidgetEnablements() { 913 if (exportCurrentSelection) { 914 resourceDetailsButton.setEnabled(true); 915 } else { 916 IResource resource = getSourceResource(); 917 resourceDetailsButton.setEnabled(resource != null 918 && resource.isAccessible()); 919 } 920 921 exportSpecifiedTypesRadio.setEnabled(!exportCurrentSelection); 922 typesToExportField.setEnabled(exportSpecifiedTypesRadio.getSelection()); 923 typesToExportEditButton.setEnabled(exportSpecifiedTypesRadio 924 .getSelection()); 925 } 926 927 930 protected final boolean validateSourceGroup() { 931 if (exportCurrentSelection) { 932 return true; 933 } 934 935 String sourceString = resourceNameField.getText(); 936 if (sourceString.equals("")) { setErrorMessage(null); 938 return false; 939 } 940 941 IResource resource = getSourceResource(); 942 943 if (resource == null) { 944 setErrorMessage(IDEWorkbenchMessages.WizardExportPage_mustExistMessage); 945 return false; 946 } 947 948 if (!resource.isAccessible()) { 949 setErrorMessage(IDEWorkbenchMessages.WizardExportPage_mustBeAccessibleMessage); 950 return false; 951 } 952 953 return true; 954 } 955 } 956 | Popular Tags |