Search in sources :

Example 1 with MultiOrganizeImportAction

use of org.eclipse.jdt.internal.ui.actions.MultiOrganizeImportAction in project xtext-eclipse by eclipse.

the class MultiOrganizeImportsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchSite activeSite = HandlerUtil.getActiveSite(event);
    MultiOrganizeImportAction javaDelegate = new MultiOrganizeImportAction(activeSite);
    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    if (currentSelection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
        if (shouldRunJavaOrganizeImports()) {
            ICompilationUnit[] compilationUnits = javaDelegate.getCompilationUnits(structuredSelection);
            if (compilationUnits.length > 0) {
                javaDelegate.run(structuredSelection);
            }
        }
        final Multimap<IProject, IFile> files = collectFiles(structuredSelection);
        Shell shell = activeSite.getShell();
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor mon) throws InvocationTargetException, InterruptedException {
                mon.beginTask(Messages.OrganizeImports, files.size() * 2);
                mon.setTaskName(Messages.OrganizeImports + " - Calculating Import optimisations for " + files.size() + " Xtend files");
                final List<Change> organizeImports = importOrganizerProvider.get().organizeImports(files, mon);
                for (int i = 0; !mon.isCanceled() && i < organizeImports.size(); i++) {
                    Change change = organizeImports.get(i);
                    mon.setTaskName("Performing changes - Xtend " + (i + 1) + " of " + files.size() + "");
                    try {
                        mon.subTask(change.getName());
                        change.perform(SubMonitor.convert(mon, 1));
                    } catch (CoreException e) {
                        throw new InvocationTargetException(e);
                    }
                    if (mon.isCanceled()) {
                        throw new InterruptedException();
                    }
                }
            }
        };
        try {
            new ProgressMonitorDialog(shell).run(true, true, op);
        } catch (InvocationTargetException e) {
            handleException(e);
        } catch (InterruptedException e) {
        // user cancelled, ok
        }
    }
    return event.getApplicationContext();
}
Also used : IWorkbenchSite(org.eclipse.ui.IWorkbenchSite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) MultiOrganizeImportAction(org.eclipse.jdt.internal.ui.actions.MultiOrganizeImportAction) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Change(org.eclipse.ltk.core.refactoring.Change) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ISelection(org.eclipse.jface.viewers.ISelection)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 MultiOrganizeImportAction (org.eclipse.jdt.internal.ui.actions.MultiOrganizeImportAction)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 Change (org.eclipse.ltk.core.refactoring.Change)1 Shell (org.eclipse.swt.widgets.Shell)1 IWorkbenchSite (org.eclipse.ui.IWorkbenchSite)1