use of com.intellij.openapi.util.Computable in project android by JetBrains.
the class CreateFileResourceQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final VirtualFile resourceDir = myFacet.getPrimaryResourceDir();
if (resourceDir == null) {
return;
}
final PsiDirectory psiResDir = PsiManager.getInstance(project).findDirectory(resourceDir);
if (psiResDir == null) {
return;
}
final String resDirName = myResourceType.getName();
PsiDirectory resSubdir = psiResDir.findSubdirectory(resDirName);
if (resSubdir == null) {
resSubdir = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return psiResDir.createSubdirectory(resDirName);
}
});
}
try {
AndroidResourceUtil.createFileResource(myResourceName, resSubdir, CreateTypedResourceFileAction.getDefaultRootTagByResourceType(myResourceType), resDirName, false);
UndoUtil.markPsiFileForUndo(myFile);
} catch (Exception e) {
LOG.error(e);
}
}
use of com.intellij.openapi.util.Computable in project intellij-plugins by JetBrains.
the class FlashUmlTest method doTestImpl.
private DiagramBuilder doTestImpl(@Nullable File projectRoot, String[] files, String[] additionalClasses, Computable<GlobalSearchScope> scopeProvider, @Nullable EnumSet<FlashUmlDependenciesSettingsOption> dependencies, @Nullable String expectedFileNamePrefix) throws Exception {
List<VirtualFile> vFiles = new ArrayList<>(files.length);
for (String file : files) {
vFiles.add(getVirtualFile(BASE_PATH + file));
}
ApplicationManager.getApplication().runWriteAction(() -> {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
ContentEntry[] contentEntries = rootModel.getContentEntries();
for (ContentEntry contentEntry : contentEntries) {
rootModel.removeContentEntry(contentEntry);
}
rootModel.commit();
});
configureByFiles(projectRoot, VfsUtilCore.toVirtualFileArray(vFiles));
final LinkedHashMap<Integer, String> markers = JSTestUtils.extractPositionMarkers(getProject(), getEditor().getDocument());
assertFalse(markers.isEmpty());
DiagramBuilder builder = null;
int i = 1;
for (Map.Entry<Integer, String> marker : markers.entrySet()) {
getEditor().getCaretModel().moveToOffset(marker.getKey());
i++;
String expectedPrefix = StringUtil.isNotEmpty(marker.getValue()) ? marker.getValue() : expectedFileNamePrefix;
final DataContext dataContext = DataManager.getInstance().getDataContext();
final DiagramProvider[] providers = DiagramProvider.findProviders(dataContext, "unknown");
final FlashUmlProvider provider = ContainerUtil.findInstance(providers, FlashUmlProvider.class);
assertNotNull("Flash UML provider not found", provider);
final String actualOriginFqn = provider.getVfsResolver().getQualifiedName(provider.getElementManager().findInDataContext(dataContext));
final Object actualOrigin = provider.getVfsResolver().resolveElementByFQN(actualOriginFqn, getProject());
builder = UmlGraphBuilderFactory.create(myProject, provider, actualOrigin, null);
Disposer.register(getTestRootDisposable(), builder);
final DiagramDataModel<Object> model = builder.getDataModel();
DiagramConfiguration configuration = DiagramConfiguration.getConfiguration();
String originalCategories = configuration.categories.get(provider.getID());
if (dependencies != null) {
model.setShowDependencies(true);
EnumSet<FlashUmlDependenciesSettingsOption> disabledOptions = EnumSet.complementOf(dependencies);
configuration.categories.put(provider.getID(), StringUtil.join(disabledOptions, option -> option.getDisplayName(), ";"));
} else {
model.setShowDependencies(false);
}
try {
model.refreshDataModel();
// first limit elements by scope
Collection<DiagramNode<Object>> nodesToRemove = new ArrayList<>();
for (DiagramNode<Object> node : model.getNodes()) {
if (node.getIdentifyingElement() instanceof JSClass && !scopeProvider.compute().contains(((JSClass) node.getIdentifyingElement()).getContainingFile().getVirtualFile())) {
nodesToRemove.add(node);
}
}
for (DiagramNode<Object> node : nodesToRemove) {
model.removeNode(node);
}
builder.updateGraph();
// then add explicitly required classes
for (String aClass : additionalClasses) {
JSClass c = JSTestUtils.findClassByQName(aClass, GlobalSearchScope.allScope(myProject));
final DiagramNode node = model.addElement(c);
if (node != null) {
builder.createDraggedNode(node, node.getTooltip(), DiagramUtils.getBestPositionForNode(builder));
builder.updateGraph();
}
}
assertModel(expectedPrefix, provider, actualOriginFqn, model);
} finally {
configuration.categories.put(provider.getID(), originalCategories);
}
}
assert builder != null;
return builder;
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class StaticInheritanceFix method doFix.
@Override
public void doFix(final Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) descriptor.getPsiElement();
final PsiClass iface = (PsiClass) referenceElement.resolve();
assert iface != null;
final PsiField[] allFields = iface.getAllFields();
final PsiClass implementingClass = ClassUtils.getContainingClass(referenceElement);
final PsiManager manager = referenceElement.getManager();
assert implementingClass != null;
final PsiFile file = implementingClass.getContainingFile();
ProgressManager.getInstance().run(new Task.Modal(project, "Replacing usages of " + iface.getName(), false) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
for (final PsiField field : allFields) {
SearchScope scope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {
@Override
public SearchScope compute() {
return implementingClass.getUseScope();
}
});
final Query<PsiReference> search = ReferencesSearch.search(field, scope, false);
for (PsiReference reference : search) {
if (!(reference instanceof PsiReferenceExpression)) {
continue;
}
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) reference;
if (!myReplaceInWholeProject) {
boolean isInheritor = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
boolean isInheritor = false;
PsiClass aClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
while (aClass != null) {
isInheritor = InheritanceUtil.isInheritorOrSelf(aClass, implementingClass, true);
if (isInheritor)
break;
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class);
}
return isInheritor;
}
});
if (!isInheritor)
continue;
}
final Runnable runnable = () -> {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(referenceExpression)) {
return;
}
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
final PsiReferenceExpression qualified = (PsiReferenceExpression) elementFactory.createExpressionFromText("xxx." + referenceExpression.getText(), referenceExpression);
final PsiReferenceExpression newReference = (PsiReferenceExpression) referenceExpression.replace(qualified);
final PsiReferenceExpression qualifier = (PsiReferenceExpression) newReference.getQualifierExpression();
assert qualifier != null : DebugUtil.psiToString(newReference, false);
final PsiClass containingClass = field.getContainingClass();
qualifier.bindToElement(containingClass);
};
invokeWriteAction(runnable, file);
}
}
final Runnable runnable = () -> {
PsiClassType classType = JavaPsiFacade.getInstance(project).getElementFactory().createType(iface);
IntentionAction fix = QuickFixFactory.getInstance().createExtendsListFix(implementingClass, classType, false);
fix.invoke(project, null, file);
};
invokeWriteAction(runnable, file);
}
});
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class SliceUsage method processChildren.
public void processChildren(@NotNull Processor<SliceUsage> processor) {
final PsiElement element = ApplicationManager.getApplication().runReadAction((Computable<PsiElement>) this::getElement);
ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
indicator.checkCanceled();
final Processor<SliceUsage> uniqueProcessor = new CommonProcessors.UniqueProcessor<>(processor, new TObjectHashingStrategy<SliceUsage>() {
@Override
public int computeHashCode(final SliceUsage object) {
return object.getUsageInfo().hashCode();
}
@Override
public boolean equals(final SliceUsage o1, final SliceUsage o2) {
return o1.getUsageInfo().equals(o2.getUsageInfo());
}
});
ApplicationManager.getApplication().runReadAction(() -> {
if (params.dataFlowToThis) {
processUsagesFlownDownTo(element, uniqueProcessor);
} else {
processUsagesFlownFromThe(element, uniqueProcessor);
}
});
}
use of com.intellij.openapi.util.Computable in project intellij-community by JetBrains.
the class ConfigurableCardPanel method createConfigurableComponent.
/**
* Creates UI component for the specified configurable.
* If a component is created successfully the configurable will be reset.
* If the configurable implements {@link MasterDetails},
* created component will not have the following modifications.
* If the configurable does not implement {@link Configurable.NoMargin},
* this method sets an empty border with default margins for created component.
* If the configurable does not implement {@link Configurable.NoScroll},
* this method adds a scroll bars for created component.
*/
public static JComponent createConfigurableComponent(Configurable configurable) {
return configurable == null ? null : ApplicationManager.getApplication().runReadAction(new Computable<JComponent>() {
@Override
public JComponent compute() {
JComponent component = null;
long time = System.currentTimeMillis();
try {
component = configurable.createComponent();
} catch (Exception unexpected) {
LOG.error("cannot create configurable component", unexpected);
} finally {
warn(configurable, "create", time);
}
if (component != null) {
reset(configurable);
if (ConfigurableWrapper.cast(MasterDetails.class, configurable) == null) {
if (ConfigurableWrapper.cast(Configurable.NoMargin.class, configurable) == null) {
if (!component.getClass().equals(JPanel.class)) {
// some custom components do not support borders
JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.CENTER, component);
component = panel;
}
component.setBorder(JBUI.Borders.empty(5, 10, 10, 10));
}
if (ConfigurableWrapper.cast(Configurable.NoScroll.class, configurable) == null) {
JScrollPane scroll = ScrollPaneFactory.createScrollPane(null, true);
scroll.setViewport(new GradientViewport(component, JBUI.insetsTop(5), true));
component = scroll;
}
}
}
return component;
}
});
}
Aggregations