1 16 package org.eclipse.ui.internal.wizards.datatransfer; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.net.URI ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 import java.util.zip.ZipException ; 30 import java.util.zip.ZipFile ; 31 32 import org.eclipse.core.resources.IProject; 33 import org.eclipse.core.resources.IProjectDescription; 34 import org.eclipse.core.resources.IResource; 35 import org.eclipse.core.resources.IWorkspace; 36 import org.eclipse.core.resources.ResourcesPlugin; 37 import org.eclipse.core.runtime.CoreException; 38 import org.eclipse.core.runtime.IPath; 39 import org.eclipse.core.runtime.IProgressMonitor; 40 import org.eclipse.core.runtime.IStatus; 41 import org.eclipse.core.runtime.OperationCanceledException; 42 import org.eclipse.core.runtime.Path; 43 import org.eclipse.core.runtime.Platform; 44 import org.eclipse.core.runtime.Status; 45 import org.eclipse.core.runtime.SubProgressMonitor; 46 import org.eclipse.jface.dialogs.Dialog; 47 import org.eclipse.jface.dialogs.ErrorDialog; 48 import org.eclipse.jface.dialogs.IDialogConstants; 49 import org.eclipse.jface.dialogs.IDialogSettings; 50 import org.eclipse.jface.dialogs.MessageDialog; 51 import org.eclipse.jface.operation.IRunnableWithProgress; 52 import org.eclipse.jface.viewers.CheckStateChangedEvent; 53 import org.eclipse.jface.viewers.CheckboxTreeViewer; 54 import org.eclipse.jface.viewers.ICheckStateListener; 55 import org.eclipse.jface.viewers.ITreeContentProvider; 56 import org.eclipse.jface.viewers.LabelProvider; 57 import org.eclipse.jface.viewers.Viewer; 58 import org.eclipse.jface.viewers.ViewerComparator; 59 import org.eclipse.jface.wizard.WizardPage; 60 import org.eclipse.osgi.util.NLS; 61 import org.eclipse.swt.SWT; 62 import org.eclipse.swt.events.FocusAdapter; 63 import org.eclipse.swt.events.SelectionAdapter; 64 import org.eclipse.swt.events.SelectionEvent; 65 import org.eclipse.swt.events.TraverseEvent; 66 import org.eclipse.swt.events.TraverseListener; 67 import org.eclipse.swt.layout.GridData; 68 import org.eclipse.swt.layout.GridLayout; 69 import org.eclipse.swt.widgets.Button; 70 import org.eclipse.swt.widgets.Composite; 71 import org.eclipse.swt.widgets.DirectoryDialog; 72 import org.eclipse.swt.widgets.FileDialog; 73 import org.eclipse.swt.widgets.Label; 74 import org.eclipse.swt.widgets.Text; 75 import org.eclipse.ui.actions.WorkspaceModifyOperation; 76 import org.eclipse.ui.dialogs.IOverwriteQuery; 77 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 78 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 79 import org.eclipse.ui.internal.ide.StatusUtil; 80 import org.eclipse.ui.statushandlers.StatusManager; 81 import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; 82 import org.eclipse.ui.wizards.datatransfer.ImportOperation; 83 84 88 public class WizardProjectsImportPage extends WizardPage implements 89 IOverwriteQuery { 90 91 94 public static final String METADATA_FOLDER = ".metadata"; 96 100 public class ProjectRecord { 101 File projectSystemFile; 102 103 Object projectArchiveFile; 104 105 String projectName; 106 107 Object parent; 108 109 int level; 110 111 IProjectDescription description; 112 113 ILeveledImportStructureProvider provider; 114 115 120 ProjectRecord(File file) { 121 projectSystemFile = file; 122 setProjectName(); 123 } 124 125 135 ProjectRecord(Object file, Object parent, int level, 136 ILeveledImportStructureProvider entryProvider) { 137 this.projectArchiveFile = file; 138 this.parent = parent; 139 this.level = level; 140 this.provider = entryProvider; 141 setProjectName(); 142 } 143 144 147 private void setProjectName() { 148 IProjectDescription newDescription = null; 149 try { 150 if (projectArchiveFile != null) { 151 InputStream stream = provider 152 .getContents(projectArchiveFile); 153 if (stream != null) { 154 newDescription = IDEWorkbenchPlugin 155 .getPluginWorkspace().loadProjectDescription( 156 stream); 157 stream.close(); 158 } 159 } else { 160 IPath path = new Path(projectSystemFile.getPath()); 161 if (isDefaultLocation(path)) { 164 projectName = path.segment(path.segmentCount() - 2); 165 newDescription = IDEWorkbenchPlugin 166 .getPluginWorkspace().newProjectDescription( 167 projectName); 168 } else { 169 newDescription = IDEWorkbenchPlugin 170 .getPluginWorkspace().loadProjectDescription( 171 path); 172 } 173 } 174 } catch (CoreException e) { 175 } catch (IOException e) { 177 } 179 180 if (newDescription == null) { 181 this.description = null; 182 projectName = ""; } else { 184 this.description = newDescription; 185 projectName = this.description.getName(); 186 } 187 } 188 189 197 private boolean isDefaultLocation(IPath path) { 198 if (path.segmentCount() < 2) 201 return false; 202 return path.removeLastSegments(2).toFile().equals( 203 Platform.getLocation().toFile()); 204 } 205 206 211 public String getProjectName() { 212 return projectName; 213 } 214 } 215 216 private final static String STORE_COPY_PROJECT_ID = "WizardProjectsImportPage.STORE_COPY_PROJECT_ID"; 219 private final static String STORE_ARCHIVE_SELECTED = "WizardProjectsImportPage.STORE_ARCHIVE_SELECTED"; 221 private Text directoryPathField; 222 223 private CheckboxTreeViewer projectsList; 224 225 private Button copyCheckbox; 226 227 private boolean copyFiles = false; 228 229 private ProjectRecord[] selectedProjects = new ProjectRecord[0]; 230 231 private static String previouslyBrowsedDirectory = ""; 235 private static String previouslyBrowsedArchive = ""; 239 private Button projectFromDirectoryRadio; 240 241 private Button projectFromArchiveRadio; 242 243 private Text archivePathField; 244 245 private Button browseDirectoriesButton; 246 247 private Button browseArchivesButton; 248 249 private IProject[] wsProjects; 250 251 private static final String [] FILE_IMPORT_MASK = { 253 "*.jar;*.zip;*.tar;*.tar.gz;*.tgz", "*.*" }; 255 private String lastPath; 257 258 262 public WizardProjectsImportPage() { 263 this("wizardExternalProjectsPage"); } 265 266 271 public WizardProjectsImportPage(String pageName) { 272 super(pageName); 273 setPageComplete(false); 274 setTitle(DataTransferMessages.WizardProjectsImportPage_ImportProjectsTitle); 275 setDescription(DataTransferMessages.WizardProjectsImportPage_ImportProjectsDescription); 276 } 277 278 283 public void createControl(Composite parent) { 284 285 initializeDialogUnits(parent); 286 287 Composite workArea = new Composite(parent, SWT.NONE); 288 setControl(workArea); 289 290 workArea.setLayout(new GridLayout()); 291 workArea.setLayoutData(new GridData(GridData.FILL_BOTH 292 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL)); 293 294 createProjectsRoot(workArea); 295 createProjectsList(workArea); 296 createOptionsArea(workArea); 297 restoreWidgetValues(); 298 Dialog.applyDialogFont(workArea); 299 300 } 301 302 307 private void createOptionsArea(Composite workArea) { 308 Composite optionsGroup = new Composite(workArea, SWT.NONE); 309 optionsGroup.setLayout(new GridLayout()); 310 optionsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 311 312 copyCheckbox = new Button(optionsGroup, SWT.CHECK); 313 copyCheckbox 314 .setText(DataTransferMessages.WizardProjectsImportPage_CopyProjectsIntoWorkspace); 315 copyCheckbox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 316 copyCheckbox.addSelectionListener(new SelectionAdapter() { 317 public void widgetSelected(SelectionEvent e) { 318 copyFiles = copyCheckbox.getSelection(); 319 } 320 }); 321 } 322 323 328 private void createProjectsList(Composite workArea) { 329 330 Label title = new Label(workArea, SWT.NONE); 331 title 332 .setText(DataTransferMessages.WizardProjectsImportPage_ProjectsListTitle); 333 334 Composite listComposite = new Composite(workArea, SWT.NONE); 335 GridLayout layout = new GridLayout(); 336 layout.numColumns = 2; 337 layout.marginWidth = 0; 338 layout.makeColumnsEqualWidth = false; 339 listComposite.setLayout(layout); 340 341 listComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 342 | GridData.GRAB_VERTICAL | GridData.FILL_BOTH)); 343 344 projectsList = new CheckboxTreeViewer(listComposite, SWT.BORDER); 345 GridData listData = new GridData(GridData.GRAB_HORIZONTAL 346 | GridData.GRAB_VERTICAL | GridData.FILL_BOTH); 347 projectsList.getControl().setLayoutData(listData); 348 349 projectsList.setContentProvider(new ITreeContentProvider() { 350 351 356 public Object [] getChildren(Object parentElement) { 357 return null; 358 } 359 360 365 public Object [] getElements(Object inputElement) { 366 return getValidProjects(); 367 } 368 369 374 public boolean hasChildren(Object element) { 375 return false; 376 } 377 378 383 public Object getParent(Object element) { 384 return null; 385 } 386 387 392 public void dispose() { 393 394 } 395 396 402 public void inputChanged(Viewer viewer, Object oldInput, 403 Object newInput) { 404 } 405 406 }); 407 408 projectsList.setLabelProvider(new LabelProvider() { 409 414 public String getText(Object element) { 415 return ((ProjectRecord) element).getProjectName(); 416 } 417 }); 418 419 projectsList.addCheckStateListener(new ICheckStateListener() { 420 425 public void checkStateChanged(CheckStateChangedEvent event) { 426 setPageComplete(projectsList.getCheckedElements().length > 0); 427 } 428 }); 429 430 projectsList.setInput(this); 431 projectsList.setComparator(new ViewerComparator()); 432 createSelectionButtons(listComposite); 433 } 434 435 440 private void createSelectionButtons(Composite listComposite) { 441 Composite buttonsComposite = new Composite(listComposite, SWT.NONE); 442 GridLayout layout = new GridLayout(); 443 layout.marginWidth = 0; 444 layout.marginHeight = 0; 445 buttonsComposite.setLayout(layout); 446 447 buttonsComposite.setLayoutData(new GridData( 448 GridData.VERTICAL_ALIGN_BEGINNING)); 449 450 Button selectAll = new Button(buttonsComposite, SWT.PUSH); 451 selectAll.setText(DataTransferMessages.DataTransfer_selectAll); 452 selectAll.addSelectionListener(new SelectionAdapter() { 453 public void widgetSelected(SelectionEvent e) { 454 projectsList.setCheckedElements(selectedProjects); 455 setPageComplete(projectsList.getCheckedElements().length > 0); 456 } 457 }); 458 Dialog.applyDialogFont(selectAll); 459 setButtonLayoutData(selectAll); 460 461 Button deselectAll = new Button(buttonsComposite, SWT.PUSH); 462 deselectAll.setText(DataTransferMessages.DataTransfer_deselectAll); 463 deselectAll.addSelectionListener(new SelectionAdapter() { 464 469 public void widgetSelected(SelectionEvent e) { 470 471 projectsList.setCheckedElements(new Object [0]); 472 setPageComplete(false); 473 } 474 }); 475 Dialog.applyDialogFont(deselectAll); 476 setButtonLayoutData(deselectAll); 477 478 Button refresh = new Button(buttonsComposite, SWT.PUSH); 479 refresh.setText(DataTransferMessages.DataTransfer_refresh); 480 refresh.addSelectionListener(new SelectionAdapter() { 481 486 public void widgetSelected(SelectionEvent e) { 487 if (projectFromDirectoryRadio.getSelection()) { 488 updateProjectsList(directoryPathField.getText().trim()); 489 } else { 490 updateProjectsList(archivePathField.getText().trim()); 491 } 492 } 493 }); 494 Dialog.applyDialogFont(refresh); 495 setButtonLayoutData(refresh); 496 } 497 498 504 private void createProjectsRoot(Composite workArea) { 505 506 Composite projectGroup = new Composite(workArea, SWT.NONE); 508 GridLayout layout = new GridLayout(); 509 layout.numColumns = 3; 510 layout.makeColumnsEqualWidth = false; 511 layout.marginWidth = 0; 512 projectGroup.setLayout(layout); 513 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 514 515 projectFromDirectoryRadio = new Button(projectGroup, SWT.RADIO); 517 projectFromDirectoryRadio 518 .setText(DataTransferMessages.WizardProjectsImportPage_RootSelectTitle); 519 520 this.directoryPathField = new Text(projectGroup, SWT.BORDER); 522 523 this.directoryPathField.setLayoutData(new GridData( 524 GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); 525 526 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH); 528 browseDirectoriesButton 529 .setText(DataTransferMessages.DataTransfer_browse); 530 setButtonLayoutData(browseDirectoriesButton); 531 532 projectFromArchiveRadio = new Button(projectGroup, SWT.RADIO); 534 projectFromArchiveRadio 535 .setText(DataTransferMessages.WizardProjectsImportPage_ArchiveSelectTitle); 536 537 archivePathField = new Text(projectGroup, SWT.BORDER); 539 540 archivePathField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL 541 | GridData.GRAB_HORIZONTAL)); 542 browseArchivesButton = new Button(projectGroup, SWT.PUSH); 544 browseArchivesButton.setText(DataTransferMessages.DataTransfer_browse); 545 setButtonLayoutData(browseArchivesButton); 546 547 projectFromDirectoryRadio.setSelection(true); 548 archivePathField.setEnabled(false); 549 browseArchivesButton.setEnabled(false); 550 551 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() { 552 558 public void widgetSelected(SelectionEvent e) { 559 handleLocationDirectoryButtonPressed(); 560 } 561 562 }); 563 564 browseArchivesButton.addSelectionListener(new SelectionAdapter() { 565 570 public void widgetSelected(SelectionEvent e) { 571 handleLocationArchiveButtonPressed(); 572 } 573 574 }); 575 576 directoryPathField.addTraverseListener(new TraverseListener() { 577 578 583 public void keyTraversed(TraverseEvent e) { 584 if (e.detail == SWT.TRAVERSE_RETURN) { 585 e.doit = false; 586 updateProjectsList(directoryPathField.getText().trim()); 587 } 588 } 589 590 }); 591 592 directoryPathField.addFocusListener(new FocusAdapter() { 593 594 599 public void focusLost(org.eclipse.swt.events.FocusEvent e) { 600 updateProjectsList(directoryPathField.getText().trim()); 601 } 602 603 }); 604 605 archivePathField.addTraverseListener(new TraverseListener() { 606 607 612 public void keyTraversed(TraverseEvent e) { 613 if (e.detail == SWT.TRAVERSE_RETURN) { 614 e.doit = false; 615 updateProjectsList(archivePathField.getText().trim()); 616 } 617 } 618 619 }); 620 621 archivePathField.addFocusListener(new FocusAdapter() { 622 627 public void focusLost(org.eclipse.swt.events.FocusEvent e) { 628 updateProjectsList(archivePathField.getText().trim()); 629 } 630 }); 631 632 projectFromDirectoryRadio.addSelectionListener(new SelectionAdapter() { 633 638 public void widgetSelected(SelectionEvent e) { 639 directoryRadioSelected(); 640 } 641 }); 642 643 projectFromArchiveRadio.addSelectionListener(new SelectionAdapter() { 644 649 public void widgetSelected(SelectionEvent e) { 650 archiveRadioSelected(); 651 } 652 }); 653 } 654 655 private void archiveRadioSelected() { 656 if (projectFromArchiveRadio.getSelection()) { 657 directoryPathField.setEnabled(false); 658 browseDirectoriesButton.setEnabled(false); 659 archivePathField.setEnabled(true); 660 browseArchivesButton.setEnabled(true); 661 updateProjectsList(archivePathField.getText()); 662 archivePathField.setFocus(); 663 copyCheckbox.setSelection(true); 664 copyCheckbox.setEnabled(false); 665 } 666 } 667 668 private void directoryRadioSelected() { 669 if (projectFromDirectoryRadio.getSelection()) { 670 directoryPathField.setEnabled(true); 671 browseDirectoriesButton.setEnabled(true); 672 archivePathField.setEnabled(false); 673 browseArchivesButton.setEnabled(false); 674 updateProjectsList(directoryPathField.getText()); 675 directoryPathField.setFocus(); 676 copyCheckbox.setEnabled(true); 677 copyCheckbox.setSelection(copyFiles); 678 } 679 } 680 681 685 public void setVisible(boolean visible) { 686 super.setVisible(visible); 687 if (visible && this.projectFromDirectoryRadio.getSelection()) { 688 this.directoryPathField.setFocus(); 689 } 690 if (visible && this.projectFromArchiveRadio.getSelection()) { 691 this.archivePathField.setFocus(); 692 } 693 } 694 695 701 public void updateProjectsList(final String path) { 702 703 if (path.equals(lastPath)) { 704 return; 705 } 706 707 lastPath = path; 708 709 if (path == null || path.length() == 0) { 711 selectedProjects = new ProjectRecord[0]; 712 projectsList.refresh(true); 713 projectsList.setCheckedElements(selectedProjects); 714 setPageComplete(projectsList.getCheckedElements().length > 0); 715 return; 716 } 717 final boolean dirSelected = this.projectFromDirectoryRadio 720 .getSelection(); 721 try { 722 getContainer().run(true, true, new IRunnableWithProgress() { 723 724 729 public void run(IProgressMonitor monitor) { 730 731 monitor 732 .beginTask( 733 DataTransferMessages.WizardProjectsImportPage_SearchingMessage, 734 100); 735 File directory = new File (path); 736 selectedProjects = new ProjectRecord[0]; 737 Collection files = new ArrayList (); 738 monitor.worked(10); 739 if (!dirSelected 740 && ArchiveFileManipulations.isTarFile(path)) { 741 TarFile sourceTarFile = getSpecifiedTarSourceFile(path); 742 if (sourceTarFile == null) { 743 return; 744 } 745 746 TarLeveledStructureProvider provider = ArchiveFileManipulations 747 .getTarStructureProvider(sourceTarFile, 748 getContainer().getShell()); 749 Object child = provider.getRoot(); 750 751 if (!collectProjectFilesFromProvider(files, provider, 752 child, 0, monitor)) { 753 return; 754 } 755 Iterator filesIterator = files.iterator(); 756 selectedProjects = new ProjectRecord[files.size()]; 757 int index = 0; 758 monitor.worked(50); 759 monitor 760 .subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage); 761 while (filesIterator.hasNext()) { 762 selectedProjects[index++] = (ProjectRecord) filesIterator 763 .next(); 764 } 765 } else if (!dirSelected 766 && ArchiveFileManipulations.isZipFile(path)) { 767 ZipFile sourceFile = getSpecifiedZipSourceFile(path); 768 if (sourceFile == null) { 769 return; 770 } 771 ZipLeveledStructureProvider provider = ArchiveFileManipulations 772 .getZipStructureProvider(sourceFile, 773 getContainer().getShell()); 774 Object child = provider.getRoot(); 775 776 if (!collectProjectFilesFromProvider(files, provider, 777 child, 0, monitor)) { 778 return; 779 } 780 Iterator filesIterator = files.iterator(); 781 selectedProjects = new ProjectRecord[files.size()]; 782 int index = 0; 783 monitor.worked(50); 784 monitor 785 .subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage); 786 while (filesIterator.hasNext()) { 787 selectedProjects[index++] = (ProjectRecord) filesIterator 788 .next(); 789 } 790 } 791 792 else if (dirSelected && directory.isDirectory()) { 793 794 if (!collectProjectFilesFromDirectory(files, directory, 795 null, monitor)) { 796 return; 797 } 798 Iterator filesIterator = files.iterator(); 799 selectedProjects = new ProjectRecord[files.size()]; 800 int index = 0; 801 monitor.worked(50); 802 monitor 803 .subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage); 804 while (filesIterator.hasNext()) { 805 File file = (File ) filesIterator.next(); 806 selectedProjects[index] = new ProjectRecord(file); 807 index++; 808 } 809 } else { 810 monitor.worked(60); 811 } 812 monitor.done(); 813 } 814 815 }); 816 } catch (InvocationTargetException e) { 817 IDEWorkbenchPlugin.log(e.getMessage(), e); 818 } catch (InterruptedException e) { 819 } 821 822 projectsList.refresh(true); 823 projectsList.setCheckedElements(getValidProjects()); 824 setPageComplete(projectsList.getCheckedElements().length > 0); 825 } 826 827 831 private ZipFile getSpecifiedZipSourceFile(String fileName) { 832 if (fileName.length() == 0) { 833 return null; 834 } 835 836 try { 837 return new ZipFile (fileName); 838 } catch (ZipException e) { 839 displayErrorDialog(DataTransferMessages.ZipImport_badFormat); 840 } catch (IOException e) { 841 displayErrorDialog(DataTransferMessages.ZipImport_couldNotRead); 842 } 843 844 archivePathField.setFocus(); 845 return null; 846 } 847 848 852 private TarFile getSpecifiedTarSourceFile(String fileName) { 853 if (fileName.length() == 0) { 854 return null; 855 } 856 857 try { 858 return new TarFile(fileName); 859 } catch (TarException e) { 860 displayErrorDialog(DataTransferMessages.TarImport_badFormat); 861 } catch (IOException e) { 862 displayErrorDialog(DataTransferMessages.ZipImport_couldNotRead); 863 } 864 865 archivePathField.setFocus(); 866 return null; 867 } 868 869 875 protected void displayErrorDialog(String message) { 876 MessageDialog.openError(getContainer().getShell(), 877 getErrorDialogTitle(), message); 878 } 879 880 883 protected String getErrorDialogTitle() { 884 return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle; 885 } 886 887 898 private boolean collectProjectFilesFromDirectory(Collection files, 899 File directory, Set directoriesVisited, IProgressMonitor monitor) { 900 901 if (monitor.isCanceled()) { 902 return false; 903 } 904 monitor.subTask(NLS.bind( 905 DataTransferMessages.WizardProjectsImportPage_CheckingMessage, 906 directory.getPath())); 907 File [] contents = directory.listFiles(); 908 if (contents == null) 909 return false; 910 911 if (directoriesVisited == null) { 913 directoriesVisited = new HashSet (); 914 try { 915 directoriesVisited.add(directory.getCanonicalPath()); 916 } catch (IOException exception) { 917 StatusManager.getManager().handle( 918 StatusUtil.newStatus(IStatus.ERROR, exception 919 .getLocalizedMessage(), exception)); 920 } 921 } 922 923 final String dotProject = IProjectDescription.DESCRIPTION_FILE_NAME; 925 for (int i = 0; i < contents.length; i++) { 926 File file = contents[i]; 927 if (file.isFile() && file.getName().equals(dotProject)) { 928 files.add(file); 929 return true; 932 } 933 } 934 for (int i = 0; i < contents.length; i++) { 936 if (contents[i].isDirectory()) { 937 if (!contents[i].getName().equals(METADATA_FOLDER)) { 938 try { 939 String canonicalPath = contents[i].getCanonicalPath(); 940 if (!directoriesVisited.add(canonicalPath)) { 941 continue; 943 } 944 } catch (IOException exception) { 945 StatusManager.getManager().handle( 946 StatusUtil.newStatus(IStatus.ERROR, exception 947 .getLocalizedMessage(), exception)); 948 949 } 950 collectProjectFilesFromDirectory(files, contents[i], 951 directoriesVisited, monitor); 952 } 953 } 954 } 955 return true; 956 } 957 958 966 private boolean collectProjectFilesFromProvider(Collection files, 967 ILeveledImportStructureProvider provider, Object entry, int level, 968 IProgressMonitor monitor) { 969 970 if (monitor.isCanceled()) { 971 return false; 972 } 973 monitor.subTask(NLS.bind( 974 DataTransferMessages.WizardProjectsImportPage_CheckingMessage, 975 provider.getLabel(entry))); 976 List children = provider.getChildren(entry); 977 if (children == null) { 978 children = new ArrayList (1); 979 } 980 Iterator childrenEnum = children.iterator(); 981 while (childrenEnum.hasNext()) { 982 Object child = childrenEnum.next(); 983 if (provider.isFolder(child)) { 984 collectProjectFilesFromProvider(files, provider, child, 985 level + 1, monitor); 986 } 987 String elementLabel = provider.getLabel(child); 988 if (elementLabel.equals(IProjectDescription.DESCRIPTION_FILE_NAME)) { 989 files.add(new ProjectRecord(child, entry, level, provider)); 990 } 991 } 992 return true; 993 } 994 995 998 protected void handleLocationDirectoryButtonPressed() { 999 1000 DirectoryDialog dialog = new DirectoryDialog(directoryPathField 1001 .getShell()); 1002 dialog 1003 .setMessage(DataTransferMessages.WizardProjectsImportPage_SelectDialogTitle); 1004 1005 String dirName = directoryPathField.getText().trim(); 1006 if (dirName.length() == 0) { 1007 dirName = previouslyBrowsedDirectory; 1008 } 1009 1010 if (dirName.length() == 0) { 1011 dialog.setFilterPath(IDEWorkbenchPlugin.getPluginWorkspace() 1012 .getRoot().getLocation().toOSString()); 1013 } else { 1014 File path = new File (dirName); 1015 if (path.exists()) { 1016 dialog.setFilterPath(new Path(dirName).toOSString()); 1017 } 1018 } 1019 1020 String selectedDirectory = dialog.open(); 1021 if (selectedDirectory != null) { 1022 previouslyBrowsedDirectory = selectedDirectory; 1023 directoryPathField.setText(previouslyBrowsedDirectory); 1024 updateProjectsList(selectedDirectory); 1025 } 1026 1027 } 1028 1029 1032 protected void handleLocationArchiveButtonPressed() { 1033 1034 FileDialog dialog = new FileDialog(archivePathField.getShell()); 1035 dialog.setFilterExtensions(FILE_IMPORT_MASK); 1036 dialog 1037 .setText(DataTransferMessages.WizardProjectsImportPage_SelectArchiveDialogTitle); 1038 1039 String fileName = archivePathField.getText().trim(); 1040 if (fileName.length() == 0) { 1041 fileName = previouslyBrowsedArchive; 1042 } 1043 1044 if (fileName.length() == 0) { 1045 dialog.setFilterPath(IDEWorkbenchPlugin.getPluginWorkspace() 1046 .getRoot().getLocation().toOSString()); 1047 } else { 1048 File path = new File (fileName); 1049 if (path.exists()) { 1050 dialog.setFilterPath(new Path(fileName).toOSString()); 1051 } 1052 } 1053 1054 String selectedArchive = dialog.open(); 1055 if (selectedArchive != null) { 1056 previouslyBrowsedArchive = selectedArchive; 1057 archivePathField.setText(previouslyBrowsedArchive); 1058 updateProjectsList(selectedArchive); 1059 } 1060 1061 } 1062 1063 1069 public boolean createProjects() { 1070 saveWidgetValues(); 1071 final Object [] selected = projectsList.getCheckedElements(); 1072 WorkspaceModifyOperation op = new WorkspaceModifyOperation() { 1073 protected void execute(IProgressMonitor monitor) 1074 throws InvocationTargetException , InterruptedException { 1075 try { 1076 monitor.beginTask("", selected.length); if (monitor.isCanceled()) { 1078 throw new OperationCanceledException(); 1079 } 1080 for (int i = 0; i < selected.length; i++) { 1081 createExistingProject((ProjectRecord) selected[i], 1082 new SubProgressMonitor(monitor, 1)); 1083 } 1084 } finally { 1085 monitor.done(); 1086 } 1087 } 1088 }; 1089 try { 1091 getContainer().run(true, true, op); 1092 } catch (InterruptedException e) { 1093 return false; 1094 } catch (InvocationTargetException e) { 1095 Throwable t = e.getTargetException(); 1097 String message = DataTransferMessages.WizardExternalProjectImportPage_errorMessage; 1098 IStatus status; 1099 if (t instanceof CoreException) { 1100 status = ((CoreException) t).getStatus(); 1101 } else { 1102 status = new Status(IStatus.ERROR, 1103 IDEWorkbenchPlugin.IDE_WORKBENCH, 1, message, t); 1104 } 1105 ErrorDialog.openError(getShell(), message, null, status); 1106 return false; 1107 } 1108 return true; 1109 } 1110 1111 1114 public void performCancel() { 1115 ArchiveFileManipulations.clearProviderCache(getContainer().getShell()); 1116 } 1117 1118 1125 private boolean createExistingProject(final ProjectRecord record, 1126 IProgressMonitor monitor) throws InvocationTargetException , 1127 InterruptedException { 1128 String projectName = record.getProjectName(); 1129 final IWorkspace workspace = ResourcesPlugin.getWorkspace(); 1130 final IProject project = workspace.getRoot().getProject(projectName); 1131 if (record.description == null) { 1132 record.description = workspace.newProjectDescription(projectName); 1134 IPath locationPath = new Path(record.projectSystemFile 1135 .getAbsolutePath()); 1136 1137 if (Platform.getLocation().isPrefixOf(locationPath)) { 1139 record.description.setLocation(null); 1140 } else { 1141 record.description.setLocation(locationPath); 1142 } 1143 } else { 1144 record.description.setName(projectName); 1145 } 1146 if (record.projectArchiveFile != null) { 1147 List fileSystemObjects = record.provider.getChildren(record.parent); 1149 record.provider.setStrip(record.level); 1150 ImportOperation operation = new ImportOperation(project 1151 .getFullPath(), record.provider.getRoot(), record.provider, 1152 this, fileSystemObjects); 1153 operation.setContext(getShell()); 1154 operation.run(monitor); 1155 return true; 1156 } 1157 File importSource = null; 1159 if (copyFiles) { 1160 URI locationURI = record.description.getLocationURI(); 1163 if (locationURI != null) { 1166 importSource = new File (locationURI); 1167 IProjectDescription desc = workspace 1168 .newProjectDescription(projectName); 1169 desc.setBuildSpec(record.description.getBuildSpec()); 1170 desc.setComment(record.description.getComment()); 1171 desc.setDynamicReferences(record.description 1172 .getDynamicReferences()); 1173 desc.setNatureIds(record.description.getNatureIds()); 1174 desc.setReferencedProjects(record.description 1175 .getReferencedProjects()); 1176 record.description = desc; 1177 } 1178 } 1179 1180 try { 1181 monitor 1182 .beginTask( 1183 DataTransferMessages.WizardProjectsImportPage_CreateProjectsTask, 1184 100); 1185 project.create(record.description, new SubProgressMonitor(monitor, 1186 30)); 1187 project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor( 1188 monitor, 70)); 1189 } catch (CoreException e) { 1190 throw new InvocationTargetException (e); 1191 } finally { 1192 monitor.done(); 1193 } 1194 1195 if (copyFiles && importSource != null) { 1197 List filesToImport = FileSystemStructureProvider.INSTANCE 1198 .getChildren(importSource); 1199 ImportOperation operation = new ImportOperation(project 1200 .getFullPath(), importSource, 1201 FileSystemStructureProvider.INSTANCE, this, filesToImport); 1202 operation.setContext(getShell()); 1203 operation.setOverwriteResources(true); operation.setCreateContainerStructure(false); 1207 operation.run(monitor); 1208 } 1209 1210 return true; 1211 } 1212 1213 1222 public String queryOverwrite(String pathString) { 1223 1224 Path path = new Path(pathString); 1225 1226 String messageString; 1227 if (path.getFileExtension() == null || path.segmentCount() < 2) { 1230 messageString = NLS.bind( 1231 IDEWorkbenchMessages.WizardDataTransfer_existsQuestion, 1232 pathString); 1233 } else { 1234 messageString = NLS 1235 .bind( 1236 IDEWorkbenchMessages.WizardDataTransfer_overwriteNameAndPathQuestion, 1237 path.lastSegment(), path.removeLastSegments(1) 1238 .toOSString()); 1239 } 1240 1241 final MessageDialog dialog = new MessageDialog(getContainer() 1242 .getShell(), IDEWorkbenchMessages.Question, null, 1243 messageString, MessageDialog.QUESTION, new String [] { 1244 IDialogConstants.YES_LABEL, 1245 IDialogConstants.YES_TO_ALL_LABEL, 1246 IDialogConstants.NO_LABEL, 1247 IDialogConstants.NO_TO_ALL_LABEL, 1248 IDialogConstants.CANCEL_LABEL }, 0); 1249 String [] response = new String [] { YES, ALL, NO, NO_ALL, CANCEL }; 1250 getControl().getDisplay().syncExec(new Runnable () { 1253 public void run() { 1254 dialog.open(); 1255 } 1256 }); 1257 return dialog.getReturnCode() < 0 ? CANCEL : response[dialog 1258 .getReturnCode()]; 1259 } 1260 1261 1266 public Button getProjectFromDirectoryRadio() { 1267 return projectFromDirectoryRadio; 1268 } 1269 1270 1275 public CheckboxTreeViewer getProjectsList() { 1276 return projectsList; 1277 } 1278 1279 1284 private IProject[] getProjectsInWorkspace() { 1285 if (wsProjects == null) { 1286 wsProjects = IDEWorkbenchPlugin.getPluginWorkspace().getRoot() 1287 .getProjects(); 1288 } 1289 return wsProjects; 1290 } 1291 1292 1304 public ProjectRecord[] getValidProjects() { 1305 List validProjects = new ArrayList (); 1306 for (int i = 0; i < selectedProjects.length; i++) { 1307 if (!isProjectInWorkspace(selectedProjects[i].getProjectName())) { 1308 validProjects.add(selectedProjects[i]); 1309 } 1310 } 1311 return (ProjectRecord[]) validProjects 1312 .toArray(new ProjectRecord[validProjects.size()]); 1313 } 1314 1315 1323 private boolean isProjectInWorkspace(String projectName) { 1324 if (projectName == null) { 1325 return false; 1326 } 1327 IProject[] workspaceProjects = getProjectsInWorkspace(); 1328 for (int i = 0; i < workspaceProjects.length; i++) { 1329 if (projectName.equals(workspaceProjects[i].getName())) { 1330 return true; 1331 } 1332 } 1333 return false; 1334 } 1335 1336 1342 public void restoreWidgetValues() { 1343 IDialogSettings settings = getDialogSettings(); 1344 if (settings != null) { 1345 copyFiles = settings.getBoolean(STORE_COPY_PROJECT_ID); 1347 copyCheckbox.setSelection(copyFiles); 1348 1349 boolean archiveSelected = settings 1351 .getBoolean(STORE_ARCHIVE_SELECTED); 1352 projectFromDirectoryRadio.setSelection(!archiveSelected); 1353 projectFromArchiveRadio.setSelection(archiveSelected); 1354 if (archiveSelected) { 1355 archiveRadioSelected(); 1356 } else { 1357 directoryRadioSelected(); 1358 } 1359 } 1360 } 1361 1362 1368 public void saveWidgetValues() { 1369 IDialogSettings settings = getDialogSettings(); 1370 if (settings != null) { 1371 settings.put(STORE_COPY_PROJECT_ID, copyCheckbox.getSelection()); 1372 1373 settings.put(STORE_ARCHIVE_SELECTED, projectFromArchiveRadio 1374 .getSelection()); 1375 } 1376 } 1377 1378 1383 public Button getCopyCheckbox() { 1384 return copyCheckbox; 1385 } 1386} 1387 | Popular Tags |