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();
}
Aggregations