Search in sources :

Example 1 with MoveProjectOperation

use of org.eclipse.ui.ide.undo.MoveProjectOperation in project egit by eclipse.

the class SharingWizard method performFinish.

@Override
public boolean performFinish() {
    final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    if (!existingPage.getInternalMode()) {
        try {
            final Map<IProject, File> projectsToMove = existingPage.getProjects(true);
            final Repository selectedRepository = existingPage.getSelectedRepository();
            getContainer().run(true, false, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    SubMonitor progress = SubMonitor.convert(monitor, projectsToMove.size() * 2);
                    for (Map.Entry<IProject, File> entry : projectsToMove.entrySet()) {
                        closeOpenEditorsForProject(activePage, entry.getKey());
                        IPath targetLocation = new Path(entry.getValue().getPath());
                        IPath currentLocation = entry.getKey().getLocation();
                        if (!targetLocation.equals(currentLocation)) {
                            MoveProjectOperation op = new MoveProjectOperation(entry.getKey(), entry.getValue().toURI(), UIText.SharingWizard_MoveProjectActionLabel);
                            try {
                                IStatus result = op.execute(progress.newChild(1), null);
                                if (!result.isOK())
                                    throw new RuntimeException();
                            } catch (ExecutionException e) {
                                if (e.getCause() != null)
                                    throw new InvocationTargetException(e.getCause());
                                throw new InvocationTargetException(e);
                            }
                        } else {
                            progress.worked(1);
                        }
                        try {
                            new ConnectProviderOperation(entry.getKey(), selectedRepository.getDirectory()).execute(progress.newChild(1));
                        } catch (CoreException e) {
                            throw new InvocationTargetException(e);
                        }
                    }
                }
            });
        } catch (InvocationTargetException e) {
            Activator.handleError(UIText.SharingWizard_failed, e.getCause(), true);
            return false;
        } catch (InterruptedException e) {
        // ignore for the moment
        }
        return true;
    } else {
        final ConnectProviderOperation op = new ConnectProviderOperation(existingPage.getProjects(true));
        try {
            getContainer().run(true, false, new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException {
                    try {
                        op.execute(monitor);
                        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

                            @Override
                            public void run() {
                                Set<File> filesToAdd = new HashSet<>();
                                // collect all files first
                                for (Entry<IProject, File> entry : existingPage.getProjects(true).entrySet()) filesToAdd.add(entry.getValue());
                                // view
                                for (File file : filesToAdd) Activator.getDefault().getRepositoryUtil().addConfiguredRepository(file);
                            }
                        });
                    } catch (CoreException ce) {
                        throw new InvocationTargetException(ce);
                    }
                }
            });
            return true;
        } catch (Throwable e) {
            if (e instanceof InvocationTargetException) {
                e = e.getCause();
            }
            if (e instanceof CoreException) {
                IStatus status = ((CoreException) e).getStatus();
                e = status.getException();
            }
            Activator.handleError(UIText.SharingWizard_failed, e, true);
            return false;
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Entry(java.util.Map.Entry) ExecutionException(org.eclipse.core.commands.ExecutionException) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) Repository(org.eclipse.jgit.lib.Repository) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ConnectProviderOperation(org.eclipse.egit.core.op.ConnectProviderOperation) IFile(org.eclipse.core.resources.IFile) File(java.io.File) MoveProjectOperation(org.eclipse.ui.ide.undo.MoveProjectOperation)

Aggregations

File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Path (org.eclipse.core.runtime.Path)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 ConnectProviderOperation (org.eclipse.egit.core.op.ConnectProviderOperation)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 Repository (org.eclipse.jgit.lib.Repository)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 MoveProjectOperation (org.eclipse.ui.ide.undo.MoveProjectOperation)1