|
34 | 34 | import org.eclipse.core.resources.IProject; |
35 | 35 | import org.eclipse.core.resources.ResourcesPlugin; |
36 | 36 | import org.eclipse.core.runtime.Assert; |
| 37 | +import org.eclipse.core.runtime.CoreException; |
37 | 38 | import org.eclipse.core.runtime.IPath; |
38 | 39 | import org.eclipse.core.runtime.IProgressMonitor; |
39 | 40 | import org.eclipse.core.runtime.IStatus; |
40 | 41 | import org.eclipse.core.runtime.Status; |
41 | 42 | import org.eclipse.core.runtime.jobs.IJobChangeEvent; |
42 | 43 | import org.eclipse.core.runtime.jobs.Job; |
43 | 44 | import org.eclipse.core.runtime.jobs.JobChangeAdapter; |
| 45 | +import org.eclipse.jface.dialogs.IDialogConstants; |
44 | 46 | import org.eclipse.jface.dialogs.IDialogSettings; |
45 | 47 | import org.eclipse.jface.dialogs.MessageDialog; |
46 | 48 | import org.eclipse.jface.fieldassist.ControlDecoration; |
|
62 | 64 | import org.eclipse.jface.viewers.ViewerFilter; |
63 | 65 | import org.eclipse.jface.wizard.IWizard; |
64 | 66 | import org.eclipse.jface.wizard.ProgressMonitorPart; |
| 67 | +import org.eclipse.jface.wizard.WizardDialog; |
65 | 68 | import org.eclipse.jface.wizard.WizardPage; |
66 | 69 | import org.eclipse.osgi.util.NLS; |
67 | 70 | import org.eclipse.swt.SWT; |
|
88 | 91 | import org.eclipse.swt.widgets.ToolItem; |
89 | 92 | import org.eclipse.swt.widgets.TreeColumn; |
90 | 93 | import org.eclipse.swt.widgets.TreeItem; |
| 94 | +import org.eclipse.ui.IPageLayout; |
| 95 | +import org.eclipse.ui.IViewPart; |
| 96 | +import org.eclipse.ui.IWorkbenchWindow; |
91 | 97 | import org.eclipse.ui.IWorkingSet; |
| 98 | +import org.eclipse.ui.PartInitException; |
92 | 99 | import org.eclipse.ui.PlatformUI; |
93 | 100 | import org.eclipse.ui.dialogs.FilteredTree; |
94 | 101 | import org.eclipse.ui.dialogs.PatternFilter; |
|
100 | 107 | import org.eclipse.ui.internal.progress.ProgressManager.JobMonitor; |
101 | 108 | import org.eclipse.ui.internal.registry.WorkingSetDescriptor; |
102 | 109 | import org.eclipse.ui.internal.registry.WorkingSetRegistry; |
| 110 | +import org.eclipse.ui.part.ISetSelectionTarget; |
103 | 111 | import org.eclipse.ui.statushandlers.StatusManager; |
104 | 112 | import org.eclipse.ui.wizards.datatransfer.ProjectConfigurator; |
105 | 113 | import org.osgi.framework.FrameworkUtil; |
@@ -607,14 +615,21 @@ public boolean isChecked(Object element) { |
607 | 615 | } |
608 | 616 | }); |
609 | 617 | tree.addCheckStateListener(event -> { |
610 | | - if (isExistingProject((File) event.getElement()) || isExistingProjectName((File) event.getElement())) { |
611 | | - tree.setChecked(event.getElement(), false); |
| 618 | + File file = (File) event.getElement(); |
| 619 | + IProject existingProject = findExistingProject(file); |
| 620 | + if (existingProject != null) { |
| 621 | + tree.setChecked(file, false); |
| 622 | + promptAlreadyImportedProject(file, existingProject); |
| 623 | + return; |
| 624 | + } |
| 625 | + if (isExistingProjectName(file)) { |
| 626 | + tree.setChecked(file, false); |
612 | 627 | return; |
613 | 628 | } |
614 | 629 | if (event.getChecked()) { |
615 | | - SmartImportRootWizardPage.this.directoriesToImport.add((File) event.getElement()); |
| 630 | + SmartImportRootWizardPage.this.directoriesToImport.add(file); |
616 | 631 | } else { |
617 | | - SmartImportRootWizardPage.this.directoriesToImport.remove(event.getElement()); |
| 632 | + SmartImportRootWizardPage.this.directoriesToImport.remove(file); |
618 | 633 | } |
619 | 634 | proposalsSelectionChanged(); |
620 | 635 | }); |
@@ -787,21 +802,92 @@ private String getValue(String s) { |
787 | 802 | } |
788 | 803 |
|
789 | 804 | } |
790 | | - protected boolean isExistingProject(File element) { |
| 805 | + |
| 806 | + protected IProject findExistingProject(File element) { |
791 | 807 | for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
792 | 808 | IPath location = project.getLocation(); |
793 | 809 | if (location != null && element.equals(location.toFile())) { |
794 | | - return true; |
| 810 | + return project; |
795 | 811 | } |
796 | 812 | } |
797 | | - return false; |
| 813 | + return null; |
| 814 | + } |
| 815 | + |
| 816 | + protected boolean isExistingProject(File element) { |
| 817 | + return findExistingProject(element) != null; |
798 | 818 | } |
799 | 819 |
|
800 | 820 | protected boolean isExistingProjectName(File element) { |
801 | 821 | String name = element.getName(); |
802 | 822 | return !name.isEmpty() && ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists(); |
803 | 823 | } |
804 | 824 |
|
| 825 | + private void promptAlreadyImportedProject(File file, IProject project) { |
| 826 | + tree.setSelection(new StructuredSelection(file), true); |
| 827 | + boolean isOpen = project.isOpen(); |
| 828 | + String title = isOpen ? DataTransferMessages.SmartImportProposals_projectAlreadyOpen_title |
| 829 | + : DataTransferMessages.SmartImportProposals_projectAlreadyClosed_title; |
| 830 | + String message = NLS.bind(isOpen ? DataTransferMessages.SmartImportProposals_projectAlreadyOpen_message |
| 831 | + : DataTransferMessages.SmartImportProposals_projectAlreadyClosed_message, project.getName()); |
| 832 | + String actionLabel = isOpen ? DataTransferMessages.SmartImportProposals_showInProjectExplorer |
| 833 | + : DataTransferMessages.SmartImportProposals_openProject; |
| 834 | + |
| 835 | + MessageDialog dialog = new MessageDialog(getShell(), title, null, message, MessageDialog.QUESTION, |
| 836 | + new String[] { actionLabel, IDialogConstants.CANCEL_LABEL }, 0); |
| 837 | + if (dialog.open() == 0) { |
| 838 | + if (isOpen) { |
| 839 | + showProjectInExplorer(project); |
| 840 | + } else { |
| 841 | + openProject(project); |
| 842 | + } |
| 843 | + if (tree.getCheckedElements().length == 0) { |
| 844 | + getContainer().getShell().getDisplay().asyncExec(() -> { |
| 845 | + if (getContainer() instanceof WizardDialog wizardDialog) { |
| 846 | + wizardDialog.close(); |
| 847 | + } |
| 848 | + }); |
| 849 | + } |
| 850 | + } |
| 851 | + } |
| 852 | + |
| 853 | + private void openProject(IProject project) { |
| 854 | + try { |
| 855 | + getContainer().run(true, true, monitor -> { |
| 856 | + try { |
| 857 | + project.open(monitor); |
| 858 | + } catch (CoreException e) { |
| 859 | + throw new InvocationTargetException(e); |
| 860 | + } |
| 861 | + }); |
| 862 | + } catch (InvocationTargetException | InterruptedException e) { |
| 863 | + StatusManager |
| 864 | + .getManager().handle( |
| 865 | + new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, |
| 866 | + NLS.bind(DataTransferMessages.SmartImportProposals_openProjectFailed, |
| 867 | + project.getName()), |
| 868 | + e.getCause() != null ? e.getCause() : e), |
| 869 | + StatusManager.LOG | StatusManager.SHOW); |
| 870 | + return; |
| 871 | + } |
| 872 | + showProjectInExplorer(project); |
| 873 | + } |
| 874 | + |
| 875 | + private void showProjectInExplorer(IProject project) { |
| 876 | + IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 877 | + if (window == null || window.getActivePage() == null) { |
| 878 | + return; |
| 879 | + } |
| 880 | + try { |
| 881 | + IViewPart view = window.getActivePage().showView(IPageLayout.ID_PROJECT_EXPLORER); |
| 882 | + if (view instanceof ISetSelectionTarget target) { |
| 883 | + target.selectReveal(new StructuredSelection(project)); |
| 884 | + } |
| 885 | + } catch (PartInitException e) { |
| 886 | + StatusManager.getManager().handle(new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, |
| 887 | + DataTransferMessages.SmartImportProposals_openFailed, e), StatusManager.LOG); |
| 888 | + } |
| 889 | + } |
| 890 | + |
805 | 891 | protected void validatePage() { |
806 | 892 | // reset error message |
807 | 893 | setErrorMessage(null); |
|
0 commit comments