use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class PlatformContentEntriesConfigurable method createEditor.
private void createEditor() {
myModifiableModel = ApplicationManager.getApplication().runReadAction(new Computable<ModifiableRootModel>() {
@Override
public ModifiableRootModel compute() {
return ModuleRootManager.getInstance(myModule).getModifiableModel();
}
});
final ModuleConfigurationStateImpl moduleConfigurationState = new ModuleConfigurationStateImpl(myModule.getProject(), new DefaultModulesProvider(myModule.getProject())) {
@Override
public ModifiableRootModel getRootModel() {
return myModifiableModel;
}
@Override
public FacetsProvider getFacetsProvider() {
return DefaultFacetsProvider.INSTANCE;
}
};
myEditor = new CommonContentEntriesEditor(myModule.getName(), moduleConfigurationState, true, myRootTypes) {
@Override
protected List<ContentEntry> addContentEntries(VirtualFile[] files) {
List<ContentEntry> entries = super.addContentEntries(files);
addContentEntryPanels(entries.toArray(new ContentEntry[entries.size()]));
return entries;
}
};
JComponent component = ApplicationManager.getApplication().runReadAction(new Computable<JComponent>() {
@Override
public JComponent compute() {
return myEditor.createComponent();
}
});
myTopPanel.add(component, BorderLayout.CENTER);
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method doRun.
protected void doRun() {
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
final Ref<UsageInfo[]> refUsages = new Ref<>();
final Ref<Language> refErrorLanguage = new Ref<>();
final Ref<Boolean> refProcessCanceled = new Ref<>();
final Ref<Boolean> anyException = new Ref<>();
final Runnable findUsagesRunnable = new Runnable() {
@Override
public void run() {
try {
refUsages.set(DumbService.getInstance(myProject).runReadActionInSmartMode(new Computable<UsageInfo[]>() {
@Override
public UsageInfo[] compute() {
return findUsages();
}
}));
} catch (UnknownReferenceTypeException e) {
refErrorLanguage.set(e.getElementLanguage());
} catch (ProcessCanceledException e) {
refProcessCanceled.set(Boolean.TRUE);
} catch (Throwable e) {
anyException.set(Boolean.TRUE);
LOG.error(e);
}
}
};
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(findUsagesRunnable, RefactoringBundle.message("progress.text"), true, myProject)) {
return;
}
if (!refErrorLanguage.isNull()) {
Messages.showErrorDialog(myProject, RefactoringBundle.message("unsupported.refs.found", refErrorLanguage.get().getDisplayName()), RefactoringBundle.message("error.title"));
return;
}
if (DumbService.isDumb(myProject)) {
DumbService.getInstance(myProject).showDumbModeNotification("Refactoring is not available until indices are ready");
return;
}
if (!refProcessCanceled.isNull()) {
Messages.showErrorDialog(myProject, "Index corruption detected. Please retry the refactoring - indexes will be rebuilt automatically", RefactoringBundle.message("error.title"));
return;
}
if (!anyException.isNull()) {
//do not proceed if find usages fails
return;
}
assert !refUsages.isNull() : "Null usages from processor " + this;
if (!preprocessUsages(refUsages))
return;
final UsageInfo[] usages = refUsages.get();
assert usages != null;
UsageViewDescriptor descriptor = createUsageViewDescriptor(usages);
boolean isPreview = isPreviewUsages(usages);
if (!isPreview) {
isPreview = !ensureElementsWritable(usages, descriptor) || UsageViewUtil.hasReadOnlyUsages(usages);
if (isPreview) {
StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("readonly.occurences.found"));
}
}
if (isPreview) {
for (UsageInfo usage : usages) {
LOG.assertTrue(usage != null, getClass());
}
previewRefactoring(usages);
} else {
execute(usages);
}
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class ExtractMethodHelper method processDuplicates.
public static void processDuplicates(@NotNull final PsiElement callElement, @NotNull final PsiElement generatedMethod, @NotNull final List<PsiElement> scope, @NotNull final SimpleDuplicatesFinder finder, @NotNull final Editor editor, @NotNull final Consumer<Pair<SimpleMatch, PsiElement>> replacer) {
finder.setReplacement(callElement);
if (ApplicationManager.getApplication().isUnitTestMode()) {
replaceDuplicates(callElement, editor, replacer, finder.findDuplicates(scope, generatedMethod));
return;
}
final Project project = callElement.getProject();
ProgressManager.getInstance().run(new Task.Backgroundable(project, RefactoringBundle.message("searching.for.duplicates"), true) {
public void run(@NotNull ProgressIndicator indicator) {
if (myProject == null || myProject.isDisposed())
return;
final List<SimpleMatch> duplicates = ApplicationManager.getApplication().runReadAction(new Computable<List<SimpleMatch>>() {
@Override
public List<SimpleMatch> compute() {
return finder.findDuplicates(scope, generatedMethod);
}
});
ApplicationManager.getApplication().invokeLater(() -> replaceDuplicates(callElement, editor, replacer, duplicates));
}
});
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class CompilerPaths method getModuleOutputPath.
/**
* The same as {@link #getModuleOutputDirectory} but returns String.
* The method still returns a non-null value if the output path is specified in Settings but does not exist on disk.
*/
@Nullable
public static String getModuleOutputPath(final Module module, boolean forTestClasses) {
final String outPathUrl;
final Application application = ApplicationManager.getApplication();
final CompilerModuleExtension extension = CompilerModuleExtension.getInstance(module);
if (extension == null) {
return null;
}
if (forTestClasses) {
if (application.isDispatchThread()) {
final String url = extension.getCompilerOutputUrlForTests();
outPathUrl = url != null ? url : extension.getCompilerOutputUrl();
} else {
outPathUrl = application.runReadAction(new Computable<String>() {
@Override
public String compute() {
final String url = extension.getCompilerOutputUrlForTests();
return url != null ? url : extension.getCompilerOutputUrl();
}
});
}
} else {
// for ordinary classes
if (application.isDispatchThread()) {
outPathUrl = extension.getCompilerOutputUrl();
} else {
outPathUrl = application.runReadAction(new Computable<String>() {
@Override
public String compute() {
return extension.getCompilerOutputUrl();
}
});
}
}
return outPathUrl != null ? VirtualFileManager.extractPath(outPathUrl) : null;
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class MoveClassesOrPackagesUtil method chooseDestinationPackage.
@Nullable
public static PsiDirectory chooseDestinationPackage(Project project, String packageName, @Nullable PsiDirectory baseDir) {
final PsiManager psiManager = PsiManager.getInstance(project);
final PackageWrapper packageWrapper = new PackageWrapper(psiManager, packageName);
final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
PsiDirectory directory;
PsiDirectory[] directories = aPackage != null ? aPackage.getDirectories() : null;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final VirtualFile baseDirVirtualFile = baseDir != null ? baseDir.getVirtualFile() : null;
final boolean isBaseDirInTestSources = baseDirVirtualFile != null && fileIndex.isInTestSourceContent(baseDirVirtualFile);
if (directories != null && directories.length == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(directories[0].getVirtualFile()) == isBaseDirInTestSources)) {
directory = directories[0];
} else {
final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
if (contentSourceRoots.size() == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(contentSourceRoots.get(0)) == isBaseDirInTestSources)) {
directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, contentSourceRoots.get(0));
}
});
} else {
final VirtualFile sourceRootForFile = chooseSourceRoot(packageWrapper, contentSourceRoots, baseDir);
if (sourceRootForFile == null)
return null;
directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return new AutocreatingSingleSourceRootMoveDestination(packageWrapper, sourceRootForFile).getTargetDirectory((PsiDirectory) null);
}
});
}
}
return directory;
}
Aggregations